From 88d26e1f88ab8f25d80776f57f5f0634ef69aeec Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Wed, 14 Jul 2021 09:53:28 -0700 Subject: [PATCH] translate IntSum to Sum in otlp_wrappers --- model/internal/otlp_wrapper.go | 21 +++++- model/internal/otlp_wrappers_test.go | 109 +++++++++++++++++++++++++-- 2 files changed, 123 insertions(+), 7 deletions(-) diff --git a/model/internal/otlp_wrapper.go b/model/internal/otlp_wrapper.go index 2acbf9a843a..426f9e5aee6 100644 --- a/model/internal/otlp_wrapper.go +++ b/model/internal/otlp_wrapper.go @@ -52,7 +52,8 @@ func MetricsCompatibilityChanges(req *otlpcollectormetrics.ExportMetricsServiceR metric.Data = intHistogramToHistogram(m) case *otlpmetrics.Metric_IntGauge: metric.Data = intGaugeToGauge(m) - // TODO: add cases for IntSum + case *otlpmetrics.Metric_IntSum: + metric.Data = intSumToSum(m) default: } } @@ -158,6 +159,24 @@ func intGaugeToGauge(src *otlpmetrics.Metric_IntGauge) *otlpmetrics.Metric_Gauge } } +func intSumToSum(src *otlpmetrics.Metric_IntSum) *otlpmetrics.Metric_Sum { + datapoints := []*otlpmetrics.NumberDataPoint{} + for _, datapoint := range src.IntSum.DataPoints { + datapoints = append(datapoints, &otlpmetrics.NumberDataPoint{ + Labels: datapoint.Labels, + TimeUnixNano: datapoint.TimeUnixNano, + StartTimeUnixNano: datapoint.StartTimeUnixNano, + Exemplars: intExemplarToExemplar(datapoint.Exemplars), + Value: &otlpmetrics.NumberDataPoint_AsInt{AsInt: datapoint.Value}, + }) + } + return &otlpmetrics.Metric_Sum{ + Sum: &otlpmetrics.Sum{ + DataPoints: datapoints, + }, + } +} + func intExemplarToExemplar(src []otlpmetrics.IntExemplar) []*otlpmetrics.Exemplar { //nolint:staticcheck // SA1019 ignore this! exemplars := []*otlpmetrics.Exemplar{} for _, exemplar := range src { diff --git a/model/internal/otlp_wrappers_test.go b/model/internal/otlp_wrappers_test.go index 4b4ce76b5a7..24fd145ee87 100644 --- a/model/internal/otlp_wrappers_test.go +++ b/model/internal/otlp_wrappers_test.go @@ -282,9 +282,9 @@ func TestDeprecatedIntGauge(t *testing.T) { Labels: []otlpcommon.StringKeyValue{ {Key: "IntGaugeKey", Value: "IntGaugeValue"}, }, - StartTimeUnixNano: 10, - TimeUnixNano: 11, - Value: 100, + StartTimeUnixNano: 12, + TimeUnixNano: 13, + Value: 101, Exemplars: []otlpmetrics.IntExemplar{}, //nolint:staticcheck // SA1019 ignore this! }, }, @@ -299,9 +299,106 @@ func TestDeprecatedIntGauge(t *testing.T) { Labels: []otlpcommon.StringKeyValue{ {Key: "IntGaugeKey", Value: "IntGaugeValue"}, }, - StartTimeUnixNano: 10, - TimeUnixNano: 11, - Value: &otlpmetrics.NumberDataPoint_AsInt{AsInt: 100}, + StartTimeUnixNano: 12, + TimeUnixNano: 13, + Value: &otlpmetrics.NumberDataPoint_AsInt{AsInt: 101}, + Exemplars: []*otlpmetrics.Exemplar{}, + }, + }, + }, + }, + }}, + }, + } + + for _, test := range tests { + t.Run(test.inputMetrics[0].Description, func(t *testing.T) { + req := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + Metrics: test.inputMetrics, + }, + }}, + }, + } + MetricsCompatibilityChanges(req) + assert.EqualValues(t, test.outputMetrics, req.ResourceMetrics[0].InstrumentationLibraryMetrics[0].Metrics) + }) + } +} + +func TestDeprecatedIntSum(t *testing.T) { + tests := []struct { + inputMetrics []*otlpmetrics.Metric + outputMetrics []*otlpmetrics.Metric + }{ + { + inputMetrics: []*otlpmetrics.Metric{{ + Data: &otlpmetrics.Metric_Sum{ + Sum: &otlpmetrics.Sum{ + DataPoints: []*otlpmetrics.NumberDataPoint{ + { + Labels: []otlpcommon.StringKeyValue{ + {Key: "SumKey", Value: "SumValue"}, + }, + StartTimeUnixNano: 20, + TimeUnixNano: 21, + Value: &otlpmetrics.NumberDataPoint_AsInt{AsInt: 200}, + Exemplars: []*otlpmetrics.Exemplar{}, + }, + }, + }, + }, + }}, + outputMetrics: []*otlpmetrics.Metric{{ + Data: &otlpmetrics.Metric_Sum{ + Sum: &otlpmetrics.Sum{ + DataPoints: []*otlpmetrics.NumberDataPoint{ + { + Labels: []otlpcommon.StringKeyValue{ + {Key: "SumKey", Value: "SumValue"}, + }, + StartTimeUnixNano: 20, + TimeUnixNano: 21, + Value: &otlpmetrics.NumberDataPoint_AsInt{AsInt: 200}, + Exemplars: []*otlpmetrics.Exemplar{}, + }, + }, + }, + }, + }}, + }, + { + inputMetrics: []*otlpmetrics.Metric{{ + Data: &otlpmetrics.Metric_IntSum{ + IntSum: &otlpmetrics.IntSum{ //nolint:staticcheck // SA1019 ignore this! + DataPoints: []*otlpmetrics.IntDataPoint{ //nolint:staticcheck // SA1019 ignore this! + { + Labels: []otlpcommon.StringKeyValue{ + {Key: "IntSumKey", Value: "IntSumValue"}, + }, + StartTimeUnixNano: 22, + TimeUnixNano: 23, + Value: 201, + Exemplars: []otlpmetrics.IntExemplar{}, //nolint:staticcheck // SA1019 ignore this! + }, + }, + }, + }, + }}, + outputMetrics: []*otlpmetrics.Metric{{ + Data: &otlpmetrics.Metric_Sum{ + Sum: &otlpmetrics.Sum{ + DataPoints: []*otlpmetrics.NumberDataPoint{ + { + Labels: []otlpcommon.StringKeyValue{ + {Key: "IntSumKey", Value: "IntSumValue"}, + }, + StartTimeUnixNano: 22, + TimeUnixNano: 23, + Value: &otlpmetrics.NumberDataPoint_AsInt{AsInt: 201}, Exemplars: []*otlpmetrics.Exemplar{}, }, },