Skip to content

Commit

Permalink
Support for OTEL_DOTNET_AUTO_ORACLEMDA_SET_DBSTATEMENT_FOR_TEXT
Browse files Browse the repository at this point in the history
  • Loading branch information
Kielek committed Mar 27, 2024
1 parent 13cd01a commit 9746fa3
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ This component adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.h
- Experimental ARM64 support for the `OpenTelemetry.AutoInstrumentation` NuGet package.
- Support for [Oracle.ManagedDataAccess.Core](https://www.nuget.org/packages/Oracle.ManagedDataAccess.Core)
and [Oracle.ManagedDataAccess](https://www.nuget.org/packages/Oracle.ManagedDataAccess)
traces instrumentation from 23.3.2-dev.
traces instrumentation from 23.3.2-dev together with support for
`OTEL_DOTNET_AUTO_ORACLEMDA_SET_DBSTATEMENT_FOR_TEXT` environment variable.

### Changed

Expand Down
1 change: 1 addition & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ the `ASPNETCORE_HOSTINGSTARTUPASSEMBLIES` environment variable to
|-----------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------|-----------------------------------------------------------------------------------------------------------------------------------|
| `OTEL_DOTNET_AUTO_ENTITYFRAMEWORKCORE_SET_DBSTATEMENT_FOR_TEXT` | Whether the Entity Framework Core instrumentation can pass SQL statements through the `db.statement` attribute. Queries might contain sensitive information. If set to `false`, `db.statement` is recorded only for executing stored procedures. | `false` | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
| `OTEL_DOTNET_AUTO_GRAPHQL_SET_DOCUMENT` | Whether the GraphQL instrumentation can pass raw queries through the `graphql.document` attribute. Queries might contain sensitive information. | `false` | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
| `OTEL_DOTNET_AUTO_ORACLEMDA_SET_DBSTATEMENT_FOR_TEXT` | Whether the Oracle Client instrumentation can pass SQL statements through the `db.statement` attribute. Queries might contain sensitive information. If set to `false`, `db.statement` is recorded only for executing stored procedures. | `false` | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |
| `OTEL_DOTNET_AUTO_SQLCLIENT_SET_DBSTATEMENT_FOR_TEXT` | Whether the SQL Client instrumentation can pass SQL statements through the `db.statement` attribute. Queries might contain sensitive information. If set to `false`, `db.statement` is recorded only for executing stored procedures. **Not supported on .NET Framework for System.Data.SqlClient.** | `false` | [Experimental](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/versioning-and-stability.md) |

## Propagators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ public static class InstrumentationOptions
/// </summary>
public const string GraphQLSetDocument = "OTEL_DOTNET_AUTO_GRAPHQL_SET_DOCUMENT";

/// <summary>
/// Configuration key for Oracle Client instrumentation to enable passing text query as a db.statement attribute.
/// </summary>
public const string OracleMdaSetDbStatementForText = "OTEL_DOTNET_AUTO_ORACLEMDA_SET_DBSTATEMENT_FOR_TEXT";

/// <summary>
/// Configuration key for SQL Client instrumentation to enable passing text query as a db.statement attribute.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public static void AddQuartz(LazyInstrumentationLoader lazyInstrumentationLoader
{
lazyInstrumentationLoader.Add(new QuartzInitializer(pluginManager));
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static void AddOracleMda(LazyInstrumentationLoader lazyInstrumentationLoader, TracerSettings tracerSettings)
{
lazyInstrumentationLoader.Add(new OracleMdaInitializer(tracerSettings));
}
}

internal static class Metrics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ public static TracerProviderBuilder UseEnvironmentVariables(
TracerInstrumentation.MySqlConnector => builder.AddSource("MySqlConnector"),
TracerInstrumentation.Azure => Wrappers.AddAzureInstrumentation(builder),
TracerInstrumentation.WcfClient => AddWcfIfNeeded(builder, pluginManager, lazyInstrumentationLoader, ref wcfInstrumentationAdded),
#if NETFRAMEWORK
TracerInstrumentation.OracleMda => builder.AddSource("Oracle.ManagedDataAccess"),
#else
TracerInstrumentation.OracleMda => builder.AddSource("Oracle.ManagedDataAccess.Core"),
#endif
TracerInstrumentation.OracleMda => Wrappers.AddOracleMdaInstrumentation(builder, lazyInstrumentationLoader, settings),
#if NET6_0_OR_GREATER
TracerInstrumentation.AspNetCore => Wrappers.AddAspNetCoreInstrumentation(builder, pluginManager, lazyInstrumentationLoader),
TracerInstrumentation.MassTransit => builder.AddSource("MassTransit"),
Expand Down Expand Up @@ -207,6 +203,18 @@ public static TracerProviderBuilder AddSqlClientInstrumentation(TracerProviderBu
return builder.AddSource("OpenTelemetry.Instrumentation.SqlClient");
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static TracerProviderBuilder AddOracleMdaInstrumentation(TracerProviderBuilder builder, LazyInstrumentationLoader lazyInstrumentationLoader, TracerSettings tracerSettings)
{
DelayedInitialization.Traces.AddOracleMda(lazyInstrumentationLoader, tracerSettings);

#if NETFRAMEWORK
return builder.AddSource("Oracle.ManagedDataAccess");
#else
return builder.AddSource("Oracle.ManagedDataAccess.Core");
#endif
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static TracerProviderBuilder AddGrpcClientInstrumentation(TracerProviderBuilder builder, PluginManager pluginManager, LazyInstrumentationLoader lazyInstrumentationLoader)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal class InstrumentationOptions
internal InstrumentationOptions(Configuration configuration)
{
GraphQLSetDocument = configuration.GetBool(ConfigurationKeys.Traces.InstrumentationOptions.GraphQLSetDocument) ?? false;
OracleMdaSetDbStatementForText = configuration.GetBool(ConfigurationKeys.Traces.InstrumentationOptions.OracleMdaSetDbStatementForText) ?? false;
SqlClientSetDbStatementForText = configuration.GetBool(ConfigurationKeys.Traces.InstrumentationOptions.SqlClientSetDbStatementForText) ?? false;
#if NET6_0_OR_GREATER
EntityFrameworkCoreSetDbStatementForText = configuration.GetBool(ConfigurationKeys.Traces.InstrumentationOptions.EntityFrameworkCoreSetDbStatementForText) ?? false;
Expand All @@ -22,6 +23,11 @@ internal InstrumentationOptions(Configuration configuration)
/// </summary>
public bool GraphQLSetDocument { get; }

/// <summary>
/// Gets a value indicating whether text query in Oracle Client can be passed as a db.statement tag.
/// </summary>
public bool OracleMdaSetDbStatementForText { get; }

/// <summary>
/// Gets a value indicating whether text query in SQL Client can be passed as a db.statement tag.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Reflection;
using OpenTelemetry.AutoInstrumentation.Configurations;

namespace OpenTelemetry.AutoInstrumentation.Loading.Initializers;

internal class OracleMdaInitializer : InstrumentationInitializer
{
private readonly TracerSettings _tracerSettings;

public OracleMdaInitializer(TracerSettings tracerSettings)
: base("Oracle.ManagedDataAccess")
{
_tracerSettings = tracerSettings;
}

public override void Initialize(ILifespanManager lifespanManager)
{
var oracleCommandType = Type.GetType("Oracle.ManagedDataAccess.Client.OracleCommand, Oracle.ManagedDataAccess");
var oracleActivitySourceType = Type.GetType("Oracle.ManagedDataAccess.OpenTelemetry.OracleActivitySource, Oracle.ManagedDataAccess");

if (oracleCommandType == null || oracleActivitySourceType == null)
{
return;
}

var oracleActivitySourceFieldInfo = oracleCommandType.GetField("OracleActivitySource", BindingFlags.Static | BindingFlags.NonPublic);
var oracleActivitySourceField = oracleActivitySourceFieldInfo?.GetValue(null);

if (oracleActivitySourceField == null)
{
return;
}

var setDbStatementForTextPropertyInfo = oracleActivitySourceType.GetProperty("SetDbStatementForText", BindingFlags.Instance | BindingFlags.NonPublic);

setDbStatementForTextPropertyInfo?.SetValue(oracleActivitySourceField, _tracerSettings.InstrumentationOptions.OracleMdaSetDbStatementForText);
}
}
39 changes: 30 additions & 9 deletions test/IntegrationTests/OracleMdaTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,45 @@ public OracleMdaTests(ITestOutputHelper output, OracleFixture oracle)
_oracle = oracle;
}

public static IEnumerable<object[]> GetData()
{
#if NETFRAMEWORK
foreach (var version in LibraryVersion.OracleMda)
#else
foreach (var version in LibraryVersion.OracleMdaCore)
#endif
{
yield return new[] { version[0], true };
yield return new[] { version[0], false };
}
}

[Theory]
[Trait("Category", "EndToEnd")]
[Trait("Containers", "Linux")]
#if NET6_0_OR_GREATER
[MemberData(nameof(LibraryVersion.OracleMdaCore), MemberType = typeof(LibraryVersion))]
#else
[MemberData(nameof(LibraryVersion.OracleMda), MemberType = typeof(LibraryVersion))]
#endif
public void SubmitsTraces(string packageVersion)
[MemberData(nameof(GetData))]
public void SubmitTraces(string packageVersion, bool dbStatementForText)
{
SetEnvironmentVariable("OTEL_DOTNET_AUTO_ORACLEMDA_SET_DBSTATEMENT_FOR_TEXT", dbStatementForText.ToString());

using var collector = new MockSpansCollector(Output);
SetExporter(collector);
#if NET6_0_OR_GREATER
collector.Expect("Oracle.ManagedDataAccess.Core");

#if NETFRAMEWORK
const string instrumentationScopeName = "Oracle.ManagedDataAccess";
#else
collector.Expect("Oracle.ManagedDataAccess");
const string instrumentationScopeName = "Oracle.ManagedDataAccess.Core";
#endif

if (dbStatementForText)
{
collector.Expect(instrumentationScopeName, span => span.Attributes.Any(attr => attr.Key == "db.statement" && !string.IsNullOrWhiteSpace(attr.Value?.StringValue)));
}
else
{
collector.Expect(instrumentationScopeName, span => span.Attributes.All(attr => attr.Key != "db.statement"));
}

RunTestApplication(new()
{
#if NET462
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ internal void TracerSettings_DefaultValues()

// Instrumentation options tests
settings.InstrumentationOptions.GraphQLSetDocument.Should().BeFalse();
settings.InstrumentationOptions.OracleMdaSetDbStatementForText.Should().BeFalse();
settings.InstrumentationOptions.SqlClientSetDbStatementForText.Should().BeFalse();
#if NET6_0_OR_GREATER
settings.InstrumentationOptions.EntityFrameworkCoreSetDbStatementForText.Should().BeFalse();
Expand Down

0 comments on commit 9746fa3

Please sign in to comment.