Skip to content

attribute: add MAP type with key deduplication#8130

Draft
iyashjayesh wants to merge 3 commits intoopen-telemetry:mainfrom
iyashjayesh:feature/map-type-deduplication
Draft

attribute: add MAP type with key deduplication#8130
iyashjayesh wants to merge 3 commits intoopen-telemetry:mainfrom
iyashjayesh:feature/map-type-deduplication

Conversation

@iyashjayesh
Copy link
Copy Markdown

@iyashjayesh iyashjayesh commented Apr 2, 2026

Closes #7935
Closes #7957

What changed

Adds a MAP attribute value type (map<string, AnyValue>) to the go.opentelemetry.io/otel/attribute package, as required by the OpenTelemetry specification.

New API surface

  • attribute.MAP - new Type constant
  • attribute.MapValue(kvs []KeyValue) Value - constructor
  • (Value).AsMap() []KeyValue - accessor

Deduplication (spec compliance)

Per the spec:

The implementation MUST by default enforce that the exported maps
contain only unique keys.

Enforcement happens at construction time in MapValue:

  • Last-write-wins: if the same key appears more than once, the last
    value is kept.
  • Insertion order preserved: the position of the first occurrence is
    maintained in the output.

Hashing

hashKV in hash.go handles the MAP case by sorting keys before
hashing, so two MAP values with the same entries in different order
produce the same hash (order-independent).

Notes for reviewers

  • []KeyValue is stored directly as any in Value.slice rather than
    via the generic SliceValue helper (which is constrained to
    bool | int64 | float64 | string). This is the only storage
    difference from other slice types.
  • Open to discussion on whether first-write-wins is preferred over
    last-write-wins - happy to adjust.

Implements issues open-telemetry#7935 and open-telemetry#7957.

Adds a new MAP attribute value type (map<string, AnyValue>) to the
attribute package, as required by the OpenTelemetry specification:
https://opentelemetry.io/docs/specs/otel/common/#anyvalue

Key changes:
- attribute/value.go: add MAP Type constant, MapValue() constructor
  with last-write-wins key deduplication, AsMap() accessor, and
  MAP support in Emit() and AsInterface().
- attribute/hash.go: add mapID type identifier and MAP case in
  hashKV() — keys are sorted before hashing for order-independence.
- attribute/type_string.go: regenerated via 'go generate ./attribute/...'

Deduplication semantics (per spec 'e.g. by removing duplicates'):
- If the same key appears more than once, the last value wins.
- The relative order of the first occurrences of each key is preserved.

Resolves: open-telemetry#7935
Resolves: open-telemetry#7957
Implements issues open-telemetry#7935 and open-telemetry#7957.

Adds a new MAP attribute value type (map<string, AnyValue>) to the
attribute package, as required by the OpenTelemetry specification:
https://opentelemetry.io/docs/specs/otel/common/#anyvalue

Key changes:
- attribute/value.go: add MAP Type constant, MapValue() constructor
  with last-write-wins key deduplication, AsMap() accessor, and
  MAP support in Emit() and AsInterface().
- attribute/hash.go: add mapID type identifier and MAP case in
  hashKV() — keys are sorted before hashing for order-independence.
- attribute/type_string.go: regenerated via 'go generate ./attribute/...'

Deduplication semantics (per spec 'e.g. by removing duplicates'):
- If the same key appears more than once, the last value wins.
- The relative order of the first occurrences of each key is preserved.

Resolves: open-telemetry#7935
Resolves: open-telemetry#7957
@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla Bot commented Apr 2, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: iyashjayesh / name: iyashjayesh (00a6be2, f937a30)
  • ✅ login: iyashjayesh / name: Yash Chauhan (53f4105)

@pellared
Copy link
Copy Markdown
Member

pellared commented Apr 2, 2026

Have you seen #7943?

@iyashjayesh
Copy link
Copy Markdown
Author

@pellared Yes, I did look at #7943 before opening this PR. It has been in draft since February and appears stalled, no commits since early March and still marked as draft. Given that both #7935 and #7957 remain open, I opened this PR as a fresh, ready-to-review implementation.

That said, I'm happy to coordinate, if @itssaharsh is actively working on #7943 and plans to bring it to a reviewable state soon, I can close this one to avoid duplication. Otherwise, I'd suggest proceeding here since this PR is ready for review and covers both issues.

Happy to do whatever the maintainers prefer!

@itssaharsh
Copy link
Copy Markdown
Contributor

I was working actively it's just that after my last commit, I got no review as I think all the maintainers are busy. So I had nothing to add to that.

@pellared
Copy link
Copy Markdown
Member

pellared commented Apr 3, 2026

@iyashjayesh, please see the comments that I have already posted in #7943. A lot of comments that I submitted previously apply to this PR as well. It also violates other patterns that we follow in this repository. This issue is complex and requires deep understanding and analysis.

@pellared pellared marked this pull request as draft April 3, 2026 11:16
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.

sdk: Implement deduplication for MAP attribute: Add MAP type

3 participants