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

sdk/metric/metricdata: Add MarshalJSON for Extrema #4827

Merged
merged 14 commits into from
Feb 1, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
m-posluszny marked this conversation as resolved.
Show resolved Hide resolved
// "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)
m-posluszny marked this conversation as resolved.
Show resolved Hide resolved
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)
}