mirror of
https://github.com/MeshCore-Beacon/beacon-server.git
synced 2026-12-06 06:14:23 +00:00
* feat(db): five pgx pools with fixed session settings and Prometheus tracing * feat(guards): per-family observe/enforce guards with client slots, deadlines and a shadow budget * fix(guards): attribute only guard deadlines and classify like the handlers * feat(db): route API statements by sqlc name; class C always unnamed * feat(admin): statement statistics by sqlc name; create pg_stat_statements where allowed * fix(db): skip pg_stat_statements when no schema is creatable * feat(background): index health against post-rebuild baselines, Postgres activity, job and ingest metrics * feat: separate API, ingest and maintenance stores; guards on the public API; metrics listener * fix(db): keep ingest writes waiting, size the ingest pool to the host, tidy * test(db): give the statements probe a query id of its own
83 lines
2.3 KiB
Go
83 lines
2.3 KiB
Go
// Copyright 2026 Beacon Contributors
|
|
// SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/MeshCore-Beacon/beacon-server/internal/metrics"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
)
|
|
|
|
// sqlcName returns X from a leading "-- name: X :kind" line, or "" (tracer label "unnamed").
|
|
// sqlc always writes that header at column 0 of the statement text.
|
|
func sqlcName(sql string) string {
|
|
rest, ok := strings.CutPrefix(sql, "-- name: ")
|
|
if !ok {
|
|
return ""
|
|
}
|
|
line, _, _ := strings.Cut(rest, "\n")
|
|
name, kind, ok := strings.Cut(line, " ")
|
|
if !ok || name == "" || !strings.HasPrefix(kind, ":") {
|
|
return ""
|
|
}
|
|
return name
|
|
}
|
|
|
|
type queryStartKey struct{}
|
|
|
|
type acquireStartKey struct{}
|
|
|
|
type queryStart struct {
|
|
at time.Time
|
|
statement string
|
|
}
|
|
|
|
// tracer records statement time and connection acquire time for one pool.
|
|
type tracer struct {
|
|
pool string
|
|
}
|
|
|
|
func newTracer(pool string) *tracer { return &tracer{pool: pool} }
|
|
|
|
var (
|
|
_ pgx.QueryTracer = (*tracer)(nil)
|
|
_ pgxpool.AcquireTracer = (*tracer)(nil)
|
|
)
|
|
|
|
// TraceQueryStart runs at the start of Query, QueryRow and Exec.
|
|
func (t *tracer) TraceQueryStart(ctx context.Context, _ *pgx.Conn, data pgx.TraceQueryStartData) context.Context {
|
|
name := sqlcName(data.SQL)
|
|
if name == "" {
|
|
name = "unnamed"
|
|
}
|
|
return context.WithValue(ctx, queryStartKey{}, queryStart{at: time.Now(), statement: name})
|
|
}
|
|
|
|
// TraceQueryEnd runs when Exec returns, or when the rows of a Query or QueryRow are closed.
|
|
func (t *tracer) TraceQueryEnd(ctx context.Context, _ *pgx.Conn, _ pgx.TraceQueryEndData) {
|
|
s, ok := ctx.Value(queryStartKey{}).(queryStart)
|
|
if !ok {
|
|
return
|
|
}
|
|
metrics.DBTime.WithLabelValues(s.statement, t.pool).Observe(time.Since(s.at).Seconds())
|
|
}
|
|
|
|
// TraceAcquireStart runs at the start of a pool Acquire.
|
|
func (t *tracer) TraceAcquireStart(ctx context.Context, _ *pgxpool.Pool, _ pgxpool.TraceAcquireStartData) context.Context {
|
|
return context.WithValue(ctx, acquireStartKey{}, time.Now())
|
|
}
|
|
|
|
// TraceAcquireEnd runs when the Acquire returns, with or without a connection.
|
|
func (t *tracer) TraceAcquireEnd(ctx context.Context, _ *pgxpool.Pool, _ pgxpool.TraceAcquireEndData) {
|
|
at, ok := ctx.Value(acquireStartKey{}).(time.Time)
|
|
if !ok {
|
|
return
|
|
}
|
|
metrics.PoolAcquire.WithLabelValues(t.pool).Observe(time.Since(at).Seconds())
|
|
}
|