Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

translate IntGauge to Gauge in otlp_wrappers #3619

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 39 additions & 14 deletions model/internal/otlp_wrapper.go
Original file line number Diff line number Diff line change
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!
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In a followup PR, we need to updated the change for the exemplars #2402, because we don't have DoubleExemplars anymore :)

So need to change the sed file, and regenerate the protos, and update the pdata generator to make Exemplars array of values.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see where the DoubleExemplar is in the sed file, though im not seeing where that's being utilized in the protos as is. They're all either Exemplar or IntExemplar, does the Double get dropped somewhere?

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
Original file line number Diff line number Diff line change
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)
})
}
}