npgsql / npgsql

Npgsql is the .NET data provider for PostgreSQL.
http://www.npgsql.org
PostgreSQL License
3.31k stars 819 forks source link

Allow specifying a tag on NpgsqlCommand that gets reported in tracing #5595

Open roji opened 8 months ago

roji commented 8 months ago

To help view and process tracing data, we could allow users to set an arbitrary string tag on NpgsqlCommand, which would get reported when tracing via OpenTelemetry.

Originally raised in https://github.com/dotnet/efcore/issues/33149#issuecomment-1962348729

MrLesk commented 1 month ago

Hi @roji we are trying to integrate Datadog pg_tracing in our services.

Since we are using EF, we can use interceptors to add the trace info as a comment ahead of each query. This works for us but I was wondering if we could have some support in npgsql so that we could do the same at a lower level. For example we are also using Masstransit with npgsql and there we don't have an interceptor option.

vonzshik commented 1 month ago

At the lowest level, theoretically, you can already do that by using Activity.Current.

using var cmd = new NpgsqlCommand(...);
using var reader = cmd.ExecuteReader();
Activity.Current?.SetTag("SomeKey", "SomeValue");
// Handle reader
MrLesk commented 1 month ago

Yeah but I would like to do it from configuration as I don’t always have access to npgsql directly.

something like otel.addnpgsql(options=> { options.addW3cTracingContext = true; }