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 16264bb commit e3b0710
Show file tree
Hide file tree
Showing 4 changed files with 153 additions and 9 deletions.
2 changes: 1 addition & 1 deletion exporter/otlpexporter/otlp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ func TestSendMetrics(t *testing.T) {
assert.EqualValues(t, 0, atomic.LoadInt32(&rcv.totalItems))

// A trace with 2 spans.
md = testdata.GenerateMetricsTwoMetrics()
md = testdata.GenerateMetricsTwoSumMetrics()

err = exp.ConsumeMetrics(context.Background(), md)
assert.NoError(t, err)
Expand Down
28 changes: 28 additions & 0 deletions internal/testdata/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,18 @@ func GenerateMetricsTwoMetrics() pdata.Metrics {
return md
}

func GenerateMetricsTwoSumMetrics() pdata.Metrics {
// TODO: this is currently used in place of GenerateMetricsTwoMetrics
// because the OTLP receiver converts IntSum to Sum types and causes a
// failure in TestSendMetrics. Once Sum supports setting either Int
// or Double, this function can be removed.
md := GenerateMetricsOneEmptyInstrumentationLibrary()
rm0ils0 := md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0)
initSumMetric(rm0ils0.Metrics().AppendEmpty())
initSumMetric(rm0ils0.Metrics().AppendEmpty())
return md
}

func GenerateMetricsOneCounterOneSummaryMetrics() pdata.Metrics {
md := GenerateMetricsOneEmptyInstrumentationLibrary()
rm0ils0 := md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0)
Expand Down Expand Up @@ -172,6 +184,22 @@ func GeneratMetricsAllTypesWithSampleDatapoints() pdata.Metrics {
return metricData
}

func initSumMetric(im pdata.Metric) {
initMetric(im, TestCounterIntMetricName, pdata.MetricDataTypeSum)

idps := im.Sum().DataPoints()
idp0 := idps.AppendEmpty()
initMetricLabels1(idp0.LabelsMap())
idp0.SetStartTimestamp(TestMetricStartTimestamp)
idp0.SetTimestamp(TestMetricTimestamp)
idp0.SetValue(123)
idp1 := idps.AppendEmpty()
initMetricLabels2(idp1.LabelsMap())
idp1.SetStartTimestamp(TestMetricStartTimestamp)
idp1.SetTimestamp(TestMetricTimestamp)
idp1.SetValue(456)
}

func initCounterIntMetric(im pdata.Metric) {
initMetric(im, TestCounterIntMetricName, pdata.MetricDataTypeIntSum)

Expand Down
23 changes: 21 additions & 2 deletions model/internal/otlp_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func MetricsFromOtlp(req *otlpcollectormetrics.ExportMetricsServiceRequest) Metr
// MetricsCompatibilityChanges performs backward compatibility conversion on Metrics:
// - Convert IntHistogram to Histogram. See https://github.com/open-telemetry/opentelemetry-proto/blob/f3b0ee0861d304f8f3126686ba9b01c106069cb0/opentelemetry/proto/metrics/v1/metrics.proto#L170
// - Convert IntGauge to Gauge. See https://github.com/open-telemetry/opentelemetry-proto/blob/f3b0ee0861d304f8f3126686ba9b01c106069cb0/opentelemetry/proto/metrics/v1/metrics.proto#L156
//
// - Convert IntSum to Sum. See https://github.com/open-telemetry/opentelemetry-proto/blob/f3b0ee0861d304f8f3126686ba9b01c106069cb0/opentelemetry/proto/metrics/v1/metrics.proto#L156
func MetricsCompatibilityChanges(req *otlpcollectormetrics.ExportMetricsServiceRequest) {
for _, rsm := range req.ResourceMetrics {
for _, ilm := range rsm.InstrumentationLibraryMetrics {
Expand All @@ -53,7 +53,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 @@ -160,6 +161,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 e3b0710

Please sign in to comment.