From ddf69b5cd523d14c8de3c5c960d50e6c3a62eafc Mon Sep 17 00:00:00 2001 From: Bogdan Drutu Date: Sun, 30 Aug 2020 11:28:15 -0700 Subject: [PATCH] Change resource processor to use the new metrics internal structs Signed-off-by: Bogdan Drutu --- .../resourceprocessor/resource_processor.go | 4 +- .../resource_processor_test.go | 9 +- testbed/tests/resource_processor_test.go | 196 +++++++++--------- 3 files changed, 105 insertions(+), 104 deletions(-) diff --git a/processor/resourceprocessor/resource_processor.go b/processor/resourceprocessor/resource_processor.go index 248071d1321..1938319d931 100644 --- a/processor/resourceprocessor/resource_processor.go +++ b/processor/resourceprocessor/resource_processor.go @@ -42,7 +42,7 @@ func (rp *resourceProcessor) ProcessTraces(_ context.Context, td pdata.Traces) ( // ProcessMetrics implements the MProcessor interface func (rp *resourceProcessor) ProcessMetrics(_ context.Context, md pdata.Metrics) (pdata.Metrics, error) { - imd := pdatautil.MetricsToOldInternalMetrics(md) + imd := pdatautil.MetricsToInternalMetrics(md) rms := imd.ResourceMetrics() for i := 0; i < rms.Len(); i++ { resource := rms.At(i).Resource() @@ -54,5 +54,5 @@ func (rp *resourceProcessor) ProcessMetrics(_ context.Context, md pdata.Metrics) } rp.attrProc.Process(resource.Attributes()) } - return pdatautil.MetricsFromOldInternalMetrics(imd), nil + return pdatautil.MetricsFromInternalMetrics(imd), nil } diff --git a/processor/resourceprocessor/resource_processor_test.go b/processor/resourceprocessor/resource_processor_test.go index 74f09041516..78df020e27b 100644 --- a/processor/resourceprocessor/resource_processor_test.go +++ b/processor/resourceprocessor/resource_processor_test.go @@ -26,7 +26,6 @@ import ( "go.opentelemetry.io/collector/consumer/pdata" "go.opentelemetry.io/collector/consumer/pdatautil" "go.opentelemetry.io/collector/internal/data/testdata" - "go.opentelemetry.io/collector/internal/dataold/testdataold" "go.opentelemetry.io/collector/processor/processorhelper" ) @@ -147,9 +146,9 @@ func generateTraceData(attributes map[string]string) pdata.Traces { } func generateMetricData(attributes map[string]string) pdata.Metrics { - md := testdataold.GenerateMetricDataOneMetricNoResource() + md := testdata.GenerateMetricsOneMetricNoResource() if attributes == nil { - return pdatautil.MetricsFromOldInternalMetrics(md) + return pdatautil.MetricsFromInternalMetrics(md) } resource := md.ResourceMetrics().At(0).Resource() resource.InitEmpty() @@ -157,7 +156,7 @@ func generateMetricData(attributes map[string]string) pdata.Metrics { resource.Attributes().InsertString(k, v) } resource.Attributes().Sort() - return pdatautil.MetricsFromOldInternalMetrics(md) + return pdatautil.MetricsFromInternalMetrics(md) } type testTraceConsumer struct { @@ -179,7 +178,7 @@ type testMetricsConsumer struct { func (tmn *testMetricsConsumer) ConsumeMetrics(_ context.Context, md pdata.Metrics) error { // sort attributes to be able to compare traces - imd := pdatautil.MetricsToOldInternalMetrics(md) + imd := pdatautil.MetricsToInternalMetrics(md) for i := 0; i < imd.ResourceMetrics().Len(); i++ { sortResourceAttributes(imd.ResourceMetrics().At(i).Resource()) } diff --git a/testbed/tests/resource_processor_test.go b/testbed/tests/resource_processor_test.go index ed2ae41e1d7..fef05c4df29 100644 --- a/testbed/tests/resource_processor_test.go +++ b/testbed/tests/resource_processor_test.go @@ -19,102 +19,109 @@ import ( "path/filepath" "testing" - "github.com/gogo/protobuf/jsonpb" "github.com/stretchr/testify/require" "go.opentelemetry.io/collector/consumer/pdata" "go.opentelemetry.io/collector/consumer/pdatautil" + "go.opentelemetry.io/collector/internal/data" otlpcommon "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/common/v1" - otlpmetrics "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/metrics/v1old" + otlpmetrics "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/metrics/v1" otlpresource "go.opentelemetry.io/collector/internal/data/opentelemetry-proto-gen/resource/v1" - "go.opentelemetry.io/collector/internal/dataold" "go.opentelemetry.io/collector/testbed/testbed" ) -const ( - mockedConsumedResourceWithTypeJSON = ` - { - "resource": { - "attributes": [ - { - "key": "opencensus.resourcetype", - "value": { "stringValue": "host" } +var ( + mockedConsumedResourceWithType = &otlpmetrics.ResourceMetrics{ + Resource: &otlpresource.Resource{ + Attributes: []*otlpcommon.KeyValue{ + { + Key: "opencensus.resourcetype", + Value: &otlpcommon.AnyValue{ + Value: &otlpcommon.AnyValue_StringValue{ + StringValue: "host", + }, + }, + }, + { + Key: "label-key", + Value: &otlpcommon.AnyValue{ + Value: &otlpcommon.AnyValue_StringValue{ + StringValue: "label-value", + }, + }, + }, + }, }, - { - "key": "label-key", - "value": { "stringValue": "label-value" } - } - ] - }, - "instrumentation_library_metrics": [ - { - "metrics": [ - { - "metric_descriptor": { - "name": "metric-name", - "description": "metric-description", - "unit": "metric-unit", - "type": 1 + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + Metrics: []*otlpmetrics.Metric{ + { + Name: "metric-name", + Description: "metric-description", + Unit: "metric-unit", + Data: &otlpmetrics.Metric_IntGauge{ + IntGauge: &otlpmetrics.IntGauge{ + DataPoints: []*otlpmetrics.IntDataPoint{ + { + Value: 0, + }, + }, + }, + }, + }, + }, + }, + }, + } + + mockedConsumedResourceNil = &otlpmetrics.ResourceMetrics{ + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + Metrics: []*otlpmetrics.Metric{ + { + Name: "metric-name", + Description: "metric-description", + Unit: "metric-unit", + Data: &otlpmetrics.Metric_IntGauge{ + IntGauge: &otlpmetrics.IntGauge{ + DataPoints: []*otlpmetrics.IntDataPoint{ + { + Value: 0, + }, + }, + }, + }, + }, + }, }, - "int64_data_points": [ - { - "value": 0 - } - ] - } - ] - } - ] - } -` + }, + } - mockedConsumedResourceNilJSON = ` - { - "instrumentation_library_metrics": [ - { - "metrics": [ - { - "metric_descriptor": { - "name": "metric-name", - "description": "metric-description", - "unit": "metric-unit", - "type": 1 - }, - "int64_data_points": [ - { - "value": 0 - } - ] - } - ] - } - ] - } -` - mockedConsumedResourceWithoutAttributesJSON = ` - { - "resource": {}, - "instrumentation_library_metrics": [ - { - "metrics": [ - { - "metric_descriptor": { - "name": "metric-name", - "description": "metric-description", - "unit": "metric-unit", - "type": 1 - }, - "int64_data_points": [ - { - "value": 0 - } - ] - } - ] - } - ] - } -` + mockedConsumedResourceWithoutAttributes = &otlpmetrics.ResourceMetrics{ + Resource: &otlpresource.Resource{ + Attributes: []*otlpcommon.KeyValue{}, + }, + InstrumentationLibraryMetrics: []*otlpmetrics.InstrumentationLibraryMetrics{ + { + Metrics: []*otlpmetrics.Metric{ + { + Name: "metric-name", + Description: "metric-description", + Unit: "metric-unit", + Data: &otlpmetrics.Metric_IntGauge{ + IntGauge: &otlpmetrics.IntGauge{ + DataPoints: []*otlpmetrics.IntDataPoint{ + { + Value: 0, + }, + }, + }, + }, + }, + }, + }, + }, + } ) type resourceProcessorTestCase struct { @@ -141,7 +148,7 @@ func getResourceProcessorTestCases(t *testing.T) []resourceProcessorTestCase { - key: opencensus.resourcetype action: delete `, - mockedConsumedMetricData: getMetricDataFromJSON(t, mockedConsumedResourceWithTypeJSON), + mockedConsumedMetricData: getMetricDataFrom(t, mockedConsumedResourceWithType), expectedMetricData: getMetricDataFromResourceMetrics(&otlpmetrics.ResourceMetrics{ Resource: &otlpresource.Resource{ Attributes: []*otlpcommon.KeyValue{ @@ -167,7 +174,7 @@ func getResourceProcessorTestCases(t *testing.T) []resourceProcessorTestCase { action: insert `, - mockedConsumedMetricData: getMetricDataFromJSON(t, mockedConsumedResourceNilJSON), + mockedConsumedMetricData: getMetricDataFrom(t, mockedConsumedResourceNil), expectedMetricData: getMetricDataFromResourceMetrics(&otlpmetrics.ResourceMetrics{ Resource: &otlpresource.Resource{ @@ -189,7 +196,7 @@ func getResourceProcessorTestCases(t *testing.T) []resourceProcessorTestCase { value: additional-label-value action: insert `, - mockedConsumedMetricData: getMetricDataFromJSON(t, mockedConsumedResourceWithoutAttributesJSON), + mockedConsumedMetricData: getMetricDataFrom(t, mockedConsumedResourceWithoutAttributes), expectedMetricData: getMetricDataFromResourceMetrics(&otlpmetrics.ResourceMetrics{ Resource: &otlpresource.Resource{ @@ -208,16 +215,11 @@ func getResourceProcessorTestCases(t *testing.T) []resourceProcessorTestCase { } func getMetricDataFromResourceMetrics(rm *otlpmetrics.ResourceMetrics) pdata.Metrics { - return pdatautil.MetricsFromOldInternalMetrics(dataold.MetricDataFromOtlp([]*otlpmetrics.ResourceMetrics{rm})) + return pdatautil.MetricsFromInternalMetrics(data.MetricDataFromOtlp([]*otlpmetrics.ResourceMetrics{rm})) } -func getMetricDataFromJSON(t *testing.T, rmString string) pdata.Metrics { - var mockedResourceMetrics otlpmetrics.ResourceMetrics - - err := jsonpb.UnmarshalString(rmString, &mockedResourceMetrics) - require.NoError(t, err, "failed to get mocked resource metrics object", err) - - return pdatautil.MetricsFromOldInternalMetrics(dataold.MetricDataFromOtlp([]*otlpmetrics.ResourceMetrics{&mockedResourceMetrics})) +func getMetricDataFrom(t *testing.T, rm *otlpmetrics.ResourceMetrics) pdata.Metrics { + return pdatautil.MetricsFromInternalMetrics(data.MetricDataFromOtlp([]*otlpmetrics.ResourceMetrics{rm})) } func TestMetricResourceProcessor(t *testing.T) { @@ -280,10 +282,10 @@ func TestMetricResourceProcessor(t *testing.T) { // Assert Resources m := tc.MockBackend.ReceivedMetrics[0] - rm := pdatautil.MetricsToOldInternalMetrics(m).ResourceMetrics() + rm := pdatautil.MetricsToInternalMetrics(m).ResourceMetrics() require.Equal(t, 1, rm.Len()) - expectidMD := pdatautil.MetricsToOldInternalMetrics(test.expectedMetricData) + expectidMD := pdatautil.MetricsToInternalMetrics(test.expectedMetricData) require.Equal(t, attributesToMap(expectidMD.ResourceMetrics().At(0).Resource().Attributes()), attributesToMap(rm.At(0).Resource().Attributes()),