Skip to content
This repository has been archived by the owner on Dec 29, 2020. It is now read-only.

Commit

Permalink
feat(metrics): observe ping times
Browse files Browse the repository at this point in the history
  • Loading branch information
ssube committed Nov 29, 2019
1 parent 327518b commit 0dcda8c
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions postgres/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ var (
},
[]string{"remote"},
)
pingTime = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "ping_time",
Namespace: "adapter",
Subsystem: "connections",
},
[]string{"remote"},
)
totalNewLabels = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "new_total",
Expand Down Expand Up @@ -129,6 +137,7 @@ func init() {
prometheus.MustRegister(curOpenConns)
prometheus.MustRegister(maxOpenConns)
prometheus.MustRegister(labelCacheSize)
prometheus.MustRegister(pingTime)
prometheus.MustRegister(totalNewLabels)
prometheus.MustRegister(totalSkipLabels)
prometheus.MustRegister(totalInvalidSamples)
Expand Down Expand Up @@ -338,20 +347,25 @@ func (c Client) parseMetric(m model.Metric) (string, string, error) {
}

func (c Client) UpdateStats() {
cname := c.Name()
stats := c.db.Stats()
level.Debug(c.logger).Log("msg", "connection stats", "open", stats.OpenConnections)

curIdleConns.WithLabelValues(c.Name()).Set(float64(stats.Idle))
curOpenConns.WithLabelValues(c.Name()).Set(float64(stats.OpenConnections))
curUsedConns.WithLabelValues(c.Name()).Set(float64(stats.InUse))
maxOpenConns.WithLabelValues(c.Name()).Set(float64(stats.MaxOpenConnections))
labelCacheSize.WithLabelValues(c.Name()).Set(float64(c.cache.Len()))
curIdleConns.WithLabelValues(cname).Set(float64(stats.Idle))
curOpenConns.WithLabelValues(cname).Set(float64(stats.OpenConnections))
curUsedConns.WithLabelValues(cname).Set(float64(stats.InUse))
maxOpenConns.WithLabelValues(cname).Set(float64(stats.MaxOpenConnections))
labelCacheSize.WithLabelValues(cname).Set(float64(c.cache.Len()))

ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()

begin := time.Now()
err := c.db.PingContext(ctx)
duration := time.Since(begin).Seconds()
if err != nil {
level.Error(c.logger).Log("msg", "error pinging server", "err", err)
}

pingTime.WithLabelValues(cname).Observe(duration)
}

0 comments on commit 0dcda8c

Please sign in to comment.