1

I'd like to use OpenTelemetry.Instrumentation.SqlClient to collect traces about database operations in ASP.NET Core Web Server. But when I run the code, no trace from Instrumentation.SqlClient is printed on console, only traces from Instrumentation.AspNetCore are shown.

My Program.cs:

using Dapper;
using MySqlConnector;
using OpenTelemetry.Trace;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddOpenTelemetryTracing(tracerProviderBuilder =>
{
    tracerProviderBuilder
        .AddConsoleExporter()
        .AddAspNetCoreInstrumentation()
        .AddSqlClientInstrumentation();
});

var app = builder.Build();

app.MapGet("/get", async (string userName) =>
{
    using var conn = new MySqlConnection("server=localhost;user=root;password=password;port=3306;database=account;");
    await conn.OpenAsync();
    var userId = await conn.QuerySingleOrDefaultAsync<int>(
        "SELECT UserId FROM Users WHERE UserName = @userName;",
        new {userName});
    return userId;
});

app.Run();

Console ouput when I connect to localhost/get?userName=username:

Activity.TraceId:          238ee4b50fa74a119a090d9397ba9e2f
Activity.SpanId:           b1e79de3c59d471a
Activity.TraceFlags:           Recorded
Activity.ActivitySourceName: OpenTelemetry.Instrumentation.AspNetCore
Activity.DisplayName: /get
Activity.Kind:        Server
Activity.StartTime:   2022-06-27T06:49:53.6323013Z
Activity.Duration:    00:00:00.4652234
Activity.Tags:
    http.host: localhost:5265
    http.method: GET
    http.target: /get
    http.url: http://localhost:5265/get?userName=testuser0001
    http.user_agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.0.0 Safari/537.36
    http.status_code: 200
   StatusCode : UNSET
Resource associated with Activity:
    service.name: unknown_service:WebApplication1

Expected Output

About OpenTelemetry SqlClient Instrumentation

2 Answers 2

4

AddSqlClientInstrumentation is for instrumenting a "SQL Server" client. I.e. Microsoft.Data.SqlClient and System.Data.SqlClient (source).

However, you use MySql (hence your use of MySqlConnection instead of SqlConnection).

You'll have to research if there is any .NET OpenTelemetry integration for MySql (clients) available.

Sign up to request clarification or add additional context in comments.

Comments

0

Also, you can try OpenTelemetry.Instrumentation.EntityFrameworkCore

https://github.com/open-telemetry/opentelemetry-dotnet-contrib/tree/main/src/OpenTelemetry.Instrumentation.EntityFrameworkCore

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.