Skip to content

Commit

Permalink
Fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrytfleung committed Sep 18, 2024
1 parent 45de543 commit 5c2f466
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 59 deletions.
2 changes: 2 additions & 0 deletions collector/receiver/telemetryapireceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions collector/receiver/telemetryapireceiver/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
2 changes: 1 addition & 1 deletion collector/receiver/telemetryapireceiver/receiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
121 changes: 63 additions & 58 deletions collector/receiver/telemetryapireceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}{
{
Expand Down Expand Up @@ -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,
},
Expand Down Expand Up @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -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,
},
Expand All @@ -343,50 +326,72 @@ 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)
metrics, err := r.createMetrics(tc.slice)
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())
// }
//}
}
})
}
Expand Down

0 comments on commit 5c2f466

Please sign in to comment.