Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Activity Filter capability for OpenTelemetry.Instrumentation.SqlClient #3342

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.EnableCo
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.EnableConnectionLevelAttributes.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.Enrich.get -> System.Action<System.Diagnostics.Activity, string, object>
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.Enrich.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.Filter.get -> System.Func<System.Diagnostics.Activity, string, object, bool>
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.Filter.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.SetDbStatement.get -> bool
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.SetDbStatement.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.SqlClientInstrumentationOptions() -> void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.EnableCo
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.EnableConnectionLevelAttributes.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.Enrich.get -> System.Action<System.Diagnostics.Activity, string, object>
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.Enrich.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.Filter.get -> System.Func<System.Diagnostics.Activity, string, object, bool>
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.Filter.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.RecordException.get -> bool
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.RecordException.set -> void
OpenTelemetry.Instrumentation.SqlClient.SqlClientInstrumentationOptions.SetDbStatementForStoredProcedure.get -> bool
Expand All @@ -11,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
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ public override void OnCustom(string name, Activity activity, object payload)

if (activity.IsAllDataRequested)
{
try
{
if (this.options.EventFilter(activity, "OnCustom", command) == false)
{
SqlClientInstrumentationEventSource.Log.FilterException(activity.OperationName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no exception here?

activity.IsAllDataRequested = false;
activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded;
return;
}
}
catch (Exception ex)
{
SqlClientInstrumentationEventSource.Log.FilterException(ex);
activity.IsAllDataRequested = false;
activity.ActivityTraceFlags &= ~ActivityTraceFlags.Recorded;
return;
}

_ = this.connectionFetcher.TryFetch(command, out var connection);
_ = this.databaseFetcher.TryFetch(connection, out var database);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,5 +75,20 @@ public void EnrichmentException(string exception)
{
this.WriteEvent(5, exception);
}

[NonEvent]
public void FilterException(Exception ex)
{
if (this.IsEnabled(EventLevel.Error, EventKeywords.All))
{
this.FilterException(ex.ToInvariantString());
}
}

[Event(6, Message = "Filter threw exception. Exception {0}.", Level = EventLevel.Error)]
public void FilterException(string exception)
{
this.WriteEvent(6, exception);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
using System.Collections.Concurrent;
using System.Data;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using OpenTelemetry.Instrumentation.SqlClient.Implementation;
using OpenTelemetry.Trace;

namespace OpenTelemetry.Instrumentation.SqlClient
Expand Down Expand Up @@ -129,6 +131,14 @@ public class SqlClientInstrumentationOptions
/// </example>
public Action<Activity, string, object> Enrich { get; set; }

/// <summary>
/// Gets or sets a Filter function that determines whether or not to collect telemetry about requests on a per request basis.
/// The Filter gets the Activity, EventName and the Command, and should return a boolean.
/// If Filter returns true, the request is collected.
/// If Filter returns false or throw exception, the request is filtered out.
/// </summary>
public Func<Activity, string, object, bool> Filter { get; set; }

#if !NETFRAMEWORK
/// <summary>
/// Gets or sets a value indicating whether the exception will be recorded as ActivityEvent or not. Default value: False.
Expand Down Expand Up @@ -258,6 +268,22 @@ internal void AddConnectionLevelDetailsToActivity(string dataSource, Activity sq
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal bool EventFilter(Activity activity, string eventName, object arg1)
{
try
{
return
this.Filter == null ||
this.Filter(activity, eventName, arg1);
}
catch (Exception ex)
{
SqlClientInstrumentationEventSource.Log.FilterException(ex);
return false;
}
}

internal class SqlConnectionDetails
{
public string ServerHostName { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,61 @@ public void SqlClientCreatesActivityWithDbSystem(

VerifySamplingParameters(sampler.LatestSamplingParameters);
}

[Fact]
public void SqlClientShouldNotCollectWhenInstrumentationFilterApplied()
{
using var sqlConnection = new SqlConnection(TestConnectionString);
using var sqlCommand = sqlConnection.CreateCommand();

var activities = new List<Activity>();
using (Sdk.CreateTracerProviderBuilder()
.AddSqlClientInstrumentation(
(opt) =>
{
opt.Filter = (activity, eventName, rawObject) =>
{
if (rawObject is SqlCommand command)
{
return !(command.CommandType == CommandType.StoredProcedure);
}

return true;
};
})
.AddInMemoryExporter(activities)
.Build())
{
var operationId = Guid.NewGuid();
sqlCommand.CommandType = CommandType.StoredProcedure;
sqlCommand.CommandText = "SP_GetOrders";

var beforeExecuteEventData = new
{
OperationId = operationId,
Command = sqlCommand,
Timestamp = (long?)1000000L,
};

this.fakeSqlClientDiagnosticSource.Write(
SqlClientDiagnosticListener.SqlDataBeforeExecuteCommand,
beforeExecuteEventData);

var afterExecuteEventData = new
{
OperationId = operationId,
Command = sqlCommand,
Timestamp = 2000000L,
};

this.fakeSqlClientDiagnosticSource.Write(
SqlClientDiagnosticListener.SqlMicrosoftAfterExecuteCommand,
afterExecuteEventData);
}

Assert.Empty(activities);
}

#endif

private static void VerifyActivityData(
Expand Down