Skip to content
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
87cfa2a
initial commit
Yun-Ting May 9, 2023
bf8a530
Merge branch 'main' into yunl/runtimeContextEnum
Yun-Ting May 9, 2023
b133982
add custom type
Yun-Ting May 11, 2023
236a5f1
CI
Yun-Ting May 12, 2023
d4c38af
update
Yun-Ting May 12, 2023
bd2d709
update
Yun-Ting May 12, 2023
db04bd1
update
Yun-Ting May 12, 2023
260327b
fix sanity check
Yun-Ting May 12, 2023
6be9d0c
format
Yun-Ting May 12, 2023
5dae942
static ctor
Yun-Ting May 15, 2023
ebee07a
added support for ReflectionRuntimeContextSlotFactory
Yun-Ting May 16, 2023
299fc2a
sanity
Yun-Ting May 16, 2023
e93903f
Merge branch 'main' into yunl/runtimeContextEnum
Yun-Ting May 16, 2023
cc00b30
test CI
Yun-Ting May 16, 2023
99c8baa
Revert "test CI"
Yun-Ting May 17, 2023
a773917
CI
Yun-Ting May 17, 2023
fc32b25
update
Yun-Ting May 17, 2023
6ef81cd
Merge branch 'yunl/runtimeContextEnum' of https://github.com/Yun-Ting…
Yun-Ting May 17, 2023
23b77b5
comment
Yun-Ting May 17, 2023
6faf599
update
Yun-Ting May 17, 2023
e6b016c
ci
Yun-Ting May 17, 2023
44bb210
CI
Yun-Ting May 18, 2023
8d76270
preprossesor
Yun-Ting May 18, 2023
05bb4b6
fix build
Yun-Ting May 18, 2023
8586200
Add the trimming attributes to only OpenTelemetry.Api, and use #if to…
eerhardt May 18, 2023
452f6a6
CI
Yun-Ting May 18, 2023
649e39d
Adjust the folder path to the attributes.
eerhardt May 18, 2023
f0c7f44
CI
Yun-Ting May 18, 2023
f8714f9
Fix nullable warning in LoggerProviderSdk
eerhardt May 18, 2023
7956ab1
CI
Yun-Ting May 18, 2023
d2024a8
address comments
Yun-Ting May 19, 2023
b9c0b33
sanity check
Yun-Ting May 19, 2023
4120ffb
fix test
Yun-Ting May 19, 2023
633da1b
indentation
Yun-Ting May 19, 2023
7c1fde4
increased timeout and removed RS0026
Yun-Ting May 19, 2023
48d0b7f
RS0026
Yun-Ting May 19, 2023
19fce6a
CI
Yun-Ting May 19, 2023
35173c8
Fix ApiCompat for OpenTelemetry.Api's net6.0 target.
eerhardt May 22, 2023
21495bc
CI
Yun-Ting May 22, 2023
586511d
Merge branch 'main' into yunl/runtimeContextEnum
eerhardt May 22, 2023
9ba6626
fixed merge; scope down suppresion of RS0026
Yun-Ting May 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 45 additions & 4 deletions src/OpenTelemetry.Api/Context/RuntimeContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,52 @@ public static class RuntimeContext
{
private static readonly ConcurrentDictionary<string, object> Slots = new();

private static Type contextSlotType;

private static RuntimeContextSlotFactory runtimeContextSlotFactory;

static RuntimeContext()
{
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.IsGenericType || !value.IsGenericTypeDefinition || value.GetGenericArguments().Length != 1)
{
throw new NotSupportedException($"Type '{value}' must be generic with a single generic type argument");
}

if (value == typeof(AsyncLocalRuntimeContextSlot<>))
{
runtimeContextSlotFactory = new RuntimeContextSlotFactory.AsyncLocalRuntimeContextSlotFactory();
}
else if (value == typeof(ThreadLocalRuntimeContextSlot<>))
{
runtimeContextSlotFactory = new RuntimeContextSlotFactory.ThreadLocalRuntimeContextSlotFactory();
}
#if NETFRAMEWORK
else if (value == typeof(RemotingRuntimeContextSlot<>))
{
runtimeContextSlotFactory = new RuntimeContextSlotFactory.RemotingRuntimeContextSlotFactory();
}
#endif
else
{
throw new NotSupportedException("${value} is not supported.");
}

contextSlotType = value;
}
}

/// <summary>
/// Register a named context slot.
Expand All @@ -49,9 +91,8 @@ 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 });
var slot = runtimeContextSlotFactory.Create<T>(slotName);

Slots[slotName] = slot;
return slot;
}
Expand Down
45 changes: 45 additions & 0 deletions src/OpenTelemetry.Api/Context/RuntimeContextSlotFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// <copyright file="RuntimeContextSlotFactory.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

#nullable enable

namespace OpenTelemetry.Context
{
internal abstract class RuntimeContextSlotFactory
{
public abstract RuntimeContextSlot<T> Create<T>(string name);

public sealed class AsyncLocalRuntimeContextSlotFactory : RuntimeContextSlotFactory
{
public override RuntimeContextSlot<T> Create<T>(string name)
=> new AsyncLocalRuntimeContextSlot<T>(name);
}

#if NETFRAMEWORK
public sealed class RemotingRuntimeContextSlotFactory : RuntimeContextSlotFactory
{
public override RuntimeContextSlot<T> Create<T>(string name)
=> new RemotingRuntimeContextSlot<T>(name);
}
#endif

public sealed class ThreadLocalRuntimeContextSlotFactory : RuntimeContextSlotFactory
{
public override RuntimeContextSlot<T> Create<T>(string name)
=> new ThreadLocalRuntimeContextSlot<T>(name);
}
}
}
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(58, warnings.Count());
Assert.Equal(55, warnings.Count());
}
}
}