From 161ebdbd3dc62018b0bdd02728e2b5f9ab9d947f Mon Sep 17 00:00:00 2001 From: Shaunak Kashyap Date: Fri, 1 Dec 2023 14:10:02 -0800 Subject: [PATCH] Add unit test --- exporter/elasticsearchexporter/model_test.go | 49 ++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/exporter/elasticsearchexporter/model_test.go b/exporter/elasticsearchexporter/model_test.go index e76d1f39e491..076a22ea21d5 100644 --- a/exporter/elasticsearchexporter/model_test.go +++ b/exporter/elasticsearchexporter/model_test.go @@ -8,9 +8,13 @@ import ( "time" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/pdata/pcommon" "go.opentelemetry.io/collector/pdata/ptrace" semconv "go.opentelemetry.io/collector/semconv/v1.18.0" + + "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/elasticsearchexporter/internal/objmodel" ) var expectedSpanBody = `{"@timestamp":"2023-04-19T03:04:05.000000006Z","Attributes.service.instance.id":"23","Duration":1000000,"EndTimestamp":"2023-04-19T03:04:06.000000006Z","Events.fooEvent.evnetMockBar":"bar","Events.fooEvent.evnetMockFoo":"foo","Events.fooEvent.time":"2023-04-19T03:04:05.000000006Z","Kind":"SPAN_KIND_CLIENT","Link":"[{\"attribute\":{},\"spanID\":\"\",\"traceID\":\"01020304050607080807060504030200\"}]","Name":"client span","Resource.cloud.platform":"aws_elastic_beanstalk","Resource.cloud.provider":"aws","Resource.deployment.environment":"BETA","Resource.service.instance.id":"23","Resource.service.name":"some-service","Resource.service.version":"env-version-1234","Scope.lib-foo":"lib-bar","Scope.name":"io.opentelemetry.rabbitmq-2.7","Scope.version":"1.30.0-alpha","SpanId":"1920212223242526","TraceId":"01020304050607080807060504030201","TraceStatus":2,"TraceStatusDescription":"Test"}` @@ -63,3 +67,48 @@ func mockResourceSpans() ptrace.Traces { event.Attributes().PutStr("evnetMockBar", "bar") return traces } + +func TestEncodeAttributes(t *testing.T) { + t.Parallel() + + attributes := pcommon.NewMap() + err := attributes.FromRaw(map[string]any{ + "s": "baz", + "o": map[string]any{ + "sub_i": 19, + }, + }) + require.NoError(t, err) + + tests := map[string]struct { + omitAttributesPrefix bool + want func() objmodel.Document + }{ + "omit": { + omitAttributesPrefix: true, + want: func() objmodel.Document { + return objmodel.DocumentFromAttributes(attributes) + }, + }, + "keep": { + omitAttributesPrefix: false, + want: func() objmodel.Document { + doc := objmodel.Document{} + doc.AddAttributes("Attributes", attributes) + return doc + }, + }, + } + + for name, test := range tests { + t.Run(name, func(t *testing.T) { + m := encodeModel{ + omitAttributesPrefix: test.omitAttributesPrefix, + } + + doc := objmodel.Document{} + m.encodeAttributes(&doc, attributes) + require.Equal(t, test.want(), doc) + }) + } +}