Metrics Collection
Learn how to collect and export application metrics with Rivaas metrics package
2 minute read
The Rivaas Metrics package provides OpenTelemetry-based metrics collection. Supports multiple exporters including Prometheus, OTLP, and stdout. Enables observability best practices with minimal configuration.
Features
- Multiple Providers: Prometheus, OTLP, and stdout exporters
- Built-in HTTP Metrics: Request duration, count, active requests, and more
- Custom Metrics: Support for counters, histograms, and gauges with error handling
- Thread-Safe: All methods are safe for concurrent use
- Context Support: All metrics methods accept context for cancellation
- Structured Logging: Pluggable logger interface for error and warning messages
- HTTP Middleware: Integration with any HTTP framework
- Security: Automatic filtering of sensitive headers
Quick Start
package main
import (
"context"
"log"
"net/http"
"os/signal"
"rivaas.dev/metrics"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
recorder, err := metrics.New(
metrics.WithPrometheus(":9090", "/metrics"),
metrics.WithServiceName("my-api"),
metrics.WithServiceVersion("v1.0.0"),
)
if err != nil {
log.Fatal(err)
}
if err := recorder.Start(ctx); err != nil {
log.Fatal(err)
}
defer recorder.Shutdown(context.Background())
// Record custom metrics
_ = recorder.IncrementCounter(ctx, "requests_total")
// Prometheus metrics available at http://localhost:9090/metrics
}package main
import (
"context"
"log"
"os/signal"
"rivaas.dev/metrics"
)
func main() {
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()
recorder, err := metrics.New(
metrics.WithOTLP("http://localhost:4318"),
metrics.WithServiceName("my-api"),
metrics.WithServiceVersion("v1.0.0"),
)
if err != nil {
log.Fatal(err)
}
if err := recorder.Start(ctx); err != nil {
log.Fatal(err)
}
defer recorder.Shutdown(context.Background())
// Metrics pushed to OTLP collector
_ = recorder.IncrementCounter(ctx, "requests_total")
}package main
import (
"context"
"log"
"rivaas.dev/metrics"
)
func main() {
recorder := metrics.MustNew(
metrics.WithStdout(),
metrics.WithServiceName("my-api"),
)
ctx := context.Background()
// Metrics printed to stdout
_ = recorder.IncrementCounter(ctx, "requests_total")
}How It Works
- Providers determine where metrics are exported (Prometheus, OTLP, stdout)
- Lifecycle management ensures proper initialization and graceful shutdown
- HTTP middleware automatically collects request metrics
- Custom metrics can be recorded with type-safe methods
- Context support enables cancellation and request tracing
Learning Path
Follow these guides to master metrics collection with Rivaas:
- Installation - Get started with the metrics package
- Basic Usage - Learn the fundamentals of metrics collection
- Providers - Understand Prometheus, OTLP, and stdout exporters
- Configuration - Configure service metadata, histograms, and advanced options
- Custom Metrics - Create counters, histograms, and gauges
- Middleware - Integrate HTTP metrics with your application
- Testing - Test your metrics with provided utilities
- Examples - See real-world usage patterns
Next Steps
- Start with Installation to set up the metrics package
- Explore the API Reference for complete technical details
- Check out code examples on GitHub
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.