country-geo-locations/internal/exporter/exporter.go
Tom Neuber 8b7f45563a
All checks were successful
ci/woodpecker/push/lint Pipeline was successful
ci/woodpecker/push/test Pipeline was successful
ci/woodpecker/push/build Pipeline was successful
ci/woodpecker/push/deploy Pipeline was successful
chore(exporter): rework exporter to fix wrong histogram usage and cache metric data
2025-01-10 14:35:08 +01:00

61 lines
1.4 KiB
Go

package exporter
import (
"net/http"
"git.ar21.de/yolokube/country-geo-locations/internal/cache"
"git.ar21.de/yolokube/country-geo-locations/internal/cmd"
"git.ar21.de/yolokube/country-geo-locations/internal/database"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type Exporter struct {
config *cmd.AppSettings
cache *cache.Cache
database *database.Database
metrics *Metrics
}
func NewExporter(config *cmd.AppSettings, c *cache.Cache, db *database.Database) *Exporter {
exporter := &Exporter{
config: config,
cache: c,
database: db,
}
exporter.metrics = NewMetrics(exporter)
return exporter
}
func (e *Exporter) Collect() {
e.metrics.collectCacheTTLMetric()
e.metrics.collectCurrentlyCachedMetric()
e.metrics.collectDatabaseReadyMetric()
e.metrics.collectDatabaseTimestampMetric()
}
func (e *Exporter) Middleware() func(next http.Handler) http.Handler {
return Middleware{
metrics: e.metrics,
}.handler
}
func (e *Exporter) Start() error {
prometheus.MustRegister(
e.metrics.metricCacheTTL,
e.metrics.metricCurrentlyCached,
e.metrics.metricDatabaseTimestamp,
e.metrics.metricDatabaseReady,
e.metrics.metricRequestsTotal,
e.metrics.metricRequestLatency,
)
server := &http.Server{
Addr: e.config.ExporterAddress,
Handler: promhttp.Handler(),
ReadHeaderTimeout: e.config.ReadHeaderTimeout,
}
return server.ListenAndServe()
}