Skip to main content

Metrics and tracing

Both are interceptors, so they see every statement a context runs, raw SQL included. Neither adds a dependency to ant's root module.

OpenTelemetry tracing​

The tracer is a separate module. It starts one span per statement, as a child of the span in the caller's context.

go get github.com/skudra-go/ant/contrib/otel
tracer := otel.New(provider.Tracer("ant"))

db, err := ant.Open[AppDB](ctx, postgres.Dialect, dsn, ant.WithInterceptor(tracer))

A complete setup​

The program imports the package as antotel because OpenTelemetry's own package is also named otel. It sends traces to an OpenTelemetry collector over OTLP.

package main

import (
"context"
"log"

"github.com/skudra-go/ant"
antotel "github.com/skudra-go/ant/contrib/otel"
"github.com/skudra-go/ant/dialect/postgres"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
)

func main() {
ctx := context.Background()

// Exports to localhost:4317, or to OTEL_EXPORTER_OTLP_ENDPOINT.
exporter, err := otlptracegrpc.New(ctx, otlptracegrpc.WithInsecure())
if err != nil {
log.Fatal(err)
}

provider := sdktrace.NewTracerProvider(sdktrace.WithBatcher(exporter))
defer provider.Shutdown(ctx)

otel.SetTracerProvider(provider)

db, err := ant.Open[AppDB](
ctx,
postgres.Dialect,
dsn,
ant.WithInterceptor(antotel.New(otel.Tracer("ant"))),
)
if err != nil {
log.Fatal(err)
}
defer db.Close()

// ... serve requests, passing each request's context to ant
}

What a span holds​

Namedb. and the statement's operation in lower case: db.select, db.insert, db.update, db.delete, db.merge, or a savepoint command (db.savepoint, db.rollback, db.release, db.save). Every other statement is db.query
KindClient
db.query.textThe SQL, with placeholders. Values are never recorded
StatusError, with the error recorded, when the statement fails

The operation is the statement's first word, after any comments and opening parentheses, so a /* tag */ comment or a hint does not change it. A WITH statement is named after the statement its common table expressions feed: WITH a AS (...) DELETE ... is db.delete. Any statement the tracer cannot name with certainty, such as CREATE TABLE or PRAGMA, is db.query, so span names stay a small, fixed set.

Pass the request's context to every ant call: the span is a child of the request's span only if the context carries it. With otelhttp, a request's trace then shows each statement.

Spans set db.system to postgresql unless told otherwise. On another database, name it with WithDialect, which uses OpenTelemetry's spelling (mysql, mariadb, sqlite, mssql), or set any value with WithSystem:

tracer := otel.New(provider.Tracer("ant"), otel.WithDialect(mysql.Dialect))

Metrics​

The metrics package counts statements by kind (SELECT, INSERT and so on) and serves them in Prometheus text format. It has no dependencies.

m := metrics.New()

db, err := ant.Open[AppDB](ctx, postgres.Dialect, dsn, ant.WithInterceptor(m))

http.Handle("/metrics", m)

The kinds are the span operations above in upper case, and QUERY for every other statement. Each kind exports a count, an error count and a duration histogram with buckets from 1 ms to 5 s:

ant_statements_total{kind="SELECT"} 42
ant_statement_errors_total{kind="SELECT"} 0
ant_statement_seconds_bucket{kind="SELECT",le="0.005"} 40
ant_statement_seconds_sum{kind="SELECT"} 0.113
ant_statement_seconds_count{kind="SELECT"} 42

Duration ends when the database answers, before rows are read.

Read the numbers in code​

Snapshot returns the totals as data, for publishing through another meter such as an OpenTelemetry callback gauge:

for _, s := range m.Snapshot() {
fmt.Println(s.Kind, s.Count, s.Errors, s.Duration)
}

Use both​

Pass WithInterceptor once for each:

db, err := ant.Open[AppDB](ctx, postgres.Dialect, dsn,
ant.WithInterceptor(m),
ant.WithInterceptor(antotel.New(otel.Tracer("ant"))),
)