@@ -32,12 +32,12 @@ internal sealed class SpanBuilderShim : ISpanBuilder
3232 /// <summary>
3333 /// The OpenTelemetry attributes. These correspond to OpenTracing Tags.
3434 /// </summary>
35- private readonly List < KeyValuePair < string , object > > attributes = new ( ) ;
35+ private readonly SpanAttributes attributes = new ( ) ;
3636
3737 /// <summary>
3838 /// The parent as an TelemetrySpan, if any.
3939 /// </summary>
40- private TelemetrySpan parentSpan ;
40+ private TelemetrySpan ? parentSpan ;
4141
4242 /// <summary>
4343 /// The parent as an SpanContext, if any.
@@ -70,7 +70,7 @@ public SpanBuilderShim(Tracer tracer, string spanName)
7070 private bool ParentSet => this . parentSpan != null || this . parentSpanContext . IsValid ;
7171
7272 /// <inheritdoc/>
73- public ISpanBuilder AsChildOf ( ISpanContext parent )
73+ public ISpanBuilder AsChildOf ( ISpanContext ? parent )
7474 {
7575 if ( parent == null )
7676 {
@@ -81,7 +81,7 @@ public ISpanBuilder AsChildOf(ISpanContext parent)
8181 }
8282
8383 /// <inheritdoc/>
84- public ISpanBuilder AsChildOf ( ISpan parent )
84+ public ISpanBuilder AsChildOf ( ISpan ? parent )
8585 {
8686 if ( parent == null )
8787 {
@@ -98,7 +98,7 @@ public ISpanBuilder AsChildOf(ISpan parent)
9898 }
9999
100100 /// <inheritdoc/>
101- public ISpanBuilder AddReference ( string referenceType , ISpanContext referencedContext )
101+ public ISpanBuilder AddReference ( string referenceType , ISpanContext ? referencedContext )
102102 {
103103 if ( referencedContext == null )
104104 {
@@ -132,30 +132,25 @@ public ISpanBuilder IgnoreActiveSpan()
132132 /// <inheritdoc/>
133133 public ISpan Start ( )
134134 {
135- TelemetrySpan span = null ;
135+ TelemetrySpan ? span = null ;
136136
137137 // If specified, this takes precedence.
138138 if ( this . ignoreActiveSpan )
139139 {
140- span = this . tracer . StartRootSpan ( this . spanName , this . spanKind , default , this . links , this . explicitStartTime ?? default ) ;
140+ span = this . tracer . StartRootSpan ( this . spanName , this . spanKind , this . attributes , this . links , this . explicitStartTime ?? default ) ;
141141 }
142142 else if ( this . parentSpan != null )
143143 {
144- span = this . tracer . StartSpan ( this . spanName , this . spanKind , this . parentSpan , default , this . links , this . explicitStartTime ?? default ) ;
144+ span = this . tracer . StartSpan ( this . spanName , this . spanKind , this . parentSpan , this . attributes , this . links , this . explicitStartTime ?? default ) ;
145145 }
146146 else if ( this . parentSpanContext . IsValid )
147147 {
148- span = this . tracer . StartSpan ( this . spanName , this . spanKind , this . parentSpanContext , default , this . links , this . explicitStartTime ?? default ) ;
148+ span = this . tracer . StartSpan ( this . spanName , this . spanKind , this . parentSpanContext , this . attributes , this . links , this . explicitStartTime ?? default ) ;
149149 }
150150
151151 if ( span == null )
152152 {
153- span = this . tracer . StartSpan ( this . spanName , this . spanKind , default ( SpanContext ) , default , null , this . explicitStartTime ?? default ) ;
154- }
155-
156- foreach ( var kvp in this . attributes )
157- {
158- span . SetAttribute ( kvp . Key , kvp . Value . ToString ( ) ) ;
153+ span = this . tracer . StartSpan ( this . spanName , this . spanKind , default ( SpanContext ) , this . attributes , null , this . explicitStartTime ?? default ) ;
159154 }
160155
161156 if ( this . error )
@@ -184,8 +179,13 @@ public ISpanBuilder WithStartTimestamp(DateTimeOffset timestamp)
184179 }
185180
186181 /// <inheritdoc/>
187- public ISpanBuilder WithTag ( string key , string value )
182+ public ISpanBuilder WithTag ( string key , string ? value )
188183 {
184+ if ( key == null )
185+ {
186+ return this ;
187+ }
188+
189189 // see https://opentracing.io/specification/conventions/ for special key handling.
190190 if ( global ::OpenTracing . Tag . Tags . SpanKind . Key . Equals ( key , StringComparison . Ordinal ) )
191191 {
@@ -204,12 +204,7 @@ public ISpanBuilder WithTag(string key, string value)
204204 }
205205 else
206206 {
207- // Keys must be non-null.
208- // Null values => string.Empty.
209- if ( key != null )
210- {
211- this . attributes . Add ( new KeyValuePair < string , object > ( key , value ?? string . Empty ) ) ;
212- }
207+ this . attributes . Add ( key , value ) ;
213208 }
214209
215210 return this ;
@@ -224,7 +219,7 @@ public ISpanBuilder WithTag(string key, bool value)
224219 }
225220 else
226221 {
227- this . attributes . Add ( new KeyValuePair < string , object > ( key , value ) ) ;
222+ this . attributes . Add ( key , value ) ;
228223 }
229224
230225 return this ;
@@ -233,31 +228,31 @@ public ISpanBuilder WithTag(string key, bool value)
233228 /// <inheritdoc/>
234229 public ISpanBuilder WithTag ( string key , int value )
235230 {
236- this . attributes . Add ( new KeyValuePair < string , object > ( key , value ) ) ;
231+ this . attributes . Add ( key , value ) ;
237232 return this ;
238233 }
239234
240235 /// <inheritdoc/>
241236 public ISpanBuilder WithTag ( string key , double value )
242237 {
243- this . attributes . Add ( new KeyValuePair < string , object > ( key , value ) ) ;
238+ this . attributes . Add ( key , value ) ;
244239 return this ;
245240 }
246241
247242 /// <inheritdoc/>
248243 public ISpanBuilder WithTag ( global ::OpenTracing . Tag . BooleanTag tag , bool value )
249244 {
250- Guard . ThrowIfNull ( tag ? . Key ) ;
245+ Guard . ThrowIfNull ( tag ) ;
251246
252247 return this . WithTag ( tag . Key , value ) ;
253248 }
254249
255250 /// <inheritdoc/>
256- public ISpanBuilder WithTag ( global ::OpenTracing . Tag . IntOrStringTag tag , string value )
251+ public ISpanBuilder WithTag ( global ::OpenTracing . Tag . IntOrStringTag tag , string ? value )
257252 {
258- Guard . ThrowIfNull ( tag ? . Key ) ;
253+ Guard . ThrowIfNull ( tag ) ;
259254
260- if ( int . TryParse ( value , out var result ) )
255+ if ( value != null && int . TryParse ( value , out var result ) )
261256 {
262257 return this . WithTag ( tag . Key , result ) ;
263258 }
@@ -268,15 +263,15 @@ public ISpanBuilder WithTag(global::OpenTracing.Tag.IntOrStringTag tag, string v
268263 /// <inheritdoc/>
269264 public ISpanBuilder WithTag ( global ::OpenTracing . Tag . IntTag tag , int value )
270265 {
271- Guard . ThrowIfNull ( tag ? . Key ) ;
266+ Guard . ThrowIfNull ( tag ) ;
272267
273268 return this . WithTag ( tag . Key , value ) ;
274269 }
275270
276271 /// <inheritdoc/>
277- public ISpanBuilder WithTag ( global ::OpenTracing . Tag . StringTag tag , string value )
272+ public ISpanBuilder WithTag ( global ::OpenTracing . Tag . StringTag tag , string ? value )
278273 {
279- Guard . ThrowIfNull ( tag ? . Key ) ;
274+ Guard . ThrowIfNull ( tag ) ;
280275
281276 return this . WithTag ( tag . Key , value ) ;
282277 }
0 commit comments