Instrument durable messenger with OTel, provision Grafana and prove SLO incident detection
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package telemetry
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"go.opentelemetry.io/otel/trace"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type DBTracer struct{}
|
||||
|
||||
func (DBTracer) TraceQueryStart(ctx context.Context, _ *pgx.Conn, d pgx.TraceQueryStartData) context.Context {
|
||||
fields := strings.Fields(d.SQL)
|
||||
op := "query"
|
||||
if len(fields) > 0 {
|
||||
switch strings.ToUpper(fields[0]) {
|
||||
case "SELECT", "INSERT", "UPDATE", "DELETE", "BEGIN", "COMMIT", "ROLLBACK":
|
||||
op = strings.ToUpper(fields[0])
|
||||
}
|
||||
}
|
||||
ctx, _ = Start(ctx, "db."+op, trace.SpanKindClient)
|
||||
return ctx
|
||||
}
|
||||
func (DBTracer) TraceQueryEnd(ctx context.Context, _ *pgx.Conn, d pgx.TraceQueryEndData) {
|
||||
s := trace.SpanFromContext(ctx)
|
||||
Failure(s, d.Err)
|
||||
s.End()
|
||||
}
|
||||
func (DBTracer) TraceAcquireStart(ctx context.Context, _ *pgxpool.Pool, _ pgxpool.TraceAcquireStartData) context.Context {
|
||||
PendingAcquires.Add(1)
|
||||
ctx, _ = Start(ctx, "db.pool.acquire", trace.SpanKindInternal)
|
||||
return ctx
|
||||
}
|
||||
func (DBTracer) TraceAcquireEnd(ctx context.Context, _ *pgxpool.Pool, d pgxpool.TraceAcquireEndData) {
|
||||
PendingAcquires.Add(-1)
|
||||
s := trace.SpanFromContext(ctx)
|
||||
Failure(s, d.Err)
|
||||
s.End()
|
||||
}
|
||||
Reference in New Issue
Block a user