Skip to content

attribute: add String method for Value type#8142

Merged
pellared merged 13 commits intoopen-telemetry:mainfrom
pellared:attr-fmt
Apr 8, 2026
Merged

attribute: add String method for Value type#8142
pellared merged 13 commits intoopen-telemetry:mainfrom
pellared:attr-fmt

Conversation

@pellared
Copy link
Copy Markdown
Member

@pellared pellared commented Apr 7, 2026

Towards #7810

Fixes #8143

String representation follows: https://opentelemetry.io/docs/specs/otel/common/#anyvalue-representation-for-non-otlp-protocols

This uses optimizations like #8039 and we inline the JSON-array/string encoding logic so we avoid the extra allocations and reflection overhead of marshaling through encoding/json (the code is inlined here not to reimplement JSON broadly, but to provide a spec-specific, allocation-conscious formatter for a constrained data model).

Benchmarks of both String and Emit (that is going to be deprecated) showcase that String is even more efficient.

goos: linux
goarch: amd64
pkg: go.opentelemetry.io/otel/attribute
cpu: 13th Gen Intel(R) Core(TM) i7-13800H
BenchmarkBool/String-20                 100000000               10.20 ns/op            0 B/op          0 allocs/op
BenchmarkBool/Emit-20                   100000000               10.33 ns/op            0 B/op          0 allocs/op
BenchmarkBoolSlice/Len2/String-20               28427863                36.15 ns/op           16 B/op          1 allocs/op
BenchmarkBoolSlice/Len2/Emit-20                  5433291               201.8 ns/op            40 B/op          5 allocs/op
BenchmarkBoolSlice/Len8/String-20               12453201                99.46 ns/op           48 B/op          1 allocs/op
BenchmarkBoolSlice/Len8/Emit-20                  2185160               546.0 ns/op            88 B/op         11 allocs/op
BenchmarkInt/String-20                          100000000               10.73 ns/op            0 B/op          0 allocs/op
BenchmarkInt/Emit-20                            100000000               11.03 ns/op            0 B/op          0 allocs/op
BenchmarkIntSlice/Len2/String-20                17855926                61.57 ns/op           48 B/op          1 allocs/op
BenchmarkIntSlice/Len2/Emit-20                   6237072               184.9 ns/op            56 B/op          4 allocs/op
BenchmarkIntSlice/Len8/String-20                 6573506               192.1 ns/op           176 B/op          1 allocs/op
BenchmarkIntSlice/Len8/Emit-20                   3620901               332.8 ns/op           136 B/op          4 allocs/op
BenchmarkInt64/String-20                        100000000               10.90 ns/op            0 B/op          0 allocs/op
BenchmarkInt64/Emit-20                          100000000               10.91 ns/op            0 B/op          0 allocs/op
BenchmarkInt64Slice/Len2/String-20              20924970                59.59 ns/op           48 B/op          1 allocs/op
BenchmarkInt64Slice/Len2/Emit-20                 6755516               184.2 ns/op            56 B/op          4 allocs/op
BenchmarkInt64Slice/Len8/String-20               6033630               207.9 ns/op           176 B/op          1 allocs/op
BenchmarkInt64Slice/Len8/Emit-20                 3491808               327.2 ns/op           136 B/op          4 allocs/op
BenchmarkFloat64/String-20                      23607802                52.21 ns/op            2 B/op          1 allocs/op
BenchmarkFloat64/Emit-20                        13578472                93.34 ns/op           16 B/op          2 allocs/op
BenchmarkFloat64Slice/Len2/String-20                    12066591               111.0 ns/op            64 B/op          1 allocs/op
BenchmarkFloat64Slice/Len2/Emit-20                       5177293               234.3 ns/op            56 B/op          4 allocs/op
BenchmarkFloat64Slice/Len8/String-20                     3041408               381.9 ns/op           208 B/op          1 allocs/op
BenchmarkFloat64Slice/Len8/Emit-20                       2369974               548.3 ns/op           136 B/op          4 allocs/op
BenchmarkString/String-20                               137506468                8.578 ns/op           0 B/op          0 allocs/op
BenchmarkString/Emit-20                                 139229646                8.542 ns/op           0 B/op          0 allocs/op
BenchmarkStringSlice/Len2/Emit-20                        5809321               228.9 ns/op           120 B/op          4 allocs/op
BenchmarkStringSlice/Len8/String-20                      5089977               240.0 ns/op            96 B/op          1 allocs/op
BenchmarkStringSlice/Len8/Emit-20                        2569848               480.0 ns/op           344 B/op          4 allocs/op
BenchmarkByteSlice/String-20                            32244670                34.31 ns/op           16 B/op          1 allocs/op
BenchmarkByteSlice/Emit-20                              36643321                34.63 ns/op           16 B/op          1 allocs/op

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 7, 2026

Codecov Report

❌ Patch coverage is 99.21569% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 82.0%. Comparing base (99b2206) to head (8eb5cae).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
attribute/value.go 99.2% 2 Missing ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##            main   #8142     +/-   ##
=======================================
+ Coverage   81.8%   82.0%   +0.2%     
=======================================
  Files        308     308             
  Lines      23802   24057    +255     
=======================================
+ Hits       19480   19741    +261     
+ Misses      3944    3939      -5     
+ Partials     378     377      -1     
Files with missing lines Coverage Δ
attribute/value.go 97.2% <99.2%> (+4.6%) ⬆️

... and 5 files with indirect coverage changes

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

@pellared pellared marked this pull request as ready for review April 7, 2026 09:10
@pellared pellared added this to the v1.44.0 milestone Apr 7, 2026
@pellared
Copy link
Copy Markdown
Member Author

pellared commented Apr 7, 2026

CC @bboreham

@pellared pellared added enhancement New feature or request pkg:attribute Related to the attribute package labels Apr 7, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds a String() method to attribute.Value so values print in a human-readable form consistent with the OpenTelemetry AnyValue (non-OTLP) string representation, with an allocation-conscious implementation and accompanying tests/benchmarks.

Changes:

  • Implement Value.String() with spec-aligned formatting for scalars and JSON-array formatting for slice types (with fast paths for small fixed arrays).
  • Inline JSON string escaping for string-slice formatting to avoid encoding/json allocations/reflection.
  • Add unit tests and benchmarks covering the new String() behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
CHANGELOG.md Documents the addition of Value.String() in the “Added” section.
attribute/value.go Adds Value.String() and supporting formatters (including inlined JSON-string escaping).
attribute/value_test.go Adds comprehensive tests for Value.String() across types and edge cases.
attribute/benchmark_test.go Adds String() benchmarks alongside existing Emit() benchmarks.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@pellared pellared merged commit 876f7c5 into open-telemetry:main Apr 8, 2026
33 checks passed
@pellared pellared deleted the attr-fmt branch April 8, 2026 07:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request pkg:attribute Related to the attribute package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

attribute: add Value.String

5 participants