Skip to main content

Metrics and tracing

Both are interceptors, so they see every statement, and neither adds a dependency to ant's root module.

Metrics​

The metrics package counts statements with no dependencies, and writes Prometheus text format, which most monitoring systems read.

m := metrics.New()

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

http.Handle("/metrics", m)

m.Snapshot() returns the numbers as data, per kind of statement, so a few lines of your own can publish them through any meter.

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

OpenTelemetry​

Tracing is its own module, kept apart so the OpenTelemetry SDK stays out of ant's core:

go get github.com/skudra-go/ant/contrib/otel

Hand it a tracer from your provider:

tracer := otel.New(provider.Tracer("ant"))

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

Each statement gets a span.