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 9 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Add `WithEndpointURL` option to the `exporters/otlp/otlpmetric/otlpmetricgrpc`, `exporters/otlp/otlpmetric/otlpmetrichttp`, `exporters/otlp/otlptrace/otlptracegrpc` and `exporters/otlp/otlptrace/otlptracehttp` packages. (#4808)

### Fixed

- Fixed 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`. (#4826)
m-posluszny marked this conversation as resolved.
Show resolved Hide resolved

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

This is a release candidate for the v1.23.0 release.
Expand Down
4 changes: 2 additions & 2 deletions exporters/stdout/stdoutmetric/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,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 @@
valid bool
}

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

Check warning on line 219 in sdk/metric/metricdata/data.go

View check run for this annotation

Codecov / codecov/patch

sdk/metric/metricdata/data.go#L218-L219

Added lines #L218 - L219 were not covered by tests
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
22 changes: 22 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,22 @@ 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, "-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)
}