Skip to content

SqlClient event counters not enabled when listening with App Insights SDK #3711

@empowerRob

Description

@empowerRob

Describe the bug

We are using Azure App Insights SDK [2.23.0] configured to capture event counters from multiple dotnet sources.

Recently we upgraded the SqlClient to a new version which brought with it a recent change in the way event counters are handled internally. This seems to have stopped the capturing of event counters by the App Insights SDK

My investigation has brought me to the following information - please confirm if my findings are correct.

This change in SqlClientEventSource.cs‎:

if (command.Command == EventCommand.Enable)
{
    Metrics.EnableEventCounters();
}

The use of only EventCommand.Enable here appears to be specific to what I think is causing the issue.

App Insights SDK calls to enable events for all event sources / counters to tracked. It calls to the method System.Diagnostics.Tracing.EventListener.EnableEvents which takes in an EventSource and sends the command of type EventCommand.Update with EventCommandEventArgs that has an internal state of enabled set true.

Here is the command it sends

eventSource.SendCommand(this, 0, 0, EventCommand.Update, true, level, matchAnyKeyword, arguments);

The change made to SqlClientEventSource.cs only enables metrics collection if EventCommand.Enable is provided. Meaning it's just not enabled when the System.Diagnostics.Tracing.EventListener.EnableEvents method is used.

Does that sound right?

EDIT: looks like this was a bug in sequencing / initialization of the SqlClientEventSource, this bug #3575 was fixed in a newer version and a backport of it to version 6.1.x has been requested and is in progress - thanks Edward Neal!

Further technical details

Microsoft.Data.SqlClient version: 6.1.1
.NET target: .NET 9.0
ApplicationInsights: 2.23.0

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions