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

Add ability to disable additional ADO.NET Command Types #6042

Merged
merged 6 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Don't use the static Tracer.Instance
  • Loading branch information
bouwkast committed Oct 7, 2024
commit 7cbe494043da30b0b32d8c28976cf32a58efc2c1
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ static bool HasDbType(Span span, string dbType)

public static bool TryGetIntegrationDetails(
string? commandTypeFullName,
Tracer? tracer,
[NotNullWhen(true)] out IntegrationId? integrationId,
[NotNullWhen(true)] out string? dbType)
{
Expand Down Expand Up @@ -170,7 +171,7 @@ public static bool TryGetIntegrationDetails(
return true;
default:
string commandTypeName = commandTypeFullName.Substring(commandTypeFullName.LastIndexOf(".", StringComparison.Ordinal) + 1);
if (IsDisabledCommandType(commandTypeName))
if (IsDisabledCommandType(commandTypeName, tracer))
{
integrationId = null;
dbType = null;
Expand Down Expand Up @@ -198,14 +199,19 @@ _ when commandTypeName.EndsWith(commandSuffix) =>
}
}

internal static bool IsDisabledCommandType(string commandTypeName)
internal static bool IsDisabledCommandType(string commandTypeName, Tracer? tracer)
{
if (tracer is null)
{
return false;
}

if (string.IsNullOrEmpty(commandTypeName))
{
return false;
}

var disabledTypes = Tracer.Instance.Settings.DisabledAdoNetCommandTypes;
var disabledTypes = tracer.Settings.DisabledAdoNetCommandTypes;

if (disabledTypes is null || disabledTypes.Count == 0)
{
Expand Down Expand Up @@ -243,7 +249,7 @@ static Cache()
{
CommandType = typeof(TCommand);

if (TryGetIntegrationDetails(CommandType.FullName, out var integrationId, out var dbTypeName))
if (TryGetIntegrationDetails(CommandType.FullName, tracer: Tracer.Instance, out var integrationId, out var dbTypeName))
{
// cache values for this TCommand type
DbTypeName = dbTypeName;
Expand Down Expand Up @@ -273,7 +279,7 @@ static Cache()

// if command.GetType() != typeof(TCommand), we are probably instrumenting a method
// defined in a base class like DbCommand and we can't use the cached values
if (TryGetIntegrationDetails(commandType.FullName, out var integrationId, out var dbTypeName))
if (TryGetIntegrationDetails(commandType.FullName, tracer, out var integrationId, out var dbTypeName))
{
var operationName = $"{dbTypeName}.query";
var tagsFromConnectionString = GetTagsFromConnectionString(command);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ internal void TryGetIntegrationDetails_CorrectNameGenerated(Type commandType, st
var command = (IDbCommand)Activator.CreateInstance(commandType)!;
command.CommandText = DbmCommandText;

bool result = DbScopeFactory.TryGetIntegrationDetails(command.GetType().FullName, out var actualIntegrationId, out var actualDbType);
bool result = DbScopeFactory.TryGetIntegrationDetails(command.GetType().FullName, tracer: Tracer.Instance, out var actualIntegrationId, out var actualDbType);
Assert.True(result);
Assert.Equal(expectedIntegrationName, actualIntegrationId.ToString());
Assert.Equal(expectedDbType, actualDbType);
Expand All @@ -262,12 +262,12 @@ internal void TryGetIntegrationDetails_CorrectNameGenerated(Type commandType, st
[Fact]
internal void TryGetIntegrationDetails_FailsForKnownCommandType()
{
bool result = DbScopeFactory.TryGetIntegrationDetails("InterceptableDbCommand", out var actualIntegrationId, out var actualDbType);
bool result = DbScopeFactory.TryGetIntegrationDetails("InterceptableDbCommand", tracer: Tracer.Instance, out var actualIntegrationId, out var actualDbType);
Assert.False(result);
Assert.False(actualIntegrationId.HasValue);
Assert.Null(actualDbType);

bool result2 = DbScopeFactory.TryGetIntegrationDetails("ProfiledDbCommand", out var actualIntegrationId2, out var actualDbType2);
bool result2 = DbScopeFactory.TryGetIntegrationDetails("ProfiledDbCommand", tracer: Tracer.Instance, out var actualIntegrationId2, out var actualDbType2);
Assert.False(result2);
Assert.False(actualIntegrationId2.HasValue);
Assert.Null(actualDbType2);
Expand All @@ -277,17 +277,17 @@ internal void TryGetIntegrationDetails_FailsForKnownCommandType()
internal void TryGetIntegrationDetails_FailsForKnownCommandTypes_AndUserDefined()
{
Tracer.Configure(TracerSettings.Create(new Dictionary<string, object> { { ConfigurationKeys.DisabledAdoNetCommandTypes, "SomeFakeDbCommand" } }));
bool result = DbScopeFactory.TryGetIntegrationDetails("InterceptableDbCommand", out var actualIntegrationId, out var actualDbType);
bool result = DbScopeFactory.TryGetIntegrationDetails("InterceptableDbCommand", tracer: Tracer.Instance, out var actualIntegrationId, out var actualDbType);
Assert.False(result);
Assert.False(actualIntegrationId.HasValue);
Assert.Null(actualDbType);

bool result2 = DbScopeFactory.TryGetIntegrationDetails("ProfiledDbCommand", out var actualIntegrationId2, out var actualDbType2);
bool result2 = DbScopeFactory.TryGetIntegrationDetails("ProfiledDbCommand", tracer: Tracer.Instance, out var actualIntegrationId2, out var actualDbType2);
Assert.False(result2);
Assert.False(actualIntegrationId2.HasValue);
Assert.Null(actualDbType2);

bool result3 = DbScopeFactory.TryGetIntegrationDetails("SomeFakeDbCommand", out var actualIntegrationId3, out var actualDbType3);
bool result3 = DbScopeFactory.TryGetIntegrationDetails("SomeFakeDbCommand", tracer: Tracer.Instance, out var actualIntegrationId3, out var actualDbType3);
Assert.False(result3);
Assert.False(actualIntegrationId3.HasValue);
Assert.Null(actualDbType3);
Expand All @@ -304,7 +304,7 @@ internal void TryGetIntegrationDetails_FailsForKnownCommandTypes_AndUserDefined(
[InlineData("Custom.DB.Command", "AdoNet", "db")]
internal void TryGetIntegrationDetails_CustomCommandType(string commandTypeFullName, string integrationId, string expectedDbType)
{
DbScopeFactory.TryGetIntegrationDetails(commandTypeFullName, out var actualIntegrationId, out var actualDbType);
DbScopeFactory.TryGetIntegrationDetails(commandTypeFullName, tracer: Tracer.Instance, out var actualIntegrationId, out var actualDbType);
Assert.Equal(integrationId, actualIntegrationId?.ToString());
Assert.Equal(expectedDbType, actualDbType);
}
Expand Down