feat(metrics): moved back the metrics endpoint to support the undocumented functionality

This commit is contained in:
Alexander 2025-03-13 13:34:23 +01:00
parent 1cca40d4c5
commit 160b5ac2c8
2 changed files with 30 additions and 40 deletions

View file

@ -1,10 +1,38 @@
package metrics
import (
"context"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"openreplay/backend/pkg/logger"
)
type MetricServer struct{}
type MetricServer struct {
registry *prometheus.Registry
}
func New(log logger.Logger, cs []prometheus.Collector) {}
func New(log logger.Logger, cs []prometheus.Collector) {
registry := prometheus.NewRegistry()
// Add go runtime metrics and process collectors.
registry.MustRegister(
collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
)
// Add extra metrics
registry.MustRegister(cs...)
// Expose /metrics HTTP endpoint using the created custom registry.
http.Handle(
"/metrics", promhttp.HandlerFor(
registry,
promhttp.HandlerOpts{
EnableOpenMetrics: true,
}),
)
go func() {
log.Error(context.Background(), "%v", http.ListenAndServe(":8888", nil))
}()
}

View file

@ -1,38 +0,0 @@
package metrics
import (
"context"
"net/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/collectors"
"github.com/prometheus/client_golang/prometheus/promhttp"
"openreplay/backend/pkg/logger"
)
type MetricServer struct {
registry *prometheus.Registry
}
func New(log logger.Logger, cs []prometheus.Collector) {
registry := prometheus.NewRegistry()
// Add go runtime metrics and process collectors.
registry.MustRegister(
collectors.NewGoCollector(),
collectors.NewProcessCollector(collectors.ProcessCollectorOpts{}),
)
// Add extra metrics
registry.MustRegister(cs...)
// Expose /metrics HTTP endpoint using the created custom registry.
http.Handle(
"/metrics", promhttp.HandlerFor(
registry,
promhttp.HandlerOpts{
EnableOpenMetrics: true,
}),
)
go func() {
log.Error(context.Background(), "%v", http.ListenAndServe(":8888", nil))
}()
}