From af65984a369b1645fa06529f195a3dab482a2733 Mon Sep 17 00:00:00 2001 From: naman47vyas Date: Tue, 13 Aug 2024 16:53:13 +0530 Subject: [PATCH] tests for metric_processor --- receiver/apachedruidreceiver/go.mod | 2 +- .../internal/testdata/expected.json | 56 -------- .../metric_processor_test.go | 123 ++++++++++++++++++ receiver/apachedruidreceiver/receiver_test.go | 12 -- 4 files changed, 124 insertions(+), 69 deletions(-) delete mode 100644 receiver/apachedruidreceiver/internal/testdata/expected.json create mode 100644 receiver/apachedruidreceiver/metric_processor_test.go diff --git a/receiver/apachedruidreceiver/go.mod b/receiver/apachedruidreceiver/go.mod index aa37de265e9c..558d5cd12d0e 100644 --- a/receiver/apachedruidreceiver/go.mod +++ b/receiver/apachedruidreceiver/go.mod @@ -4,6 +4,7 @@ go 1.21.0 require ( github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent v0.0.0-00010101000000-000000000000 + github.com/stretchr/testify v1.9.0 github.com/tj/assert v0.0.3 go.opentelemetry.io/collector/component v0.102.2-0.20240606174409-6888f8f7a45f go.opentelemetry.io/collector/config/confighttp v0.102.0 @@ -24,7 +25,6 @@ require ( github.com/prometheus/client_model v0.6.1 // indirect github.com/prometheus/common v0.54.0 // indirect github.com/prometheus/procfs v0.15.0 // indirect - github.com/stretchr/testify v1.9.0 // indirect go.opentelemetry.io/otel/exporters/prometheus v0.49.0 // indirect go.opentelemetry.io/otel/sdk v1.27.0 // indirect go.opentelemetry.io/otel/sdk/metric v1.27.0 // indirect diff --git a/receiver/apachedruidreceiver/internal/testdata/expected.json b/receiver/apachedruidreceiver/internal/testdata/expected.json deleted file mode 100644 index cd26f1077a4e..000000000000 --- a/receiver/apachedruidreceiver/internal/testdata/expected.json +++ /dev/null @@ -1,56 +0,0 @@ -[ - { - "feed": "metrics", - "metric": "segment/scan/active", - "service": "druid/historical", - "host": "localhost:8093", - "version": "30.0.0", - "value": 0, - "timestamp": "2024-08-07T08:54:12.610Z" - }, - { - "feed": "metrics", - "metric": "query/cache/delta/numEntries", - "service": "druid/historical", - "host": "localhost:8093", - "version": "30.0.0", - "value": 0, - "timestamp": "2024-08-07T08:54:12.610Z" - }, - { - "feed": "metrics", - "metric": "query/cache/delta/sizeBytes", - "service": "druid/historical", - "host": "localhost:8093", - "version": "30.0.0", - "value": 0, - "timestamp": "2024-08-07T08:54:12.610Z" - }, - { - "feed": "metrics", - "metric": "query/cache/delta/hits", - "service": "druid/historical", - "host": "localhost:8093", - "version": "30.0.0", - "value": 0, - "timestamp": "2024-08-07T08:54:12.610Z" - }, - { - "feed": "metrics", - "metric": "query/cache/delta/misses", - "service": "druid/historical", - "host": "localhost:8093", - "version": "30.0.0", - "value": 0, - "timestamp": "2024-08-07T08:54:12.610Z" - }, - { - "feed": "metrics", - "metric": "query/cache/delta/evictions", - "service": "druid/historical", - "host": "localhost:8093", - "version": "30.0.0", - "value": 0, - "timestamp": "2024-08-07T08:54:12.610Z" - } -] \ No newline at end of file diff --git a/receiver/apachedruidreceiver/metric_processor_test.go b/receiver/apachedruidreceiver/metric_processor_test.go new file mode 100644 index 000000000000..0d2e52bc17b6 --- /dev/null +++ b/receiver/apachedruidreceiver/metric_processor_test.go @@ -0,0 +1,123 @@ +package apachedruidreceiver + +import ( + "encoding/json" + "testing" + + "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/apachedruidreceiver/internal/metadata" + "github.com/stretchr/testify/assert" + "go.opentelemetry.io/collector/pdata/pmetric" +) + +func TestConvertToJsonNumber(t *testing.T) { + testCases := []struct { + name string + input interface{} + expected json.Number + expectedError bool + }{ + {"Valid json.Number", json.Number("42"), json.Number("42"), false}, + {"Valid float64", float64(42.5), json.Number("42.5"), false}, + {"Valid string", "42.5", json.Number("42.5"), false}, + {"Invalid type", []int{1, 2, 3}, json.Number(""), true}, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + result, err := convertToJsonNumber(tc.input) + if tc.expectedError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tc.expected, result) + } + }) + } +} + +func TestExtractAttributes(t *testing.T) { + input := map[string]interface{}{ + "metric": "test_metric", + "value": 42, + "host": "localhost", + "port": 8080, + "tags": []string{"tag1", "tag2"}, + } + + result := extractAttributes(input) + + assert.Equal(t, 3, result.Len()) + hostVal, ok := result.Get("host") + assert.True(t, ok) + assert.Equal(t, "localhost", hostVal.AsString()) + portVal, ok := result.Get("port") + assert.True(t, ok) + assert.Equal(t, "8080", portVal.AsString()) + tagsVal, ok := result.Get("tags") + assert.True(t, ok) + assert.Equal(t, "tag1, tag2", tagsVal.AsString()) +} + +func TestSetMetricMetadata(t *testing.T) { + metrics := pmetric.NewMetrics() + scopeMetric := metrics.ResourceMetrics().AppendEmpty().ScopeMetrics().AppendEmpty().Metrics().AppendEmpty() + + metadata := metadata.Metric{ + Unit: "bytes", + Description: "Test metric description", + } + + setMetricMetadata(scopeMetric, "test_metric", metadata) + + assert.Equal(t, "test_metric", scopeMetric.Name()) + assert.Equal(t, "bytes", scopeMetric.Unit()) + assert.Equal(t, "Test metric description", scopeMetric.Description()) +} + +func TestExtractMetricInfo(t *testing.T) { + testCases := []struct { + name string + input map[string]interface{} + expectedValue json.Number + expectedName string + expectedError bool + }{ + { + name: "Valid input", + input: map[string]interface{}{ + "metric": "segment/scan/active", + "value": 42.5, + }, + expectedValue: json.Number("42.5"), + expectedName: "druid.segment.scan.active", + expectedError: false, + }, + { + name: "Missing value", + input: map[string]interface{}{ + "metric": "segment/scan/active", + }, + expectedError: true, + }, + { + name: "Missing metric", + input: map[string]interface{}{ + "value": 42.5, + }, + expectedError: true, + }, + } + + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + value, name, err := extractMetricInfo(tc.input) + if tc.expectedError { + assert.Error(t, err) + } else { + assert.NoError(t, err) + assert.Equal(t, tc.expectedValue, value) + assert.Equal(t, tc.expectedName, name) + } + }) + } +} diff --git a/receiver/apachedruidreceiver/receiver_test.go b/receiver/apachedruidreceiver/receiver_test.go index f5828595f447..ca1707e1f37b 100644 --- a/receiver/apachedruidreceiver/receiver_test.go +++ b/receiver/apachedruidreceiver/receiver_test.go @@ -321,18 +321,6 @@ func TestHandleMetrics(t *testing.T) { metricsSlice := sm.Metrics() require.Equal(t, 5, metricsSlice.Len(), "should have 5 metrics") - expectedMetrics := []string{ - "druid.query.cache.delta.numEntries", - "druid.query.cache.delta.sizeBytes", - "druid.query.cache.delta.hits", - "druid.query.cache.delta.misses", - "druid.query.cache.delta.evictions", - } - - for i := 0; i < metricsSlice.Len(); i++ { - metric := metricsSlice.At(i) - assert.Contains(t, expectedMetrics, metric.Name(), "unexpected metric name") - } }) }