Skip to content

Commit 2097f47

Browse files
authored
[api] Context nullable annotations (open-telemetry#5850)
1 parent 3a09a91 commit 2097f47

File tree

9 files changed

+188
-52
lines changed

9 files changed

+188
-52
lines changed

src/OpenTelemetry.Api/.publicApi/Stable/PublicAPI.Shipped.txt

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,16 @@
11
#nullable enable
2-
~OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>.AsyncLocalRuntimeContextSlot(string name) -> void
3-
~OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>.Value.get -> object
4-
~OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>.Value.set -> void
5-
~OpenTelemetry.Context.IRuntimeContextSlotValueAccessor.Value.get -> object
6-
~OpenTelemetry.Context.IRuntimeContextSlotValueAccessor.Value.set -> void
7-
~OpenTelemetry.Context.RuntimeContextSlot<T>.Name.get -> string
8-
~OpenTelemetry.Context.RuntimeContextSlot<T>.RuntimeContextSlot(string name) -> void
9-
~OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.ThreadLocalRuntimeContextSlot(string name) -> void
10-
~OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.Value.get -> object
11-
~OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.Value.set -> void
12-
~static OpenTelemetry.Context.RuntimeContext.ContextSlotType.get -> System.Type
13-
~static OpenTelemetry.Context.RuntimeContext.ContextSlotType.set -> void
14-
~static OpenTelemetry.Context.RuntimeContext.GetSlot<T>(string slotName) -> OpenTelemetry.Context.RuntimeContextSlot<T>
15-
~static OpenTelemetry.Context.RuntimeContext.GetValue(string slotName) -> object
16-
~static OpenTelemetry.Context.RuntimeContext.GetValue<T>(string slotName) -> T
17-
~static OpenTelemetry.Context.RuntimeContext.RegisterSlot<T>(string slotName) -> OpenTelemetry.Context.RuntimeContextSlot<T>
18-
~static OpenTelemetry.Context.RuntimeContext.SetValue(string slotName, object value) -> void
19-
~static OpenTelemetry.Context.RuntimeContext.SetValue<T>(string slotName, T value) -> void
2+
static OpenTelemetry.Context.RuntimeContext.ContextSlotType.get -> System.Type!
3+
static OpenTelemetry.Context.RuntimeContext.ContextSlotType.set -> void
4+
static OpenTelemetry.Context.RuntimeContext.GetSlot<T>(string! slotName) -> OpenTelemetry.Context.RuntimeContextSlot<T>!
5+
static OpenTelemetry.Context.RuntimeContext.GetValue(string! slotName) -> object?
6+
static OpenTelemetry.Context.RuntimeContext.GetValue<T>(string! slotName) -> T?
7+
static OpenTelemetry.Context.RuntimeContext.RegisterSlot<T>(string! slotName) -> OpenTelemetry.Context.RuntimeContextSlot<T>!
8+
static OpenTelemetry.Context.RuntimeContext.SetValue(string! slotName, object? value) -> void
9+
static OpenTelemetry.Context.RuntimeContext.SetValue<T>(string! slotName, T value) -> void
2010
abstract OpenTelemetry.Context.Propagation.TextMapPropagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string!, System.Collections.Generic.IEnumerable<string!>?>! getter) -> OpenTelemetry.Context.Propagation.PropagationContext
2111
abstract OpenTelemetry.Context.Propagation.TextMapPropagator.Fields.get -> System.Collections.Generic.ISet<string!>?
2212
abstract OpenTelemetry.Context.Propagation.TextMapPropagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string!, string!>! setter) -> void
23-
abstract OpenTelemetry.Context.RuntimeContextSlot<T>.Get() -> T
13+
abstract OpenTelemetry.Context.RuntimeContextSlot<T>.Get() -> T?
2414
abstract OpenTelemetry.Context.RuntimeContextSlot<T>.Set(T value) -> void
2515
abstract OpenTelemetry.Logs.LoggerProviderBuilder.AddInstrumentation<TInstrumentation>(System.Func<TInstrumentation>! instrumentationFactory) -> OpenTelemetry.Logs.LoggerProviderBuilder!
2616
abstract OpenTelemetry.Metrics.MeterProviderBuilder.AddInstrumentation<TInstrumentation>(System.Func<TInstrumentation>! instrumentationFactory) -> OpenTelemetry.Metrics.MeterProviderBuilder!
@@ -46,7 +36,12 @@ OpenTelemetry.BaseProvider.~BaseProvider() -> void
4636
OpenTelemetry.BaseProvider.BaseProvider() -> void
4737
OpenTelemetry.BaseProvider.Dispose() -> void
4838
OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>
39+
OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>.AsyncLocalRuntimeContextSlot(string! name) -> void
40+
OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>.Value.get -> object?
41+
OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>.Value.set -> void
4942
OpenTelemetry.Context.IRuntimeContextSlotValueAccessor
43+
OpenTelemetry.Context.IRuntimeContextSlotValueAccessor.Value.get -> object?
44+
OpenTelemetry.Context.IRuntimeContextSlotValueAccessor.Value.set -> void
5045
OpenTelemetry.Context.Propagation.B3Propagator
5146
OpenTelemetry.Context.Propagation.B3Propagator.B3Propagator() -> void
5247
OpenTelemetry.Context.Propagation.B3Propagator.B3Propagator(bool singleHeader) -> void
@@ -68,7 +63,12 @@ OpenTelemetry.Context.Propagation.TraceContextPropagator.TraceContextPropagator(
6863
OpenTelemetry.Context.RuntimeContext
6964
OpenTelemetry.Context.RuntimeContextSlot<T>
7065
OpenTelemetry.Context.RuntimeContextSlot<T>.Dispose() -> void
66+
OpenTelemetry.Context.RuntimeContextSlot<T>.Name.get -> string!
67+
OpenTelemetry.Context.RuntimeContextSlot<T>.RuntimeContextSlot(string! name) -> void
7168
OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>
69+
OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.ThreadLocalRuntimeContextSlot(string! name) -> void
70+
OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.Value.get -> object?
71+
OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.Value.set -> void
7272
OpenTelemetry.Logs.IDeferredLoggerProviderBuilder
7373
OpenTelemetry.Logs.IDeferredLoggerProviderBuilder.Configure(System.Action<System.IServiceProvider!, OpenTelemetry.Logs.LoggerProviderBuilder!>! configure) -> OpenTelemetry.Logs.LoggerProviderBuilder!
7474
OpenTelemetry.Logs.LoggerProvider
@@ -165,7 +165,7 @@ OpenTelemetry.Trace.TracerProviderBuilder
165165
OpenTelemetry.Trace.TracerProviderBuilder.TracerProviderBuilder() -> void
166166
override OpenTelemetry.Baggage.Equals(object? obj) -> bool
167167
override OpenTelemetry.Baggage.GetHashCode() -> int
168-
override OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>.Get() -> T
168+
override OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>.Get() -> T?
169169
override OpenTelemetry.Context.AsyncLocalRuntimeContextSlot<T>.Set(T value) -> void
170170
override OpenTelemetry.Context.Propagation.B3Propagator.Extract<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Func<T, string!, System.Collections.Generic.IEnumerable<string!>?>! getter) -> OpenTelemetry.Context.Propagation.PropagationContext
171171
override OpenTelemetry.Context.Propagation.B3Propagator.Fields.get -> System.Collections.Generic.ISet<string!>!
@@ -182,7 +182,7 @@ override OpenTelemetry.Context.Propagation.TraceContextPropagator.Extract<T>(Ope
182182
override OpenTelemetry.Context.Propagation.TraceContextPropagator.Fields.get -> System.Collections.Generic.ISet<string!>!
183183
override OpenTelemetry.Context.Propagation.TraceContextPropagator.Inject<T>(OpenTelemetry.Context.Propagation.PropagationContext context, T carrier, System.Action<T, string!, string!>! setter) -> void
184184
override OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.Dispose(bool disposing) -> void
185-
override OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.Get() -> T
185+
override OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.Get() -> T?
186186
override OpenTelemetry.Context.ThreadLocalRuntimeContextSlot<T>.Set(T value) -> void
187187
override OpenTelemetry.Trace.Link.Equals(object? obj) -> bool
188188
override OpenTelemetry.Trace.Link.GetHashCode() -> int
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
~OpenTelemetry.Context.RemotingRuntimeContextSlot<T>.RemotingRuntimeContextSlot(string name) -> void
2-
~OpenTelemetry.Context.RemotingRuntimeContextSlot<T>.Value.get -> object
3-
~OpenTelemetry.Context.RemotingRuntimeContextSlot<T>.Value.set -> void
41
OpenTelemetry.Context.RemotingRuntimeContextSlot<T>
5-
override OpenTelemetry.Context.RemotingRuntimeContextSlot<T>.Get() -> T
2+
OpenTelemetry.Context.RemotingRuntimeContextSlot<T>.RemotingRuntimeContextSlot(string! name) -> void
3+
OpenTelemetry.Context.RemotingRuntimeContextSlot<T>.Value.get -> object?
4+
OpenTelemetry.Context.RemotingRuntimeContextSlot<T>.Value.set -> void
5+
override OpenTelemetry.Context.RemotingRuntimeContextSlot<T>.Get() -> T?
66
override OpenTelemetry.Context.RemotingRuntimeContextSlot<T>.Set(T value) -> void

src/OpenTelemetry.Api/Context/AsyncLocalRuntimeContextSlot.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#nullable enable
5+
46
using System.Runtime.CompilerServices;
57

68
namespace OpenTelemetry.Context;
@@ -24,15 +26,25 @@ public AsyncLocalRuntimeContextSlot(string name)
2426
}
2527

