Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
ycombinator committed Jan 17, 2024
1 parent 51bef51 commit 161ebdb
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions exporter/elasticsearchexporter/model_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"}`
Expand Down Expand Up @@ -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)
})
}
}

0 comments on commit 161ebdb

Please sign in to comment.