Skip to content

Commit

Permalink
Merge branch 'main' into reyang/clarify-versioning-story
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Jun 6, 2023
2 parents 472e9c0 + a457558 commit 6d4e08f
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
9 changes: 9 additions & 0 deletions src/OpenTelemetry.Api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

## Unreleased

* **Breaking change** In order to make `RuntimeContext` compatible with
ahead-of-time compilation (AOT),
`RuntimeContext.ContextSlotType` can only be assigned one
of the following types: `AsyncLocalRuntimeContextSlot<>`,
`ThreadLocalRuntimeContextSlot<>`, and `RemotingRuntimeContextSlot<>`. A
`System.NotSupportedException` will be thrown if you try to assign any type
other than the three types mentioned.
([#4542](https://github.com/open-telemetry/opentelemetry-dotnet/pull/4542))

## 1.5.0

Released 2023-Jun-05
Expand Down
50 changes: 46 additions & 4 deletions src/OpenTelemetry.Api/Context/RuntimeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,38 @@ public static class RuntimeContext
{
private static readonly ConcurrentDictionary<string, object> Slots = new();

private static Type contextSlotType = typeof(AsyncLocalRuntimeContextSlot<>);

/// <summary>
/// Gets or sets the actual context carrier implementation.
/// </summary>
public static Type ContextSlotType { get; set; } = typeof(AsyncLocalRuntimeContextSlot<>);
public static Type ContextSlotType
{
get => contextSlotType;
set
{
Guard.ThrowIfNull(value, nameof(value));

if (value == typeof(AsyncLocalRuntimeContextSlot<>))
{
contextSlotType = value;
}
else if (value == typeof(ThreadLocalRuntimeContextSlot<>))
{
contextSlotType = value;
}
#if NETFRAMEWORK
else if (value == typeof(RemotingRuntimeContextSlot<>))
{
contextSlotType = value;
}
#endif
else
{
throw new NotSupportedException($"{value} is not a supported type.");
}
}
}

/// <summary>
/// Register a named context slot.
Expand All @@ -41,6 +69,7 @@ public static class RuntimeContext
public static RuntimeContextSlot<T> RegisterSlot<T>(string slotName)
{
Guard.ThrowIfNullOrEmpty(slotName);
RuntimeContextSlot<T> slot = null;

lock (Slots)
{
Expand All @@ -49,9 +78,22 @@ public static RuntimeContextSlot<T> RegisterSlot<T>(string slotName)
throw new InvalidOperationException($"Context slot already registered: '{slotName}'");
}

var type = ContextSlotType.MakeGenericType(typeof(T));
var ctor = type.GetConstructor(new Type[] { typeof(string) });
var slot = (RuntimeContextSlot<T>)ctor.Invoke(new object[] { slotName });
if (ContextSlotType == typeof(AsyncLocalRuntimeContextSlot<>))
{
slot = new AsyncLocalRuntimeContextSlot<T>(slotName);
}
else if (ContextSlotType == typeof(ThreadLocalRuntimeContextSlot<>))
{
slot = new ThreadLocalRuntimeContextSlot<T>(slotName);
}

#if NETFRAMEWORK
else if (ContextSlotType == typeof(RemotingRuntimeContextSlot<>))
{
slot = new RemotingRuntimeContextSlot<T>(slotName);
}
#endif

Slots[slotName] = slot;
return slot;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void EnsureAotCompatibility()
Assert.True(process.ExitCode == 0, "Publishing the AotCompatibility app failed. See test output for more details.");

var warnings = expectedOutput.ToString().Split('\n', '\r').Where(line => line.Contains("warning IL"));
Assert.Equal(43, warnings.Count());
Assert.Equal(40, warnings.Count());
}
}
}

0 comments on commit 6d4e08f

Please sign in to comment.