middleman/pkg/metrics/prometheus.go
2025-05-01 17:55:42 +02:00

75 lines
3.0 KiB
Go

package metrics
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"net/http"
"sync"
)
const (
namespace = "middleman"
registeredRegexPrefix = "registered_regex_"
registeredRegexTotalName = registeredRegexPrefix + "total"
incomingHTTPRequestPrefix = "incoming_http_requests_"
incomingHTTPRequestTotalName = incomingHTTPRequestPrefix + "total"
processingDurationPrefix = "processing_duration_"
processingDurationName = processingDurationPrefix + "seconds"
processedHTTPRequestsPrefix = "processed_http_requests_"
processedHTTPRequestsTotalName = processedHTTPRequestsPrefix + "total"
healthcheckResponseTimePrefix = "healthcheck_response_time_"
healthcheckResponseTimeName = healthcheckResponseTimePrefix + "seconds"
nameResolutionDurationPrefix = "name_resolution_duration_"
nameResolutionDurationName = nameResolutionDurationPrefix + "seconds"
)
var (
once sync.Once
promRegistry = prometheus.NewRegistry()
RegisteredRegexCounterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: registeredRegexTotalName,
Help: "Total number of registered regex",
}, []string{"client", "http_method", "regex"})
IncomingHTTPRequestCounterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: incomingHTTPRequestTotalName,
Help: "Total number of incoming HTTP requests",
}, []string{"client", "http_method", "path"})
ProcessingDurationHistogramVec = prometheus.NewHistogramVec(prometheus.HistogramOpts{
Namespace: namespace,
Name: processingDurationName,
Help: "Number of seconds to process an incoming request",
}, []string{"client", "http_method", "path"})
ProcessedHTTPRequestCounterVec = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: namespace,
Name: processedHTTPRequestsTotalName,
Help: "Total number of processed HTTP requests",
}, []string{"client", "http_methods", "path", "status_code"})
HealthcheckResponseTimeGauge = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: namespace,
Name: healthcheckResponseTimeName,
Help: "Number of seconds to perform healthcheck request",
})
NameResolutionDurationGauge = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Namespace: namespace,
Name: nameResolutionDurationName,
Help: "Number of seconds to perform name resolution using the local resolver",
}, []string{"client"})
)
func PrometheusHandler() http.Handler {
return promhttp.HandlerFor(promRegistry, promhttp.HandlerOpts{})
}
func RegisterPrometheus() {
once.Do(func() {
promRegistry.MustRegister(RegisteredRegexCounterVec)
promRegistry.MustRegister(IncomingHTTPRequestCounterVec)
promRegistry.MustRegister(ProcessingDurationHistogramVec)
promRegistry.MustRegister(ProcessedHTTPRequestCounterVec)
promRegistry.MustRegister(HealthcheckResponseTimeGauge)
promRegistry.MustRegister(NameResolutionDurationGauge)
})
}