Skip to content

Commit

Permalink
[Tracer] Don't inject DBM data into stored procedures (#4466)
Browse files Browse the repository at this point in the history
* Don't inject into stored procedures

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

Co-authored-by: Pierre Bonet <pierre.bonet@datadoghq.com>

* Building locally to update trimming file

---------

Co-authored-by: Pierre Bonet <pierre.bonet@datadoghq.com>
Co-authored-by: Maximo Bautista <max.bautista97@gmail.com>
  • Loading branch information
3 people authored Jul 28, 2023
1 parent 03634d6 commit 8bf7761
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@
</assembly>
<assembly fullname="System.Data" />
<assembly fullname="System.Data.Common">
<type fullname="System.Data.CommandType" />
<type fullname="System.Data.Common.DbCommand" />
<type fullname="System.Data.Common.DbConnectionStringBuilder" />
<type fullname="System.Data.DataColumn" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ private static Scope CreateDbCommandScope(Tracer tracer, IDbCommand command, Int
IastModule.OnSqlQuery(commandText, integrationId);
}

if (tracer.Settings.DbmPropagationMode != DbmPropagationLevel.Disabled)
if (tracer.Settings.DbmPropagationMode != DbmPropagationLevel.Disabled
&& command.CommandType != CommandType.StoredProcedure)
{
var propagatedCommand = DatabaseMonitoringPropagator.PropagateSpanData(tracer.Settings.DbmPropagationMode, tracer.DefaultServiceName, scope.Span.Context, integrationId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Linq;
using Datadog.Trace.ClrProfiler.AutoInstrumentation.AdoNet;
using Datadog.Trace.Configuration;
using Datadog.Trace.Configuration.Telemetry;
using Datadog.Trace.TestHelpers;
using FluentAssertions;
using Xunit;
using DbType = Datadog.Trace.ClrProfiler.AutoInstrumentation.AdoNet.DbType;

Expand All @@ -29,6 +32,26 @@ public static IEnumerable<object[]> GetDbCommands()
yield return new object[] { new Oracle.DataAccess.Client.OracleCommand(), nameof(IntegrationId.Oracle), DbType.Oracle };
}

public static TheoryData<IDbCommand> GetDbmCommandsWithText()
=> new()
{
new System.Data.SqlClient.SqlCommand { CommandText = "SELECT 1" },
new Microsoft.Data.SqlClient.SqlCommand { CommandText = "SELECT 1" },
new MySql.Data.MySqlClient.MySqlCommand { CommandText = "SELECT 1" },
new MySqlConnector.MySqlCommand { CommandText = "SELECT 1" },
new Npgsql.NpgsqlCommand { CommandText = "SELECT 1" },
// We don't support SqlLite or Oracle in DBM
// new Microsoft.Data.Sqlite.SqliteCommand { CommandText = "SELECT 1" },
// new System.Data.SQLite.SQLiteCommand { CommandText = "SELECT 1" },
// new Oracle.ManagedDataAccess.Client.OracleCommand { CommandText = "SELECT 1" },
// new Oracle.DataAccess.Client.OracleCommand { CommandText = "SELECT 1" },
};

public static IEnumerable<object[]> GetEnabledDbmData()
=> from command in GetDbmCommandsWithText()
from dbm in new[] { "service", "full" }
select new[] { command[0], dbm };

[Theory]
[MemberData(nameof(GetDbCommands))]
public void CreateDbCommandScope_ReturnsScopeForEnabledIntegration(IDbCommand command, string integrationName, string dbType)
Expand Down Expand Up @@ -109,6 +132,71 @@ public void CreateDbCommandScope_IgnoresReplacementServiceNameWhenNotProvided(ID
Assert.NotEqual("my-custom-type", scope.Span.ServiceName);
}

[Theory]
[MemberData(nameof(GetEnabledDbmData))]
public void CreateDbCommandScope_InjectsDbmWhenEnabled(IDbCommand command, string dbmMode)
{
var previousCommandData = command.CommandText;

var collection = new NameValueCollection
{
{ ConfigurationKeys.DbmPropagationMode, dbmMode }
};
IConfigurationSource source = new NameValueConfigurationSource(collection);
var tracerSettings = new TracerSettings(source, NullConfigurationTelemetry.Instance);
var tracer = TracerHelper.Create(tracerSettings);

using var scope = CreateDbCommandScope(tracer, command);
scope.Should().NotBeNull();

// Should have injected the data
command.CommandText.Should().NotBe(previousCommandData);
command.CommandText.Should().Contain(previousCommandData);
}

[Theory]
[MemberData(nameof(GetEnabledDbmData))]
public void CreateDbCommandScope_DoesNotInjectDbmIntoStoredProcedures(IDbCommand command, string dbmMode)
{
var previousCommandData = command.CommandText;
command.CommandType = CommandType.StoredProcedure;

var collection = new NameValueCollection
{
{ ConfigurationKeys.DbmPropagationMode, dbmMode }
};
IConfigurationSource source = new NameValueConfigurationSource(collection);
var tracerSettings = new TracerSettings(source, NullConfigurationTelemetry.Instance);
var tracer = TracerHelper.Create(tracerSettings);

using var scope = CreateDbCommandScope(tracer, command);
scope.Should().NotBeNull();

// Should not have injected the data
command.CommandText.Should().Be(previousCommandData);
}

[Theory]
[MemberData(nameof(GetDbmCommandsWithText))]
public void CreateDbCommandScope_DoesNotInjectDbmWhenDisabled(IDbCommand command)
{
var previousCommandData = command.CommandText;

var collection = new NameValueCollection
{
{ ConfigurationKeys.DbmPropagationMode, "disabled" }
};
IConfigurationSource source = new NameValueConfigurationSource(collection);
var tracerSettings = new TracerSettings(source, NullConfigurationTelemetry.Instance);
var tracer = TracerHelper.Create(tracerSettings);

using var scope = CreateDbCommandScope(tracer, command);
scope.Should().NotBeNull();

// Should not have injected the data
command.CommandText.Should().Be(previousCommandData);
}

[Theory]
[MemberData(nameof(GetDbCommands))]
internal void TryGetIntegrationDetails_CorrectNameGenerated(IDbCommand command, string expectedIntegrationName, string expectedDbType)
Expand Down

0 comments on commit 8bf7761

Please sign in to comment.