Nik Afiq 657b6aeb22 feat: implement initial application structure with health and hello endpoints
- Add bootstrap package to initialize application components including logger, tracer, and HTTP server.
- Create config package to load runtime settings from environment variables.
- Implement observability features including logging, metrics, and tracing.
- Add health check and hello use cases with corresponding HTTP handlers.
- Introduce middleware for request ID, access logging, metrics, and recovery.
- Set up HTTP router with defined routes for health and hello endpoints.
- Include tests for health and hello endpoints to ensure proper functionality.
- Add OpenTelemetry collector configuration for trace exporting.
2026-03-05 21:22:43 +09:00

59 lines
1.6 KiB
Go

package observability
import (
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
// HTTPMetrics contains metric collectors used by HTTP middleware.
type HTTPMetrics struct {
RequestsTotal *prometheus.CounterVec
RequestDuration *prometheus.HistogramVec
InFlight prometheus.Gauge
}
func NewRegistry() *prometheus.Registry {
registry := prometheus.NewRegistry()
registry.MustRegister(
collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
)
return registry
}
func NewHTTPMetrics(registerer prometheus.Registerer) *HTTPMetrics {
factory := promauto.With(registerer)
return &HTTPMetrics{
RequestsTotal: factory.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "Total number of HTTP requests by method, route, and status.",
},
[]string{"method", "route", "status"},
),
RequestDuration: factory.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "HTTP request duration by method, route, and status.",
Buckets: prometheus.DefBuckets,
},
[]string{"method", "route", "status"},
),
InFlight: factory.NewGauge(
prometheus.GaugeOpts{
Name: "http_in_flight_requests",
Help: "Current number of in-flight HTTP requests.",
},
),
}
}
func NewPrometheusHandler(registry *prometheus.Registry) http.Handler {
return promhttp.HandlerFor(registry, promhttp.HandlerOpts{})
}