forked from mtusi2026/mtusi2026lab6
29 lines
780 B
Go
29 lines
780 B
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"go.opentelemetry.io/otel"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/codes"
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
type queryTracer struct{}
|
|
|
|
func (queryTracer) TraceQueryStart(ctx context.Context, _ *pgx.Conn, _ pgx.TraceQueryStartData) context.Context {
|
|
ctx, _ = otel.Tracer("postgres").Start(ctx, "postgres query", trace.WithSpanKind(trace.SpanKindClient),
|
|
trace.WithAttributes(attribute.String("db.system", "postgresql")))
|
|
return ctx
|
|
}
|
|
|
|
func (queryTracer) TraceQueryEnd(ctx context.Context, _ *pgx.Conn, data pgx.TraceQueryEndData) {
|
|
span := trace.SpanFromContext(ctx)
|
|
if data.Err != nil {
|
|
span.RecordError(data.Err)
|
|
span.SetStatus(codes.Error, "query failed")
|
|
}
|
|
span.End()
|
|
}
|