forked from mtusi2026/mtusi2026lab6
41 lines
1.4 KiB
Go
41 lines
1.4 KiB
Go
package telemetry
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
type queryEndKey struct{}
|
|
|
|
type acquireEndKey struct{}
|
|
|
|
// TraceQueryStart creates a SQL client span without recording SQL text or arguments.
|
|
func (r *Runtime) TraceQueryStart(ctx context.Context, _ *pgx.Conn, _ pgx.TraceQueryStartData) context.Context {
|
|
ctx, end := r.Start(ctx, "postgres.query", "client")
|
|
return context.WithValue(ctx, queryEndKey{}, end)
|
|
}
|
|
|
|
// TraceQueryEnd closes the actual driver operation and records only success or failure.
|
|
func (r *Runtime) TraceQueryEnd(ctx context.Context, _ *pgx.Conn, data pgx.TraceQueryEndData) {
|
|
if end, ok := ctx.Value(queryEndKey{}).(func(error)); ok {
|
|
end(data.Err)
|
|
}
|
|
}
|
|
|
|
// TraceAcquireStart counts acquisition attempts still waiting for a pool result.
|
|
func (r *Runtime) TraceAcquireStart(ctx context.Context, _ *pgxpool.Pool, _ pgxpool.TraceAcquireStartData) context.Context {
|
|
r.instruments.waiting.Add(1)
|
|
ctx, end := r.Start(ctx, "postgres.acquire", "client")
|
|
return context.WithValue(ctx, acquireEndKey{}, end)
|
|
}
|
|
|
|
// TraceAcquireEnd releases the acquisition gauge on either success or failure.
|
|
func (r *Runtime) TraceAcquireEnd(ctx context.Context, _ *pgxpool.Pool, data pgxpool.TraceAcquireEndData) {
|
|
r.instruments.waiting.Add(-1)
|
|
if end, ok := ctx.Value(acquireEndKey{}).(func(error)); ok {
|
|
end(data.Err)
|
|
}
|
|
}
|