61 lines
1.4 KiB
Go
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()
|
|
}
|