Skip to content

Commit

Permalink
translate IntSum to Sum in otlp_wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Boten committed Jul 14, 2021
1 parent 09e28b2 commit 88d26e1
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 7 deletions.
21 changes: 20 additions & 1 deletion model/internal/otlp_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:
}
}
Expand Down Expand Up @@ -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 {
Expand Down
109 changes: 103 additions & 6 deletions model/internal/otlp_wrappers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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!
},
},
Expand All @@ -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{},
},
},
Expand Down

0 comments on commit 88d26e1

Please sign in to comment.