Skip to content

Conversation

@bboreham
Copy link
Contributor

@bboreham bboreham commented Jan 20, 2026

This gives nicer results when you pass one to fmt.Println, for example.

Disclosure: Claude wrote the tests for me. I deleted about a third of the cases it came up with as uninteresting.

Fixes: #7810

This gives nicer results when you pass one to fmt.Println, for example.

Signed-off-by: Bryan Boreham <[email protected]>
@codecov
Copy link

codecov bot commented Jan 20, 2026

Codecov Report

❌ Patch coverage is 75.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 86.0%. Comparing base (b1e7bde) to head (f1e0756).

Files with missing lines Patch % Lines
attribute/value.go 50.0% 0 Missing and 1 partial ⚠️
Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##            main   #7812     +/-   ##
=======================================
- Coverage   86.1%   86.0%   -0.1%     
=======================================
  Files        302     302             
  Lines      22046   22050      +4     
=======================================
- Hits       18991   18977     -14     
+ Misses      2675    2673      -2     
- Partials     380     400     +20     
Files with missing lines Coverage Δ
attribute/kv.go 84.6% <100.0%> (-15.4%) ⬇️
attribute/value.go 82.4% <50.0%> (-10.3%) ⬇️

... and 2 files with indirect coverage changes

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

{
name: "string with spaces",
kv: attribute.String("key", "hello world"),
want: "{key:hello world}",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I find it surprising that strings are not quoted, especially with embedded spaces like here.
But this is what Value.Emit() did, so leaving this here for comment.

{
name: "invalid/uninitialized KeyValue",
kv: attribute.KeyValue{},
want: "{:unknown}",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Maybe this should print something clearer?
Originally I wanted this for debugging purposes, so exactly what is in the object may be important.

Copy link
Member

Choose a reason for hiding this comment

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

I propose <invalid>.

{
name: "bool slice",
value: attribute.BoolSliceValue([]bool{true, false, true}),
want: "[true false true]",
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Also it was surprising to me that this one has no commas, while the int, float and string slices do.

@bboreham
Copy link
Contributor Author

I dispute the findings of the code coverage CI check. I added two functions which are completely covered by tests; how could this make coverage go down?

Comment on lines +21 to +24
// String implements the Stringer interface, used when you pass an object to fmt.Println, etc.
func (kv KeyValue) String() string {
return "{" + string(kv.Key) + ":" + kv.Value.String() + "}"
}
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// String implements the Stringer interface, used when you pass an object to fmt.Println, etc.
func (kv KeyValue) String() string {
return "{" + string(kv.Key) + ":" + kv.Value.String() + "}"
}
// String returns key-value pair as a string, formatted like "key:value".
//
// The returned string is meant for debugging;
// the string representation is not stable.
func (kv KeyValue) String() string {
return string(kv.Key) + ":" + kv.Value.String()
}

Why? To follow prior art https://pkg.go.dev/go.opentelemetry.io/otel/log#KeyValue.String (more: #5117).

- Add `go.opentelemetry.io/otel/semconv/v1.39.0` package.
The package contains semantic conventions from the `v1.39.0` version of the OpenTelemetry Semantic Conventions.
See the [migration documentation](./semconv/v1.39.0/MIGRATION.md) for information on how to upgrade from `go.opentelemetry.io/otel/semconv/v1.38.0.`(#7783)
- Add `String` method to `attribute.Value` and `KeyValue`. (#7812)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
- Add `String` method to `attribute.Value` and `KeyValue`. (#7812)
- Add `String` method to `attribute.Value` and `KeyValue` in `go.opentelemetry.io/otel/attribute`. (#7812)

Comment on lines +225 to +226
// String implements the Stringer interface, used when you pass an object to fmt.Println, etc.
// Do not confuse with AsString, which you should call if you know the value is of String type.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
// String implements the Stringer interface, used when you pass an object to fmt.Println, etc.
// Do not confuse with AsString, which you should call if you know the value is of String type.
// String returns Value's value as a string, formatted like [fmt.Sprint].
//
// The returned string is meant for debugging;
// the string representation is not stable.

{
name: "invalid/uninitialized KeyValue",
kv: attribute.KeyValue{},
want: "{:unknown}",
Copy link
Member

Choose a reason for hiding this comment

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

I propose <invalid>.

// String implements the Stringer interface, used when you pass an object to fmt.Println, etc.
// Do not confuse with AsString, which you should call if you know the value is of String type.
func (v Value) String() string {
return v.Emit()
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't we deprecate Emit so that users migrate to the new method?

@pellared pellared changed the title Attribute: Add String method to Value and KeyValue attribute: add String method to Value and KeyValue Jan 20, 2026
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.

Support String() method on attribute.Value and attribute.KeyValue

2 participants