From 4fb7a195f9d128c3dfe1a3b5bbf7c7f3926325de Mon Sep 17 00:00:00 2001 From: Michal Posluszny Date: Mon, 15 Jan 2024 21:00:17 +0100 Subject: [PATCH 1/9] json marshal for Extrema type, makefile typo fix --- Makefile | 2 +- sdk/metric/metricdata/data.go | 10 +++++++++ .../metricdatatest/assertion_test.go | 22 +++++++++++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 35fc189961b..d3b501e3e73 100644 --- a/Makefile +++ b/Makefile @@ -192,7 +192,7 @@ test-coverage: | $(GOCOVMERGE) done; \ $(GOCOVMERGE) $$(find . -name coverage.out) > coverage.txt -# Adding a directory will include all benchmarks in that direcotry if a filter is not specified. +# Adding a directory will include all benchmarks in that directory if a filter is not specified. BENCHMARK_TARGETS := sdk/trace .PHONY: benchmark benchmark: $(BENCHMARK_TARGETS:%=benchmark/%) diff --git a/sdk/metric/metricdata/data.go b/sdk/metric/metricdata/data.go index 995d42b38f1..d6515c4275c 100644 --- a/sdk/metric/metricdata/data.go +++ b/sdk/metric/metricdata/data.go @@ -15,6 +15,8 @@ package metricdata // import "go.opentelemetry.io/otel/sdk/metric/metricdata" import ( + "encoding/json" + "fmt" "time" "go.opentelemetry.io/otel/attribute" @@ -211,6 +213,14 @@ type Extrema[N int64 | float64] struct { valid bool } +// MarshalJson converts the Extrema value to JSON number +func (e *Extrema[N]) MarshalJSON() ([]byte, error) { + if !e.valid { + return []byte("0"), nil + } + return []byte(json.Number(fmt.Sprint(e.value))), nil +} + // NewExtrema returns an Extrema set to v. func NewExtrema[N int64 | float64](v N) Extrema[N] { return Extrema[N]{value: v, valid: true} diff --git a/sdk/metric/metricdata/metricdatatest/assertion_test.go b/sdk/metric/metricdata/metricdatatest/assertion_test.go index 9d647a54004..584910962a0 100644 --- a/sdk/metric/metricdata/metricdatatest/assertion_test.go +++ b/sdk/metric/metricdata/metricdatatest/assertion_test.go @@ -15,6 +15,7 @@ package metricdatatest // import "go.opentelemetry.io/otel/sdk/metric/metricdata/metricdatatest" import ( + "encoding/json" "testing" "time" @@ -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, @@ -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) + 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) +} From 04cb3e7c3cbb2c39fb5d8c58afdff0d85aec4b05 Mon Sep 17 00:00:00 2001 From: Michal Posluszny Date: Thu, 18 Jan 2024 18:10:38 +0100 Subject: [PATCH 2/9] MarshalText implementation, cleanup --- Makefile | 2 +- sdk/metric/metricdata/data.go | 12 ++++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index d3b501e3e73..35fc189961b 100644 --- a/Makefile +++ b/Makefile @@ -192,7 +192,7 @@ test-coverage: | $(GOCOVMERGE) done; \ $(GOCOVMERGE) $$(find . -name coverage.out) > coverage.txt -# Adding a directory will include all benchmarks in that directory if a filter is not specified. +# Adding a directory will include all benchmarks in that direcotry if a filter is not specified. BENCHMARK_TARGETS := sdk/trace .PHONY: benchmark benchmark: $(BENCHMARK_TARGETS:%=benchmark/%) diff --git a/sdk/metric/metricdata/data.go b/sdk/metric/metricdata/data.go index d6515c4275c..c87403eb113 100644 --- a/sdk/metric/metricdata/data.go +++ b/sdk/metric/metricdata/data.go @@ -16,7 +16,6 @@ package metricdata // import "go.opentelemetry.io/otel/sdk/metric/metricdata" import ( "encoding/json" - "fmt" "time" "go.opentelemetry.io/otel/attribute" @@ -213,12 +212,17 @@ type Extrema[N int64 | float64] struct { valid bool } -// MarshalJson converts the Extrema value to JSON number -func (e *Extrema[N]) MarshalJSON() ([]byte, error) { +// MarshalText converts the Extrema value to text. +func (e Extrema[N]) MarshalText() ([]byte, error) { if !e.valid { return []byte("0"), nil } - return []byte(json.Number(fmt.Sprint(e.value))), 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. From 1e122a44ef9d54b5f734e9c4993e121ede961364 Mon Sep 17 00:00:00 2001 From: Michal Posluszny Date: Thu, 18 Jan 2024 18:18:38 +0100 Subject: [PATCH 3/9] update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index fe670d79cc2..4f07db4341d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Experimental cardinality limiting is added to the metric SDK. See [metric documentation](./sdk/metric/EXPERIMENTAL.md#cardinality-limit) for more information about this feature and how to enable it. (#4457) - Add `NewMemberRaw` and `NewKeyValuePropertyRaw` in `go.opentelemetry.io/otel/baggage`. (#4804) +- Add `MarshalText` and `MarshalJSON` in `go.opentelemetry.io/otel/sdk/metric/metricdata`. (#4826) ### Changed From 0c73bde8ca5a9bed77fb5a09390bd61bf6c97a86 Mon Sep 17 00:00:00 2001 From: Michal Posluszny Date: Thu, 18 Jan 2024 18:35:02 +0100 Subject: [PATCH 4/9] changelog fix --- CHANGELOG.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4f07db4341d..f198b382a9c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +### 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) + ## [1.22.0/0.45.0] 2024-01-17 ### Added @@ -24,7 +28,6 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - Experimental cardinality limiting is added to the metric SDK. See [metric documentation](./sdk/metric/EXPERIMENTAL.md#cardinality-limit) for more information about this feature and how to enable it. (#4457) - Add `NewMemberRaw` and `NewKeyValuePropertyRaw` in `go.opentelemetry.io/otel/baggage`. (#4804) -- Add `MarshalText` and `MarshalJSON` in `go.opentelemetry.io/otel/sdk/metric/metricdata`. (#4826) ### Changed From 1ebe38ccd6271a5431ff745a83ee161e522d1bb1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Robert=20Paj=C4=85k?= Date: Thu, 18 Jan 2024 18:38:09 +0100 Subject: [PATCH 5/9] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f198b382a9c..e74141f26d3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### 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) +- 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) ## [1.22.0/0.45.0] 2024-01-17 From c8d68cb063dc17272d0342efded9f277a23ebc88 Mon Sep 17 00:00:00 2001 From: Michal Posluszny Date: Thu, 18 Jan 2024 18:55:40 +0100 Subject: [PATCH 6/9] MarshalText behavior update on invalid Extrema --- sdk/metric/metricdata/data.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/metric/metricdata/data.go b/sdk/metric/metricdata/data.go index c87403eb113..9180565d455 100644 --- a/sdk/metric/metricdata/data.go +++ b/sdk/metric/metricdata/data.go @@ -215,7 +215,7 @@ type Extrema[N int64 | float64] struct { // MarshalText converts the Extrema value to text. func (e Extrema[N]) MarshalText() ([]byte, error) { if !e.valid { - return []byte("0"), nil + return nil, nil } return json.Marshal(e.value) } From fafac78bffa0dc21877c2bfd03217c4f4adcdc84 Mon Sep 17 00:00:00 2001 From: Michal Posluszny Date: Fri, 19 Jan 2024 19:07:45 +0100 Subject: [PATCH 7/9] Fix Marshal outputs json text representation of nil --- exporters/stdout/stdoutmetric/example_test.go | 4 ++-- sdk/metric/metricdata/data.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/exporters/stdout/stdoutmetric/example_test.go b/exporters/stdout/stdoutmetric/example_test.go index 683d6ee0fe5..ab43479786c 100644 --- a/exporters/stdout/stdoutmetric/example_test.go +++ b/exporters/stdout/stdoutmetric/example_test.go @@ -257,8 +257,8 @@ func Example() { // 6, // 0 // ], - // "Min": {}, - // "Max": {}, + // "Min": null, + // "Max": null, // "Sum": 57 // } // ], diff --git a/sdk/metric/metricdata/data.go b/sdk/metric/metricdata/data.go index 9180565d455..32c17934fc4 100644 --- a/sdk/metric/metricdata/data.go +++ b/sdk/metric/metricdata/data.go @@ -215,7 +215,7 @@ type Extrema[N int64 | float64] struct { // MarshalText converts the Extrema value to text. func (e Extrema[N]) MarshalText() ([]byte, error) { if !e.valid { - return nil, nil + return json.Marshal(nil) } return json.Marshal(e.value) } From 4766eecb1b0dc7c51ecd31a99ffa9ff4cac850cf Mon Sep 17 00:00:00 2001 From: Michal Posluszny Date: Sat, 20 Jan 2024 13:22:19 +0100 Subject: [PATCH 8/9] Updated mockData to include example with Min and Max values. --- exporters/stdout/stdoutmetric/example_test.go | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/exporters/stdout/stdoutmetric/example_test.go b/exporters/stdout/stdoutmetric/example_test.go index ab43479786c..3086c064d01 100644 --- a/exporters/stdout/stdoutmetric/example_test.go +++ b/exporters/stdout/stdoutmetric/example_test.go @@ -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", @@ -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", From 141bcc959066ef32108227eff91bde6f8c823264 Mon Sep 17 00:00:00 2001 From: Michal Posluszny Date: Mon, 22 Jan 2024 17:00:54 +0100 Subject: [PATCH 9/9] null value test, changelog fix --- CHANGELOG.md | 2 +- sdk/metric/metricdata/metricdatatest/assertion_test.go | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e1163c6cb3..fb9630663ad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### 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) +- 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`. (#4827) ## [1.23.0-rc.1] 2024-01-18 diff --git a/sdk/metric/metricdata/metricdatatest/assertion_test.go b/sdk/metric/metricdata/metricdatatest/assertion_test.go index 584910962a0..4219375c4ba 100644 --- a/sdk/metric/metricdata/metricdatatest/assertion_test.go +++ b/sdk/metric/metricdata/metricdatatest/assertion_test.go @@ -1023,6 +1023,8 @@ func AssertMarshal[N int64 | float64](t *testing.T, expected string, i *metricda } func TestAssertMarshal(t *testing.T) { + AssertMarshal(t, "null", &metricdata.Extrema[int64]{}) + AssertMarshal(t, "-1", &minFloat64A) AssertMarshal(t, "3", &minFloat64B) AssertMarshal(t, "-9.999999", &minFloat64D)