Skip to content

Commit cb78c3b

Browse files
committed
Introduces Prometheus Pushgateway for metrics persistence
1 parent 44b0f61 commit cb78c3b

4 files changed

Lines changed: 38 additions & 2 deletions

File tree

internal/config/keys.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ const METRICS_PROMETHEUS_PORT = "metrics.prometheus.port"
5959
// Prometheus IP address / hostname
6060
const METRICS_PROMETHEUS_HOST = "metrics.prometheus.host"
6161

62+
// Port used by optional Prometheus Pushgateway
63+
const METRICS_PROMETHEUS_PUSHGATEWAY_PORT = "metrics.prometheus.pushgateway.port"
64+
65+
// Prometheus optional Pushgateway IP address / hostname
66+
const METRICS_PROMETHEUS_PUSHGATEWAY_HOST = "metrics.prometheus.pushgateway.host"
67+
6268
// Interval (in seconds) for metrics retriever
6369
const METRICS_RETRIEVER_INTERVAL = "metrics.retriever.interval"
6470

internal/metrics/metrics.go

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ package metrics
22

33
import (
44
"fmt"
5+
"github.com/prometheus/client_golang/prometheus/push"
56
"log"
7+
"time"
68

79
"net/http"
810

@@ -17,7 +19,7 @@ import (
1719
var Enabled bool
1820
var registry = prometheus.NewRegistry()
1921
var ScrapingHandler http.Handler = nil
20-
var durationBuckets = []float64{0.002, 0.005, 0.010, 0.02, 0.03, 0.05, 0.1, 0.15, 0.3, 0.6, 1.0}
22+
var durationBuckets = prometheus.ExponentialBuckets(0.01, 2, 15)
2123

2224
const (
2325
COMPLETIONS = "completed_count"
@@ -105,6 +107,31 @@ func Init() {
105107
ScrapingHandler = promhttp.HandlerFor(registry, promhttp.HandlerOpts{
106108
EnableOpenMetrics: true})
107109

110+
pushgatewayHost := config.GetString(config.METRICS_PROMETHEUS_PUSHGATEWAY_HOST, "")
111+
if pushgatewayHost != "" {
112+
113+
pushgatewayPort := config.GetInt(config.METRICS_PROMETHEUS_PUSHGATEWAY_PORT, 9091)
114+
hostport := fmt.Sprintf("http://%s:%d", pushgatewayHost, pushgatewayPort)
115+
116+
log.Println("Using Prometheus Pushgateway at:", hostport)
117+
118+
go func(pushgatewayUrl string) {
119+
ticker := time.NewTicker(30 * time.Second)
120+
121+
for {
122+
select {
123+
case <-ticker.C:
124+
// Push the entire registry in one go
125+
err := push.New(pushgatewayUrl, "serverledge").Gatherer(registry).Push()
126+
if err != nil {
127+
log.Printf("Could not push metrics: %v", err)
128+
}
129+
}
130+
}
131+
}(hostport)
132+
133+
}
134+
108135
go MetricsRetriever()
109136
}
110137

prometheus.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ scrape_configs:
1111
# scheme defaults to 'http'.
1212

1313
static_configs:
14-
- targets: ["172.17.0.1:1323", "172.17.0.1:1324"]
14+
#- targets: ["172.17.0.1:1323", "172.17.0.1:1324"]
15+
- targets: ["172.17.0.1:9091"]
1516

1617

scripts/start-pushgateway.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/sh
2+
docker run -d -p 9091:9091 --name pushgateway prom/pushgateway

0 commit comments

Comments
 (0)