[DO NOT MERGE] attribute: add String method for KeyValue#8205
[DO NOT MERGE] attribute: add String method for KeyValue#8205pellared wants to merge 15 commits intoopen-telemetry:mainfrom
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #8205 +/- ##
=====================================
Coverage 83.7% 83.7%
=====================================
Files 314 314
Lines 29727 29795 +68
=====================================
+ Hits 24905 24963 +58
- Misses 4448 4457 +9
- Partials 374 375 +1
🚀 New features to boost your workflow:
|
|
Is there precedence in OTel for this form of a key-value encoding? |
There is some precedence, but I would frame it as debug-output precedent rather than a spec-defined canonical encoding. Inside this repo, I also checked a couple of other SDKs: .NET’s and Python console exporters prints attributes as From the spec side, there it is not defined how a attribute pair should be formatted to a string. Yet, for maps the current spec actually says they SHOULD be represented as JSON objects. This was what we originally used as inspiration. I think this also makes biggest sense for us since e.g. Given that, I think the safest justification is: However, I also started to think if we should not follow https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/common/README.md#maps more closely and have something like I also checked a couple of other SDKs: .NET’s and Python console exporters prints attributes as Key:Value, while Java has internal key-value debug encodings that use = or JSON depending on the structure. Therefore, that is no single canonical cross-SDK string form. If we think it is worth standardizing, I would be open to a small spec follow-up to define a recommended string form for an attribute pair in non-OTLP / debug contexts. |
|
@open-telemetry/go-approvers, can you make a high-level review this PR? For sure a benchmark is missing (added in TODO). There is no need to "approve", but I want to have an agreement on the direction. Please also check #8205 (comment) |
|
I like standardizing this, and I think JSON is readable and universal. Do we want to go further, and able to print out a slice of attribute.KeyVal as |
The thing that came to mind was metricdatatest |
I created #8210 |
|
@open-telemetry/go-approvers, this is ready for review |
There was a problem hiding this comment.
Pull request overview
Adds a String() method to attribute.KeyValue so it implements fmt.Stringer and prints a human-readable, spec-aligned JSON representation (intended for debugging), addressing the request from #8146 and advancing #7810.
Changes:
- Implement
attribute.KeyValue.String()that renders a single-entry JSON object using the non-OTLP attribute representation rules. - Add unit tests covering all
attribute.Valuekinds (including special float values) forKeyValue.String(). - Update benchmarks to measure
KeyValue.String()alongsideValue.String()andValue.Emit(), and note the change inCHANGELOG.md.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
| attribute/kv.go | Adds KeyValue.String() implementation using existing JSON append helpers. |
| attribute/kv_test.go | Adds test coverage for KeyValue.String() across value types. |
| attribute/value.go | Introduces/reuses sizing constants and updates slice builder growth estimates. |
| attribute/benchmark_test.go | Refactors benchmarks to include KeyValue.String() and compare with Value.String()/Emit(). |
| CHANGELOG.md | Documents the new KeyValue.String() feature. |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
|
Planning to merge tomorrow morning (CEST time). |
|
Shouldn't this wait on open-telemetry/opentelemetry-specification#5028 to merge first? |
I added in Go doc:
Note that even if it is merged in the spec, we should wait for the spec for stabilization... WDYT? |
|
I want to be cautious about adding API to the |
|
Changing to draft to prevent accidental merging. |
Blocked by:
Fixes #8146
Towards #7810
I decided to not implement "array fast paths" for the slice attribute types as currently
KeyValue.Stringis only/mainly for debugging and I didn't want to make the PR bigger. I can make a separate PR if someone wants me to address it. EDIT: So far Copilot is not happy with it #8205 (comment) 😉Benchmarks: