diff --git a/consumer/pdata/metric.go b/consumer/pdata/metric.go index 89864aab23a..aac48ab8c3e 100644 --- a/consumer/pdata/metric.go +++ b/consumer/pdata/metric.go @@ -15,6 +15,7 @@ package pdata import ( + "go.opentelemetry.io/collector/internal" otlpcollectormetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1" otlpmetrics "go.opentelemetry.io/collector/internal/data/protogen/metrics/v1" ) @@ -37,57 +38,54 @@ func (at AggregationTemporality) String() string { // Outside of the core repository the metrics pipeline cannot be converted to the new model since data.MetricData is // part of the internal package. type Metrics struct { - orig *[]*otlpmetrics.ResourceMetrics + orig *otlpcollectormetrics.ExportMetricsServiceRequest } // NewMetrics creates a new Metrics. func NewMetrics() Metrics { - orig := []*otlpmetrics.ResourceMetrics(nil) - return Metrics{&orig} + return Metrics{orig: &otlpcollectormetrics.ExportMetricsServiceRequest{}} } -// MetricsFromOtlp creates the internal Metrics representation from the OTLP. -func MetricsFromOtlp(orig []*otlpmetrics.ResourceMetrics) Metrics { - return Metrics{&orig} +// MetricsFromInternalRep creates Logs from the internal representation. +// Should not be used outside this module. +func MetricsFromInternalRep(wrapper internal.MetricsWrapper) Metrics { + return Metrics{orig: internal.MetricsToOtlp(wrapper)} } -// MetricsToOtlp converts the internal Metrics to the OTLP. -func MetricsToOtlp(md Metrics) []*otlpmetrics.ResourceMetrics { - return *md.orig +// MetricsFromOtlpProtoBytes converts OTLP Collector ExportMetricsServiceRequest +// ProtoBuf bytes to the internal Metrics. +// +// Returns an invalid Metrics instance if error is not nil. +func MetricsFromOtlpProtoBytes(data []byte) (Metrics, error) { + req := otlpcollectormetrics.ExportMetricsServiceRequest{} + if err := req.Unmarshal(data); err != nil { + return Metrics{}, err + } + return Metrics{orig: &req}, nil +} + +// InternalRep returns internal representation of the Metrics. +// Should not be used outside this module. +func (md Metrics) InternalRep() internal.MetricsWrapper { + return internal.MetricsFromOtlp(md.orig) } // ToOtlpProtoBytes returns the internal MetricData to the OTLP Collector // ExportMetricsServiceRequest ProtoBuf bytes. This is intended to export // OTLP Protobuf bytes for OTLP/HTTP transports. func (md Metrics) ToOtlpProtoBytes() ([]byte, error) { - metrics := otlpcollectormetrics.ExportMetricsServiceRequest{ - ResourceMetrics: *md.orig, - } - return metrics.Marshal() -} - -// FromOtlpProtoBytes converts OTLP Collector ExportMetricsServiceRequest -// ProtoBuf bytes to the internal Metrics. Overrides current data. -// Calling this function on zero-initialized structure causes panic. -// Use it with NewMetrics or on existing initialized Metrics. -func (md Metrics) FromOtlpProtoBytes(data []byte) error { - metrics := otlpcollectormetrics.ExportMetricsServiceRequest{} - if err := metrics.Unmarshal(data); err != nil { - return err - } - *md.orig = metrics.ResourceMetrics - return nil + return md.orig.Marshal() } // Clone returns a copy of MetricData. func (md Metrics) Clone() Metrics { - rms := NewResourceMetricsSlice() - md.ResourceMetrics().CopyTo(rms) - return Metrics(rms) + cloneMd := NewMetrics() + md.ResourceMetrics().CopyTo(cloneMd.ResourceMetrics()) + return cloneMd } func (md Metrics) ResourceMetrics() ResourceMetricsSlice { - return newResourceMetricsSlice(md.orig) + return newResourceMetricsSlice(&md.orig.ResourceMetrics) } // MetricCount calculates the total number of metrics. @@ -107,14 +105,7 @@ func (md Metrics) MetricCount() int { // Size returns size in bytes. func (md Metrics) Size() int { - size := 0 - for i := 0; i < len(*md.orig); i++ { - if (*md.orig)[i] == nil { - continue - } - size += (*(*md.orig)[i]).Size() - } - return size + return md.orig.Size() } // MetricAndDataPointCount calculates the total number of metrics and data points. diff --git a/consumer/pdata/metric_test.go b/consumer/pdata/metric_test.go index bc7ff334ddc..7d5c224383f 100644 --- a/consumer/pdata/metric_test.go +++ b/consumer/pdata/metric_test.go @@ -23,6 +23,8 @@ import ( goproto "google.golang.org/protobuf/proto" "google.golang.org/protobuf/types/known/emptypb" + "go.opentelemetry.io/collector/internal" + otlpcollectormetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1" otlpcommon "go.opentelemetry.io/collector/internal/data/protogen/common/v1" otlpmetrics "go.opentelemetry.io/collector/internal/data/protogen/metrics/v1" otlpresource "go.opentelemetry.io/collector/internal/data/protogen/resource/v1" @@ -187,39 +189,22 @@ func TestMetricSize(t *testing.T) { doubleHistogram.DataPoints().Resize(1) doubleHistogram.DataPoints().At(0).SetCount(123) doubleHistogram.DataPoints().At(0).SetSum(123) - otlp := MetricsToOtlp(md) - size := 0 - sizeBytes := 0 - for _, rmerics := range otlp { - size += rmerics.Size() - bts, err := rmerics.Marshal() - require.NoError(t, err) - sizeBytes += len(bts) - } + otlp := internal.MetricsToOtlp(md.InternalRep()) + size := otlp.Size() + bytes, err := otlp.Marshal() + require.NoError(t, err) assert.Equal(t, size, md.Size()) - assert.Equal(t, sizeBytes, md.Size()) + assert.Equal(t, len(bytes), md.Size()) } func TestMetricsSizeWithNil(t *testing.T) { - assert.Equal(t, 0, MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{nil}).Size()) + assert.Equal(t, 0, NewMetrics().Size()) } func TestMetricCountWithEmpty(t *testing.T) { - assert.EqualValues(t, 0, MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{{}}).MetricCount()) - assert.EqualValues(t, 0, MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{{}}, - }, - }).MetricCount()) - assert.EqualValues(t, 1, MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - Metrics: []*otlpmetrics.Metric{{}}, - }, - }, - }, - }).MetricCount()) + assert.EqualValues(t, 0, generateMetricsEmptyResource().MetricCount()) + assert.EqualValues(t, 0, generateMetricsEmptyInstrumentation().MetricCount()) + assert.EqualValues(t, 1, generateMetricsEmptyMetrics().MetricCount()) } func TestMetricAndDataPointCount(t *testing.T) { @@ -275,47 +260,19 @@ func TestMetricAndDataPointCount(t *testing.T) { } func TestMetricAndDataPointCountWithEmpty(t *testing.T) { - ms, dps := MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{{}}).MetricAndDataPointCount() + ms, dps := generateMetricsEmptyResource().MetricAndDataPointCount() assert.EqualValues(t, 0, ms) assert.EqualValues(t, 0, dps) - ms, dps = MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{{}}, - }, - }).MetricAndDataPointCount() + ms, dps = generateMetricsEmptyInstrumentation().MetricAndDataPointCount() assert.EqualValues(t, 0, ms) assert.EqualValues(t, 0, dps) - ms, dps = MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - Metrics: []*otlpmetrics.Metric{{}}, - }, - }, - }, - }).MetricAndDataPointCount() + ms, dps = generateMetricsEmptyMetrics().MetricAndDataPointCount() assert.EqualValues(t, 1, ms) assert.EqualValues(t, 0, dps) - ms, dps = MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - Metrics: []*otlpmetrics.Metric{{ - Data: &otlpmetrics.Metric_DoubleGauge{ - DoubleGauge: &otlpmetrics.DoubleGauge{ - DataPoints: []*otlpmetrics.DoubleDataPoint{ - {}, - }, - }, - }, - }}, - }, - }, - }, - }).MetricAndDataPointCount() + ms, dps = generateMetricsEmptyDataPoints().MetricAndDataPointCount() assert.EqualValues(t, 1, ms) assert.EqualValues(t, 1, dps) @@ -353,17 +310,19 @@ func TestMetricAndDataPointCountWithNilDataPoints(t *testing.T) { } func TestOtlpToInternalReadOnly(t *testing.T) { - metricData := MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{ - { - Resource: generateTestProtoResource(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), - Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric(), generateTestProtoDoubleSumMetric(), generateTestProtoDoubleHistogramMetric()}, + metricData := MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateTestProtoResource(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), + Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric(), generateTestProtoDoubleSumMetric(), generateTestProtoDoubleHistogramMetric()}, + }, }, }, }, - }) + })) resourceMetrics := metricData.ResourceMetrics() assert.EqualValues(t, 1, resourceMetrics.Len()) @@ -439,46 +398,52 @@ func TestOtlpToInternalReadOnly(t *testing.T) { } func TestOtlpToFromInternalReadOnly(t *testing.T) { - metricData := MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{ - { - Resource: generateTestProtoResource(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), - Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric(), generateTestProtoDoubleSumMetric(), generateTestProtoDoubleHistogramMetric()}, + metricData := MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateTestProtoResource(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), + Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric(), generateTestProtoDoubleSumMetric(), generateTestProtoDoubleHistogramMetric()}, + }, }, }, }, - }) + })) // Test that nothing changed - assert.EqualValues(t, []*otlpmetrics.ResourceMetrics{ - { - Resource: generateTestProtoResource(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), - Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric(), generateTestProtoDoubleSumMetric(), generateTestProtoDoubleHistogramMetric()}, + assert.EqualValues(t, &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateTestProtoResource(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), + Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric(), generateTestProtoDoubleSumMetric(), generateTestProtoDoubleHistogramMetric()}, + }, }, }, }, - }, MetricsToOtlp(metricData)) + }, internal.MetricsToOtlp(metricData.InternalRep())) } func TestOtlpToFromInternalIntGaugeMutating(t *testing.T) { newLabels := NewStringMap().InitFromMap(map[string]string{"k": "v"}) - metricData := MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{ - { - Resource: generateTestProtoResource(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), - Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric()}, + md := MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateTestProtoResource(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), + Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric()}, + }, }, }, }, - }) - resourceMetrics := metricData.ResourceMetrics() + })) + resourceMetrics := md.ResourceMetrics() metric := resourceMetrics.At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0) // Mutate MetricDescriptor metric.SetName("new_my_metric_int") @@ -504,30 +469,32 @@ func TestOtlpToFromInternalIntGaugeMutating(t *testing.T) { assert.EqualValues(t, newLabels, int64DataPoints.At(0).LabelsMap()) // Test that everything is updated. - assert.EqualValues(t, []*otlpmetrics.ResourceMetrics{ - { - Resource: generateTestProtoResource(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), - Metrics: []*otlpmetrics.Metric{ - { - Name: "new_my_metric_int", - Description: "My new metric", - Unit: "1", - Data: &otlpmetrics.Metric_IntGauge{ - IntGauge: &otlpmetrics.IntGauge{ - DataPoints: []*otlpmetrics.IntDataPoint{ - { - Labels: []otlpcommon.StringKeyValue{ - { - Key: "k", - Value: "v", + assert.EqualValues(t, &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateTestProtoResource(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), + Metrics: []*otlpmetrics.Metric{ + { + Name: "new_my_metric_int", + Description: "My new metric", + Unit: "1", + Data: &otlpmetrics.Metric_IntGauge{ + IntGauge: &otlpmetrics.IntGauge{ + DataPoints: []*otlpmetrics.IntDataPoint{ + { + Labels: []otlpcommon.StringKeyValue{ + { + Key: "k", + Value: "v", + }, }, + StartTimeUnixNano: startTime + 1, + TimeUnixNano: endTime + 1, + Value: 124, }, - StartTimeUnixNano: startTime + 1, - TimeUnixNano: endTime + 1, - Value: 124, }, }, }, @@ -537,24 +504,26 @@ func TestOtlpToFromInternalIntGaugeMutating(t *testing.T) { }, }, }, - }, MetricsToOtlp(metricData)) + }, internal.MetricsToOtlp(md.InternalRep())) } func TestOtlpToFromInternalDoubleSumMutating(t *testing.T) { newLabels := NewStringMap().InitFromMap(map[string]string{"k": "v"}) - metricData := MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{ - { - Resource: generateTestProtoResource(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), - Metrics: []*otlpmetrics.Metric{generateTestProtoDoubleSumMetric()}, + md := MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateTestProtoResource(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), + Metrics: []*otlpmetrics.Metric{generateTestProtoDoubleSumMetric()}, + }, }, }, }, - }) - resourceMetrics := metricData.ResourceMetrics() + })) + resourceMetrics := md.ResourceMetrics() metric := resourceMetrics.At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0) // Mutate MetricDescriptor metric.SetName("new_my_metric_double") @@ -580,31 +549,33 @@ func TestOtlpToFromInternalDoubleSumMutating(t *testing.T) { assert.EqualValues(t, newLabels, doubleDataPoints.At(0).LabelsMap()) // Test that everything is updated. - assert.EqualValues(t, []*otlpmetrics.ResourceMetrics{ - { - Resource: generateTestProtoResource(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), - Metrics: []*otlpmetrics.Metric{ - { - Name: "new_my_metric_double", - Description: "My new metric", - Unit: "1", - Data: &otlpmetrics.Metric_DoubleSum{ - DoubleSum: &otlpmetrics.DoubleSum{ - AggregationTemporality: otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE, - DataPoints: []*otlpmetrics.DoubleDataPoint{ - { - Labels: []otlpcommon.StringKeyValue{ - { - Key: "k", - Value: "v", + assert.EqualValues(t, &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateTestProtoResource(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), + Metrics: []*otlpmetrics.Metric{ + { + Name: "new_my_metric_double", + Description: "My new metric", + Unit: "1", + Data: &otlpmetrics.Metric_DoubleSum{ + DoubleSum: &otlpmetrics.DoubleSum{ + AggregationTemporality: otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE, + DataPoints: []*otlpmetrics.DoubleDataPoint{ + { + Labels: []otlpcommon.StringKeyValue{ + { + Key: "k", + Value: "v", + }, }, + StartTimeUnixNano: startTime + 1, + TimeUnixNano: endTime + 1, + Value: 124.1, }, - StartTimeUnixNano: startTime + 1, - TimeUnixNano: endTime + 1, - Value: 124.1, }, }, }, @@ -614,24 +585,26 @@ func TestOtlpToFromInternalDoubleSumMutating(t *testing.T) { }, }, }, - }, MetricsToOtlp(metricData)) + }, internal.MetricsToOtlp(md.InternalRep())) } func TestOtlpToFromInternalHistogramMutating(t *testing.T) { newLabels := NewStringMap().InitFromMap(map[string]string{"k": "v"}) - metricData := MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{ - { - Resource: generateTestProtoResource(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), - Metrics: []*otlpmetrics.Metric{generateTestProtoDoubleHistogramMetric()}, + md := MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateTestProtoResource(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), + Metrics: []*otlpmetrics.Metric{generateTestProtoDoubleHistogramMetric()}, + }, }, }, }, - }) - resourceMetrics := metricData.ResourceMetrics() + })) + resourceMetrics := md.ResourceMetrics() metric := resourceMetrics.At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0) // Mutate MetricDescriptor metric.SetName("new_my_metric_histogram") @@ -657,32 +630,34 @@ func TestOtlpToFromInternalHistogramMutating(t *testing.T) { assert.EqualValues(t, []float64{1}, histogramDataPoints.At(0).ExplicitBounds()) histogramDataPoints.At(0).SetBucketCounts([]uint64{21, 32}) // Test that everything is updated. - assert.EqualValues(t, []*otlpmetrics.ResourceMetrics{ - { - Resource: generateTestProtoResource(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), - Metrics: []*otlpmetrics.Metric{ - { - Name: "new_my_metric_histogram", - Description: "My new metric", - Unit: "1", - Data: &otlpmetrics.Metric_DoubleHistogram{ - DoubleHistogram: &otlpmetrics.DoubleHistogram{ - AggregationTemporality: otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA, - DataPoints: []*otlpmetrics.DoubleHistogramDataPoint{ - { - Labels: []otlpcommon.StringKeyValue{ - { - Key: "k", - Value: "v", + assert.EqualValues(t, &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateTestProtoResource(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), + Metrics: []*otlpmetrics.Metric{ + { + Name: "new_my_metric_histogram", + Description: "My new metric", + Unit: "1", + Data: &otlpmetrics.Metric_DoubleHistogram{ + DoubleHistogram: &otlpmetrics.DoubleHistogram{ + AggregationTemporality: otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_DELTA, + DataPoints: []*otlpmetrics.DoubleHistogramDataPoint{ + { + Labels: []otlpcommon.StringKeyValue{ + { + Key: "k", + Value: "v", + }, }, + StartTimeUnixNano: startTime + 1, + TimeUnixNano: endTime + 1, + BucketCounts: []uint64{21, 32}, + ExplicitBounds: []float64{1}, }, - StartTimeUnixNano: startTime + 1, - TimeUnixNano: endTime + 1, - BucketCounts: []uint64{21, 32}, - ExplicitBounds: []float64{1}, }, }, }, @@ -692,7 +667,7 @@ func TestOtlpToFromInternalHistogramMutating(t *testing.T) { }, }, }, - }, MetricsToOtlp(metricData)) + }, internal.MetricsToOtlp(md.InternalRep())) } func TestMetricsToFromOtlpProtoBytes(t *testing.T) { @@ -701,14 +676,13 @@ func TestMetricsToFromOtlpProtoBytes(t *testing.T) { bytes, err := send.ToOtlpProtoBytes() assert.NoError(t, err) - recv := NewMetrics() - err = recv.FromOtlpProtoBytes(bytes) + recv, err := MetricsFromOtlpProtoBytes(bytes) assert.NoError(t, err) assert.EqualValues(t, send, recv) } func TestMetricsFromInvalidOtlpProtoBytes(t *testing.T) { - err := NewMetrics().FromOtlpProtoBytes([]byte{0xFF}) + _, err := MetricsFromOtlpProtoBytes([]byte{0xFF}) assert.EqualError(t, err, "unexpected EOF") } @@ -731,13 +705,15 @@ func BenchmarkMetricsClone(b *testing.B) { } func BenchmarkOtlpToFromInternal_PassThrough(b *testing.B) { - resourceMetricsList := []*otlpmetrics.ResourceMetrics{ - { - Resource: generateTestProtoResource(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), - Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric(), generateTestProtoDoubleSumMetric(), generateTestProtoDoubleHistogramMetric()}, + req := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateTestProtoResource(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), + Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric(), generateTestProtoDoubleSumMetric(), generateTestProtoDoubleHistogramMetric()}, + }, }, }, }, @@ -745,19 +721,24 @@ func BenchmarkOtlpToFromInternal_PassThrough(b *testing.B) { b.ResetTimer() for n := 0; n < b.N; n++ { - md := MetricsFromOtlp(resourceMetricsList) - MetricsToOtlp(md) + md := MetricsFromInternalRep(internal.MetricsFromOtlp(req)) + newReq := internal.MetricsToOtlp(md.InternalRep()) + if len(req.ResourceMetrics) != len(newReq.ResourceMetrics) { + b.Fail() + } } } func BenchmarkOtlpToFromInternal_IntGauge_MutateOneLabel(b *testing.B) { - resourceMetricsList := []*otlpmetrics.ResourceMetrics{ - { - Resource: generateTestProtoResource(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), - Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric()}, + req := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateTestProtoResource(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), + Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric()}, + }, }, }, }, @@ -765,20 +746,25 @@ func BenchmarkOtlpToFromInternal_IntGauge_MutateOneLabel(b *testing.B) { b.ResetTimer() for n := 0; n < b.N; n++ { - md := MetricsFromOtlp(resourceMetricsList) + md := MetricsFromInternalRep(internal.MetricsFromOtlp(req)) md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0).IntGauge().DataPoints().At(0).LabelsMap().Upsert("key0", "value2") - MetricsToOtlp(md) + newReq := internal.MetricsToOtlp(md.InternalRep()) + if len(req.ResourceMetrics) != len(newReq.ResourceMetrics) { + b.Fail() + } } } func BenchmarkOtlpToFromInternal_DoubleSum_MutateOneLabel(b *testing.B) { - resourceMetricsList := []*otlpmetrics.ResourceMetrics{ - { - Resource: generateTestProtoResource(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), - Metrics: []*otlpmetrics.Metric{generateTestProtoDoubleSumMetric()}, + req := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateTestProtoResource(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), + Metrics: []*otlpmetrics.Metric{generateTestProtoDoubleSumMetric()}, + }, }, }, }, @@ -786,20 +772,25 @@ func BenchmarkOtlpToFromInternal_DoubleSum_MutateOneLabel(b *testing.B) { b.ResetTimer() for n := 0; n < b.N; n++ { - md := MetricsFromOtlp(resourceMetricsList) + md := MetricsFromInternalRep(internal.MetricsFromOtlp(req)) md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0).DoubleSum().DataPoints().At(0).LabelsMap().Upsert("key0", "value2") - MetricsToOtlp(md) + newReq := internal.MetricsToOtlp(md.InternalRep()) + if len(req.ResourceMetrics) != len(newReq.ResourceMetrics) { + b.Fail() + } } } func BenchmarkOtlpToFromInternal_HistogramPoints_MutateOneLabel(b *testing.B) { - resourceMetricsList := []*otlpmetrics.ResourceMetrics{ - { - Resource: generateTestProtoResource(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), - Metrics: []*otlpmetrics.Metric{generateTestProtoDoubleHistogramMetric()}, + req := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateTestProtoResource(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), + Metrics: []*otlpmetrics.Metric{generateTestProtoDoubleHistogramMetric()}, + }, }, }, }, @@ -807,28 +798,34 @@ func BenchmarkOtlpToFromInternal_HistogramPoints_MutateOneLabel(b *testing.B) { b.ResetTimer() for n := 0; n < b.N; n++ { - md := MetricsFromOtlp(resourceMetricsList) + md := MetricsFromInternalRep(internal.MetricsFromOtlp(req)) md.ResourceMetrics().At(0).InstrumentationLibraryMetrics().At(0).Metrics().At(0).DoubleHistogram().DataPoints().At(0).LabelsMap().Upsert("key0", "value2") - MetricsToOtlp(md) + newReq := internal.MetricsToOtlp(md.InternalRep()) + if len(req.ResourceMetrics) != len(newReq.ResourceMetrics) { + b.Fail() + } } } func BenchmarkMetrics_ToOtlpProtoBytes_PassThrough(b *testing.B) { - metrics := MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{ - { - Resource: generateTestProtoResource(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), - Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric(), generateTestProtoDoubleSumMetric(), generateTestProtoDoubleHistogramMetric()}, + req := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateTestProtoResource(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + InstrumentationLibrary: generateTestProtoInstrumentationLibrary(), + Metrics: []*otlpmetrics.Metric{generateTestProtoIntGaugeMetric(), generateTestProtoDoubleSumMetric(), generateTestProtoDoubleHistogramMetric()}, + }, }, }, }, - }) + } + md := MetricsFromInternalRep(internal.MetricsFromOtlp(req)) b.ResetTimer() for n := 0; n < b.N; n++ { - _, _ = metrics.ToOtlpProtoBytes() + _, _ = md.ToOtlpProtoBytes() } } @@ -852,9 +849,9 @@ func BenchmarkMetricsFromOtlp(b *testing.B) { b.ResetTimer() b.ReportAllocs() for n := 0; n < b.N; n++ { - traces := NewMetrics() - require.NoError(b, traces.FromOtlpProtoBytes(buf)) - assert.Equal(b, baseMetrics.ResourceMetrics().Len(), traces.ResourceMetrics().Len()) + md, err := MetricsFromOtlpProtoBytes(buf) + require.NoError(b, err) + assert.Equal(b, baseMetrics.ResourceMetrics().Len(), md.ResourceMetrics().Len()) } } @@ -986,3 +983,57 @@ func generateTestProtoDoubleHistogramMetric() *otlpmetrics.Metric { }, } } + +func generateMetricsEmptyResource() Metrics { + return MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{{}}, + })) +} + +func generateMetricsEmptyInstrumentation() Metrics { + return MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{{}}, + }, + }, + })) +} + +func generateMetricsEmptyMetrics() Metrics { + return MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + Metrics: []*otlpmetrics.Metric{{}}, + }, + }, + }, + }, + })) +} + +func generateMetricsEmptyDataPoints() Metrics { + return MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + Metrics: []*otlpmetrics.Metric{ + { + Data: &otlpmetrics.Metric_DoubleGauge{ + DoubleGauge: &otlpmetrics.DoubleGauge{ + DataPoints: []*otlpmetrics.DoubleDataPoint{ + {}, + }, + }, + }, + }, + }, + }, + }, + }, + }, + })) +} diff --git a/consumer/simple/metrics_test.go b/consumer/simple/metrics_test.go index ab8a76378bb..7d280cae7ea 100644 --- a/consumer/simple/metrics_test.go +++ b/consumer/simple/metrics_test.go @@ -25,6 +25,7 @@ import ( "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/internal" "go.opentelemetry.io/collector/testutil/metricstestutil" ) @@ -283,7 +284,8 @@ func TestMetrics(t *testing.T) { mCount, dpCount := metrics.MetricAndDataPointCount() require.Equal(t, 7, mCount) require.Equal(t, 8, dpCount) - asJSON, _ := json.MarshalIndent(pdata.MetricsToOtlp(metricstestutil.SortedMetrics(metrics)), "", " ") + req := internal.MetricsToOtlp(metricstestutil.SortedMetrics(metrics).InternalRep()) + asJSON, _ := json.MarshalIndent(req.ResourceMetrics, "", " ") require.Equal(t, expected, string(asJSON)) } diff --git a/exporter/fileexporter/file_exporter.go b/exporter/fileexporter/file_exporter.go index f0a8a5b4554..580cb4f73dc 100644 --- a/exporter/fileexporter/file_exporter.go +++ b/exporter/fileexporter/file_exporter.go @@ -25,7 +25,6 @@ import ( "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/consumer/pdata" "go.opentelemetry.io/collector/internal" - otlpmetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1" otlptrace "go.opentelemetry.io/collector/internal/data/protogen/collector/trace/v1" ) @@ -47,10 +46,7 @@ func (e *fileExporter) ConsumeTraces(_ context.Context, td pdata.Traces) error { } func (e *fileExporter) ConsumeMetrics(_ context.Context, md pdata.Metrics) error { - request := otlpmetrics.ExportMetricsServiceRequest{ - ResourceMetrics: pdata.MetricsToOtlp(md), - } - return exportMessageAsLine(e, &request) + return exportMessageAsLine(e, internal.MetricsToOtlp(md.InternalRep())) } func (e *fileExporter) ConsumeLogs(_ context.Context, ld pdata.Logs) error { diff --git a/exporter/fileexporter/file_exporter_test.go b/exporter/fileexporter/file_exporter_test.go index 0d7f413b7a6..8a7b3071594 100644 --- a/exporter/fileexporter/file_exporter_test.go +++ b/exporter/fileexporter/file_exporter_test.go @@ -61,10 +61,10 @@ func TestFileMetricsExporterNoErrors(t *testing.T) { assert.NoError(t, lme.Shutdown(context.Background())) var unmarshaler = &jsonpb.Unmarshaler{} - var j collectormetrics.ExportMetricsServiceRequest - assert.NoError(t, unmarshaler.Unmarshal(mf, &j)) + j := &collectormetrics.ExportMetricsServiceRequest{} + assert.NoError(t, unmarshaler.Unmarshal(mf, j)) - assert.EqualValues(t, pdata.MetricsToOtlp(md), j.ResourceMetrics) + assert.EqualValues(t, internal.MetricsToOtlp(md.InternalRep()), j) } func TestFileLogsExporterNoErrors(t *testing.T) { diff --git a/exporter/kafkaexporter/otlp_marshaller_test.go b/exporter/kafkaexporter/otlp_marshaller_test.go index 39f12fd1c4b..069c8a4614c 100644 --- a/exporter/kafkaexporter/otlp_marshaller_test.go +++ b/exporter/kafkaexporter/otlp_marshaller_test.go @@ -44,8 +44,7 @@ func TestOTLPMetricsPbMarshaller(t *testing.T) { messages, err := m.Marshal(md) require.NoError(t, err) require.Len(t, messages, 1) - extracted := pdata.NewMetrics() - err = extracted.FromOtlpProtoBytes(messages[0].Value) + extracted, err := pdata.MetricsFromOtlpProtoBytes(messages[0].Value) require.NoError(t, err) assert.EqualValues(t, md, extracted) } diff --git a/exporter/otlpexporter/otlp.go b/exporter/otlpexporter/otlp.go index a2907eecba3..f03023cb525 100644 --- a/exporter/otlpexporter/otlp.go +++ b/exporter/otlpexporter/otlp.go @@ -76,10 +76,8 @@ func (e *exporterImp) pushTraceData(ctx context.Context, td pdata.Traces) error } func (e *exporterImp) pushMetricsData(ctx context.Context, md pdata.Metrics) error { - request := &otlpmetrics.ExportMetricsServiceRequest{ - ResourceMetrics: pdata.MetricsToOtlp(md), - } - if err := e.w.exportMetrics(ctx, request); err != nil { + req := internal.MetricsToOtlp(md.InternalRep()) + if err := e.w.exportMetrics(ctx, req); err != nil { return fmt.Errorf("failed to push metrics data via OTLP exporter: %w", err) } return nil diff --git a/exporter/otlpexporter/otlp_test.go b/exporter/otlpexporter/otlp_test.go index 9118fd7d657..afc94a24f93 100644 --- a/exporter/otlpexporter/otlp_test.go +++ b/exporter/otlpexporter/otlp_test.go @@ -158,7 +158,7 @@ func (r *mockMetricsReceiver) Export( req *otlpmetrics.ExportMetricsServiceRequest, ) (*otlpmetrics.ExportMetricsServiceResponse, error) { atomic.AddInt32(&r.requestCount, 1) - _, recordCount := pdata.MetricsFromOtlp(req.ResourceMetrics).MetricAndDataPointCount() + _, recordCount := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(req)).MetricAndDataPointCount() atomic.AddInt32(&r.totalItems, int32(recordCount)) r.mux.Lock() defer r.mux.Unlock() @@ -311,9 +311,7 @@ func TestSendMetrics(t *testing.T) { // A trace with 2 spans. md = testdata.GenerateMetricsTwoMetrics() - expectedOTLPReq := &otlpmetrics.ExportMetricsServiceRequest{ - ResourceMetrics: testdata.GenerateMetricsOtlpTwoMetrics(), - } + expectedOTLPReq := internal.MetricsToOtlp(md.Clone().InternalRep()) err = exp.ConsumeMetrics(context.Background(), md) assert.NoError(t, err) diff --git a/exporter/prometheusremotewriteexporter/exporter.go b/exporter/prometheusremotewriteexporter/exporter.go index ad66be0a563..420832d3bd2 100644 --- a/exporter/prometheusremotewriteexporter/exporter.go +++ b/exporter/prometheusremotewriteexporter/exporter.go @@ -33,6 +33,7 @@ import ( "go.opentelemetry.io/collector/consumer/consumererror" "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/internal" otlp "go.opentelemetry.io/collector/internal/data/protogen/metrics/v1" "go.opentelemetry.io/collector/internal/version" ) @@ -101,7 +102,7 @@ func (prwe *PrwExporter) PushMetrics(ctx context.Context, md pdata.Metrics) erro tsMap := map[string]*prompb.TimeSeries{} dropped := 0 var errs []error - resourceMetrics := pdata.MetricsToOtlp(md) + resourceMetrics := internal.MetricsToOtlp(md.InternalRep()).ResourceMetrics for _, resourceMetric := range resourceMetrics { if resourceMetric == nil { continue diff --git a/exporter/prometheusremotewriteexporter/exporter_test.go b/exporter/prometheusremotewriteexporter/exporter_test.go index fce766f18fc..1224cf5fc45 100644 --- a/exporter/prometheusremotewriteexporter/exporter_test.go +++ b/exporter/prometheusremotewriteexporter/exporter_test.go @@ -33,6 +33,8 @@ import ( "go.opentelemetry.io/collector/config/configmodels" "go.opentelemetry.io/collector/consumer/pdata" "go.opentelemetry.io/collector/exporter/exporterhelper" + "go.opentelemetry.io/collector/internal" + otlpcollectormetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1" otlp "go.opentelemetry.io/collector/internal/data/protogen/metrics/v1" "go.opentelemetry.io/collector/internal/testdata" "go.opentelemetry.io/collector/internal/version" @@ -262,208 +264,238 @@ func Test_PushMetrics(t *testing.T) { // success cases intSumBatch := testdata.GenerateMetricsManyMetricsSameResource(10) - doubleSumMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - validMetrics1[validDoubleSum], - validMetrics2[validDoubleSum], + doubleSumMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + validMetrics1[validDoubleSum], + validMetrics2[validDoubleSum], + }, }, }, }, }, } - doubleSumBatch := pdata.MetricsFromOtlp(doubleSumMetric) - - intGaugeMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - validMetrics1[validIntGauge], - validMetrics2[validIntGauge], + doubleSumBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(doubleSumMetric)) + + intGaugeMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + validMetrics1[validIntGauge], + validMetrics2[validIntGauge], + }, }, }, }, }, } - intGaugeBatch := pdata.MetricsFromOtlp(intGaugeMetric) - - doubleGaugeMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - validMetrics1[validDoubleGauge], - validMetrics2[validDoubleGauge], + intGaugeBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(intGaugeMetric)) + + doubleGaugeMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + validMetrics1[validDoubleGauge], + validMetrics2[validDoubleGauge], + }, }, }, }, }, } - doubleGaugeBatch := pdata.MetricsFromOtlp(doubleGaugeMetric) - - intHistogramMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - validMetrics1[validIntHistogram], - validMetrics2[validIntHistogram], + doubleGaugeBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(doubleGaugeMetric)) + + intHistogramMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + validMetrics1[validIntHistogram], + validMetrics2[validIntHistogram], + }, }, }, }, }, } - intHistogramBatch := pdata.MetricsFromOtlp(intHistogramMetric) - - doubleHistogramMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - validMetrics1[validDoubleHistogram], - validMetrics2[validDoubleHistogram], + intHistogramBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(intHistogramMetric)) + + doubleHistogramMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + validMetrics1[validDoubleHistogram], + validMetrics2[validDoubleHistogram], + }, }, }, }, }, } - doubleHistogramBatch := pdata.MetricsFromOtlp(doubleHistogramMetric) - - doubleSummaryMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - validMetrics1[validDoubleSummary], - validMetrics2[validDoubleSummary], + doubleHistogramBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(doubleHistogramMetric)) + + doubleSummaryMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + validMetrics1[validDoubleSummary], + validMetrics2[validDoubleSummary], + }, }, }, }, }, } - doubleSummaryBatch := pdata.MetricsFromOtlp(doubleSummaryMetric) + doubleSummaryBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(doubleSummaryMetric)) // len(BucketCount) > len(ExplicitBounds) - unmatchedBoundBucketIntHistMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - validMetrics2[unmatchedBoundBucketIntHist], + unmatchedBoundBucketIntHistMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + validMetrics2[unmatchedBoundBucketIntHist], + }, }, }, }, }, } - unmatchedBoundBucketIntHistBatch := pdata.MetricsFromOtlp(unmatchedBoundBucketIntHistMetric) - - unmatchedBoundBucketDoubleHistMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - validMetrics2[unmatchedBoundBucketDoubleHist], + unmatchedBoundBucketIntHistBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(unmatchedBoundBucketIntHistMetric)) + + unmatchedBoundBucketDoubleHistMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + validMetrics2[unmatchedBoundBucketDoubleHist], + }, }, }, }, }, } - unmatchedBoundBucketDoubleHistBatch := pdata.MetricsFromOtlp(unmatchedBoundBucketDoubleHistMetric) + unmatchedBoundBucketDoubleHistBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(unmatchedBoundBucketDoubleHistMetric)) // fail cases - nilDataPointIntGaugeMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - errorMetrics[nilDataPointIntGauge], + nilDataPointIntGaugeMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + errorMetrics[nilDataPointIntGauge], + }, }, }, }, }, } - nilDataPointIntGaugeBatch := pdata.MetricsFromOtlp(nilDataPointIntGaugeMetric) - - nilDataPointDoubleGaugeMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - errorMetrics[nilDataPointDoubleGauge], + nilDataPointIntGaugeBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(nilDataPointIntGaugeMetric)) + + nilDataPointDoubleGaugeMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + errorMetrics[nilDataPointDoubleGauge], + }, }, }, }, }, } - nilDataPointDoubleGaugeBatch := pdata.MetricsFromOtlp(nilDataPointDoubleGaugeMetric) - - nilDataPointIntSumMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - errorMetrics[nilDataPointIntSum], + nilDataPointDoubleGaugeBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(nilDataPointDoubleGaugeMetric)) + + nilDataPointIntSumMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + errorMetrics[nilDataPointIntSum], + }, }, }, }, }, } - nilDataPointIntSumBatch := pdata.MetricsFromOtlp(nilDataPointIntSumMetric) - - nilDataPointDoubleSumMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - errorMetrics[nilDataPointDoubleSum], + nilDataPointIntSumBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(nilDataPointIntSumMetric)) + + nilDataPointDoubleSumMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + errorMetrics[nilDataPointDoubleSum], + }, }, }, }, }, } - nilDataPointDoubleSumBatch := pdata.MetricsFromOtlp(nilDataPointDoubleSumMetric) - - nilDataPointIntHistogramMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - errorMetrics[nilDataPointIntHistogram], + nilDataPointDoubleSumBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(nilDataPointDoubleSumMetric)) + + nilDataPointIntHistogramMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + errorMetrics[nilDataPointIntHistogram], + }, }, }, }, }, } - nilDataPointIntHistogramBatch := pdata.MetricsFromOtlp(nilDataPointIntHistogramMetric) - - nilDataPointDoubleHistogramMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - errorMetrics[nilDataPointDoubleHistogram], + nilDataPointIntHistogramBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(nilDataPointIntHistogramMetric)) + + nilDataPointDoubleHistogramMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + errorMetrics[nilDataPointDoubleHistogram], + }, }, }, }, }, } - nilDataPointDoubleHistogramBatch := pdata.MetricsFromOtlp(nilDataPointDoubleHistogramMetric) - - nilDataPointDoubleSummaryMetric := []*otlp.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ - { - Metrics: []*otlp.Metric{ - errorMetrics[nilDataPointDoubleSummary], + nilDataPointDoubleHistogramBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(nilDataPointDoubleHistogramMetric)) + + nilDataPointDoubleSummaryMetric := &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlp.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlp.InstrumentationLibraryMetrics{ + { + Metrics: []*otlp.Metric{ + errorMetrics[nilDataPointDoubleSummary], + }, }, }, }, }, } - nilDataPointDoubleSummaryBatch := pdata.MetricsFromOtlp(nilDataPointDoubleSummaryMetric) + nilDataPointDoubleSummaryBatch := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(nilDataPointDoubleSummaryMetric)) checkFunc := func(t *testing.T, r *http.Request, expected int) { body, err := ioutil.ReadAll(r.Body) diff --git a/internal/otlp_wrapper.go b/internal/otlp_wrapper.go index f069f130b14..a5c187c3ed2 100644 --- a/internal/otlp_wrapper.go +++ b/internal/otlp_wrapper.go @@ -16,8 +16,24 @@ package internal import ( otlpcollectorlog "go.opentelemetry.io/collector/internal/data/protogen/collector/logs/v1" + otlpcollectormetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1" ) +// MetricsWrapper is an intermediary struct that is declared in an internal package +// as a way to prevent certain functions of pdata.Metrics data type to be callable by +// any code outside of this module. +type MetricsWrapper struct { + req *otlpcollectormetrics.ExportMetricsServiceRequest +} + +func MetricsToOtlp(mw MetricsWrapper) *otlpcollectormetrics.ExportMetricsServiceRequest { + return mw.req +} + +func MetricsFromOtlp(req *otlpcollectormetrics.ExportMetricsServiceRequest) MetricsWrapper { + return MetricsWrapper{req: req} +} + // LogsWrapper is an intermediary struct that is declared in an internal package // as a way to prevent certain functions of pdata.Logs data type to be callable by // any code outside of this module. diff --git a/internal/testdata/metric.go b/internal/testdata/metric.go index 2410e5fa186..d837be25e31 100644 --- a/internal/testdata/metric.go +++ b/internal/testdata/metric.go @@ -18,6 +18,7 @@ import ( "time" "go.opentelemetry.io/collector/consumer/pdata" + otlpcollectormetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1" otlpmetrics "go.opentelemetry.io/collector/internal/data/protogen/metrics/v1" ) @@ -47,8 +48,8 @@ func GenerateMetricsEmpty() pdata.Metrics { return md } -func generateMetricsOtlpEmpty() []*otlpmetrics.ResourceMetrics { - return []*otlpmetrics.ResourceMetrics(nil) +func generateMetricsOtlpEmpty() *otlpcollectormetrics.ExportMetricsServiceRequest { + return &otlpcollectormetrics.ExportMetricsServiceRequest{} } func GenerateMetricsOneEmptyResourceMetrics() pdata.Metrics { @@ -57,9 +58,11 @@ func GenerateMetricsOneEmptyResourceMetrics() pdata.Metrics { return md } -func generateMetricsOtlpOneEmptyResourceMetrics() []*otlpmetrics.ResourceMetrics { - return []*otlpmetrics.ResourceMetrics{ - {}, +func generateMetricsOtlpOneEmptyResourceMetrics() *otlpcollectormetrics.ExportMetricsServiceRequest { + return &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + {}, + }, } } @@ -70,10 +73,12 @@ func GenerateMetricsNoLibraries() pdata.Metrics { return md } -func generateMetricsOtlpNoLibraries() []*otlpmetrics.ResourceMetrics { - return []*otlpmetrics.ResourceMetrics{ - { - Resource: generateOtlpResource1(), +func generateMetricsOtlpNoLibraries() *otlpcollectormetrics.ExportMetricsServiceRequest { + return &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateOtlpResource1(), + }, }, } } @@ -85,12 +90,14 @@ func GenerateMetricsOneEmptyInstrumentationLibrary() pdata.Metrics { } // generateMetricsOtlpOneEmptyInstrumentationLibrary returns the OTLP representation of the GenerateMetricsOneEmptyInstrumentationLibrary. -func generateMetricsOtlpOneEmptyInstrumentationLibrary() []*otlpmetrics.ResourceMetrics { - return []*otlpmetrics.ResourceMetrics{ - { - Resource: generateOtlpResource1(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - {}, +func generateMetricsOtlpOneEmptyInstrumentationLibrary() *otlpcollectormetrics.ExportMetricsServiceRequest { + return &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateOtlpResource1(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + {}, + }, }, }, } @@ -106,13 +113,15 @@ func GenerateMetricsOneMetricNoResource() pdata.Metrics { return md } -func generateMetricsOtlpOneMetricNoResource() []*otlpmetrics.ResourceMetrics { - return []*otlpmetrics.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - Metrics: []*otlpmetrics.Metric{ - generateOtlpCounterIntMetric(), +func generateMetricsOtlpOneMetricNoResource() *otlpcollectormetrics.ExportMetricsServiceRequest { + return &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + Metrics: []*otlpmetrics.Metric{ + generateOtlpCounterIntMetric(), + }, }, }, }, @@ -128,14 +137,16 @@ func GenerateMetricsOneMetric() pdata.Metrics { return md } -func generateMetricsOtlpOneMetric() []*otlpmetrics.ResourceMetrics { - return []*otlpmetrics.ResourceMetrics{ - { - Resource: generateOtlpResource1(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - Metrics: []*otlpmetrics.Metric{ - generateOtlpCounterIntMetric(), +func generateMetricsOtlpOneMetric() *otlpcollectormetrics.ExportMetricsServiceRequest { + return &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateOtlpResource1(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + Metrics: []*otlpmetrics.Metric{ + generateOtlpCounterIntMetric(), + }, }, }, }, @@ -169,15 +180,17 @@ func GenerateMetricsOneCounterOneSummaryMetrics() pdata.Metrics { return md } -func GenerateMetricsOtlpTwoMetrics() []*otlpmetrics.ResourceMetrics { - return []*otlpmetrics.ResourceMetrics{ - { - Resource: generateOtlpResource1(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - Metrics: []*otlpmetrics.Metric{ - generateOtlpCounterIntMetric(), - generateOtlpCounterIntMetric(), +func generateMetricsOtlpTwoMetrics() *otlpcollectormetrics.ExportMetricsServiceRequest { + return &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateOtlpResource1(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + Metrics: []*otlpmetrics.Metric{ + generateOtlpCounterIntMetric(), + generateOtlpCounterIntMetric(), + }, }, }, }, @@ -193,9 +206,9 @@ func GenerateMetricsOneMetricNoLabels() pdata.Metrics { return md } -func generateMetricsOtlpOneMetricNoLabels() []*otlpmetrics.ResourceMetrics { +func generateMetricsOtlpOneMetricNoLabels() *otlpcollectormetrics.ExportMetricsServiceRequest { md := generateMetricsOtlpOneMetric() - mis := md[0].InstrumentationLibraryMetrics[0].Metrics[0].Data.(*otlpmetrics.Metric_IntSum).IntSum + mis := md.ResourceMetrics[0].InstrumentationLibraryMetrics[0].Metrics[0].Data.(*otlpmetrics.Metric_IntSum).IntSum mis.DataPoints[0].Labels = nil mis.DataPoints[1].Labels = nil return md @@ -249,20 +262,22 @@ func GenerateMetricsMetricTypeInvalid() pdata.Metrics { return md } -func generateMetricsOtlpAllTypesNoDataPoints() []*otlpmetrics.ResourceMetrics { - return []*otlpmetrics.ResourceMetrics{ - { - Resource: generateOtlpResource1(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - Metrics: []*otlpmetrics.Metric{ - generateOtlpMetric(TestGaugeDoubleMetricName, pdata.MetricDataTypeDoubleGauge), - generateOtlpMetric(TestGaugeIntMetricName, pdata.MetricDataTypeIntGauge), - generateOtlpMetric(TestCounterDoubleMetricName, pdata.MetricDataTypeDoubleSum), - generateOtlpMetric(TestCounterIntMetricName, pdata.MetricDataTypeIntSum), - generateOtlpMetric(TestDoubleHistogramMetricName, pdata.MetricDataTypeDoubleHistogram), - generateOtlpMetric(TestIntHistogramMetricName, pdata.MetricDataTypeIntHistogram), - generateOtlpMetric(TestDoubleSummaryMetricName, pdata.MetricDataTypeDoubleSummary), +func generateMetricsOtlpAllTypesNoDataPoints() *otlpcollectormetrics.ExportMetricsServiceRequest { + return &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateOtlpResource1(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + Metrics: []*otlpmetrics.Metric{ + generateOtlpMetric(TestGaugeDoubleMetricName, pdata.MetricDataTypeDoubleGauge), + generateOtlpMetric(TestGaugeIntMetricName, pdata.MetricDataTypeIntGauge), + generateOtlpMetric(TestCounterDoubleMetricName, pdata.MetricDataTypeDoubleSum), + generateOtlpMetric(TestCounterIntMetricName, pdata.MetricDataTypeIntSum), + generateOtlpMetric(TestDoubleHistogramMetricName, pdata.MetricDataTypeDoubleHistogram), + generateOtlpMetric(TestIntHistogramMetricName, pdata.MetricDataTypeIntHistogram), + generateOtlpMetric(TestDoubleSummaryMetricName, pdata.MetricDataTypeDoubleSummary), + }, }, }, }, @@ -290,18 +305,20 @@ func GeneratMetricsAllTypesWithSampleDatapoints() pdata.Metrics { return metricData } -func generateMetricsOtlpAllTypesWithSampleDatapoints() []*otlpmetrics.ResourceMetrics { - return []*otlpmetrics.ResourceMetrics{ - { - Resource: generateOtlpResource1(), - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - Metrics: []*otlpmetrics.Metric{ - generateOtlpCounterIntMetric(), - generateOtlpSumDoubleMetric(), - generateOtlpDoubleHistogramMetric(), - generateOtlpIntHistogramMetric(), - generateOTLPDoubleSummaryMetric(), +func generateMetricsOtlpAllTypesWithSampleDatapoints() *otlpcollectormetrics.ExportMetricsServiceRequest { + return &otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + Resource: generateOtlpResource1(), + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + Metrics: []*otlpmetrics.Metric{ + generateOtlpCounterIntMetric(), + generateOtlpSumDoubleMetric(), + generateOtlpDoubleHistogramMetric(), + generateOtlpIntHistogramMetric(), + generateOTLPDoubleSummaryMetric(), + }, }, }, }, diff --git a/internal/testdata/metric_test.go b/internal/testdata/metric_test.go index 8d7ebfcc231..ea4f17f0983 100644 --- a/internal/testdata/metric_test.go +++ b/internal/testdata/metric_test.go @@ -20,13 +20,14 @@ import ( "github.com/stretchr/testify/assert" "go.opentelemetry.io/collector/consumer/pdata" - otlpmetrics "go.opentelemetry.io/collector/internal/data/protogen/metrics/v1" + "go.opentelemetry.io/collector/internal" + otlpcollectormetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1" ) type traceMetricsCase struct { name string td pdata.Metrics - otlp []*otlpmetrics.ResourceMetrics + otlp *otlpcollectormetrics.ExportMetricsServiceRequest } func generateAllMetricsTestCases() []traceMetricsCase { @@ -64,7 +65,7 @@ func generateAllMetricsTestCases() []traceMetricsCase { { name: "two-metrics", td: GenerateMetricsTwoMetrics(), - otlp: GenerateMetricsOtlpTwoMetrics(), + otlp: generateMetricsOtlpTwoMetrics(), }, { name: "one-metric-no-labels", @@ -90,9 +91,9 @@ func TestToFromOtlpMetrics(t *testing.T) { for i := range allTestCases { test := allTestCases[i] t.Run(test.name, func(t *testing.T) { - td := pdata.MetricsFromOtlp(test.otlp) + td := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(test.otlp)) assert.EqualValues(t, test.td, td) - otlp := pdata.MetricsToOtlp(td) + otlp := internal.MetricsToOtlp(td.InternalRep()) assert.EqualValues(t, test.otlp, otlp) }) } diff --git a/receiver/otlpreceiver/metrics/otlp.go b/receiver/otlpreceiver/metrics/otlp.go index 7b1bdfad1bf..268f265515c 100644 --- a/receiver/otlpreceiver/metrics/otlp.go +++ b/receiver/otlpreceiver/metrics/otlp.go @@ -20,6 +20,7 @@ import ( "go.opentelemetry.io/collector/client" "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/internal" collectormetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1" "go.opentelemetry.io/collector/obsreport" ) @@ -51,7 +52,7 @@ const ( func (r *Receiver) Export(ctx context.Context, req *collectormetrics.ExportMetricsServiceRequest) (*collectormetrics.ExportMetricsServiceResponse, error) { receiverCtx := obsreport.ReceiverContext(ctx, r.instanceName, receiverTransport) - md := pdata.MetricsFromOtlp(req.ResourceMetrics) + md := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(req)) err := r.sendToNextConsumer(receiverCtx, md) if err != nil { diff --git a/receiver/otlpreceiver/metrics/otlp_test.go b/receiver/otlpreceiver/metrics/otlp_test.go index c4b8ad01e7e..5122d083639 100644 --- a/receiver/otlpreceiver/metrics/otlp_test.go +++ b/receiver/otlpreceiver/metrics/otlp_test.go @@ -28,6 +28,7 @@ import ( "go.opentelemetry.io/collector/consumer" "go.opentelemetry.io/collector/consumer/consumertest" "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/internal" collectormetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1" otlpcommon "go.opentelemetry.io/collector/internal/data/protogen/common/v1" otlpmetrics "go.opentelemetry.io/collector/internal/data/protogen/metrics/v1" @@ -54,41 +55,43 @@ func TestExport(t *testing.T) { unixnanos1 := uint64(12578940000000012345) unixnanos2 := uint64(12578940000000054321) - resourceMetrics := []*otlpmetrics.ResourceMetrics{ - { - InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ - { - Metrics: []*otlpmetrics.Metric{ - { - Name: "mymetric", - Description: "My metric", - Unit: "ms", - Data: &otlpmetrics.Metric_IntSum{ - IntSum: &otlpmetrics.IntSum{ - IsMonotonic: true, - AggregationTemporality: otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE, - DataPoints: []*otlpmetrics.IntDataPoint{ - { - Labels: []otlpcommon.StringKeyValue{ - { - Key: "key1", - Value: "value1", + req := &collectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{ + { + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + Metrics: []*otlpmetrics.Metric{ + { + Name: "mymetric", + Description: "My metric", + Unit: "ms", + Data: &otlpmetrics.Metric_IntSum{ + IntSum: &otlpmetrics.IntSum{ + IsMonotonic: true, + AggregationTemporality: otlpmetrics.AggregationTemporality_AGGREGATION_TEMPORALITY_CUMULATIVE, + DataPoints: []*otlpmetrics.IntDataPoint{ + { + Labels: []otlpcommon.StringKeyValue{ + { + Key: "key1", + Value: "value1", + }, }, + StartTimeUnixNano: unixnanos1, + TimeUnixNano: unixnanos2, + Value: 123, }, - StartTimeUnixNano: unixnanos1, - TimeUnixNano: unixnanos2, - Value: 123, - }, - { - Labels: []otlpcommon.StringKeyValue{ - { - Key: "key2", - Value: "value2", + { + Labels: []otlpcommon.StringKeyValue{ + { + Key: "key2", + Value: "value2", + }, }, + StartTimeUnixNano: unixnanos1, + TimeUnixNano: unixnanos2, + Value: 456, }, - StartTimeUnixNano: unixnanos1, - TimeUnixNano: unixnanos2, - Value: 456, }, }, }, @@ -102,11 +105,7 @@ func TestExport(t *testing.T) { // Keep metric data to compare the test result against it // Clone needed because OTLP proto XXX_ fields are altered in the GRPC downstream - metricData := pdata.MetricsFromOtlp(resourceMetrics).Clone() - - req := &collectormetrics.ExportMetricsServiceRequest{ - ResourceMetrics: resourceMetrics, - } + metricData := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(req)).Clone() resp, err := metricsClient.Export(context.Background(), req) require.NoError(t, err, "Failed to export metrics: %v", err) diff --git a/testbed/testbed/data_providers.go b/testbed/testbed/data_providers.go index cf8c2bf5338..7f57f5aaf4d 100644 --- a/testbed/testbed/data_providers.go +++ b/testbed/testbed/data_providers.go @@ -355,7 +355,7 @@ func NewFileDataProvider(filePath string, dataType configmodels.DataType) (*File } message = &msg - md := pdata.MetricsFromOtlp(msg.ResourceMetrics) + md := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(&msg)) _, dataPointCount = md.MetricAndDataPointCount() case configmodels.LogsDataType: @@ -389,7 +389,7 @@ func (dp *FileDataProvider) GenerateTraces() (pdata.Traces, bool) { } func (dp *FileDataProvider) GenerateMetrics() (pdata.Metrics, bool) { - md := pdata.MetricsFromOtlp(dp.message.(*otlpmetricscol.ExportMetricsServiceRequest).ResourceMetrics) + md := pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(dp.message.(*otlpmetricscol.ExportMetricsServiceRequest))) dp.batchesGenerated.Inc() _, dataPointCount := md.MetricAndDataPointCount() dp.dataItemsGenerated.Add(uint64(dataPointCount)) diff --git a/testbed/tests/resource_processor_test.go b/testbed/tests/resource_processor_test.go index 1fc17167cf8..9cdc17e9ebb 100644 --- a/testbed/tests/resource_processor_test.go +++ b/testbed/tests/resource_processor_test.go @@ -23,6 +23,8 @@ import ( "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/consumer/pdata" + "go.opentelemetry.io/collector/internal" + otlpcollectormetrics "go.opentelemetry.io/collector/internal/data/protogen/collector/metrics/v1" otlpcommon "go.opentelemetry.io/collector/internal/data/protogen/common/v1" otlpmetrics "go.opentelemetry.io/collector/internal/data/protogen/metrics/v1" otlpresource "go.opentelemetry.io/collector/internal/data/protogen/resource/v1" @@ -147,8 +149,8 @@ func getResourceProcessorTestCases() []resourceProcessorTestCase { - key: opencensus.resourcetype action: delete `, - mockedConsumedMetricData: getMetricDataFrom(mockedConsumedResourceWithType), - expectedMetricData: getMetricDataFromResourceMetrics(&otlpmetrics.ResourceMetrics{ + mockedConsumedMetricData: metricsFromResourceMetrics(mockedConsumedResourceWithType), + expectedMetricData: metricsFromResourceMetrics(&otlpmetrics.ResourceMetrics{ Resource: otlpresource.Resource{ Attributes: []otlpcommon.KeyValue{ { @@ -173,8 +175,8 @@ func getResourceProcessorTestCases() []resourceProcessorTestCase { action: insert `, - mockedConsumedMetricData: getMetricDataFrom(mockedConsumedResourceNil), - expectedMetricData: getMetricDataFromResourceMetrics(&otlpmetrics.ResourceMetrics{ + mockedConsumedMetricData: metricsFromResourceMetrics(mockedConsumedResourceNil), + expectedMetricData: metricsFromResourceMetrics(&otlpmetrics.ResourceMetrics{ Resource: otlpresource.Resource{ Attributes: []otlpcommon.KeyValue{ { @@ -194,8 +196,8 @@ func getResourceProcessorTestCases() []resourceProcessorTestCase { value: additional-label-value action: insert `, - mockedConsumedMetricData: getMetricDataFrom(mockedConsumedResourceWithoutAttributes), - expectedMetricData: getMetricDataFromResourceMetrics(&otlpmetrics.ResourceMetrics{ + mockedConsumedMetricData: metricsFromResourceMetrics(mockedConsumedResourceWithoutAttributes), + expectedMetricData: metricsFromResourceMetrics(&otlpmetrics.ResourceMetrics{ Resource: otlpresource.Resource{ Attributes: []otlpcommon.KeyValue{ { @@ -211,12 +213,10 @@ func getResourceProcessorTestCases() []resourceProcessorTestCase { return tests } -func getMetricDataFromResourceMetrics(rm *otlpmetrics.ResourceMetrics) pdata.Metrics { - return pdata.MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{rm}) -} - -func getMetricDataFrom(rm *otlpmetrics.ResourceMetrics) pdata.Metrics { - return pdata.MetricsFromOtlp([]*otlpmetrics.ResourceMetrics{rm}) +func metricsFromResourceMetrics(rm *otlpmetrics.ResourceMetrics) pdata.Metrics { + return pdata.MetricsFromInternalRep(internal.MetricsFromOtlp(&otlpcollectormetrics.ExportMetricsServiceRequest{ + ResourceMetrics: []*otlpmetrics.ResourceMetrics{rm}, + })) } func TestMetricResourceProcessor(t *testing.T) {