Skip to content

fix(append-opentelemetry): emit unique log attribute keys by default - #331

Merged
tisonkun merged 3 commits into
mainfrom
fix/otel-attribute-keys
Dec 2, 2026
Merged

tisonkun merged 3 commits into
mainfrom
fix/otel-attribute-keys

Conversation

@tisonkun

@tisonkun tisonkun commented Dec 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Enforce unique top-level log attribute keys by default in OpentelemetryLog per the OpenTelemetry attribute collection specification, since opentelemetry_sdk appends attributes verbatim and OTLP exporters forward repeated keys directly to receivers.

  • Emit top-level log attributes in source location (code.*), remaining Diagnostic, then explicit Record::key_values() order, keeping only the last occurrence of each key in its surviving position by default so explicit record fields override colliding diagnostics and diagnostics override source location.
  • Add OpentelemetryLog::allow_duplicate_keys(self) -> Self to forward attributes without deduplication when callers already guarantee key uniqueness or target a custom processor designed for repeated keys.
  • Avoid heap allocations on the common source-location-only path (attributes.is_empty()), deduplicate up to 16 extra attributes in place without set allocations, and switch to a pre-sized HashSet for larger attribute collections.

Validation

  • cargo x check, cargo x test, and cargo x lint passed.
  • Covered cross-source collisions across <= 16 and > 16 attributes, ValueSerde structured map attributes, and allow_duplicate_keys() forwarding order in tests-integration.

Performance

Median nanoseconds per log record over nine interleaved release runs from Logger::log through SimpleLogProcessor and OTLP ExportLogsServiceRequest protobuf encoding into a reused buffer:

Workload Default Allow duplicate keys
Four source location attributes 583 576
Location, two diagnostics, three event fields 1,184 1,114
Same workload replayed from an owned record 1,193 1,064
Same workload with 64 additional unique fields 6,791 5,954

The Rust SDK appends log attributes as supplied, so a diagnostic and an event field with the same key can both reach an OTLP receiver.

Apply source location, diagnostics, and explicit record fields in increasing precedence. Keep the last occurrence of each key in source order. Add allow_duplicate_keys() for callers that manage key uniqueness themselves.

Signed-off-by: tison <wander4096@gmail.com>
…ute dedup

Skip temporary allocations when no diagnostic or record attributes remain, deduplicate up to 16 attributes in place without allocating a set, switch larger attribute collections to a pre-sized `HashSet`, pre-size nested list and map conversions, cover the `> 16` collision boundary, and align documentation across `OpentelemetryLog`, `README.md`, `DESIGN.md`, and `CHANGELOG.md`.

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun
tisonkun merged commit aceba80 into main Dec 2, 2026
@tisonkun
tisonkun deleted the fix/otel-attribute-keys branch December 2, 2026 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant