Usage statistics & metrics
The Go SDK can optionally batch and send:
- usage statistics (feature checks + usage)
- metrics (measurements / counters / observations)
This is done via gRPC.
Enable usage statistics
client, err := toggly.NewClient(toggly.Config{
AppKey: "YOUR_APP_KEY",
Environment: "Production",
EnableUsage: true,
})
Record usage
Checks are automatically recorded when EnableUsage=true.
RecordUsage is count-only. For attempt plus duration and errors, use a scope:
ctx := toggly.Context{Identity: "user-123"}
err := client.Use(context.Background(), "MyFeature", ctx, func(scope usage.FeatureUsageScope) error {
return processOrder()
})
// Pass context.Canceled (or a wrapped cancel) from fn to record attempt only.
Bare “used” events without timing:
client.RecordUsage("MyFeature", true, toggly.Context{Identity: "user-123"})
Enable metrics
client, err := toggly.NewClient(toggly.Config{
AppKey: "YOUR_APP_KEY",
Environment: "Production",
EnableMetrics: true,
})
Emit metrics
The final argument is the variant name; use an empty string for a boolean feature.
m := client.MetricsClient()
if m != nil {
featureKey := "MyFeature"
m.Increment("MyFeature.Counter", 1, &featureKey, "")
m.Measure("MyFeature.LatencyMs", 12.3, &featureKey, "")
m.Observe("MyFeature.PayloadSize", 2048, &featureKey, "")
}
Flushing
Both usage and metrics are flushed periodically (defaults to 1 minute). You can set:
UsageFlushIntervalMetricsFlushInterval
The underlying clients also expose a Flush(ctx) method if you need a manual flush.