Skip to content

[bug] SqlClientInstrumentation tracing enrichment doesn't appear to log #2573

Open
@scottrudy

Description

@scottrudy

Component

OpenTelemetry.Instrumentation.SqlClient

Package Version

Package Name Version
Azure.Monitor.OpenTelemetry.AspNetCore 1.2.0
OpenTelemetry.Exporter.OpenTelemetryProtocol 1.11.1
OpenTelemetry.Extensions.Hosting 1.11.1
OpenTelemetry.Instrumentation.AspNetCore 1.11.0
OpenTelemetry.Instrumentation.Http 1.11.0
OpenTelemetry.Instrumentation.Runtime 1.11.0
OpenTelemetry.Instrumentation.SqlClient 1.11.0-beta.1

Runtime Version

net9.0

Description

I have an Azure Function application that executes stored procedures on Azure SQL Server. I am trying to log the parameters passed into the store procedure. I am able to see the instrumentation enrichment execute when SQL Server is used, but I don't see any of the information anywhere in the traces or logs. The Span shows only SpanId, Name, Kind, db.name, db.statement, db.system and peer.service.

Steps to Reproduce

I have the following extension methods applied to an Azure Function and I've tried running it locally with an Aspire host and in Azure:

public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder {
    builder.Logging.AddOpenTelemetry(logging => {
        logging.IncludeFormattedMessage = true;
        logging.IncludeScopes = true;
    });

    builder.Services.AddOpenTelemetry()
        .WithMetrics(metrics =>  {
            metrics.AddAspNetCoreInstrumentation()
                .AddHttpClientInstrumentation()
                .AddRuntimeInstrumentation();
        })
        .WithTracing(tracing => {
            tracing.AddSource(builder.Environment.ApplicationName)
                .AddAspNetCoreInstrumentation()
                .AddHttpClientInstrumentation()
                // This doesn't appear to work
                .AddSqlClientInstrumentation(options => {
                    options.RecordException = true;
                    options.Enrich = (activity, eventName, rawObject) => {
                        if (eventName == "OnCustom" && rawObject is IDbCommand command) {
                            foreach (IDbDataParameter param in command.Parameters) {
                                activity.SetTag($"db.statement.parameter.{param.ParameterName}", param.Value?.ToString() ?? "null");
                            }
                        }
                    };
                });
        });

    builder.AddOpenTelemetryExporters();

    return builder;
}

private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder {
    var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]);
    var useAzureMonitor = !string.IsNullOrWhiteSpace(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]);

    if (useOtlpExporter) builder.Services.AddOpenTelemetry().UseOtlpExporter();
    if (useAzureMonitor) builder.Services.AddOpenTelemetry().UseAzureMonitor();
    return builder;
}

Expected Result

I expected to see a list of the parameters passed into the stored procedure with their name and values (e.g. db.statement.parameter.username: scottrudy

Actual Result

The Span for the trace shows only SpanId, Name, Kind, db.name, db.statement, db.system and peer.service.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingcomp:instrumentation.sqlclientThings related to OpenTelemetry.Instrumentation.SqlClient

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions