Skip to content

Commit 1c01770

Browse files
ysolomchenkoKielekCodeBlanch
authored
[Shims, W3C.Tests] Nullable (open-telemetry#5797)
Co-authored-by: Piotr Kiełkowicz <[email protected]> Co-authored-by: Mikel Blanchard <[email protected]>
1 parent 9b83188 commit 1c01770

File tree

16 files changed

+170
-137
lines changed

16 files changed

+170
-137
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
#nullable enable
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
OpenTelemetry.Shims.OpenTracing.TracerShim
2-
OpenTelemetry.Shims.OpenTracing.TracerShim.ActiveSpan.get -> OpenTracing.ISpan
3-
OpenTelemetry.Shims.OpenTracing.TracerShim.BuildSpan(string operationName) -> OpenTracing.ISpanBuilder
4-
OpenTelemetry.Shims.OpenTracing.TracerShim.Extract<TCarrier>(OpenTracing.Propagation.IFormat<TCarrier> format, TCarrier carrier) -> OpenTracing.ISpanContext
5-
OpenTelemetry.Shims.OpenTracing.TracerShim.Inject<TCarrier>(OpenTracing.ISpanContext spanContext, OpenTracing.Propagation.IFormat<TCarrier> format, TCarrier carrier) -> void
6-
OpenTelemetry.Shims.OpenTracing.TracerShim.ScopeManager.get -> OpenTracing.IScopeManager
7-
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.TracerProvider tracerProvider) -> void
8-
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.TracerProvider tracerProvider, OpenTelemetry.Context.Propagation.TextMapPropagator textFormat) -> void
2+
OpenTelemetry.Shims.OpenTracing.TracerShim.ActiveSpan.get -> OpenTracing.ISpan?
3+
OpenTelemetry.Shims.OpenTracing.TracerShim.BuildSpan(string! operationName) -> OpenTracing.ISpanBuilder!
4+
OpenTelemetry.Shims.OpenTracing.TracerShim.Extract<TCarrier>(OpenTracing.Propagation.IFormat<TCarrier>! format, TCarrier carrier) -> OpenTracing.ISpanContext?
5+
OpenTelemetry.Shims.OpenTracing.TracerShim.Inject<TCarrier>(OpenTracing.ISpanContext! spanContext, OpenTracing.Propagation.IFormat<TCarrier>! format, TCarrier carrier) -> void
6+
OpenTelemetry.Shims.OpenTracing.TracerShim.ScopeManager.get -> OpenTracing.IScopeManager!
7+
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.TracerProvider! tracerProvider) -> void
8+
OpenTelemetry.Shims.OpenTracing.TracerShim.TracerShim(OpenTelemetry.Trace.TracerProvider! tracerProvider, OpenTelemetry.Context.Propagation.TextMapPropagator? textFormat) -> void

src/OpenTelemetry.Shims.OpenTracing/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ Notes](../../RELEASENOTES.md).
66

77
## Unreleased
88

