Build on it
Go client
pingwright-go is Pingwright's Go library. One call to Init sends a service's logs, metrics and errors, over standard OpenTelemetry (OTLP/HTTP) under the hood.
- Logs: a
log/sloglogger that writes locally and to Pingwright, with trace and span ids attached. - Errors:
CaptureErrorandRecoverturn errors and panics into issues, grouped by type and stack. - Metrics: Go runtime metrics, HTTP server metrics from
Middleware, and your own throughMeter.
The source is on GitHub (MIT) and the API reference on pkg.go.dev.
Logs, Metrics and Errors are beta feature previews: turn on the ones you send under Settings › Feature previews. Ingest refuses a signal while its preview is off.
Install
go get github.com/Pingwright-Labs/pingwright-goIt needs Go 1.26 or newer, as the OpenTelemetry Go SDK it builds on does.
You also need an ingest key (pwi_…). Create one under Settings › Telemetry; it's shown once. Settings › Telemetry › Connect a service walks you through it and fills the key into the snippet.
Quick start
package main
import (
"context"
"log"
"log/slog"
"os"
"time"
"github.com/Pingwright-Labs/pingwright-go"
)
func main() {
ctx := context.Background()
shutdown, err := pingwright.Init(ctx, pingwright.Options{
Key: os.Getenv("PINGWRIGHT_KEY"), // pwi_..., from Settings › Telemetry
Service: "billing-api",
Environment: "production",
})
if err != nil {
log.Fatal(err)
}
defer func() { // flushes on exit, for at most 5 seconds
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
_ = shutdown(ctx)
}()
defer pingwright.Recover(ctx) // reports a crash, then crashes as before
slog.Info("started", "port", 8080) // stderr and Pingwright
}Init makes pingwright.Logger() the slog default, so existing slog.Info(...) calls, and the standard log package, go to Pingwright without changes. Key falls back to the PINGWRIGHT_KEY environment variable, so you can leave it out.
Init returns an error only for a configuration that cannot work: a key that is blank or contains whitespace, or an endpoint that is not an http(s) URL. Everything else degrades instead of failing. Without a key, it prints one warning and runs in local mode: logging, CaptureError, Recover, Middleware and Meter all work, and nothing is sent, so development and tests need no setup.
The library never blocks your code on the network. Records are batched and exported in the background, and export failures are written to your local output (at most once every 30 seconds per distinct error), never back to Pingwright.
net/http
Wrap the whole mux so every route is covered:
mux := http.NewServeMux()
mux.HandleFunc("GET /orders/{id}", func(w http.ResponseWriter, r *http.Request) {
slog.InfoContext(r.Context(), "loading order", "id", r.PathValue("id"))
// ...
})
log.Fatal(http.ListenAndServe(":8080", pingwright.Middleware(mux)))Middleware:
- records OpenTelemetry HTTP server metrics (
http.server.request.durationand friends), withhttp.routefrom the Go 1.22 mux pattern; - reports a panic in a handler as an error and answers 500 instead of crashing. If the handler had already started the response, a 500 is no longer possible: the panic is reported and the connection is aborted, so the client sees a broken response rather than a truncated one that looks complete;
- picks up an incoming
traceparentheader, so logs written withr.Context()carry the caller's trace id.
Errors and panics
if err := charge(ctx, order); err != nil {
pingwright.CaptureError(ctx, err, slog.String("order.id", order.ID))
return err
}CaptureError sends a log record at ERROR with the OpenTelemetry exception attributes, which Pingwright groups into issues by type and the top frames of your own code:
| Attribute | Value |
|---|---|
exception.type | the Go type of the innermost error under plain wrappers (fmt.Errorf("…: %w"), errors.Join, *url.Error, pkg/errors), for example *net.OpError |
exception.message | err.Error() |
exception.stacktrace | the stack at the call site, in runtime/debug.Stack format |
Put defer pingwright.Recover(ctx) at the top of main and of each goroutine. It reports a panic at FATAL with the stack from the line that panicked, flushes it (up to 5 seconds), and panics again, so the process still crashes the way it would have:
go func() {
defer pingwright.Recover(ctx)
work(ctx)
}()Errors travel to the logs endpoint, so they become issues only when:
- the Errors and Logs previews are both on, and
- the key may send errors and logs.
They are sent even below LogLevel, and land in the organization's first Errors project. With logs allowed but errors not, they are kept as error logs only and no issue is made.
Metrics
Go runtime metrics (go.memory.used, go.goroutine.count, GC and the scheduler) are sent every 30 seconds without any code. For your own, use pingwright.Meter, a standard OpenTelemetry meter:
meter := pingwright.Meter("checkout")
orders, _ := meter.Int64Counter("orders.placed")
latency, _ := meter.Float64Histogram("payment.duration", metric.WithUnit("s"))
orders.Add(ctx, 1, metric.WithAttributes(attribute.String("plan", "pro")))
latency.Record(ctx, time.Since(start).Seconds())Init also sets the global OpenTelemetry MeterProvider, so libraries that use otel.Meter report through Pingwright too. Chart and query everything in Metrics.
Options and environment variables
| Option | Environment variable | Default |
|---|---|---|
Key (pwi_…) | PINGWRIGHT_KEY | none: local mode |
Service (service.name) | OTEL_SERVICE_NAME | the executable's name |
Environment (deployment.environment.name) | OTEL_RESOURCE_ATTRIBUTES | none |
Version (service.version) | OTEL_RESOURCE_ATTRIBUTES | none |
Endpoint | PINGWRIGHT_ENDPOINT | https://ingest.pingwright.app |
ResourceAttributes | OTEL_RESOURCE_ATTRIBUTES | none |
LogLevel (a slog.Leveler, such as slog.LevelDebug or a *slog.LevelVar) | slog.LevelInfo, locally and sent | |
Output / TextOutput | JSON to os.Stderr | |
Handler (your own slog.Handler for local output) | none |
An option set in code wins over its environment variable.
To turn parts off:
pingwright.Options{
DisableLogs: true, // no log records (errors are still sent)
DisableErrors: true, // CaptureError and Recover send a plain error log
DisableMetrics: true, // no metrics at all
DisableRuntimeMetrics: true, // keep HTTP and custom metrics, skip Go runtime metrics
KeepDefaultLogger: true, // don't replace slog.Default
Output: io.Discard, // nothing written locally
}Leave PINGWRIGHT_KEY unset to send nothing at all.
What is sent
- Logs to
/v1/logs(OTLP/HTTP protobuf, gzipped,Authorization: Bearer pwi_…), batched, atLogLeveland above, with trace and span ids from the context. - Errors as log records at
ERROR(CaptureError,Middleware) orFATAL(Recover) with theexception.*attributes above. - Metrics to
/v1/metricsevery 30 seconds: Go runtime metrics, HTTP server metrics fromMiddleware, and your own. - Resource attributes:
service.name,deployment.environment.name,service.version,host.name, the OpenTelemetry SDK version, and your own. For metrics, Pingwright keepsservice.name,service.namespace,service.instance.id,deployment.environment.name,host.name,cloud.regionand the Kubernetes cluster, namespace, deployment, statefulset, daemonset, node and pod names as labels, and drops the rest. - No traces. Pingwright doesn't store traces yet, so none are exported. If you run your own tracer, its trace ids still end up on log records.
Exports time out after 10 seconds and are retried with backoff.
Shutdown
Call the function Init returns before the process exits, with a context that has a deadline. It flushes pending logs and metrics in parallel and returns by the deadline, or after 5 seconds when the context has none. After it, Logger() keeps writing to your local output, and Init may be called again (useful in tests). A second Init before shutdown returns ErrAlreadyInitialized.
Recover flushes on a crash by itself. log.Fatal and os.Exit skip deferred calls, so call shutdown before them if the last records matter.
Plain OpenTelemetry instead
The library is a convenience over the OpenTelemetry Go SDK. If you'd rather wire up otlploghttp, otlpmetrichttp and the otelslog bridge yourself, use the standard variables from Logs and Metrics. For errors from plain OpenTelemetry, emit log records at ERROR or above with exception.type, exception.message and exception.stacktrace; or use sentry-go with a DSN.