Skip to content

Commit

Permalink
sdk/metric/metricdata: Add MarshalJSON for Extrema (#4827)
Browse files Browse the repository at this point in the history
* json marshal for Extrema type, makefile typo fix

* MarshalText implementation, cleanup

* update changelog

* changelog fix

* Update CHANGELOG.md

* MarshalText behavior update on invalid Extrema

* Fix Marshal outputs json text representation of nil

* Updated mockData to include example with Min and Max values.

* null value test, changelog fix

---------

Co-authored-by: Robert Pająk <[email protected]>
Co-authored-by: Tyler Yahn <[email protected]>
Co-authored-by: Aaron Clawson <[email protected]>
  • Loading branch information
4 people authored Feb 1, 2024
1 parent 242d23a commit 569854e
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Fix `ContainerID` resource detection on systemd when cgroup path has a colon. (#4449)
- Fix `go.opentelemetry.io/otel/sdk/metric` to cache instruments to avoid leaking memory when the same instrument is created multiple times. (#4820)
- Fix missing Mix and Max values for `go.opentelemetry.io/otel/exporters/stdout/stdoutmetric` by introducing `MarshalText` and `MarshalJSON` for type `Extrema` in `go.opentelemetry.io/sdk/metric/metricdata`. (#4827)

## [1.23.0-rc.1] 2024-01-18

Expand Down
63 changes: 61 additions & 2 deletions exporters/stdout/stdoutmetric/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,27 @@ var (
},
},
},
{
Name: "requests.size",
Description: "Size of received requests",
Unit: "kb",
Data: metricdata.Histogram[int64]{
Temporality: metricdata.DeltaTemporality,
DataPoints: []metricdata.HistogramDataPoint[int64]{
{
Attributes: attribute.NewSet(attribute.String("server", "central")),
StartTime: now,
Time: now.Add(1 * time.Second),
Count: 10,
Bounds: []float64{1, 5, 10},
BucketCounts: []uint64{1, 3, 6, 0},
Sum: 128,
Min: metricdata.NewExtrema[int64](3),
Max: metricdata.NewExtrema[int64](30),
},
},
},
},
{
Name: "latency",
Description: "Time spend processing received requests",
Expand Down Expand Up @@ -228,6 +249,44 @@ func Example() {
// }
// },
// {
// "Name": "requests.size",
// "Description": "Size of received requests",
// "Unit": "kb",
// "Data": {
// "DataPoints": [
// {
// "Attributes": [
// {
// "Key": "server",
// "Value": {
// "Type": "STRING",
// "Value": "central"
// }
// }
// ],
// "StartTime": "0001-01-01T00:00:00Z",
// "Time": "0001-01-01T00:00:00Z",
// "Count": 10,
// "Bounds": [
// 1,
// 5,
// 10
// ],
// "BucketCounts": [
// 1,
// 3,
// 6,
// 0
// ],
// "Min": 3,
// "Max": 30,
// "Sum": 128
// }
// ],
// "Temporality": "DeltaTemporality"
// }
// },
// {
// "Name": "latency",
// "Description": "Time spend processing received requests",
// "Unit": "ms",
Expand Down Expand Up @@ -257,8 +316,8 @@ func Example() {
// 6,
// 0
// ],
// "Min": {},
// "Max": {},
// "Min": null,
// "Max": null,
// "Sum": 57
// }
// ],
Expand Down
14 changes: 14 additions & 0 deletions sdk/metric/metricdata/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package metricdata // import "go.opentelemetry.io/otel/sdk/metric/metricdata"

import (
"encoding/json"
"time"

"go.opentelemetry.io/otel/attribute"
Expand Down Expand Up @@ -211,6 +212,19 @@ type Extrema[N int64 | float64] struct {
valid bool
}

// MarshalText converts the Extrema value to text.
func (e Extrema[N]) MarshalText() ([]byte, error) {
if !e.valid {
return json.Marshal(nil)
}
return json.Marshal(e.value)
}

// MarshalJSON converts the Extrema value to JSON number.
func (e *Extrema[N]) MarshalJSON() ([]byte, error) {
return e.MarshalText()
}

// NewExtrema returns an Extrema set to v.
func NewExtrema[N int64 | float64](v N) Extrema[N] {
return Extrema[N]{value: v, valid: true}
Expand Down
24 changes: 24 additions & 0 deletions sdk/metric/metricdata/metricdatatest/assertion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package metricdatatest // import "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest"

import (
"encoding/json"
"testing"
"time"

Expand Down Expand Up @@ -164,6 +165,8 @@ var (
minFloat64C = metricdata.NewExtrema(-1.)
minInt64C = metricdata.NewExtrema[int64](-1)

minFloat64D = metricdata.NewExtrema(-9.999999)

histogramDataPointInt64A = metricdata.HistogramDataPoint[int64]{
Attributes: attrA,
StartTime: startA,
Expand Down Expand Up @@ -1010,3 +1013,24 @@ func TestAssertAttributesFail(t *testing.T) {
}
assert.False(t, AssertHasAttributes(fakeT, sum, attribute.Bool("A", true)))
}

func AssertMarshal[N int64 | float64](t *testing.T, expected string, i *metricdata.Extrema[N]) {
t.Helper()

b, err := json.Marshal(i)
assert.NoError(t, err)
assert.Equal(t, expected, string(b))
}

func TestAssertMarshal(t *testing.T) {
AssertMarshal(t, "null", &metricdata.Extrema[int64]{})

AssertMarshal(t, "-1", &minFloat64A)
AssertMarshal(t, "3", &minFloat64B)
AssertMarshal(t, "-9.999999", &minFloat64D)
AssertMarshal(t, "99", &maxFloat64B)

AssertMarshal(t, "-1", &minInt64A)
AssertMarshal(t, "3", &minInt64B)
AssertMarshal(t, "99", &maxInt64B)
}

0 comments on commit 569854e

Please sign in to comment.