Skip to content

Commit 37a6fd8

Browse files
committed
Make TracerSettings dependency explicit
1 parent 85a6731 commit 37a6fd8

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

tracer/src/Datadog.Trace/ClrProfiler/AutoInstrumentation/AdoNet/DbScopeFactory.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ static bool HasDbType(Span span, string dbType)
138138
}
139139

140140
public static bool TryGetIntegrationDetails(
141+
HashSet<string> disabledAdoNetCommandTypes,
141142
string? commandTypeFullName,
142143
[NotNullWhen(true)] out IntegrationId? integrationId,
143144
[NotNullWhen(true)] out string? dbType)
@@ -172,7 +173,7 @@ public static bool TryGetIntegrationDetails(
172173
return true;
173174
default:
174175
string commandTypeName = commandTypeFullName.Substring(commandTypeFullName.LastIndexOf(".", StringComparison.Ordinal) + 1);
175-
if (IsDisabledCommandType(commandTypeName))
176+
if (IsDisabledCommandType(commandTypeName, disabledAdoNetCommandTypes))
176177
{
177178
integrationId = null;
178179
dbType = null;
@@ -200,14 +201,14 @@ _ when commandTypeName.EndsWith(commandSuffix) =>
200201
}
201202
}
202203

203-
internal static bool IsDisabledCommandType(string commandTypeName)
204+
internal static bool IsDisabledCommandType(string commandTypeName, HashSet<string> disabledAdoNetCommandTypes)
204205
{
205206
if (string.IsNullOrEmpty(commandTypeName))
206207
{
207208
return false;
208209
}
209210

210-
var disabledTypes = Tracer.Instance.Settings.DisabledAdoNetCommandTypes;
211+
var disabledTypes = disabledAdoNetCommandTypes;
211212

212213
if (disabledTypes is null || disabledTypes.Count == 0)
213214
{
@@ -245,7 +246,7 @@ static Cache()
245246
{
246247
CommandType = typeof(TCommand);
247248

248-
if (TryGetIntegrationDetails(CommandType.FullName, out var integrationId, out var dbTypeName))
249+
if (TryGetIntegrationDetails(Tracer.Instance.Settings.DisabledAdoNetCommandTypes, CommandType.FullName, out var integrationId, out var dbTypeName))
249250
{
250251
// cache values for this TCommand type
251252
DbTypeName = dbTypeName;
@@ -275,7 +276,7 @@ static Cache()
275276

276277
// if command.GetType() != typeof(TCommand), we are probably instrumenting a method
277278
// defined in a base class like DbCommand and we can't use the cached values
278-
if (TryGetIntegrationDetails(commandType.FullName, out var integrationId, out var dbTypeName))
279+
if (TryGetIntegrationDetails(tracer.Settings.DisabledAdoNetCommandTypes, commandType.FullName, out var integrationId, out var dbTypeName))
279280
{
280281
var operationName = $"{dbTypeName}.query";
281282
var tagsFromConnectionString = GetTagsFromConnectionString(command);

tracer/test/Datadog.Trace.ClrProfiler.Managed.Tests/DbScopeFactoryTests.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ internal void TryGetIntegrationDetails_CorrectNameGenerated(Type commandType, st
689689
var command = (IDbCommand)Activator.CreateInstance(commandType)!;
690690
command.CommandText = DbmCommandText;
691691

692-
bool result = DbScopeFactory.TryGetIntegrationDetails(command.GetType().FullName, out var actualIntegrationId, out var actualDbType);
692+
bool result = DbScopeFactory.TryGetIntegrationDetails([], command.GetType().FullName, out var actualIntegrationId, out var actualDbType);
693693
Assert.True(result);
694694
Assert.Equal(expectedIntegrationName, actualIntegrationId.ToString());
695695
Assert.Equal(expectedDbType, actualDbType);
@@ -698,12 +698,13 @@ internal void TryGetIntegrationDetails_CorrectNameGenerated(Type commandType, st
698698
[Fact]
699699
internal void TryGetIntegrationDetails_FailsForKnownCommandType()
700700
{
701-
bool result = DbScopeFactory.TryGetIntegrationDetails("InterceptableDbCommand", out var actualIntegrationId, out var actualDbType);
701+
var defaultDisabledCommands = new TracerSettings().DisabledAdoNetCommandTypes;
702+
bool result = DbScopeFactory.TryGetIntegrationDetails(defaultDisabledCommands, "InterceptableDbCommand", out var actualIntegrationId, out var actualDbType);
702703
Assert.False(result);
703704
Assert.False(actualIntegrationId.HasValue);
704705
Assert.Null(actualDbType);
705706

706-
bool result2 = DbScopeFactory.TryGetIntegrationDetails("ProfiledDbCommand", out var actualIntegrationId2, out var actualDbType2);
707+
bool result2 = DbScopeFactory.TryGetIntegrationDetails(defaultDisabledCommands, "ProfiledDbCommand", out var actualIntegrationId2, out var actualDbType2);
707708
Assert.False(result2);
708709
Assert.False(actualIntegrationId2.HasValue);
709710
Assert.Null(actualDbType2);
@@ -712,18 +713,18 @@ internal void TryGetIntegrationDetails_FailsForKnownCommandType()
712713
[Fact]
713714
internal void TryGetIntegrationDetails_FailsForKnownCommandTypes_AndUserDefined()
714715
{
715-
Tracer.Configure(TracerSettings.Create(new Dictionary<string, object> { { ConfigurationKeys.DisabledAdoNetCommandTypes, "SomeFakeDbCommand" } }));
716-
bool result = DbScopeFactory.TryGetIntegrationDetails("InterceptableDbCommand", out var actualIntegrationId, out var actualDbType);
716+
var disabledCommandTypes = TracerSettings.Create(new() { { ConfigurationKeys.DisabledAdoNetCommandTypes, "SomeFakeDbCommand" } }).DisabledAdoNetCommandTypes;
717+
bool result = DbScopeFactory.TryGetIntegrationDetails(disabledCommandTypes, "InterceptableDbCommand", out var actualIntegrationId, out var actualDbType);
717718
Assert.False(result);
718719
Assert.False(actualIntegrationId.HasValue);
719720
Assert.Null(actualDbType);
720721

721-
bool result2 = DbScopeFactory.TryGetIntegrationDetails("ProfiledDbCommand", out var actualIntegrationId2, out var actualDbType2);
722+
bool result2 = DbScopeFactory.TryGetIntegrationDetails(disabledCommandTypes, "ProfiledDbCommand", out var actualIntegrationId2, out var actualDbType2);
722723
Assert.False(result2);
723724
Assert.False(actualIntegrationId2.HasValue);
724725
Assert.Null(actualDbType2);
725726

726-
bool result3 = DbScopeFactory.TryGetIntegrationDetails("SomeFakeDbCommand", out var actualIntegrationId3, out var actualDbType3);
727+
bool result3 = DbScopeFactory.TryGetIntegrationDetails(disabledCommandTypes, "SomeFakeDbCommand", out var actualIntegrationId3, out var actualDbType3);
727728
Assert.False(result3);
728729
Assert.False(actualIntegrationId3.HasValue);
729730
Assert.Null(actualDbType3);
@@ -740,7 +741,8 @@ internal void TryGetIntegrationDetails_FailsForKnownCommandTypes_AndUserDefined(
740741
[InlineData("Custom.DB.Command", "AdoNet", "db")]
741742
internal void TryGetIntegrationDetails_CustomCommandType(string commandTypeFullName, string integrationId, string expectedDbType)
742743
{
743-
DbScopeFactory.TryGetIntegrationDetails(commandTypeFullName, out var actualIntegrationId, out var actualDbType);
744+
var defaultDisabledCommands = new TracerSettings().DisabledAdoNetCommandTypes;
745+
DbScopeFactory.TryGetIntegrationDetails(defaultDisabledCommands, commandTypeFullName, out var actualIntegrationId, out var actualDbType);
744746
Assert.Equal(integrationId, actualIntegrationId?.ToString());
745747
Assert.Equal(expectedDbType, actualDbType);
746748
}

0 commit comments

Comments
 (0)