Skip to content

Commit

Permalink
[repo] Take advantage of System.Threading.Lock when compiling against…
Browse files Browse the repository at this point in the history
… .NET9 (#5861)
  • Loading branch information
CodeBlanch authored Sep 24, 2024
1 parent c1f1c16 commit c8a8913
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 10 deletions.
1 change: 1 addition & 0 deletions OpenTelemetry.sln
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Shims", "Shims", "{A0CB9A10-F22D-4E66-A449-74B3D0361A9C}"
ProjectSection(SolutionItems) = preProject
src\Shared\Shims\IsExternalInit.cs = src\Shared\Shims\IsExternalInit.cs
src\Shared\Shims\Lock.cs = src\Shared\Shims\Lock.cs
src\Shared\Shims\NullableAttributes.cs = src\Shared\Shims\NullableAttributes.cs
EndProjectSection
EndProject
Expand Down
1 change: 1 addition & 0 deletions src/OpenTelemetry.Api/OpenTelemetry.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<Compile Include="$(RepoRoot)\src\Shared\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" />
<Compile Include="$(RepoRoot)\src\Shared\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\Shared\SemanticConventions.cs" Link="Includes\SemanticConventions.cs" />
<Compile Include="$(RepoRoot)\src\Shared\Shims\Lock.cs" Link="Includes\Shims\Lock.cs" />
<Compile Include="$(RepoRoot)\src\Shared\SpanAttributeConstants.cs" Link="Includes\SpanAttributeConstants.cs" />
<Compile Include="$(RepoRoot)\src\Shared\StatusHelper.cs" Link="Includes\StatusHelper.cs" />
</ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ namespace OpenTelemetry.Exporter;
public class ConsoleLogRecordExporter : ConsoleExporter<LogRecord>
{
private const int RightPaddingLength = 35;
private readonly object syncObject = new();
private readonly Lock syncObject = new();
private bool disposed;
private string? disposedStackTrace;
private bool isDisposeMessageSent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<Compile Include="$(RepoRoot)\src\Shared\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" />
<Compile Include="$(RepoRoot)\src\Shared\Guard.cs" Link="Includes\Guard.cs" />
<Compile Include="$(RepoRoot)\src\Shared\MathHelper.cs" Link="Includes\MathHelper.cs" />
<Compile Include="$(RepoRoot)\src\Shared\Shims\Lock.cs" Link="Includes\Shims\Lock.cs" />
<Compile Include="$(RepoRoot)\src\Shared\Shims\NullableAttributes.cs" Link="Includes\Shims\NullableAttributes.cs" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ internal sealed class PrometheusHttpListener : IDisposable
{
private readonly PrometheusExporter exporter;
private readonly HttpListener httpListener = new();
private readonly object syncObject = new();
private readonly Lock syncObject = new();

private CancellationTokenSource? tokenSource;
private Task? workerThread;
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Internal/SelfDiagnosticsEventListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal sealed class SelfDiagnosticsEventListener : EventListener
// Buffer size of the log line. A UTF-16 encoded character in C# can take up to 4 bytes if encoded in UTF-8.
private const int BUFFERSIZE = 4 * 5120;
private const string EventSourceNamePrefix = "OpenTelemetry-";
private readonly object lockObj = new();
private readonly Lock lockObj = new();
private readonly EventLevel logLevel;
private readonly SelfDiagnosticsConfigRefresher configRefresher;
private readonly ThreadLocal<byte[]?> writeBuffer = new(() => null);
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry/Metrics/AggregatorStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ internal sealed class AggregatorStore
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.";
private static readonly Comparison<KeyValuePair<string, object?>> DimensionComparisonDelegate = (x, y) => x.Key.CompareTo(y.Key);

private readonly object lockZeroTags = new();
private readonly object lockOverflowTag = new();
private readonly Lock lockZeroTags = new();
private readonly Lock lockOverflowTag = new();
private readonly int tagsKeysInterestingCount;

// This holds the reclaimed MetricPoints that are available for reuse.
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Metrics/MeterProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ internal sealed class MeterProviderSdk : MeterProvider

private readonly List<object> instrumentations = new();
private readonly List<Func<Instrument, MetricStreamConfiguration?>> viewConfigs;
private readonly object collectLock = new();
private readonly Lock collectLock = new();
private readonly MeterListener listener;
private readonly MetricReader? reader;
private readonly CompositeMetricReader? compositeMetricReader;
Expand Down
4 changes: 2 additions & 2 deletions src/OpenTelemetry/Metrics/Reader/MetricReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public abstract partial class MetricReader : IDisposable
};
};

private readonly object newTaskLock = new();
private readonly object onCollectLock = new();
private readonly Lock newTaskLock = new();
private readonly Lock onCollectLock = new();
private readonly TaskCompletionSource<bool> shutdownTcs = new();
private MetricReaderTemporalityPreference temporalityPreference = MetricReaderTemporalityPreferenceUnspecified;
private Func<Type, AggregationTemporality> temporalityFunc = CumulativeTemporalityPreferenceFunc;
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Metrics/Reader/MetricReaderExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public abstract partial class MetricReader
{
private readonly HashSet<string> metricStreamNames = new(StringComparer.OrdinalIgnoreCase);
private readonly ConcurrentDictionary<MetricStreamIdentity, Metric> instrumentIdentityToMetric = new();
private readonly object instrumentCreationLock = new();
private readonly Lock instrumentCreationLock = new();
private int metricLimit;
private int cardinalityLimit;
private Metric?[]? metrics;
Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry/SimpleExportProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace OpenTelemetry;
public abstract class SimpleExportProcessor<T> : BaseExportProcessor<T>
where T : class
{
private readonly object syncObject = new();
private readonly Lock syncObject = new();

/// <summary>
/// Initializes a new instance of the <see cref="SimpleExportProcessor{T}"/> class.
Expand Down
18 changes: 18 additions & 0 deletions src/Shared/Shims/Lock.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

#if !NET9_0_OR_GREATER
namespace OpenTelemetry;

// Note: .NET9 added the System.Threading.Lock class. The goal here is when
// compiling against .NET9+ code should use System.Threading.Lock class for
// better perf. Legacy code can use this class which will perform a classic
// monitor-based lock against a reference of this class. This type is not in the
// System.Threading namespace so that the compiler doesn't get confused when it
// sees it used. It is in OpenTelemetry namespace and not OpenTelemetry.Internal
// namespace so that code should be able to use it without the presence of a
// dedicated "using OpenTelemetry.Internal" just for the shim.
internal sealed class Lock
{
}
#endif

0 comments on commit c8a8913

Please sign in to comment.