From 98d2fd1cbb9b9e91acf762cf5caad4af0c45e424 Mon Sep 17 00:00:00 2001 From: Liz Fong-Jones Date: Fri, 17 Jul 2020 14:43:17 -0400 Subject: [PATCH 1/3] supports marshaling values as json --- api/kv/kv.go | 4 ++++ api/kv/kv_test.go | 23 +++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/api/kv/kv.go b/api/kv/kv.go index 8f2238c173a..826c2b27591 100644 --- a/api/kv/kv.go +++ b/api/kv/kv.go @@ -15,6 +15,7 @@ package kv import ( + "encoding/json" "fmt" "reflect" @@ -138,5 +139,8 @@ func Infer(k string, value interface{}) KeyValue { case reflect.String: return String(k, rv.String()) } + if b, err := json.Marshal(value); value != nil && err == nil { + return String(k, string(b)) + } return String(k, fmt.Sprint(value)) } diff --git a/api/kv/kv_test.go b/api/kv/kv_test.go index 9d2ccf47653..5d6b9d00a5f 100644 --- a/api/kv/kv_test.go +++ b/api/kv/kv_test.go @@ -124,6 +124,17 @@ func TestKeyValueConstructors(t *testing.T) { func TestInfer(t *testing.T) { builder := &strings.Builder{} builder.WriteString("foo") + jsonifyStruct := struct { + Public string + private string + Tagged string `json:"tagName"` + Empty string + OmitEmpty string `json:",omitempty"` + Omit string `json:"-"` + }{"foo", "bar", "baz", "", "", "omitted"} + invalidStruct := struct { + N complex64 + }{complex(0, 0)} for _, testcase := range []struct { key string value interface{} @@ -190,6 +201,18 @@ func TestInfer(t *testing.T) { wantType: value.STRING, wantValue: "", }, + { + key: "JSON struct serialized correctly", + value: &jsonifyStruct, + wantType: value.STRING, + wantValue: `{"Public":"foo","tagName":"baz","Empty":""}`, + }, + { + key: "Invalid JSON struct falls back to string", + value: &invalidStruct, + wantType: value.STRING, + wantValue: "&{(0+0i)}", + }, } { t.Logf("Running test case %s", testcase.key) keyValue := kv.Infer(testcase.key, testcase.value) From dcc0690afb25955956b26fd97194f124dacd1b2d Mon Sep 17 00:00:00 2001 From: Liz Fong-Jones Date: Fri, 17 Jul 2020 15:12:48 -0400 Subject: [PATCH 2/3] add changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 64f5f38fb12..1a9abcdca5b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - OTLP .proto files from `open-telemetry/opentelemetry-proto` imported as a git submodule under `internal/opentelemetry-proto`. References to `github.com/open-telemetry/opentelemetry-proto` changed to `go.opentelemetry.io/otel/internal/opentelemetry-proto-gen`. (#948) +### Changed + +- Non-nil structs will be marshalled using JSON rather than Sprintf. (#948) + ### Removed - Removed dependency on `github.com/open-telemetry/opentelemetry-collector`. (#943) From adab393d4fa7f2ebec2f2652c6f0ff398629641b Mon Sep 17 00:00:00 2001 From: Liz Fong-Jones Date: Fri, 17 Jul 2020 16:06:47 -0400 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1a9abcdca5b..38494fe2c16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,11 +12,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Github action to generate protobuf Go bindings locally in `internal/opentelemetry-proto-gen`. (#938) - OTLP .proto files from `open-telemetry/opentelemetry-proto` imported as a git submodule under `internal/opentelemetry-proto`. - References to `github.com/open-telemetry/opentelemetry-proto` changed to `go.opentelemetry.io/otel/internal/opentelemetry-proto-gen`. (#948) + References to `github.com/open-telemetry/opentelemetry-proto` changed to `go.opentelemetry.io/otel/internal/opentelemetry-proto-gen`. (#942) ### Changed -- Non-nil structs will be marshalled using JSON rather than Sprintf. (#948) +- Non-nil value structs for key-value pairs will be marshalled using JSON rather than Sprintf. (#948) ### Removed