feat(sdk): enforce log record attribute count limit#3571
Open
Wassbdr wants to merge 1 commit into
Open
Conversation
The logs SDK did not enforce any attribute count limit, unlike spans. SdkLoggerProvider now caps the number of attributes retained per log record (default 128), dropping attributes added beyond the limit when the record is emitted. The limit is configurable, in precedence order, via: - LoggerProviderBuilder::with_max_attributes_per_log - OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT - OTEL_ATTRIBUTE_COUNT_LIMIT (general fallback) Enforcement happens once in SdkLogger::emit (before handing the record to processors) via a new GrowableArray::truncate. Documented the new env var and added a CHANGELOG entry. Part of open-telemetry#3374
cijothomas
reviewed
Jun 27, 2026
| } | ||
| logger.emit(record); | ||
| provider.force_flush().unwrap(); | ||
| count |
Member
There was a problem hiding this comment.
nit: no need to return the count here.
cijothomas
reviewed
Jun 27, 2026
|
|
||
| // Enforce the configured attribute count limit before handing the | ||
| // record to processors, dropping the attributes added last. | ||
| record.truncate_attributes(provider.max_attributes_per_log() as usize); |
Member
There was a problem hiding this comment.
if the original intend behind enforcing this limit is to protect the sdk from taking up too much memory when a mistaken code adds too much attributes, then we should prevent it from being added in the first place. Truncating afterwards won't help.
Could you try eagerly checking the limit in each add_attribute() call? That'd also make it relatively easy to track the dropped count.
Please do benchmark and see if we regress measurably.
cijothomas
reviewed
Jun 27, 2026
| /// Attributes added beyond this limit are dropped when the record is | ||
| /// emitted. When not set, this is read from the | ||
| /// `OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIT` environment variable, which falls | ||
| /// back to `OTEL_ATTRIBUTE_COUNT_LIMIT`, and otherwise defaults to `128`. |
Member
There was a problem hiding this comment.
nit: after this is merged, we should follow up immediately with your other PR so this variable will be consistently applied in spans too.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Part of #3374. Prerequisite for #3568.
Context
In #3568 I added
OTEL_ATTRIBUTE_COUNT_LIMITfor spans. @cijothomas pointed out that the general variable is meant to apply to logs too, and that the logs SDK does not enforce any attribute limit today — so exposing the general variable before logs support exists would mislead users. This PR adds that missing logs support first.Change
SdkLoggerProvidernow enforces a maximum attribute count per log record (default128), dropping attributes added beyond the limit when the record is emitted. Configurable, in precedence order, via:LoggerProviderBuilder::with_max_attributes_per_log(code-based)OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMITOTEL_ATTRIBUTE_COUNT_LIMIT(general fallback)128Enforcement is done once in
SdkLogger::emit, before the record is handed to processors, using a newGrowableArray::truncate. The limit is resolved at provider build time and stored onLoggerProviderInner.This mirrors the span behavior (
max_attributes_per_span+OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT) and keeps the hot path cheap: a single truncate per emitted record, a no-op when within the limit.Notes / scope
dropped_attributes_countonSdkLogRecordor wire it through OTLP (the proto transform currently sends0). That's a separate, larger change and can follow; happy to do it if preferred.Tests
GrowableArray::truncateunit test (inline region, overflow region, boundary, no-op, zero).OTEL_LOGRECORD_ATTRIBUTE_COUNT_LIMIToverOTEL_ATTRIBUTE_COUNT_LIMIT; general var as fallback.cargo test -p opentelemetry_sdk --all-features --lib,cargo clippy --all-targets --all-features -- -Dwarnings, andcargo fmt --all -- --checkall pass.