Skip to content

Commit c8a8913

Browse files
authored
[repo] Take advantage of System.Threading.Lock when compiling against .NET9 (open-telemetry#5861)
1 parent c1f1c16 commit c8a8913

File tree

12 files changed

+31
-10
lines changed

12 files changed

+31
-10
lines changed

OpenTelemetry.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ EndProject
281281
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shims", "Shims", "{A0CB9A10-F22D-4E66-A449-74B3D0361A9C}"
282282
ProjectSection(SolutionItems) = preProject
283283
src\Shared\Shims\IsExternalInit.cs = src\Shared\Shims\IsExternalInit.cs
284+
src\Shared\Shims\Lock.cs = src\Shared\Shims\Lock.cs
284285
src\Shared\Shims\NullableAttributes.cs = src\Shared\Shims\NullableAttributes.cs
285286
EndProjectSection
286287
EndProject

src/OpenTelemetry.Api/OpenTelemetry.Api.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<Compile Include="$(RepoRoot)\src\Shared\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" />
1717
<Compile Include="$(RepoRoot)\src\Shared\Guard.cs" Link="Includes\Guard.cs" />
1818
<Compile Include="$(RepoRoot)\src\Shared\SemanticConventions.cs" Link="Includes\SemanticConventions.cs" />
19+
<Compile Include="$(RepoRoot)\src\Shared\Shims\Lock.cs" Link="Includes\Shims\Lock.cs" />
1920
<Compile Include="$(RepoRoot)\src\Shared\SpanAttributeConstants.cs" Link="Includes\SpanAttributeConstants.cs" />
2021
<Compile Include="$(RepoRoot)\src\Shared\StatusHelper.cs" Link="Includes\StatusHelper.cs" />
2122
</ItemGroup>

src/OpenTelemetry.Exporter.Console/ConsoleLogRecordExporter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace OpenTelemetry.Exporter;
1010
public class ConsoleLogRecordExporter : ConsoleExporter<LogRecord>
1111
{
1212
private const int RightPaddingLength = 35;
13-
private readonly object syncObject = new();
13+
private readonly Lock syncObject = new();
1414
private bool disposed;
1515
private string? disposedStackTrace;
1616
private bool isDisposeMessageSent;

src/OpenTelemetry.Exporter.Prometheus.HttpListener/OpenTelemetry.Exporter.Prometheus.HttpListener.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
<Compile Include="$(RepoRoot)\src\Shared\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" />
2020
<Compile Include="$(RepoRoot)\src\Shared\Guard.cs" Link="Includes\Guard.cs" />
2121
<Compile Include="$(RepoRoot)\src\Shared\MathHelper.cs" Link="Includes\MathHelper.cs" />
22+
<Compile Include="$(RepoRoot)\src\Shared\Shims\Lock.cs" Link="Includes\Shims\Lock.cs" />
2223
<Compile Include="$(RepoRoot)\src\Shared\Shims\NullableAttributes.cs" Link="Includes\Shims\NullableAttributes.cs" />
2324
</ItemGroup>
2425

src/OpenTelemetry.Exporter.Prometheus.HttpListener/PrometheusHttpListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ internal sealed class PrometheusHttpListener : IDisposable
1111
{
1212
private readonly PrometheusExporter exporter;
1313
private readonly HttpListener httpListener = new();
14-
private readonly object syncObject = new();
14+
private readonly Lock syncObject = new();
1515

1616
private CancellationTokenSource? tokenSource;
1717
private Task? workerThread;

src/OpenTelemetry/Internal/SelfDiagnosticsEventListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ internal sealed class SelfDiagnosticsEventListener : EventListener
1616
// Buffer size of the log line. A UTF-16 encoded character in C# can take up to 4 bytes if encoded in UTF-8.
1717
private const int BUFFERSIZE = 4 * 5120;
1818
private const string EventSourceNamePrefix = "OpenTelemetry-";
19-
private readonly object lockObj = new();
19+
private readonly Lock lockObj = new();
2020
private readonly EventLevel logLevel;
2121
private readonly SelfDiagnosticsConfigRefresher configRefresher;
2222
private readonly ThreadLocal<byte[]?> writeBuffer = new(() => null);

src/OpenTelemetry/Metrics/AggregatorStore.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ internal sealed class AggregatorStore
3131
private static readonly string MetricPointCapHitFixMessage = "Consider opting in for the experimental SDK feature to emit all the throttled metrics under the overflow attribute by setting env variable OTEL_DOTNET_EXPERIMENTAL_METRICS_EMIT_OVERFLOW_ATTRIBUTE = true. You could also modify instrumentation to reduce the number of unique key/value pair combinations. Or use Views to drop unwanted tags. Or use MeterProviderBuilder.SetMaxMetricPointsPerMetricStream to set higher limit.";
3232
private static readonly Comparison<KeyValuePair<string, object?>> DimensionComparisonDelegate = (x, y) => x.Key.CompareTo(y.Key);
3333

34-
private readonly object lockZeroTags = new();
35-
private readonly object lockOverflowTag = new();
34+
private readonly Lock lockZeroTags = new();
35+
private readonly Lock lockOverflowTag = new();
3636
private readonly int tagsKeysInterestingCount;
3737

3838
// This holds the reclaimed MetricPoints that are available for reuse.

src/OpenTelemetry/Metrics/MeterProviderSdk.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ internal sealed class MeterProviderSdk : MeterProvider
3030

3131
private readonly List<object> instrumentations = new();
3232
private readonly List<Func<Instrument, MetricStreamConfiguration?>> viewConfigs;
33-
private readonly object collectLock = new();
33+
private readonly Lock collectLock = new();
3434
private readonly MeterListener listener;
3535
private readonly MetricReader? reader;
3636
private readonly CompositeMetricReader? compositeMetricReader;

src/OpenTelemetry/Metrics/Reader/MetricReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public abstract partial class MetricReader : IDisposable
3636
};
3737
};
3838

39-
private readonly object newTaskLock = new();
40-
private readonly object onCollectLock = new();
39+
private readonly Lock newTaskLock = new();
40+
private readonly Lock onCollectLock = new();
4141
private readonly TaskCompletionSource<bool> shutdownTcs = new();
4242
private MetricReaderTemporalityPreference temporalityPreference = MetricReaderTemporalityPreferenceUnspecified;
4343
private Func<Type, AggregationTemporality> temporalityFunc = CumulativeTemporalityPreferenceFunc;

src/OpenTelemetry/Metrics/Reader/MetricReaderExt.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public abstract partial class MetricReader
1616
{
1717
private readonly HashSet<string> metricStreamNames = new(StringComparer.OrdinalIgnoreCase);
1818
private readonly ConcurrentDictionary<MetricStreamIdentity, Metric> instrumentIdentityToMetric = new();
19-
private readonly object instrumentCreationLock = new();
19+
private readonly Lock instrumentCreationLock = new();
2020
private int metricLimit;
2121
private int cardinalityLimit;
2222
private Metric?[]? metrics;

0 commit comments

Comments
 (0)