What's the expected way to do logging AND tracing? #1677
Replies: 4 comments
-
I think this is probably necessary. |
Beta Was this translation helpful? Give feedback.
-
I want to have a instrumented logger and the otel tracer. Did you write a MultiTracer to support this use case? |
Beta Was this translation helpful? Give feedback.
-
I have the same problem. I was able to set up both loggers and tracers by providing the following implementation in my application. Here is a sample of my implementation using pgx. // https://github.com/jackc/pgx/issues/1061#issuecomment-1205942323
type MultiQueryTracer struct {
Tracers []pgx.QueryTracer
}
func (m *MultiQueryTracer) TraceQueryStart(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryStartData) context.Context {
for _, t := range m.Tracers {
ctx = t.TraceQueryStart(ctx, conn, data)
}
return ctx
}
func (m *MultiQueryTracer) TraceQueryEnd(ctx context.Context, conn *pgx.Conn, data pgx.TraceQueryEndData) {
for _, t := range m.Tracers {
t.TraceQueryEnd(ctx, conn, data)
}
}
func New(ctx context.Context, slogger *slog.Logger) *pgxpool.Pool {
config, err := pgxpool.ParseConfig(dsn)
if err != nil {
panic(err)
}
// FYI: NewLoggerAdapter: https://github.com/mcosta74/pgx-slog
adapterLogger := logger.NewLoggerAdapter(slogger)
m := MultiQueryTracer{
Tracers: []pgx.QueryTracer{
// tracer: https://github.com/exaring/otelpgx
otelpgx.NewTracer(),
// logger
&tracelog.TraceLog{
Logger: adapterLogger,
LogLevel: tracelog.LogLevelTrace,
},
},
}
config.ConnConfig.Tracer = &m
pool, err := pgxpool.NewWithConfig(ctx, config)
if err != nil {
panic(err)
}
if err := pool.Ping(ctx); err != nil {
panic(err)
}
return pool
} |
Beta Was this translation helpful? Give feedback.
-
If anyone's struggling with this, I threw this together based on a comment in this issue and another (which are in the code file). This uses the slog logger interface. https://gist.github.com/zaydek/91f27cdd35c6240701f81415c3ba7c07 |
Beta Was this translation helpful? Give feedback.
-
Hi! I'm in the process of porting from v4 to v5. At our company, we use Datadog for tracing. But Datadog does not have a tracing wrapper for v4 pgx; it does have a way to add tracing to
sql.DB
. A pgx-native tracer seems to be in the works for v5. I prefer to usezerolog
for logging.As a result, in v4 I use a l somewhat complex initialization process:
This accomplishes zerolog for logging and Datadog for tracing and has been in production for a year.
As I look at the
Tracer
interface inv5
, I'm having trouble understanding what the expected method is for accomplishing both logging and tracing? Do I need to implement anMultiTracer
adaptor, which relays all of theTracer
calls to multiple underlying Tracers, one of which is logging, and one of which is my tracing client (Datadog)?Beta Was this translation helpful? Give feedback.
All reactions