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

Change resource processor to use the new metrics internal structs #1685

Merged
merged 1 commit into from
Aug 30, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions processor/resourceprocessor/resource_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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
}
9 changes: 4 additions & 5 deletions processor/resourceprocessor/resource_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -147,17 +146,17 @@ 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()
for k, v := range attributes {
resource.Attributes().InsertString(k, v)
}
resource.Attributes().Sort()
return pdatautil.MetricsFromOldInternalMetrics(md)
return pdatautil.MetricsFromInternalMetrics(md)
}

type testTraceConsumer struct {
Expand All @@ -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())
}
Expand Down
196 changes: 99 additions & 97 deletions testbed/tests/resource_processor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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) {
Expand Down Expand Up @@ -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()),
Expand Down