File tree 4 files changed +87
-8
lines changed
opentelemetry-exporter-zipkin/test/common
opentelemetry-sdk-trace-base
4 files changed +87
-8
lines changed Original file line number Diff line number Diff line change @@ -236,6 +236,40 @@ describe('transform', () => {
236
236
version : '1' ,
237
237
} ) ;
238
238
} ) ;
239
+ it ( 'should map OpenTelemetry constructor attributes to a Zipkin tag' , ( ) => {
240
+ const span = new Span (
241
+ tracer ,
242
+ api . ROOT_CONTEXT ,
243
+ 'my-span' ,
244
+ spanContext ,
245
+ api . SpanKind . SERVER ,
246
+ parentId ,
247
+ [ ] ,
248
+ undefined ,
249
+ undefined ,
250
+ {
251
+ key1 : 'value1' ,
252
+ key2 : 'value2' ,
253
+ }
254
+ ) ;
255
+ const tags : zipkinTypes . Tags = _toZipkinTags (
256
+ span ,
257
+ defaultStatusCodeTagName ,
258
+ defaultStatusErrorTagName
259
+ ) ;
260
+
261
+ assert . deepStrictEqual ( tags , {
262
+ key1 : 'value1' ,
263
+ key2 : 'value2' ,
264
+ [ SemanticResourceAttributes . SERVICE_NAME ] : 'zipkin-test' ,
265
+ 'telemetry.sdk.language' : language ,
266
+ 'telemetry.sdk.name' : 'opentelemetry' ,
267
+ 'telemetry.sdk.version' : VERSION ,
268
+ cost : '112.12' ,
269
+ service : 'ui' ,
270
+ version : '1' ,
271
+ } ) ;
272
+ } ) ;
239
273
it ( 'should map OpenTelemetry SpanStatus.code to a Zipkin tag' , ( ) => {
240
274
const span = new Span (
241
275
tracer ,
Original file line number Diff line number Diff line change @@ -100,7 +100,8 @@ export class Span implements APISpan, ReadableSpan {
100
100
parentSpanId ?: string ,
101
101
links : Link [ ] = [ ] ,
102
102
startTime ?: TimeInput ,
103
- _deprecatedClock ?: unknown // keeping this argument even though it is unused to ensure backwards compatibility
103
+ _deprecatedClock ?: unknown , // keeping this argument even though it is unused to ensure backwards compatibility
104
+ initAttributes ?: SpanAttributes
104
105
) {
105
106
this . name = spanName ;
106
107
this . _spanContext = spanContext ;
@@ -119,6 +120,11 @@ export class Span implements APISpan, ReadableSpan {
119
120
this . resource = parentTracer . resource ;
120
121
this . instrumentationLibrary = parentTracer . instrumentationLibrary ;
121
122
this . _spanLimits = parentTracer . getSpanLimits ( ) ;
123
+
124
+ if ( initAttributes != null ) {
125
+ this . setAttributes ( initAttributes ) ;
126
+ }
127
+
122
128
this . _spanProcessor = parentTracer . getActiveSpanProcessor ( ) ;
123
129
this . _spanProcessor . onStart ( this , context ) ;
124
130
this . _attributeValueLengthLimit =
Original file line number Diff line number Diff line change @@ -132,6 +132,12 @@ export class Tracer implements api.Tracer {
132
132
return nonRecordingSpan ;
133
133
}
134
134
135
+ // Set initial span attributes. The attributes object may have been mutated
136
+ // by the sampler, so we sanitize the merged attributes before setting them.
137
+ const initAttributes = sanitizeAttributes (
138
+ Object . assign ( attributes , samplingResult . attributes )
139
+ ) ;
140
+
135
141
const span = new Span (
136
142
this ,
137
143
context ,
@@ -140,14 +146,10 @@ export class Tracer implements api.Tracer {
140
146
spanKind ,
141
147
parentSpanId ,
142
148
links ,
143
- options . startTime
144
- ) ;
145
- // Set initial span attributes. The attributes object may have been mutated
146
- // by the sampler, so we sanitize the merged attributes before setting them.
147
- const initAttributes = sanitizeAttributes (
148
- Object . assign ( attributes , samplingResult . attributes )
149
+ options . startTime ,
150
+ undefined ,
151
+ initAttributes
149
152
) ;
150
- span . setAttributes ( initAttributes ) ;
151
153
return span ;
152
154
}
153
155
Original file line number Diff line number Diff line change @@ -1054,6 +1054,25 @@ describe('Span', () => {
1054
1054
assert . ok ( started ) ;
1055
1055
} ) ;
1056
1056
1057
+ it ( 'should call onStart synchronously when span is started with initial attributes' , ( ) => {
1058
+ let initAttributes ;
1059
+ const processor : SpanProcessor = {
1060
+ onStart : ( span ) => {
1061
+ initAttributes = { ...span . attributes } ;
1062
+ } ,
1063
+ forceFlush : ( ) => Promise . resolve ( ) ,
1064
+ onEnd ( ) { } ,
1065
+ shutdown : ( ) => Promise . resolve ( ) ,
1066
+ } ;
1067
+
1068
+ const provider = new BasicTracerProvider ( ) ;
1069
+
1070
+ provider . addSpanProcessor ( processor ) ;
1071
+
1072
+ provider . getTracer ( 'default' ) . startSpan ( 'test' , { attributes : { foo : 'bar' } } ) ;
1073
+ assert . deepStrictEqual ( initAttributes , { foo : 'bar' } ) ;
1074
+ } ) ;
1075
+
1057
1076
it ( 'should call onEnd synchronously when span is ended' , ( ) => {
1058
1077
let ended = false ;
1059
1078
const processor : SpanProcessor = {
@@ -1222,5 +1241,23 @@ describe('Span', () => {
1222
1241
} ) ;
1223
1242
} ) ;
1224
1243
} ) ;
1244
+
1245
+ describe ( 'when initial attributes are specified' , ( ) => {
1246
+ it ( 'should store specified attributes' , ( ) => {
1247
+ const span = new Span (
1248
+ tracer ,
1249
+ ROOT_CONTEXT ,
1250
+ name ,
1251
+ spanContext ,
1252
+ SpanKind . CLIENT ,
1253
+ undefined ,
1254
+ undefined ,
1255
+ undefined ,
1256
+ undefined ,
1257
+ { foo : 'bar' }
1258
+ ) ;
1259
+ assert . deepStrictEqual ( span . attributes , { foo : 'bar' } ) ;
1260
+ } ) ;
1261
+ } ) ;
1225
1262
} ) ;
1226
1263
} ) ;
You can’t perform that action at this time.
0 commit comments