From 5c2f4666c00314006030c6c8f0712b5c83eb5012 Mon Sep 17 00:00:00 2001 From: Jerry Leung Date: Tue, 17 Sep 2024 17:43:23 -0700 Subject: [PATCH] Fix --- .../receiver/telemetryapireceiver/go.mod | 2 + .../receiver/telemetryapireceiver/go.sum | 4 + .../receiver/telemetryapireceiver/receiver.go | 2 +- .../telemetryapireceiver/receiver_test.go | 121 +++++++++--------- 4 files changed, 70 insertions(+), 59 deletions(-) diff --git a/collector/receiver/telemetryapireceiver/go.mod b/collector/receiver/telemetryapireceiver/go.mod index 8ece43aba8..ad379d38aa 100644 --- a/collector/receiver/telemetryapireceiver/go.mod +++ b/collector/receiver/telemetryapireceiver/go.mod @@ -41,6 +41,8 @@ require ( github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.2 // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.107.0 // indirect + github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.107.0 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect github.com/prometheus/client_golang v1.19.1 // indirect github.com/prometheus/client_model v0.6.1 // indirect diff --git a/collector/receiver/telemetryapireceiver/go.sum b/collector/receiver/telemetryapireceiver/go.sum index 4998268d81..21a9dc95e7 100644 --- a/collector/receiver/telemetryapireceiver/go.sum +++ b/collector/receiver/telemetryapireceiver/go.sum @@ -49,6 +49,10 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA= github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= +github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.107.0 h1:g1pkpFfe+dnhpfvo+f9yFIkbvTdiOvNmFOUFNzVAgvk= +github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest v0.107.0/go.mod h1:oG/PliNiIOUHVARyDrFdvxFvG8DUPEjMGlmxjEqeoKM= +github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.107.0 h1:zTeRh4V3rMlXgNvfbDBnET6nvhOeZpYIbKTjVbSl9Ws= +github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatautil v0.107.0/go.mod h1:/RtBag3LuHIkqN4bo8Erd3jCzA3gea70l9WyJ9TncXM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= diff --git a/collector/receiver/telemetryapireceiver/receiver.go b/collector/receiver/telemetryapireceiver/receiver.go index 2c7932c476..df22738276 100644 --- a/collector/receiver/telemetryapireceiver/receiver.go +++ b/collector/receiver/telemetryapireceiver/receiver.go @@ -438,7 +438,7 @@ func newTelemetryAPIReceiver( errorsCounter: 0, invocationsCounter: 0, timeoutsCounter: 0, - metricsBuilder: metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), settings), + metricsBuilder: metadata.NewMetricsBuilder(cfg.MetricsBuilderConfig, settings), }, nil } diff --git a/collector/receiver/telemetryapireceiver/receiver_test.go b/collector/receiver/telemetryapireceiver/receiver_test.go index a1867df8ab..e6a48476a6 100644 --- a/collector/receiver/telemetryapireceiver/receiver_test.go +++ b/collector/receiver/telemetryapireceiver/receiver_test.go @@ -16,7 +16,8 @@ package telemetryapireceiver // import "github.com/open-telemetry/opentelemetry- import ( "context" - "go.opentelemetry.io/collector/pdata/pmetric" + "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/pdatatest/pmetrictest" + "github.com/open-telemetry/opentelemetry-lambda/collector/receiver/telemetryapireceiver/internal/metadata" "net/http/httptest" "strings" "testing" @@ -179,7 +180,7 @@ func TestCreateMetrics(t *testing.T) { desc string slice []event expectedResourceMetrics int - expectedMetrics []map[string]any + expectedMetrics map[string]int expectError bool }{ { @@ -211,11 +212,8 @@ func TestCreateMetrics(t *testing.T) { }, }, expectedResourceMetrics: 1, - expectedMetrics: []map[string]any{ - { - "Name": "faas.coldstarts", - "Value": int64(1), - }, + expectedMetrics: map[string]int{ + "faas.coldstarts": 1, }, expectError: false, }, @@ -246,11 +244,8 @@ func TestCreateMetrics(t *testing.T) { }, }, expectedResourceMetrics: 1, - expectedMetrics: []map[string]any{ - { - "Name": "faas.invocations", - "Value": int64(1), - }, + expectedMetrics: map[string]int{ + "faas.invocations": 1, }, expectError: false, }, @@ -276,15 +271,9 @@ func TestCreateMetrics(t *testing.T) { }, }, expectedResourceMetrics: 1, - expectedMetrics: []map[string]any{ - { - "Name": "faas.errors", - "Value": int64(1), - }, - { - "Name": "faas.invocations", - "Value": int64(1), - }, + expectedMetrics: map[string]int{ + "faas.errors": 1, + "faas.invocations": 1, }, expectError: false, }, @@ -310,15 +299,9 @@ func TestCreateMetrics(t *testing.T) { }, }, expectedResourceMetrics: 1, - expectedMetrics: []map[string]any{ - { - "Name": "faas.errors", - "Value": int64(1), - }, - { - "Name": "faas.invocations", - "Value": int64(1), - }, + expectedMetrics: map[string]int{ + "faas.errors": 1, + "faas.invocations": 1, }, expectError: false, }, @@ -343,27 +326,21 @@ func TestCreateMetrics(t *testing.T) { }, }, expectedResourceMetrics: 1, - expectedMetrics: []map[string]any{ - { - "Name": "faas.errors", - "Value": int64(1), - }, - { - "Name": "faas.invocations", - "Value": int64(1), - }, - { - "Name": "faas.timeouts", - "Value": int64(1), - }, + expectedMetrics: map[string]int{ + "faas.errors": 1, + "faas.invocations": 1, + "faas.timeouts": 1, }, expectError: false, }, } + for _, tc := range testCases { t.Run(tc.desc, func(t *testing.T) { r, err := newTelemetryAPIReceiver( - &Config{}, + &Config{ + MetricsBuilderConfig: metadata.DefaultMetricsBuilderConfig(), + }, receivertest.NewNopSettings(), ) require.NoError(t, err) @@ -371,22 +348,50 @@ func TestCreateMetrics(t *testing.T) { if tc.expectError { require.Error(t, err) } else { - require.Equal(t, tc.expectedResourceMetrics, metrics.ResourceMetrics().Len()) - if metrics.ResourceMetrics().Len() > 0 { - resourceMetric := metrics.ResourceMetrics().At(0) - require.Equal(t, 1, resourceMetric.ScopeMetrics().Len()) - scopeMetric := resourceMetric.ScopeMetrics().At(0) - require.Equal(t, scopeName, scopeMetric.Scope().Name()) - require.Equal(t, len(tc.expectedMetrics), scopeMetric.Metrics().Len()) - for idx, m := range tc.expectedMetrics { - metric := scopeMetric.Metrics().At(idx) - require.Equal(t, m["Name"], metric.Name()) - require.True(t, metric.Sum().IsMonotonic()) - require.Equal(t, pmetric.AggregationTemporalityDelta, metric.Sum().AggregationTemporality()) - require.Equal(t, 1, metric.Sum().DataPoints().Len()) - require.Equal(t, m["Value"], metric.Sum().DataPoints().At(0).IntValue()) + now := pcommon.NewTimestampFromTime(time.Now().UTC()) + expectedMB := metadata.NewMetricsBuilder(metadata.DefaultMetricsBuilderConfig(), receivertest.NewNopSettings()) + for k, v := range tc.expectedMetrics { + switch k { + case "faas.coldstarts": + for _ = range v { + expectedMB.RecordFaasColdstartsDataPoint(now, "1", metadata.AttributeFaasTriggerOther) + } + case "faas.errors": + for _ = range v { + expectedMB.RecordFaasErrorsDataPoint(now, "1", metadata.AttributeFaasTriggerOther) + } + case "faas.invocations": + for _ = range v { + expectedMB.RecordFaasInvocationsDataPoint(now, "1", metadata.AttributeFaasTriggerOther) + } + case "faas.timeouts": + for _ = range v { + expectedMB.RecordFaasTimeoutsDataPoint(now, "1", metadata.AttributeFaasTriggerOther) + } + default: + } } + expectedMB.EmitForResource(metadata.WithResource(r.resource)) + expectedMetrics := expectedMB.Emit() + require.NoError(t, pmetrictest.CompareMetrics(expectedMetrics, metrics, pmetrictest.IgnoreResourceMetricsOrder(), pmetrictest.IgnoreMetricDataPointsOrder(), pmetrictest.IgnoreStartTimestamp(), pmetrictest.IgnoreTimestamp())) + // + //require.Equal(t, tc.expectedResourceMetrics, metrics.ResourceMetrics().Len()) + //if metrics.ResourceMetrics().Len() > 0 { + // resourceMetric := metrics.ResourceMetrics().At(0) + // require.Equal(t, 1, resourceMetric.ScopeMetrics().Len()) + // scopeMetric := resourceMetric.ScopeMetrics().At(0) + // require.Equal(t, scopeName, scopeMetric.Scope().Name()) + // require.Equal(t, len(tc.expectedMetrics), scopeMetric.Metrics().Len()) + // for idx, m := range tc.expectedMetrics { + // metric := scopeMetric.Metrics().At(idx) + // require.Equal(t, m["Name"], metric.Name()) + // require.True(t, metric.Sum().IsMonotonic()) + // require.Equal(t, pmetric.AggregationTemporalityDelta, metric.Sum().AggregationTemporality()) + // require.Equal(t, 1, metric.Sum().DataPoints().Len()) + // require.Equal(t, m["Value"], metric.Sum().DataPoints().At(0).IntValue()) + // } + //} } }) }