@@ -14,6 +14,7 @@ import (
14
14
"go.opentelemetry.io/collector/component/componentstatus"
15
15
"go.opentelemetry.io/collector/consumer"
16
16
"go.opentelemetry.io/collector/consumer/consumertest"
17
+ "go.opentelemetry.io/collector/pdata/pcommon"
17
18
"go.opentelemetry.io/collector/pdata/ptrace"
18
19
"go.opentelemetry.io/collector/processor"
19
20
"go.opentelemetry.io/collector/processor/processortest"
@@ -104,6 +105,12 @@ func TestNilBatch(t *testing.T) {
104
105
m .assertBatchesLen (1 )
105
106
}
106
107
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
+
107
114
func TestBasicTranslation (t * testing.T ) {
108
115
tests := []struct {
109
116
name string
@@ -143,35 +150,27 @@ func TestBasicTranslation(t *testing.T) {
143
150
},
144
151
fn : func (out * ptrace.Traces ) {
145
152
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
+
147
159
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" )
165
167
require .Equal (t , int64 (0 ), ddError .Int ())
166
- _ , ok := span . Attributes () .Get ("datadog.error.msg" )
168
+ _ , ok := sattr .Get ("datadog.error.msg" )
167
169
require .False (t , ok )
168
- _ , ok = span . Attributes () .Get ("datadog.error.type" )
170
+ _ , ok = sattr .Get ("datadog.error.type" )
169
171
require .False (t , ok )
170
- _ , ok = span . Attributes () .Get ("datadog.error.stack" )
172
+ _ , ok = sattr .Get ("datadog.error.stack" )
171
173
require .False (t , ok )
172
-
173
- ddHost , _ := res .Attributes ().Get ("datadog.host.name" )
174
- require .Equal (t , "test-host-name" , ddHost .AsString ())
175
174
},
176
175
},
177
176
{
@@ -186,7 +185,9 @@ func TestBasicTranslation(t *testing.T) {
186
185
"resource.name" : "test-resource" ,
187
186
"deployment.environment.name" : "spanenv2" ,
188
187
"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" ,
190
191
},
191
192
Spans : []* testutil.OTLPSpan {
192
193
{
@@ -216,7 +217,6 @@ func TestBasicTranslation(t *testing.T) {
216
217
"datadog.host.name" : "specified-hostname" ,
217
218
"datadog.span.kind" : "specified-span-kind" ,
218
219
"datadog.env" : "specified-env" ,
219
- "datadog.version" : "specified-version" ,
220
220
"datadog.http_status_code" : "500" ,
221
221
"datadog.error" : 1 ,
222
222
"datadog.error.msg" : "specified-error-msg" ,
@@ -231,30 +231,24 @@ func TestBasicTranslation(t *testing.T) {
231
231
},
232
232
fn : func (out * ptrace.Traces ) {
233
233
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
+
234
240
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" )
248
248
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" )
258
252
},
259
253
},
260
254
{
@@ -265,6 +259,9 @@ func TestBasicTranslation(t *testing.T) {
265
259
LibName : "libname" ,
266
260
LibVersion : "1.2" ,
267
261
Attributes : map [string ]any {
262
+ "datadog.service" : "specified-service" ,
263
+ "datadog.env" : "specified-env" ,
264
+ "datadog.version" : "specified-version" ,
268
265
"service.name" : "test-service" ,
269
266
"resource.name" : "test-resource" ,
270
267
"deployment.environment.name" : "spanenv2" ,
@@ -292,13 +289,10 @@ func TestBasicTranslation(t *testing.T) {
292
289
ParentID : [8 ]byte {0 , 0 , 0 , 0 , 0 , 0 , 0 , 1 },
293
290
Kind : ptrace .SpanKindServer ,
294
291
Attributes : map [string ]any {
295
- "datadog.service" : "specified-service" ,
296
292
"datadog.resource" : "specified-resource" ,
297
293
"datadog.name" : "specified-operation" ,
298
294
"datadog.type" : "specified-type" ,
299
295
"datadog.span.kind" : "specified-span-kind" ,
300
- "datadog.env" : "specified-env" ,
301
- "datadog.version" : "specified-version" ,
302
296
"datadog.http_status_code" : "500" ,
303
297
"datadog.error" : 1 ,
304
298
"datadog.error.msg" : "specified-error-msg" ,
@@ -313,30 +307,24 @@ func TestBasicTranslation(t *testing.T) {
313
307
},
314
308
fn : func (out * ptrace.Traces ) {
315
309
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
+
316
316
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" )
330
324
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" )
340
328
},
341
329
},
342
330
}
0 commit comments