forked from mtusi2026/mtusi2026lab6
105 lines
3.0 KiB
Go
105 lines
3.0 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"strings"
|
|
"sync/atomic"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"go.opentelemetry.io/otel/attribute"
|
|
"go.opentelemetry.io/otel/metric"
|
|
"go.opentelemetry.io/otel/trace"
|
|
|
|
"mtusi2026/internal/telemetry"
|
|
)
|
|
|
|
type postgresTracer struct{ waiting atomic.Int64 }
|
|
|
|
func NewPool(ctx context.Context, cfg *pgxpool.Config) (*pgxpool.Pool, error) {
|
|
tracer := &postgresTracer{}
|
|
cfg.ConnConfig.Tracer = tracer
|
|
pool, err := pgxpool.NewWithConfig(ctx, cfg)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
err = tracer.observe(pool)
|
|
if err != nil {
|
|
pool.Close()
|
|
return nil, err
|
|
}
|
|
return pool, nil
|
|
}
|
|
|
|
func (t *postgresTracer) observe(pool *pgxpool.Pool) error {
|
|
used, err := telemetry.Meter.Int64ObservableGauge("messenger.db.pool.used")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
waiting, err := telemetry.Meter.Int64ObservableGauge("messenger.db.pool.waiting")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
capacity, err := telemetry.Meter.Int64ObservableGauge("messenger.db.pool.capacity")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = telemetry.Meter.RegisterCallback(func(_ context.Context, observer metric.Observer) error {
|
|
stats := pool.Stat()
|
|
observer.ObserveInt64(used, int64(stats.AcquiredConns()))
|
|
observer.ObserveInt64(waiting, t.waiting.Load())
|
|
observer.ObserveInt64(capacity, int64(stats.MaxConns()))
|
|
return nil
|
|
}, used, waiting, capacity)
|
|
return err
|
|
}
|
|
|
|
func (t *postgresTracer) TraceQueryStart(ctx context.Context, _ *pgx.Conn, data pgx.TraceQueryStartData) context.Context {
|
|
if !trace.SpanFromContext(ctx).IsRecording() {
|
|
return ctx
|
|
}
|
|
|
|
sql := strings.TrimSpace(data.SQL)
|
|
first := strings.IndexAny(sql, " \t\r\n;")
|
|
if first >= 0 {
|
|
sql = sql[:first]
|
|
}
|
|
operation := strings.ToUpper(sql)
|
|
switch operation {
|
|
case "SELECT", "INSERT", "UPDATE", "DELETE", "BEGIN", "COMMIT", "ROLLBACK":
|
|
default:
|
|
operation = "OTHER"
|
|
}
|
|
ctx, _ = telemetry.Tracer.Start(ctx, "db.query", trace.WithSpanKind(trace.SpanKindClient),
|
|
trace.WithAttributes(attribute.String("db.system.name", "postgresql"), attribute.String("db.operation.name", operation)))
|
|
return ctx
|
|
}
|
|
|
|
func (t *postgresTracer) TraceQueryEnd(ctx context.Context, _ *pgx.Conn, data pgx.TraceQueryEndData) {
|
|
if span := trace.SpanFromContext(ctx); span.IsRecording() {
|
|
err := data.Err
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
err = nil
|
|
}
|
|
telemetry.Finish(span, err)
|
|
}
|
|
}
|
|
|
|
func (t *postgresTracer) TraceAcquireStart(ctx context.Context, _ *pgxpool.Pool, _ pgxpool.TraceAcquireStartData) context.Context {
|
|
t.waiting.Add(1)
|
|
if !trace.SpanFromContext(ctx).IsRecording() {
|
|
return ctx
|
|
}
|
|
ctx, _ = telemetry.Tracer.Start(ctx, "db.pool.acquire", trace.WithSpanKind(trace.SpanKindClient),
|
|
trace.WithAttributes(attribute.String("db.system.name", "postgresql")))
|
|
return ctx
|
|
}
|
|
|
|
func (t *postgresTracer) TraceAcquireEnd(ctx context.Context, _ *pgxpool.Pool, data pgxpool.TraceAcquireEndData) {
|
|
t.waiting.Add(-1)
|
|
if span := trace.SpanFromContext(ctx); span.IsRecording() {
|
|
telemetry.Finish(span, data.Err)
|
|
}
|
|
}
|