Optimize diagnostic source event subscription to include isEnabledFilter (#3965)

This commit is contained in:
Han Gao 2022-12-08 09:01:31 +08:00 committed by GitHub
parent 7401069298
commit 589e891d62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 5 deletions

View File

@ -13,4 +13,4 @@ OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.SetDbSta
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.SetDbStatementForText.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.SqlClientInstrumentationOptions() -> void
OpenTelemetry.Trace.TracerProviderBuilderExtensions
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddSqlClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions> configureSqlClientInstrumentationOptions = null) -> OpenTelemetry.Trace.TracerProviderBuilder
static OpenTelemetry.Trace.TracerProviderBuilderExtensions.AddSqlClientInstrumentation(this OpenTelemetry.Trace.TracerProviderBuilder builder, System.Action<OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions> configureSqlClientInstrumentationOptions = null) -> OpenTelemetry.Trace.TracerProviderBuilder

View File

@ -14,6 +14,9 @@
// limitations under the License.
// </copyright>
using System;
#if !NETFRAMEWORK
using System.Collections.Generic;
#endif
using OpenTelemetry.Instrumentation.SqlClient.Implementation;
namespace OpenTelemetry.Instrumentation.SqlClient
@ -28,6 +31,19 @@ namespace OpenTelemetry.Instrumentation.SqlClient
#if NETFRAMEWORK
private readonly SqlEventSourceListener sqlEventSourceListener;
#else
private static readonly HashSet<string> DiagnosticSourceEvents = new()
{
"System.Data.SqlClient.WriteCommandBefore",
"Microsoft.Data.SqlClient.WriteCommandBefore",
"System.Data.SqlClient.WriteCommandAfter",
"Microsoft.Data.SqlClient.WriteCommandAfter",
"System.Data.SqlClient.WriteCommandError",
"Microsoft.Data.SqlClient.WriteCommandError",
};
private readonly Func<string, object, object, bool> isEnabled = (eventName, _, _)
=> DiagnosticSourceEvents.Contains(eventName);
private readonly DiagnosticSourceSubscriber diagnosticSourceSubscriber;
#endif
@ -35,7 +51,8 @@ namespace OpenTelemetry.Instrumentation.SqlClient
/// Initializes a new instance of the <see cref="SqlClientInstrumentation"/> class.
/// </summary>
/// <param name="options">Configuration options for sql instrumentation.</param>
public SqlClientInstrumentation(SqlClientInstrumentationOptions options = null)
public SqlClientInstrumentation(
SqlClientInstrumentationOptions options = null)
{
#if NETFRAMEWORK
this.sqlEventSourceListener = new SqlEventSourceListener(options);
@ -43,7 +60,7 @@ namespace OpenTelemetry.Instrumentation.SqlClient
this.diagnosticSourceSubscriber = new DiagnosticSourceSubscriber(
name => new SqlClientDiagnosticListener(name, options),
listener => listener.Name == SqlClientDiagnosticListenerName,
null);
this.isEnabled);
this.diagnosticSourceSubscriber.Subscribe();
#endif
}

View File

@ -470,7 +470,9 @@ namespace OpenTelemetry.Instrumentation.SqlClient.Tests
}
#if !NETFRAMEWORK
private Activity[] RunCommandWithFilter(Action<SqlCommand> sqlCommandSetup, Func<object, bool> filter)
private Activity[] RunCommandWithFilter(
Action<SqlCommand> sqlCommandSetup,
Func<object, bool> filter)
{
using var sqlConnection = new SqlConnection(TestConnectionString);
using var sqlCommand = sqlConnection.CreateCommand();
@ -526,7 +528,10 @@ namespace OpenTelemetry.Instrumentation.SqlClient.Tests
public void Write(string name, object value)
{
this.listener.Write(name, value);
if (this.listener.IsEnabled(name))
{
this.listener.Write(name, value);
}
}
public void Dispose()