9+
* Fixed an issue causing all tag values added via the `ISpanBuilder` API to be
10+
converted to strings on the `ISpan` started from the builder.
11+
([#5797](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5797))
12+
913
## 1.9.0-beta.2
1014

1115
Released 2024-Jun-24

src/OpenTelemetry.Shims.OpenTracing/OpenTelemetry.Shims.OpenTracing.csproj

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@
44
<Description>OpenTracing shim for OpenTelemetry .NET</Description>
55
<PackageTags>$(PackageTags);distributed-tracing;OpenTracing</PackageTags>
66
<MinVerTagPrefix>coreunstable-</MinVerTagPrefix>
7-
8-
<!-- this is temporary. will remove in future PR. -->
9-
<Nullable>disable</Nullable>
107
</PropertyGroup>
118

129
<ItemGroup>

src/OpenTelemetry.Shims.OpenTracing/ScopeManagerShim.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ internal sealed class ScopeManagerShim : IScopeManager
1919
#endif
2020

2121
/// <inheritdoc/>
22-
public IScope Active
22+
public IScope? Active
2323
{
2424
get
2525
{
@@ -56,7 +56,7 @@ public IScope Activate(ISpan span, bool finishSpanOnDispose)
5656
Interlocked.Decrement(ref this.spanScopeTableCount);
5757
}
5858
#endif
59-
scope.Dispose();
59+
scope!.Dispose();
6060
});
6161

6262
SpanScopeTable.Add(shim.Span, instrumentation);
@@ -69,9 +69,9 @@ public IScope Activate(ISpan span, bool finishSpanOnDispose)
6969

7070
private sealed class ScopeInstrumentation : IScope
7171
{
72-
private readonly Action disposeAction;
72+
private readonly Action? disposeAction;
7373

74-
public ScopeInstrumentation(TelemetrySpan span, Action disposeAction = null)
74+
public ScopeInstrumentation(TelemetrySpan span, Action? disposeAction = null)
7575
{
7676
this.Span = new SpanShim(span);
7777
this.disposeAction = disposeAction;

src/OpenTelemetry.Shims.OpenTracing/SpanBuilderShim.cs

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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
}

src/OpenTelemetry.Shims.OpenTracing/SpanShim.cs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public SpanShim(TelemetrySpan span)
3939
/// <inheritdoc/>
4040
public ISpanContext Context => this.spanContextShim;
4141

42-
public TelemetrySpan Span { get; private set; }
42+
public TelemetrySpan Span { get; }
4343

4444
/// <inheritdoc/>
4545
public void Finish()
@@ -54,7 +54,7 @@ public void Finish(DateTimeOffset finishTimestamp)
5454
}
5555

5656
/// <inheritdoc/>
57-
public string GetBaggageItem(string key)
57+
public string? GetBaggageItem(string key)
5858
=> Baggage.GetBaggage(key);
5959

6060
/// <inheritdoc/>
@@ -137,7 +137,7 @@ public ISpan Log(DateTimeOffset timestamp, string @event)
137137
}
138138

139139
/// <inheritdoc/>
140-
public ISpan SetBaggageItem(string key, string value)
140+
public ISpan SetBaggageItem(string key, string? value)
141141
{
142142
Baggage.SetBaggage(key, value);
143143
return this;
@@ -153,7 +153,7 @@ public ISpan SetOperationName(string operationName)
153153
}
154154

155155
/// <inheritdoc/>
156-
public ISpan SetTag(string key, string value)
156+
public ISpan SetTag(string key, string? value)
157157
{
158158
Guard.ThrowIfNull(key);
159159

@@ -201,30 +201,38 @@ public ISpan SetTag(string key, double value)
201201
/// <inheritdoc/>
202202
public ISpan SetTag(global::OpenTracing.Tag.BooleanTag tag, bool value)
203203
{
204-
return this.SetTag(tag?.Key, value);
204+
Guard.ThrowIfNull(tag);
205+
206+
return this.SetTag(tag.Key, value);
205207
}
206208

207209
/// <inheritdoc/>
208-
public ISpan SetTag(global::OpenTracing.Tag.IntOrStringTag tag, string value)
210+
public ISpan SetTag(global::OpenTracing.Tag.IntOrStringTag tag, string? value)
209211
{
210-
if (int.TryParse(value, out var result))
212+
Guard.ThrowIfNull(tag);
213+
214+
if (value != null && int.TryParse(value, out var result))
211215
{
212-
return this.SetTag(tag?.Key, result);
216+
return this.SetTag(tag.Key, result);
213217
}
214218

215-
return this.SetTag(tag?.Key, value);
219+
return this.SetTag(tag.Key, value);
216220
}
217221

218222
/// <inheritdoc/>
219223
public ISpan SetTag(global::OpenTracing.Tag.IntTag tag, int value)
220224
{
221-
return this.SetTag(tag?.Key, value);
225+
Guard.ThrowIfNull(tag);
226+
227+
return this.SetTag(tag.Key, value);
222228
}
223229

224230
/// <inheritdoc/>
225-
public ISpan SetTag(global::OpenTracing.Tag.StringTag tag, string value)
231+
public ISpan SetTag(global::OpenTracing.Tag.StringTag tag, string? value)
226232
{
227-
return this.SetTag(tag?.Key, value);
233+
Guard.ThrowIfNull(tag);
234+
235+
return this.SetTag(tag.Key, value);
228236
}
229237

230238
/// <summary>
@@ -234,7 +242,7 @@ public ISpan SetTag(global::OpenTracing.Tag.StringTag tag, string value)
234242
/// <returns>A 2-Tuple containing the event name and payload information.</returns>
235243
private static Tuple<string, IDictionary<string, object>> ConvertToEventPayload(IEnumerable<KeyValuePair<string, object>> fields)
236244
{
237-
string eventName = null;
245+
string? eventName = null;
238246
var attributes = new Dictionary<string, object>();
239247

240248
foreach (var field in fields)
@@ -268,7 +276,7 @@ private static Tuple<string, IDictionary<string, object>> ConvertToEventPayload(
268276
else
269277
{
270278
// TODO should we completely ignore unsupported types?
271-
attributes.Add(field.Key, field.Value.ToString());
279+
attributes.Add(field.Key, field.Value.ToString()!);
272280
}
273281
}
274282

src/OpenTelemetry.Shims.OpenTracing/TracerShim.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace OpenTelemetry.Shims.OpenTracing;
1414
public class TracerShim : global::OpenTracing.ITracer
1515
{
1616
private readonly Trace.Tracer tracer;
17-
private readonly TextMapPropagator definedPropagator;
17+
private readonly TextMapPropagator? definedPropagator;
1818

1919
/// <summary>
2020
/// Initializes a new instance of the <see cref="TracerShim"/> class.
@@ -30,7 +30,7 @@ public TracerShim(Trace.TracerProvider tracerProvider)
3030
/// </summary>
3131
/// <param name="tracerProvider"><see cref="Trace.TracerProvider"/>.</param>
3232
/// <param name="textFormat"><see cref="TextMapPropagator"/>.</param>
33-
public TracerShim(Trace.TracerProvider tracerProvider, TextMapPropagator textFormat)
33+
public TracerShim(Trace.TracerProvider tracerProvider, TextMapPropagator? textFormat)
3434
{
3535
Guard.ThrowIfNull(tracerProvider);
3636

@@ -46,7 +46,7 @@ public TracerShim(Trace.TracerProvider tracerProvider, TextMapPropagator textFor
4646
public global::OpenTracing.IScopeManager ScopeManager { get; }
4747

4848
/// <inheritdoc/>
49-
public global::OpenTracing.ISpan ActiveSpan => this.ScopeManager.Active?.Span;
49+
public global::OpenTracing.ISpan? ActiveSpan => this.ScopeManager.Active?.Span;
5050

5151
private TextMapPropagator Propagator
5252
{
@@ -63,7 +63,7 @@ private TextMapPropagator Propagator
6363
}
6464

6565
/// <inheritdoc/>
66-
public global::OpenTracing.ISpanContext Extract<TCarrier>(IFormat<TCarrier> format, TCarrier carrier)
66+
public global::OpenTracing.ISpanContext? Extract<TCarrier>(IFormat<TCarrier> format, TCarrier carrier)
6767
{
6868
Guard.ThrowIfNull(format);
6969
Guard.ThrowIfNull(carrier);
@@ -79,7 +79,7 @@ private TextMapPropagator Propagator
7979
carrierMap.Add(entry.Key, new[] { entry.Value });
8080
}
8181

82-
static IEnumerable<string> GetCarrierKeyValue(Dictionary<string, IEnumerable<string>> source, string key)
82+
static IEnumerable<string>? GetCarrierKeyValue(Dictionary<string, IEnumerable<string>> source, string key)
8383
{
8484
if (key == null || !source.TryGetValue(key, out var value))
8585
{

test/OpenTelemetry.Instrumentation.W3cTraceContext.Tests/OpenTelemetry.Instrumentation.W3cTraceContext.Tests.csproj

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
<PropertyGroup>
44
<Description>Unit test project for OpenTelemetry ASP.NET Core instrumentation for W3C Trace Context Trace</Description>
55
<TargetFrameworks>$(TargetFrameworksForAspNetCoreTests)</TargetFrameworks>
6-
<!-- this is temporary. will remove in future PR. -->
7-
<Nullable>disable</Nullable>
86
</PropertyGroup>
97

108
<ItemGroup>

0 commit comments

Comments
 (0)