112 lines
2.9 KiB
Go
112 lines
2.9 KiB
Go
package exporter
|
|
|
|
import (
|
|
"log"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
const (
|
|
namespace string = "country_geo_locations"
|
|
cacheSubsystem string = "cache"
|
|
dbSubsystem string = "db"
|
|
|
|
metricNameCacheTTL string = "ttl"
|
|
metricNameCurrentlyCached string = "currently_cached"
|
|
metricNameDatabaseTimestamp string = "timestamp"
|
|
metricNameDatabaseReady string = "ready"
|
|
metricNameRequestsTotal string = "requests_total"
|
|
metricNameRequestLatency string = "request_latency"
|
|
)
|
|
|
|
type Metrics struct {
|
|
exporter *Exporter
|
|
|
|
metricCacheTTL prometheus.Gauge
|
|
metricCurrentlyCached prometheus.Gauge
|
|
metricDatabaseTimestamp *prometheus.GaugeVec
|
|
metricDatabaseReady prometheus.Gauge
|
|
metricRequestsTotal prometheus.Counter
|
|
metricRequestLatency prometheus.Histogram
|
|
}
|
|
|
|
func NewMetrics(e *Exporter) *Metrics {
|
|
return &Metrics{
|
|
exporter: e,
|
|
metricCacheTTL: prometheus.NewGauge(
|
|
prometheus.GaugeOpts{
|
|
Namespace: namespace,
|
|
Subsystem: cacheSubsystem,
|
|
Name: metricNameCacheTTL,
|
|
Help: "Duration for cached requests",
|
|
},
|
|
),
|
|
metricCurrentlyCached: prometheus.NewGauge(
|
|
prometheus.GaugeOpts{
|
|
Namespace: namespace,
|
|
Subsystem: cacheSubsystem,
|
|
Name: metricNameCurrentlyCached,
|
|
Help: "Number of cached entries",
|
|
},
|
|
),
|
|
metricDatabaseTimestamp: prometheus.NewGaugeVec(
|
|
prometheus.GaugeOpts{
|
|
Namespace: namespace,
|
|
Subsystem: dbSubsystem,
|
|
Name: metricNameDatabaseTimestamp,
|
|
Help: "Timestamp of the CSV file",
|
|
},
|
|
[]string{metricNameDatabaseTimestamp},
|
|
),
|
|
metricDatabaseReady: prometheus.NewGauge(
|
|
prometheus.GaugeOpts{
|
|
Namespace: namespace,
|
|
Subsystem: dbSubsystem,
|
|
Name: metricNameDatabaseReady,
|
|
Help: "Ready status of the database",
|
|
},
|
|
),
|
|
metricRequestsTotal: prometheus.NewCounter(
|
|
prometheus.CounterOpts{
|
|
Namespace: namespace,
|
|
Name: metricNameRequestsTotal,
|
|
Help: "Counter for total requests",
|
|
},
|
|
),
|
|
metricRequestLatency: prometheus.NewHistogram(
|
|
prometheus.HistogramOpts{
|
|
Namespace: namespace,
|
|
Name: metricNameRequestLatency,
|
|
Help: "Latency statistics for requests",
|
|
Buckets: prometheus.ExponentialBuckets(0.000001, 1.5, 40),
|
|
},
|
|
),
|
|
}
|
|
}
|
|
|
|
func (m *Metrics) collectCacheTTLMetric() {
|
|
m.metricCacheTTL.Set(m.exporter.config.CacheTTL.Seconds())
|
|
}
|
|
|
|
func (m *Metrics) collectCurrentlyCachedMetric() {
|
|
m.metricCurrentlyCached.Set(float64(m.exporter.cache.Count()))
|
|
}
|
|
|
|
func (m *Metrics) collectDatabaseTimestampMetric() {
|
|
timestamp, err := m.exporter.database.Timestamp()
|
|
if err != nil {
|
|
log.Printf("failed to read file timestamp: %v", err)
|
|
return
|
|
}
|
|
|
|
m.metricDatabaseTimestamp.WithLabelValues(timestamp.String()).Set(float64(timestamp.Unix()))
|
|
}
|
|
|
|
func (m *Metrics) collectDatabaseReadyMetric() {
|
|
var dbReady float64
|
|
if m.exporter.database.IsReady() {
|
|
dbReady = 1
|
|
}
|
|
|
|
m.metricDatabaseReady.Set(dbReady)
|
|
}
|