2628
/// <inheritdoc/>
27-
public object Value
29+
public object? Value
2830
{
2931
get => this.slot.Value;
30-
set => this.slot.Value = (T)value;
32+
set
33+
{
34+
if (typeof(T).IsValueType && value is null)
35+
{
36+
this.slot.Value = default!;
37+
}
38+
else
39+
{
40+
this.slot.Value = (T)value!;
41+
}
42+
}
3143
}
3244

3345
/// <inheritdoc/>
3446
[MethodImpl(MethodImplOptions.AggressiveInlining)]
35-
public override T Get()
47+
public override T? Get()
3648
{
3749
return this.slot.Value;
3850
}

src/OpenTelemetry.Api/Context/IRuntimeContextSlotValueAccessor.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#nullable enable
5+
46
namespace OpenTelemetry.Context;
57

68
/// <summary>
@@ -11,5 +13,5 @@ public interface IRuntimeContextSlotValueAccessor
1113
/// <summary>
1214
/// Gets or sets the value of the slot as an <see cref="object"/>.
1315
/// </summary>
14-
object Value { get; set; }
16+
object? Value { get; set; }
1517
}

src/OpenTelemetry.Api/Context/RemotingRuntimeContextSlot.cs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
#if NETFRAMEWORK
5+
#nullable enable
6+
57
using System.Collections;
68
using System.Reflection;
79
using System.Runtime.CompilerServices;
@@ -38,25 +40,39 @@ public RemotingRuntimeContextSlot(string name)
3840
}
3941

