Skip to content

Commit

Permalink
use proper grouping for prom metrics
Browse files Browse the repository at this point in the history
  • Loading branch information
arriven committed Mar 26, 2022
1 parent 325cf35 commit d1763ad
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/utils/metrics/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func InitOrFail(ctx context.Context, prometheusOn bool, prometheusPushGateways,

// Init prometheus counters.
func Init(clientID, country string) {
constLabels := prometheus.Labels{ClientIDLabel: clientID}
constLabels := prometheus.Labels{}
if country != "" {
constLabels[CountryLabel] = country
}
Expand Down Expand Up @@ -262,7 +262,7 @@ func getTLSConfig() (*tls.Config, error) {
}

func pushMetrics(ctx context.Context, clientID string, gateways []string) {
jobName := utils.GetEnvStringDefault("PROMETHEUS_JOB_NAME", clientID)
jobName := utils.GetEnvStringDefault("PROMETHEUS_JOB_NAME", "db1000n_default_add")
gateway := gateways[rand.Intn(len(gateways))] //nolint:gosec // Cryptographically secure random not required
tickerPeriod := utils.GetEnvDurationDefault("PROMETHEUS_PUSH_PERIOD", time.Minute)
ticker := time.NewTicker(tickerPeriod)
Expand All @@ -287,18 +287,18 @@ func pushMetrics(ctx context.Context, clientID string, gateways []string) {
return
}

pusher := push.New(gateway, jobName).Gatherer(prometheus.DefaultGatherer).Client(httpClient).BasicAuth(user, password)
pusher := push.New(gateway, jobName).Gatherer(prometheus.DefaultGatherer).Grouping(ClientIDLabel, clientID).Client(httpClient).BasicAuth(user, password)

for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if err := pusher.Push(); err != nil {
if err := pusher.Add(); err != nil {
log.Println("Can't push metrics to gateway, trying to change gateway")

gateway = gateways[rand.Intn(len(gateways))] //nolint:gosec // Cryptographically secure random not required
pusher = push.New(gateway, jobName).Gatherer(prometheus.DefaultGatherer).Client(httpClient).BasicAuth(user, password)
pusher = push.New(gateway, jobName).Gatherer(prometheus.DefaultGatherer).Grouping(ClientIDLabel, clientID).Client(httpClient).BasicAuth(user, password)
}
}
}
Expand Down

0 comments on commit d1763ad

Please sign in to comment.