Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit attributes slices as their json representation #5159

Merged
merged 6 commits into from May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Expand Up @@ -51,6 +51,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Improve performance of baggage member character validation in `go.opentelemetry.io/otel/baggage`. (#5214)
- The `otel-collector` example now uses docker compose to bring up services instead of kubernetes. (#5244)

### Fixed

- Slice attribute values in `go.opentelemetry.io/otel/attribute` are now emitted as their JSON representation. (#5159)

## [1.25.0/0.47.0/0.0.8/0.1.0-alpha] 2024-04-05

### Added
Expand Down
15 changes: 15 additions & 0 deletions attribute/key_test.go
Expand Up @@ -63,11 +63,21 @@ func TestEmit(t *testing.T) {
v: attribute.BoolValue(true),
want: "true",
},
{
name: `test Key.Emit() can emit a string representing self.INT64SLICE`,
v: attribute.Int64SliceValue([]int64{1, 42}),
want: `[1,42]`,
},
{
name: `test Key.Emit() can emit a string representing self.INT64`,
v: attribute.Int64Value(42),
want: "42",
},
{
name: `test Key.Emit() can emit a string representing self.FLOAT64SLICE`,
v: attribute.Float64SliceValue([]float64{1.0, 42.5}),
want: `[1,42.5]`,
},
{
name: `test Key.Emit() can emit a string representing self.FLOAT64`,
v: attribute.Float64Value(42.1),
Expand All @@ -78,6 +88,11 @@ func TestEmit(t *testing.T) {
v: attribute.StringValue("foo"),
want: "foo",
},
{
name: `test Key.Emit() can emit a string representing self.STRINGSLICE`,
v: attribute.StringSliceValue([]string{"foo", "bar"}),
want: `["foo","bar"]`,
},
} {
t.Run(testcase.name, func(t *testing.T) {
// proto: func (v attribute.Value) Emit() string {
Expand Down
18 changes: 15 additions & 3 deletions attribute/value.go
Expand Up @@ -231,15 +231,27 @@
case BOOL:
return strconv.FormatBool(v.AsBool())
case INT64SLICE:
return fmt.Sprint(v.asInt64Slice())
j, err := json.Marshal(v.asInt64Slice())
if err != nil {
return fmt.Sprintf("invalid: %v", v.asInt64Slice())

Check warning on line 236 in attribute/value.go

View check run for this annotation

Codecov / codecov/patch

attribute/value.go#L236

Added line #L236 was not covered by tests
}
return string(j)
case INT64:
return strconv.FormatInt(v.AsInt64(), 10)
case FLOAT64SLICE:
return fmt.Sprint(v.asFloat64Slice())
j, err := json.Marshal(v.asFloat64Slice())
if err != nil {
return fmt.Sprintf("invalid: %v", v.asFloat64Slice())

Check warning on line 244 in attribute/value.go

View check run for this annotation

Codecov / codecov/patch

attribute/value.go#L244

Added line #L244 was not covered by tests
}
return string(j)
case FLOAT64:
return fmt.Sprint(v.AsFloat64())
case STRINGSLICE:
return fmt.Sprint(v.asStringSlice())
j, err := json.Marshal(v.asStringSlice())
if err != nil {
return fmt.Sprintf("invalid: %v", v.asStringSlice())

Check warning on line 252 in attribute/value.go

View check run for this annotation

Codecov / codecov/patch

attribute/value.go#L252

Added line #L252 was not covered by tests
}
return string(j)
case STRING:
return v.stringly
default:
Expand Down
6 changes: 4 additions & 2 deletions sdk/resource/resource_test.go
Expand Up @@ -601,8 +601,9 @@ func TestWithProcessCommandArgs(t *testing.T) {
)

require.NoError(t, err)
jsonCommandArgs, _ := json.Marshal(fakeCommandArgs)
require.EqualValues(t, map[string]string{
"process.command_args": fmt.Sprint(fakeCommandArgs),
"process.command_args": string(jsonCommandArgs),
}, toMap(res))
}

Expand Down Expand Up @@ -671,11 +672,12 @@ func TestWithProcess(t *testing.T) {
)

require.NoError(t, err)
jsonCommandArgs, _ := json.Marshal(fakeCommandArgs)
require.EqualValues(t, map[string]string{
"process.pid": fmt.Sprint(fakePID),
"process.executable.name": fakeExecutableName,
"process.executable.path": fakeExecutablePath,
"process.command_args": fmt.Sprint(fakeCommandArgs),
"process.command_args": string(jsonCommandArgs),
"process.owner": fakeOwner,
"process.runtime.name": fakeRuntimeName,
"process.runtime.version": fakeRuntimeVersion,
Expand Down