Documentation
¶
Overview ¶
Package metrics emits StatsD metrics through a client carried in the context.
The client is created once and injected by middleware:
metric, err := metrics.New()
if err != nil {
return err
}
server := httpserver.New(
httpserver.WithMiddlewares(metrics.Metrics(metric)),
)
Instrumented code then emits without holding a reference to the client:
metrics.Increment(ctx, "orders.accepted", "channel:web") metrics.Histogram(ctx, "orders.total", total, 1, "currency:brl")
The context lookup is the point: a handler deep in a call chain emits a metric without the client being threaded through every signature.
It also means emission is silently a no-op when the context carries no client. That is deliberate - a missing metrics client should not break a request - but it does mean a metric that never appears in Datadog is usually a middleware that was never installed. Only the HTTP middleware injects today, so Kafka consumers and background jobs have no way to emit.
New reads no configuration: the agent address is fixed at localhost:8125.
Index ¶
- func Gauge(ctx context.Context, name string, value float64, rate float64, tags ...string)
- func Histogram(ctx context.Context, name string, value float64, rate float64, tags ...string)
- func Increment(ctx context.Context, name string, tags ...string)
- func Metrics(metric Metric) func(handler http.Handler) http.Handler
- type Metric
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Gauge ¶
Gauge sets the named gauge to value at the given sample rate (1 emits every point). No-op when ctx carries no Metric.
func Histogram ¶
Histogram records value in the named histogram at the given sample rate (1 emits every point). No-op when ctx carries no Metric.
func Increment ¶
Increment adds one to the named counter, tagged with tags.
It is a no-op when ctx carries no Metric, so a metric that never reaches Datadog usually means the Metrics middleware was not installed. Emission failures are logged, not returned.
Example (WithoutClient) ¶
Emission outside an HTTP request is currently a no-op, because the middleware is the only thing that injects a client.
package main
import (
"context"
"github.com/diegodesousas/go-devkit/pkg/metrics"
)
func main() {
metrics.Increment(context.Background(), "orders.accepted") // does nothing
}
Output:
func Metrics ¶
Metrics returns middleware putting metric in the request context, which is what makes Increment, Histogram and Gauge work downstream.
It is the only injection point in this package, so code outside an HTTP request - Kafka consumers, background jobs - currently has no way to emit.
Example ¶
Requires a Datadog agent on localhost:8125, so this example is compiled but not run.
package main
import (
"net/http"
"github.com/diegodesousas/go-devkit/pkg/httpserver"
"github.com/diegodesousas/go-devkit/pkg/metrics"
)
func main() {
metric, err := metrics.New()
if err != nil {
panic(err)
}
// The middleware is what puts the client in the request context. Without
// it, every emission downstream is silently a no-op.
httpserver.New(
httpserver.WithMiddlewares(metrics.Metrics(metric)),
httpserver.WithRoutes(
httpserver.NewPost("/orders", handleOrder),
),
)
}
func handleOrder(w http.ResponseWriter, r *http.Request) error {
ctx := r.Context()
metrics.Increment(ctx, "orders.accepted", "channel:web")
metrics.Histogram(ctx, "orders.total", 250, 1, "currency:brl")
return nil
}
Output:
Types ¶
type Metric ¶
type Metric interface {
statsd.ClientInterface
}
Metric is the StatsD client. It aliases the full datadog-go client interface, so a substitute has to implement every method - in practice this is satisfied by a real client, not a hand-written fake.