@@ -14,6 +14,7 @@ import (
1414 "go.opentelemetry.io/collector/component/componentstatus"
1515 "go.opentelemetry.io/collector/consumer"
1616 "go.opentelemetry.io/collector/consumer/consumertest"
17+ "go.opentelemetry.io/collector/pdata/pcommon"
1718 "go.opentelemetry.io/collector/pdata/ptrace"
1819 "go.opentelemetry.io/collector/processor"
1920 "go.opentelemetry.io/collector/processor/processortest"
@@ -104,6 +105,12 @@ func TestNilBatch(t *testing.T) {
104105 m .assertBatchesLen (1 )
105106}
106107
108+ func assertKeyInAttributesMatchesValue (t * testing.T , attr pcommon.Map , key string , expected string ) {
109+ v , ok := attr .Get (key )
110+ require .True (t , ok )
111+ require .Equal (t , expected , v .AsString ())
112+ }
113+
107114func TestBasicTranslation (t * testing.T ) {
108115 tests := []struct {
109116 name string
@@ -143,35 +150,27 @@ func TestBasicTranslation(t *testing.T) {
143150 },
144151 fn : func (out * ptrace.Traces ) {
145152 rs := out .ResourceSpans ().At (0 )
146- res := rs .Resource ()
153+ rattr := rs .Resource ().Attributes ()
154+ assertKeyInAttributesMatchesValue (t , rattr , "datadog.service" , "test-service" )
155+ assertKeyInAttributesMatchesValue (t , rattr , "datadog.env" , "spanenv2" )
156+ assertKeyInAttributesMatchesValue (t , rattr , "datadog.version" , "v2" )
157+ assertKeyInAttributesMatchesValue (t , rattr , "datadog.host.name" , "test-host-name" )
158+
147159 span := rs .ScopeSpans ().At (0 ).Spans ().At (0 )
148- ddservice , _ := span .Attributes ().Get ("datadog.service" )
149- require .Equal (t , "test-service" , ddservice .AsString ())
150- ddname , _ := span .Attributes ().Get ("datadog.name" )
151- require .Equal (t , "test-operation" , ddname .AsString ())
152- ddresource , _ := span .Attributes ().Get ("datadog.resource" )
153- require .Equal (t , "test-resource" , ddresource .AsString ())
154- ddType , _ := span .Attributes ().Get ("datadog.type" )
155- require .Equal (t , "web" , ddType .AsString ())
156- ddSpanKind , _ := span .Attributes ().Get ("datadog.span.kind" )
157- require .Equal (t , "server" , ddSpanKind .AsString ())
158- env , _ := span .Attributes ().Get ("datadog.env" )
159- require .Equal (t , "spanenv2" , env .AsString ())
160- version , _ := span .Attributes ().Get ("datadog.version" )
161- require .Equal (t , "v2" , version .AsString ())
162- statusCode , _ := span .Attributes ().Get ("datadog.http_status_code" )
163- require .Equal (t , "200" , statusCode .AsString ())
164- ddError , _ := span .Attributes ().Get ("datadog.error" )
160+ sattr := span .Attributes ()
161+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.name" , "test-operation" )
162+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.resource" , "test-resource" )
163+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.type" , "web" )
164+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.span.kind" , "server" )
165+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.http_status_code" , "200" )
166+ ddError , _ := sattr .Get ("datadog.error" )
165167 require .Equal (t , int64 (0 ), ddError .Int ())
166- _ , ok := span . Attributes () .Get ("datadog.error.msg" )
168+ _ , ok := sattr .Get ("datadog.error.msg" )
167169 require .False (t , ok )
168- _ , ok = span . Attributes () .Get ("datadog.error.type" )
170+ _ , ok = sattr .Get ("datadog.error.type" )
169171 require .False (t , ok )
170- _ , ok = span . Attributes () .Get ("datadog.error.stack" )
172+ _ , ok = sattr .Get ("datadog.error.stack" )
171173 require .False (t , ok )
172-
173- ddHost , _ := res .Attributes ().Get ("datadog.host.name" )
174- require .Equal (t , "test-host-name" , ddHost .AsString ())
175174 },
176175 },
177176 {
@@ -186,7 +185,9 @@ func TestBasicTranslation(t *testing.T) {
186185 "resource.name" : "test-resource" ,
187186 "deployment.environment.name" : "spanenv2" ,
188187 "host.name" : "overridden-host-name" ,
189- "datadog.host.name" : "" ,
188+ "datadog.host.name" : "specified-host-name" ,
189+ "datadog.version" : "specified-version" ,
190+ "service.version" : "overridden-version" ,
190191 },
191192 Spans : []* testutil.OTLPSpan {
192193 {
@@ -216,7 +217,6 @@ func TestBasicTranslation(t *testing.T) {
216217 "datadog.host.name" : "specified-hostname" ,
217218 "datadog.span.kind" : "specified-span-kind" ,
218219 "datadog.env" : "specified-env" ,
219- "datadog.version" : "specified-version" ,
220220 "datadog.http_status_code" : "500" ,
221221 "datadog.error" : 1 ,
222222 "datadog.error.msg" : "specified-error-msg" ,
@@ -231,30 +231,24 @@ func TestBasicTranslation(t *testing.T) {
231231 },
232232 fn : func (out * ptrace.Traces ) {
233233 rs := out .ResourceSpans ().At (0 )
234+ rattr := rs .Resource ().Attributes ()
235+ assertKeyInAttributesMatchesValue (t , rattr , "datadog.service" , "test-service" )
236+ assertKeyInAttributesMatchesValue (t , rattr , "datadog.env" , "spanenv2" )
237+ assertKeyInAttributesMatchesValue (t , rattr , "datadog.version" , "overridden-version" )
238+ assertKeyInAttributesMatchesValue (t , rattr , "datadog.host.name" , "overridden-host-name" )
239+
234240 span := rs .ScopeSpans ().At (0 ).Spans ().At (0 )
235- ddservice , _ := span .Attributes ().Get ("datadog.service" )
236- require .Equal (t , "test-service" , ddservice .AsString ())
237- ddname , _ := span .Attributes ().Get ("datadog.name" )
238- require .Equal (t , "test-operation" , ddname .AsString ())
239- ddresource , _ := span .Attributes ().Get ("datadog.resource" )
240- require .Equal (t , "test-resource" , ddresource .AsString ())
241- ddType , _ := span .Attributes ().Get ("datadog.type" )
242- require .Equal (t , "web" , ddType .AsString ())
243- env , _ := span .Attributes ().Get ("datadog.env" )
244- require .Equal (t , "spanenv2" , env .AsString ())
245- statusCode , _ := span .Attributes ().Get ("datadog.http_status_code" )
246- require .Equal (t , "200" , statusCode .AsString ())
247- ddError , _ := span .Attributes ().Get ("datadog.error" )
241+ sattr := span .Attributes ()
242+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.name" , "test-operation" )
243+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.resource" , "test-resource" )
244+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.type" , "web" )
245+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.span.kind" , "server" )
246+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.http_status_code" , "200" )
247+ ddError , _ := sattr .Get ("datadog.error" )
248248 require .Equal (t , int64 (1 ), ddError .Int ())
249- ddErrorMsg , _ := span .Attributes ().Get ("datadog.error.msg" )
250- require .Equal (t , "overridden-msg" , ddErrorMsg .AsString ())
251- ddErrorType , _ := span .Attributes ().Get ("datadog.error.type" )
252- require .Equal (t , "overridden-type" , ddErrorType .AsString ())
253- ddErrorStack , _ := span .Attributes ().Get ("datadog.error.stack" )
254- require .Equal (t , "overridden-stack" , ddErrorStack .AsString ())
255-
256- ddHost , _ := rs .Resource ().Attributes ().Get ("datadog.host.name" )
257- require .Equal (t , "overridden-host-name" , ddHost .AsString ())
249+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.error.msg" , "overridden-msg" )
250+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.error.type" , "overridden-type" )
251+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.error.stack" , "overridden-stack" )
258252 },
259253 },
260254 {
@@ -265,6 +259,9 @@ func TestBasicTranslation(t *testing.T) {
265259 LibName : "libname" ,
266260 LibVersion : "1.2" ,
267261 Attributes : map [string ]any {
262+ "datadog.service" : "specified-service" ,
263+ "datadog.env" : "specified-env" ,
264+ "datadog.version" : "specified-version" ,
268265 "service.name" : "test-service" ,
269266 "resource.name" : "test-resource" ,
270267 "deployment.environment.name" : "spanenv2" ,
@@ -292,13 +289,10 @@ func TestBasicTranslation(t *testing.T) {
292289 ParentID : [8 ]byte {0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 },
293290 Kind : ptrace .SpanKindServer ,
294291 Attributes : map [string ]any {
295- "datadog.service" : "specified-service" ,
296292 "datadog.resource" : "specified-resource" ,
297293 "datadog.name" : "specified-operation" ,
298294 "datadog.type" : "specified-type" ,
299295 "datadog.span.kind" : "specified-span-kind" ,
300- "datadog.env" : "specified-env" ,
301- "datadog.version" : "specified-version" ,
302296 "datadog.http_status_code" : "500" ,
303297 "datadog.error" : 1 ,
304298 "datadog.error.msg" : "specified-error-msg" ,
@@ -313,30 +307,24 @@ func TestBasicTranslation(t *testing.T) {
313307 },
314308 fn : func (out * ptrace.Traces ) {
315309 rs := out .ResourceSpans ().At (0 )
310+ rattr := rs .Resource ().Attributes ()
311+ assertKeyInAttributesMatchesValue (t , rattr , "datadog.service" , "specified-service" )
312+ assertKeyInAttributesMatchesValue (t , rattr , "datadog.env" , "specified-env" )
313+ assertKeyInAttributesMatchesValue (t , rattr , "datadog.version" , "specified-version" )
314+ assertKeyInAttributesMatchesValue (t , rattr , "datadog.host.name" , "" )
315+
316316 span := rs .ScopeSpans ().At (0 ).Spans ().At (0 )
317- ddservice , _ := span .Attributes ().Get ("datadog.service" )
318- require .Equal (t , "specified-service" , ddservice .AsString ())
319- ddname , _ := span .Attributes ().Get ("datadog.name" )
320- require .Equal (t , "specified-operation" , ddname .AsString ())
321- ddresource , _ := span .Attributes ().Get ("datadog.resource" )
322- require .Equal (t , "specified-resource" , ddresource .AsString ())
323- ddType , _ := span .Attributes ().Get ("datadog.type" )
324- require .Equal (t , "specified-type" , ddType .AsString ())
325- env , _ := span .Attributes ().Get ("datadog.env" )
326- require .Equal (t , "specified-env" , env .AsString ())
327- statusCode , _ := span .Attributes ().Get ("datadog.http_status_code" )
328- require .Equal (t , "500" , statusCode .AsString ())
329- ddError , _ := span .Attributes ().Get ("datadog.error" )
317+ sattr := span .Attributes ()
318+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.name" , "specified-operation" )
319+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.resource" , "specified-resource" )
320+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.type" , "specified-type" )
321+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.span.kind" , "specified-span-kind" )
322+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.http_status_code" , "500" )
323+ ddError , _ := sattr .Get ("datadog.error" )
330324 require .Equal (t , int64 (1 ), ddError .Int ())
331- ddErrorMsg , _ := span .Attributes ().Get ("datadog.error.msg" )
332- require .Equal (t , "specified-error-msg" , ddErrorMsg .AsString ())
333- ddErrorType , _ := span .Attributes ().Get ("datadog.error.type" )
334- require .Equal (t , "specified-error-type" , ddErrorType .AsString ())
335- ddErrorStack , _ := span .Attributes ().Get ("datadog.error.stack" )
336- require .Equal (t , "specified-error-stack" , ddErrorStack .AsString ())
337-
338- ddHost , _ := rs .Resource ().Attributes ().Get ("datadog.host.name" )
339- require .Empty (t , ddHost .AsString ())
325+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.error.msg" , "specified-error-msg" )
326+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.error.type" , "specified-error-type" )
327+ assertKeyInAttributesMatchesValue (t , sattr , "datadog.error.stack" , "specified-error-stack" )
340328 },
341329 },
342330 }
0 commit comments