Skip to content

perf(protocol): optimize logging for performance and aggregation#2212

Merged
nimrod-teich merged 5 commits into
mainfrom
fix/logging-optimizations
Feb 1, 2026
Merged

perf(protocol): optimize logging for performance and aggregation#2212
nimrod-teich merged 5 commits into
mainfrom
fix/logging-optimizations

Conversation

@nimrod-teich

@nimrod-teich nimrod-teich commented Feb 1, 2026

Copy link
Copy Markdown
Contributor

Summary

This PR improves logging performance and enables better log aggregation across the protocol layer.

Performance Optimizations

  • Use structured logging instead of fmt.Sprintf: Replace premature string formatting with structured LogAttr attributes. This avoids allocations when log levels are disabled (e.g., DEBUG/TRACE in production).

  • Use strings.Builder in LogLavaEventWithLevel: Fix O(n²) string concatenation in a loop that was creating unnecessary allocations.

  • Skip rolling logger event creation when disabled: Only create zerolog events for the rolling logger when it's actually enabled, avoiding allocations for unused events.

Log Aggregation Improvements

  • Move dynamic values to structured attributes: Messages like "chainID could not get provider" now become "could not get provider" with chainID as an attribute. This enables proper grouping of similar errors in log aggregation tools (Grafana Loki, Datadog, etc.).

  • Add missing GUID attributes: Ensure request correlation by adding GUID to log statements where context is available but wasn't being logged.

Changes

File Description
utils/lavalog.go Use strings.Builder, add rolling logger guards
protocol/relaycore/relay_processor.go Add GUID to debug/error logs
protocol/rpcconsumer/rpcconsumer_server.go Fix log formatting, add GUID
protocol/rpcsmartrouter/rpcsmartrouter_server.go Fix log formatting, add GUID
protocol/rpcprovider/rpcprovider_server.go Fix log formatting, add GUID
protocol/lavasession/consumer_session_manager.go Move chainID to attribute
protocol/chainlib/*.go Use structured attributes
protocol/metrics/queue_sender.go Use structured attributes

Test plan

  • Verify logs still appear correctly at all log levels
  • Run existing unit tests
  • Check log output format in development mode
  • Verify GUID appears in correlated log entries

Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • read the contribution guide
  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the main branch
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Replace premature fmt.Sprintf calls in log statements with structured
logging attributes. This avoids string allocation and formatting when
the log level is disabled, improving performance in hot paths.
Replace inefficient string concatenation in a loop with strings.Builder.
The previous implementation created O(n^2) allocations due to += in a loop.
Also pre-allocate the eventAttrs slice to avoid reallocations.
Only create rolling logger events when the rolling logger is enabled.
Previously, events were created for all severity levels except PANIC/FATAL
even when the rolling logger was disabled, causing unnecessary allocations.
Also add nil checks to avoid method calls on nil events.
Move dynamic values from log message strings to structured attributes
to enable proper log aggregation. Messages like "chainID could not get
provider" now become "could not get provider" with chainID as attribute.
Add GUID to log statements where context is available to enable
better correlation and tracing of requests across log entries.
@codecov

codecov Bot commented Feb 1, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 12.82051% with 68 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
utils/lavalog.go 3.33% 25 Missing and 4 partials ⚠️
protocol/relaycore/relay_processor.go 14.28% 6 Missing ⚠️
protocol/rpcconsumer/rpcconsumer_server.go 14.28% 6 Missing ⚠️
protocol/rpcsmartrouter/rpcsmartrouter_server.go 14.28% 6 Missing ⚠️
protocol/metrics/queue_sender.go 37.50% 5 Missing ⚠️
protocol/chainlib/rest.go 0.00% 4 Missing ⚠️
protocol/chainlib/chain_fetcher.go 0.00% 3 Missing ⚠️
protocol/rpcprovider/rpcprovider_server.go 0.00% 3 Missing ⚠️
protocol/statetracker/updaters/state_query.go 0.00% 3 Missing ⚠️
protocol/chainlib/jsonRPC.go 0.00% 2 Missing ⚠️
... and 1 more
Flag Coverage Δ
consensus 8.54% <3.33%> (-0.01%) ⬇️
protocol 33.86% <18.75%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
protocol/chainlib/chainproxy/connector.go 56.89% <100.00%> (-0.19%) ⬇️
protocol/lavasession/consumer_session_manager.go 62.76% <100.00%> (+0.64%) ⬆️
protocol/metrics/rpcconsumer_logs.go 18.85% <0.00%> (ø)
protocol/chainlib/jsonRPC.go 44.70% <0.00%> (ø)
protocol/chainlib/chain_fetcher.go 24.20% <0.00%> (ø)
protocol/rpcprovider/rpcprovider_server.go 1.88% <0.00%> (-0.01%) ⬇️
protocol/statetracker/updaters/state_query.go 0.00% <0.00%> (ø)
protocol/chainlib/rest.go 42.44% <0.00%> (ø)
protocol/metrics/queue_sender.go 59.55% <37.50%> (ø)
protocol/relaycore/relay_processor.go 51.57% <14.28%> (ø)
... and 3 more

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread utils/lavalog.go

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SMASHING!

@github-actions

github-actions Bot commented Feb 1, 2026

Copy link
Copy Markdown

Test Results

    7 files  ±0     85 suites  ±0   30m 42s ⏱️ -20s
3 337 tests ±0  3 336 ✅ ±0  1 💤 ±0  0 ❌ ±0 
3 530 runs  ±0  3 529 ✅ ±0  1 💤 ±0  0 ❌ ±0 

Results for commit 2b1d01a. ± Comparison against base commit fbf2ce0.

@nimrod-teich nimrod-teich merged commit 4479425 into main Feb 1, 2026
32 of 33 checks passed
@nimrod-teich nimrod-teich deleted the fix/logging-optimizations branch February 1, 2026 10:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants