Skip to content

Commit b1bf339

Browse files
authored
Backport changes for initial metrics enablement #3575 (#3718)
1 parent 607b8c9 commit b1bf339

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Diagnostics/SqlClientMetrics.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,19 @@ internal sealed partial class SqlClientMetrics
9292
private string? _instanceName;
9393
#endif
9494

95-
public SqlClientMetrics(SqlClientEventSource eventSource)
95+
public SqlClientMetrics(SqlClientEventSource eventSource, bool enableMetrics)
9696
{
9797
_eventSource = eventSource;
9898

9999
#if NETFRAMEWORK
100100
// On .NET Framework, metrics are exposed as performance counters and are always enabled.
101101
// On .NET Core, metrics are exposed as EventCounters, and require explicit enablement.
102102
EnablePerformanceCounters();
103+
#else
104+
if (enableMetrics)
105+
{
106+
EnableEventCounters();
107+
}
103108
#endif
104109
}
105110

src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientEventSource.cs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ namespace Microsoft.Data.SqlClient
1515
[EventSource(Name = "Microsoft.Data.SqlClient.EventSource")]
1616
internal partial class SqlClientEventSource : EventSource
1717
{
18+
private static bool s_initialMetricsEnabled = false;
19+
1820
// Defines the singleton instance for the Resources ETW provider
1921
public static readonly SqlClientEventSource Log = new();
2022

2123
// Provides access to metrics.
22-
public static readonly SqlClientMetrics Metrics = new SqlClientMetrics(Log);
24+
public static readonly SqlClientMetrics Metrics = new SqlClientMetrics(Log, s_initialMetricsEnabled);
2325

2426
private SqlClientEventSource() { }
2527

@@ -33,7 +35,14 @@ protected override void OnEventCommand(EventCommandEventArgs command)
3335

3436
if (command.Command == EventCommand.Enable)
3537
{
36-
Metrics.EnableEventCounters();
38+
if (Metrics == null)
39+
{
40+
s_initialMetricsEnabled = true;
41+
}
42+
else
43+
{
44+
Metrics.EnableEventCounters();
45+
}
3746
}
3847
}
3948
#endif

0 commit comments

Comments
 (0)