1
0
mirror of https://github.com/golang/go synced 2024-11-18 18:54:42 -07:00

internal/telemetry/export/ocagent: correctly JSON marshal BucketOptionsExplicit

This change adds a custom MarshalJSON method to BucketOptionsExplicit for
marshalling parity with protobuf/jsonpb. This allows the OpenCensus service
to correctly decode a distribution's BucketOptions.

Updates golang/go#33819

Change-Id: Ia9dc868e1cbfc32a956f6a276dfd1591f7d4d31a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/208398
Reviewed-by: Emmanuel Odeke <emm.odeke@gmail.com>
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
Nathan Dias 2019-11-21 23:10:21 -06:00 committed by Emmanuel Odeke
parent f191eec953
commit 2d3bb8ce05
2 changed files with 20 additions and 2 deletions

View File

@ -148,6 +148,24 @@ type BucketOptions interface {
func (BucketOptionsExplicit) tagBucketOptions() {}
// Declared for the purpose of custom JSON marshalling without cycles.
type bucketOptionsExplicitAlias BucketOptionsExplicit
// MarshalJSON creates JSON formatted the same way as jsonpb so that the
// OpenCensus service can correctly determine the underlying value type.
// This custom MarshalJSON exists because,
// by default BucketOptionsExplicit is JSON marshalled as:
// {"bounds":[1,2,3]}
// but it should be marshalled as:
// {"explicit":{"bounds":[1,2,3]}}
func (be *BucketOptionsExplicit) MarshalJSON() ([]byte, error) {
return json.Marshal(&struct {
Explicit *bucketOptionsExplicitAlias `json:"explicit,omitempty"`
}{
Explicit: (*bucketOptionsExplicitAlias)(be),
})
}
type Bucket struct {
Count int64 `json:"count,omitempty"`
Exemplar *Exemplar `json:"exemplar,omitempty"`

View File

@ -44,7 +44,7 @@ func TestMarshalJSON(t *testing.T) {
Count: 2,
},
},
BucketOptions: BucketOptionsExplicit{
BucketOptions: &BucketOptionsExplicit{
Bounds: []float64{
0, 5,
},
@ -52,7 +52,7 @@ func TestMarshalJSON(t *testing.T) {
},
},
},
`{"distributionValue":{"count":3,"sum":10,"bucket_options":{"bounds":[0,5]},"buckets":[{"count":1},{"count":2}]}}`,
`{"distributionValue":{"count":3,"sum":10,"bucket_options":{"explicit":{"bounds":[0,5]}},"buckets":[{"count":1},{"count":2}]}}`,
},
{
"nil point",