Skip to content

Commit 5fa9f38

Browse files
committed
use InstrumentationHandleManager
1 parent e086b61 commit 5fa9f38

7 files changed

+31
-58
lines changed

src/OpenTelemetry.Instrumentation.SqlClient/Implementation/SqlClientDiagnosticListener.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public SqlClientDiagnosticListener(string sourceName)
5353

5454
public override void OnEventWritten(string name, object? payload)
5555
{
56-
if (SqlClientInstrumentation.TracingHandles == 0 && SqlClientInstrumentation.MetricHandles == 0)
56+
if (SqlClientInstrumentation.Instance.HandleManager.TracingHandles == 0
57+
&& SqlClientInstrumentation.Instance.HandleManager.MetricHandles == 0)
5758
{
5859
return;
5960
}
@@ -304,7 +305,7 @@ public override void OnEventWritten(string name, object? payload)
304305

305306
private void RecordDuration(Activity? activity, object? payload, bool hasError = false)
306307
{
307-
if (SqlClientInstrumentation.MetricHandles == 0)
308+
if (SqlClientInstrumentation.Instance.HandleManager.MetricHandles == 0)
308309
{
309310
return;
310311
}

src/OpenTelemetry.Instrumentation.SqlClient/Implementation/SqlEventSourceListener.netfx.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ private void OnBeginExecute(EventWrittenEventArgs eventData)
124124
(https://github.com/dotnet/SqlClient/blob/f4568ce68da21db3fe88c0e72e1287368aaa1dc8/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlCommand.cs#L6641)
125125
*/
126126

127-
if (SqlClientInstrumentation.TracingHandles == 0 && SqlClientInstrumentation.MetricHandles == 0)
127+
if (SqlClientInstrumentation.Instance.HandleManager.TracingHandles == 0
128+
&& SqlClientInstrumentation.Instance.HandleManager.MetricHandles == 0)
128129
{
129130
return;
130131
}
@@ -186,7 +187,8 @@ private void OnEndExecute(EventWrittenEventArgs eventData)
186187
[2] -> SqlExceptionNumber
187188
*/
188189

189-
if (SqlClientInstrumentation.TracingHandles == 0 && SqlClientInstrumentation.MetricHandles == 0)
190+
if (SqlClientInstrumentation.Instance.HandleManager.TracingHandles == 0
191+
&& SqlClientInstrumentation.Instance.HandleManager.MetricHandles == 0)
190192
{
191193
return;
192194
}
@@ -197,7 +199,8 @@ private void OnEndExecute(EventWrittenEventArgs eventData)
197199
return;
198200
}
199201

200-
if (SqlClientInstrumentation.TracingHandles == 0 && SqlClientInstrumentation.MetricHandles != 0)
202+
if (SqlClientInstrumentation.Instance.HandleManager.TracingHandles == 0
203+
&& SqlClientInstrumentation.Instance.HandleManager.MetricHandles != 0)
201204
{
202205
this.RecordDuration(null, eventData);
203206
return;
@@ -239,7 +242,7 @@ private void OnEndExecute(EventWrittenEventArgs eventData)
239242

240243
private void RecordDuration(Activity? activity, EventWrittenEventArgs eventData)
241244
{
242-
if (SqlClientInstrumentation.MetricHandles == 0)
245+
if (SqlClientInstrumentation.Instance.HandleManager.MetricHandles == 0)
243246
{
244247
return;
245248
}

src/OpenTelemetry.Instrumentation.SqlClient/OpenTelemetry.Instrumentation.SqlClient.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
<Compile Include="$(RepoRoot)\src\Shared\EnvironmentVariables\*.cs" Link="Includes\EnvironmentVariables\%(Filename).cs" />
2424
<Compile Include="$(RepoRoot)\src\Shared\ExceptionExtensions.cs" Link="Includes\ExceptionExtensions.cs" />
2525
<Compile Include="$(RepoRoot)\src\Shared\Guard.cs" Link="Includes\Guard.cs" />
26+
<Compile Include="$(RepoRoot)\src\Shared\InstrumentationHandleManager.cs" Link="Includes\InstrumentationHandleManager.cs" />
2627
<Compile Include="$(RepoRoot)\src\Shared\ListenerHandler.cs" Link="Includes\ListenerHandler.cs" />
2728
<Compile Include="$(RepoRoot)\src\Shared\NullableAttributes.cs" Link="Includes\NullableAttributes.cs" />
2829
<Compile Include="$(RepoRoot)\src\Shared\PropertyFetcher.cs" Link="Includes\PropertyFetcher.cs" />

src/OpenTelemetry.Instrumentation.SqlClient/SqlClientInstrumentation.cs

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ internal sealed class SqlClientInstrumentation : IDisposable
1818
{
1919
public static readonly SqlClientInstrumentation Instance = new SqlClientInstrumentation();
2020

21+
public readonly InstrumentationHandleManager HandleManager = new();
22+
2123
internal const string SqlClientDiagnosticListenerName = "SqlClientDiagnosticListener";
2224
#if NET
2325
internal const string SqlClientTrimmingUnsupportedMessage = "Trimming is not yet supported with SqlClient instrumentation.";
2426
#endif
25-
internal static int MetricHandles;
26-
internal static int TracingHandles;
2727
#if NETFRAMEWORK
2828
private readonly SqlEventSourceListener sqlEventSourceListener;
2929
#else
@@ -62,10 +62,6 @@ private SqlClientInstrumentation()
6262

6363
public static SqlClientTraceInstrumentationOptions TracingOptions { get; set; } = new SqlClientTraceInstrumentationOptions();
6464

65-
public static IDisposable AddMetricHandle() => new MetricHandle();
66-
67-
public static IDisposable AddTracingHandle() => new TracingHandle();
68-
6965
/// <inheritdoc/>
7066
public void Dispose()
7167
{
@@ -75,48 +71,4 @@ public void Dispose()
7571
this.diagnosticSourceSubscriber?.Dispose();
7672
#endif
7773
}
78-
79-
#if NET
80-
[RequiresUnreferencedCode(SqlClientTrimmingUnsupportedMessage)]
81-
#endif
82-
private sealed class MetricHandle : IDisposable
83-
{
84-
private bool disposed;
85-
86-
public MetricHandle()
87-
{
88-
Interlocked.Increment(ref MetricHandles);
89-
}
90-
91-
public void Dispose()
92-
{
93-
if (!this.disposed)
94-
{
95-
Interlocked.Decrement(ref MetricHandles);
96-
this.disposed = true;
97-
}
98-
}
99-
}
100-
101-
#if NET
102-
[RequiresUnreferencedCode(SqlClientTrimmingUnsupportedMessage)]
103-
#endif
104-
private sealed class TracingHandle : IDisposable
105-
{
106-
private bool disposed;
107-
108-
public TracingHandle()
109-
{
110-
Interlocked.Increment(ref TracingHandles);
111-
}
112-
113-
public void Dispose()
114-
{
115-
if (!this.disposed)
116-
{
117-
Interlocked.Decrement(ref TracingHandles);
118-
this.disposed = true;
119-
}
120-
}
121-
}
12274
}

src/OpenTelemetry.Instrumentation.SqlClient/SqlClientMeterProviderBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public static MeterProviderBuilder AddSqlClientInstrumentation(this MeterProvide
2929

3030
builder.AddInstrumentation(sp =>
3131
{
32-
return SqlClientInstrumentation.AddMetricHandle();
32+
return SqlClientInstrumentation.Instance.HandleManager.AddMetricHandle();
3333
});
3434

3535
builder.AddMeter(SqlActivitySourceHelper.MeterName);

src/OpenTelemetry.Instrumentation.SqlClient/TracerProviderBuilderExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static TracerProviderBuilder AddSqlClientInstrumentation(
7070
{
7171
var sqlOptions = sp.GetRequiredService<IOptionsMonitor<SqlClientTraceInstrumentationOptions>>().Get(name);
7272
SqlClientInstrumentation.TracingOptions = sqlOptions;
73-
return SqlClientInstrumentation.AddTracingHandle();
73+
return SqlClientInstrumentation.Instance.HandleManager.AddTracingHandle();
7474
});
7575

7676
builder.AddSource(SqlActivitySourceHelper.ActivitySourceName);

src/Shared/InstrumentationHandleManager.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@
33

44
namespace OpenTelemetry.Instrumentation;
55

6+
#if NET
7+
using System.Diagnostics.CodeAnalysis;
8+
#endif
9+
10+
#if NET
11+
[RequiresUnreferencedCode(InstrumentationHandleManagerTrimmingUnsupportedMessage)]
12+
#endif
613
internal sealed class InstrumentationHandleManager
714
{
15+
#if NET
16+
internal const string InstrumentationHandleManagerTrimmingUnsupportedMessage = "Trimming is not yet supported for InstrumentationHandleManager.";
17+
#endif
818
private int metricHandles;
919
private int tracingHandles;
1020

@@ -30,6 +40,9 @@ internal sealed class InstrumentationHandleManager
3040
/// <returns>An IDisposable object.</returns>
3141
public IDisposable AddTracingHandle() => new TracingHandle(this);
3242

43+
#if NET
44+
[RequiresUnreferencedCode(InstrumentationHandleManagerTrimmingUnsupportedMessage)]
45+
#endif
3346
private sealed class MetricHandle : IDisposable
3447
{
3548
private readonly InstrumentationHandleManager manager;
@@ -51,6 +64,9 @@ public void Dispose()
5164
}
5265
}
5366

67+
#if NET
68+
[RequiresUnreferencedCode(InstrumentationHandleManagerTrimmingUnsupportedMessage)]
69+
#endif
5470
private sealed class TracingHandle : IDisposable
5571
{
5672
private readonly InstrumentationHandleManager manager;

0 commit comments

Comments
 (0)