Skip to content

Commit

Permalink
Emit attributes slices as their json representation (#5159)
Browse files Browse the repository at this point in the history
* emit slices as their json representation

* add changelog

* fix resource tests

* indicate invalid slice if we couldn't turn them into json

* move changelog entry to the unreleased section

---------

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
  • Loading branch information
dmathieu and MrAlias authored May 7, 2024
1 parent 0fc35e0 commit f8b9fe3
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -231,15 +231,27 @@ func (v Value) Emit() string {
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())
}
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())
}
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())
}
return string(j)
case STRING:
return v.stringly
default:
Expand Down
6 changes: 4 additions & 2 deletions sdk/resource/resource_test.go
Original file line number Diff line number Diff line change
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

0 comments on commit f8b9fe3

Please sign in to comment.