Skip to content

Commit

Permalink
translate IntGauge to Gauge 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 9accefa commit 09e28b2
Show file tree
Hide file tree
Showing 2 changed files with 136 additions and 14 deletions.
53 changes: 39 additions & 14 deletions model/internal/otlp_wrapper.go
Expand Up @@ -41,6 +41,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
//
func MetricsCompatibilityChanges(req *otlpcollectormetrics.ExportMetricsServiceRequest) {
for _, rsm := range req.ResourceMetrics {
Expand All @@ -49,7 +50,9 @@ func MetricsCompatibilityChanges(req *otlpcollectormetrics.ExportMetricsServiceR
switch m := metric.Data.(type) {
case *otlpmetrics.Metric_IntHistogram:
metric.Data = intHistogramToHistogram(m)
// TODO: add cases for IntGauge and IntSum
case *otlpmetrics.Metric_IntGauge:
metric.Data = intGaugeToGauge(m)
// TODO: add cases for IntSum
default:
}
}
Expand Down Expand Up @@ -118,18 +121,6 @@ func LogsFromOtlp(req *otlpcollectorlog.ExportLogsServiceRequest) LogsWrapper {
func intHistogramToHistogram(src *otlpmetrics.Metric_IntHistogram) *otlpmetrics.Metric_Histogram {
datapoints := []*otlpmetrics.HistogramDataPoint{}
for _, datapoint := range src.IntHistogram.DataPoints {
exemplars := []*otlpmetrics.Exemplar{}
for _, exemplar := range datapoint.Exemplars {
exemplars = append(exemplars, &otlpmetrics.Exemplar{
FilteredLabels: exemplar.FilteredLabels,
TimeUnixNano: exemplar.TimeUnixNano,
Value: &otlpmetrics.Exemplar_AsInt{
AsInt: exemplar.Value,
},
SpanId: exemplar.SpanId,
TraceId: exemplar.TraceId,
})
}
datapoints = append(datapoints, &otlpmetrics.HistogramDataPoint{
Labels: datapoint.Labels,
TimeUnixNano: datapoint.TimeUnixNano,
Expand All @@ -138,7 +129,7 @@ func intHistogramToHistogram(src *otlpmetrics.Metric_IntHistogram) *otlpmetrics.
Sum: float64(datapoint.Sum),
BucketCounts: datapoint.BucketCounts,
ExplicitBounds: datapoint.ExplicitBounds,
Exemplars: exemplars,
Exemplars: intExemplarToExemplar(datapoint.Exemplars),
})
}
return &otlpmetrics.Metric_Histogram{
Expand All @@ -148,3 +139,37 @@ func intHistogramToHistogram(src *otlpmetrics.Metric_IntHistogram) *otlpmetrics.
},
}
}

func intGaugeToGauge(src *otlpmetrics.Metric_IntGauge) *otlpmetrics.Metric_Gauge {
datapoints := []*otlpmetrics.NumberDataPoint{}
for _, datapoint := range src.IntGauge.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_Gauge{
Gauge: &otlpmetrics.Gauge{
DataPoints: datapoints,
},
}
}

func intExemplarToExemplar(src []otlpmetrics.IntExemplar) []*otlpmetrics.Exemplar { //nolint:staticcheck // SA1019 ignore this!
exemplars := []*otlpmetrics.Exemplar{}
for _, exemplar := range src {
exemplars = append(exemplars, &otlpmetrics.Exemplar{
FilteredLabels: exemplar.FilteredLabels,
TimeUnixNano: exemplar.TimeUnixNano,
Value: &otlpmetrics.Exemplar_AsInt{
AsInt: exemplar.Value,
},
SpanId: exemplar.SpanId,
TraceId: exemplar.TraceId,
})
}
return exemplars
}
97 changes: 97 additions & 0 deletions model/internal/otlp_wrappers_test.go
Expand Up @@ -231,3 +231,100 @@ func TestDeprecatedIntHistogram(t *testing.T) {
})
}
}

func TestDeprecatedIntGauge(t *testing.T) {
tests := []struct {
inputMetrics []*otlpmetrics.Metric
outputMetrics []*otlpmetrics.Metric
}{
{
inputMetrics: []*otlpmetrics.Metric{{
Data: &otlpmetrics.Metric_Gauge{
Gauge: &otlpmetrics.Gauge{
DataPoints: []*otlpmetrics.NumberDataPoint{
{
Labels: []otlpcommon.StringKeyValue{
{Key: "GaugeKey", Value: "GaugeValue"},
},
StartTimeUnixNano: 10,
TimeUnixNano: 11,
Value: &otlpmetrics.NumberDataPoint_AsInt{AsInt: 100},
Exemplars: []*otlpmetrics.Exemplar{},
},
},
},
},
}},
outputMetrics: []*otlpmetrics.Metric{{
Data: &otlpmetrics.Metric_Gauge{
Gauge: &otlpmetrics.Gauge{
DataPoints: []*otlpmetrics.NumberDataPoint{
{
Labels: []otlpcommon.StringKeyValue{
{Key: "GaugeKey", Value: "GaugeValue"},
},
StartTimeUnixNano: 10,
TimeUnixNano: 11,
Value: &otlpmetrics.NumberDataPoint_AsInt{AsInt: 100},
Exemplars: []*otlpmetrics.Exemplar{},
},
},
},
},
}},
},
{
inputMetrics: []*otlpmetrics.Metric{{
Data: &otlpmetrics.Metric_IntGauge{
IntGauge: &otlpmetrics.IntGauge{ //nolint:staticcheck // SA1019 ignore this!
DataPoints: []*otlpmetrics.IntDataPoint{ //nolint:staticcheck // SA1019 ignore this!
{
Labels: []otlpcommon.StringKeyValue{
{Key: "IntGaugeKey", Value: "IntGaugeValue"},
},
StartTimeUnixNano: 10,
TimeUnixNano: 11,
Value: 100,
Exemplars: []otlpmetrics.IntExemplar{}, //nolint:staticcheck // SA1019 ignore this!
},
},
},
},
}},
outputMetrics: []*otlpmetrics.Metric{{
Data: &otlpmetrics.Metric_Gauge{
Gauge: &otlpmetrics.Gauge{
DataPoints: []*otlpmetrics.NumberDataPoint{
{
Labels: []otlpcommon.StringKeyValue{
{Key: "IntGaugeKey", Value: "IntGaugeValue"},
},
StartTimeUnixNano: 10,
TimeUnixNano: 11,
Value: &otlpmetrics.NumberDataPoint_AsInt{AsInt: 100},
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)
})
}
}

0 comments on commit 09e28b2

Please sign in to comment.