mirror of
https://github.com/SamyRai/tercul-backend.git
synced 2025-12-27 02:51:34 +00:00
This commit introduces a comprehensive observability stack to the application, addressing a key objective from the TODO list. The following features have been implemented: - **Centralized Logging:** Replaced the existing custom logger with `zerolog` for structured, leveled, and performant logging. The logger is configured to output human-friendly console logs in development and JSON logs in production. - **Distributed Tracing:** Integrated OpenTelemetry to provide distributed tracing capabilities. A new middleware has been added to create spans for all incoming HTTP requests, and the trace context is propagated. - **Prometheus Metrics:** Added Prometheus metrics to monitor HTTP request latency and total request counts. A new `/metrics` endpoint is exposed on port 9090 to serve these metrics. - **Request ID:** Implemented a middleware to add a unique request ID to every incoming request and response, improving traceability. The new observability components are encapsulated in the `internal/observability` package, and the existing `internal/platform/log` package has been refactored to be a backward-compatible wrapper around the new logger. The main application entry point (`cmd/api/main.go`) has been updated to initialize and gracefully shut down the new observability components.
56 lines
1.7 KiB
Go
56 lines
1.7 KiB
Go
package observability
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promauto"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
)
|
|
|
|
// Metrics contains the Prometheus metrics for the application.
|
|
type Metrics struct {
|
|
RequestsTotal *prometheus.CounterVec
|
|
RequestDuration *prometheus.HistogramVec
|
|
}
|
|
|
|
// NewMetrics creates and registers the Prometheus metrics.
|
|
func NewMetrics(reg prometheus.Registerer) *Metrics {
|
|
return &Metrics{
|
|
RequestsTotal: promauto.With(reg).NewCounterVec(
|
|
prometheus.CounterOpts{
|
|
Name: "http_requests_total",
|
|
Help: "Total number of HTTP requests.",
|
|
},
|
|
[]string{"method", "path", "status"},
|
|
),
|
|
RequestDuration: promauto.With(reg).NewHistogramVec(
|
|
prometheus.HistogramOpts{
|
|
Name: "http_request_duration_seconds",
|
|
Help: "Duration of HTTP requests.",
|
|
Buckets: prometheus.DefBuckets,
|
|
},
|
|
[]string{"method", "path"},
|
|
),
|
|
}
|
|
}
|
|
|
|
// PrometheusMiddleware returns an HTTP middleware that records Prometheus metrics.
|
|
func (m *Metrics) PrometheusMiddleware(next http.Handler) http.Handler {
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
start := time.Now()
|
|
rw := &responseWriter{ResponseWriter: w}
|
|
next.ServeHTTP(rw, r)
|
|
|
|
duration := time.Since(start).Seconds()
|
|
m.RequestDuration.WithLabelValues(r.Method, r.URL.Path).Observe(duration)
|
|
m.RequestsTotal.WithLabelValues(r.Method, r.URL.Path, http.StatusText(rw.statusCode)).Inc()
|
|
})
|
|
}
|
|
|
|
// PrometheusHandler returns an HTTP handler for serving Prometheus metrics.
|
|
func PrometheusHandler(reg prometheus.Gatherer) http.Handler {
|
|
return promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
|
|
}
|