4042
/// <inheritdoc/>
41-
public object Value
43+
public object? Value
4244
{
4345
get => this.Get();
44-
set => this.Set((T)value);
46+
set
47+
{
48+
if (typeof(T).IsValueType && value is null)
49+
{
50+
this.Set(default!);
51+
}
52+
else
53+
{
54+
this.Set((T)value!);
55+
}
56+
}
4557
}
4658

4759
/// <inheritdoc/>
4860
[MethodImpl(MethodImplOptions.AggressiveInlining)]
49-
public override T Get()
61+
public override T? Get()
5062
{
51-
if (!(CallContext.LogicalGetData(this.Name) is BitArray wrapper))
63+
if (CallContext.LogicalGetData(this.Name) is not BitArray wrapper)
5264
{
5365
return default;
5466
}
5567

5668
var value = WrapperField.GetValue(wrapper);
57-
return value is T t
58-
? t
59-
: default;
69+
70+
if (typeof(T).IsValueType && value is null)
71+
{
72+
return default;
73+
}
74+
75+
return (T)value;
6076
}
6177

6278
/// <inheritdoc/>

src/OpenTelemetry.Api/Context/RuntimeContext.cs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#nullable enable
5+
46
using System.Collections.Concurrent;
57
using System.Runtime.CompilerServices;
68
using OpenTelemetry.Internal;
@@ -56,7 +58,8 @@ public static Type ContextSlotType
5658
public static RuntimeContextSlot<T> RegisterSlot<T>(string slotName)
5759
{
5860
Guard.ThrowIfNullOrEmpty(slotName);
59-
RuntimeContextSlot<T> slot = null;
61+
62+
RuntimeContextSlot<T>? slot = null;
6063

6164
lock (Slots)
6265
{
@@ -80,6 +83,10 @@ public static RuntimeContextSlot<T> RegisterSlot<T>(string slotName)
8083
slot = new RemotingRuntimeContextSlot<T>(slotName);
8184
}
8285
#endif
86+
else
87+
{
88+
throw new NotSupportedException($"ContextSlotType '{ContextSlotType}' is not supported");
89+
}
8390

8491
Slots[slotName] = slot;
8592
return slot;
@@ -95,9 +102,10 @@ public static RuntimeContextSlot<T> RegisterSlot<T>(string slotName)
95102
public static RuntimeContextSlot<T> GetSlot<T>(string slotName)
96103
{
97104
Guard.ThrowIfNullOrEmpty(slotName);
105+
98106
var slot = GuardNotFound(slotName);
99-
var contextSlot = Guard.ThrowIfNotOfType<RuntimeContextSlot<T>>(slot);
100-
return contextSlot;
107+
108+
return Guard.ThrowIfNotOfType<RuntimeContextSlot<T>>(slot);
101109
}
102110

103111
/*
@@ -143,7 +151,7 @@ public static void SetValue<T>(string slotName, T value)
143151
/// <typeparam name="T">The type of the value.</typeparam>
144152
/// <returns>The value retrieved from the context slot.</returns>
145153
[MethodImpl(MethodImplOptions.AggressiveInlining)]
146-
public static T GetValue<T>(string slotName)
154+
public static T? GetValue<T>(string slotName)
147155
{
148156
return GetSlot<T>(slotName).Get();
149157
}
@@ -153,25 +161,27 @@ public static T GetValue<T>(string slotName)
153161
/// </summary>
154162
/// <param name="slotName">The name of the context slot.</param>
155163
/// <param name="value">The value to be set.</param>
156-
public static void SetValue(string slotName, object value)
164+
public static void SetValue(string slotName, object? value)
157165
{
158166
Guard.ThrowIfNullOrEmpty(slotName);
167+
159168
var slot = GuardNotFound(slotName);
160-
var runtimeContextSlotValueAccessor = Guard.ThrowIfNotOfType<IRuntimeContextSlotValueAccessor>(slot);
161-
runtimeContextSlotValueAccessor.Value = value;
169+
170+
Guard.ThrowIfNotOfType<IRuntimeContextSlotValueAccessor>(slot).Value = value;
162171
}
163172

164173
/// <summary>
165174
/// Gets the value from a registered slot.
166175
/// </summary>
167176
/// <param name="slotName">The name of the context slot.</param>
168177
/// <returns>The value retrieved from the context slot.</returns>
169-
public static object GetValue(string slotName)
178+
public static object? GetValue(string slotName)
170179
{
171180
Guard.ThrowIfNullOrEmpty(slotName);
181+
172182
var slot = GuardNotFound(slotName);
173-
var runtimeContextSlotValueAccessor = Guard.ThrowIfNotOfType<IRuntimeContextSlotValueAccessor>(slot);
174-
return runtimeContextSlotValueAccessor.Value;
183+
184+
return Guard.ThrowIfNotOfType<IRuntimeContextSlotValueAccessor>(slot).Value;
175185
}
176186

177187
// For testing purpose

src/OpenTelemetry.Api/Context/RuntimeContextSlot.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#nullable enable
5+
6+
using OpenTelemetry.Internal;
7+
48
namespace OpenTelemetry.Context;
59

610
/// <summary>
@@ -15,6 +19,8 @@ public abstract class RuntimeContextSlot<T> : IDisposable
1519
/// <param name="name">The name of the context slot.</param>
1620
protected RuntimeContextSlot(string name)
1721
{
22+
Guard.ThrowIfNullOrEmpty(name);
23+
1824
this.Name = name;
1925
}
2026

@@ -27,7 +33,7 @@ protected RuntimeContextSlot(string name)
2733
/// Get the value from the context slot.
2834
/// </summary>
2935
/// <returns>The value retrieved from the context slot.</returns>
30-
public abstract T Get();
36+
public abstract T? Get();
3137

3238
/// <summary>
3339
/// Set the value to the context slot.

src/OpenTelemetry.Api/Context/ThreadLocalRuntimeContextSlot.cs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#nullable enable
5+
46
using System.Runtime.CompilerServices;
57

68
namespace OpenTelemetry.Context;
@@ -25,15 +27,25 @@ public ThreadLocalRuntimeContextSlot(string name)
2527
}
2628

2729
/// <inheritdoc/>
28-
public object Value
30+
public object? Value
2931
{
3032
get => this.slot.Value;
31-
set => this.slot.Value = (T)value;
33+
set
34+
{
35+
if (typeof(T).IsValueType && value is null)
36+
{
37+
this.slot.Value = default!;
38+
}
39+
else
40+
{
41+
this.slot.Value = (T)value!;
42+
}
43+
}
3244
}
3345

3446
/// <inheritdoc/>
3547
[MethodImpl(MethodImplOptions.AggressiveInlining)]
36-
public override T Get()
48+
public override T? Get()
3749
{
3850
return this.slot.Value;
3951
}

0 commit comments

Comments
 (0)