Skip to content

Commit

Permalink
tests for metric_processor
Browse files Browse the repository at this point in the history
  • Loading branch information
naman47vyas committed Aug 13, 2024
1 parent 9a0ec06 commit af65984
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 69 deletions.
2 changes: 1 addition & 1 deletion receiver/apachedruidreceiver/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
56 changes: 0 additions & 56 deletions receiver/apachedruidreceiver/internal/testdata/expected.json

This file was deleted.

123 changes: 123 additions & 0 deletions receiver/apachedruidreceiver/metric_processor_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
12 changes: 0 additions & 12 deletions receiver/apachedruidreceiver/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
})

}

0 comments on commit af65984

Please sign in to comment.