Skip to content

Commit

Permalink
[Prometheus] Add new prometheus metrics and metrics endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
ddolbik committed Apr 27, 2018
1 parent 7c2cc77 commit 3bf6a11
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 35 deletions.
67 changes: 36 additions & 31 deletions metrics/prometheus/metrics.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,42 @@
package prometheus

import "github.com/prometheus/client_golang/prometheus"

type Metrics struct {
Counter *prometheus.CounterVec
ResponseTime *prometheus.HistogramVec
}
// Metrics prototypes
// Example:
// Counter *prometheus.CounterVec
// ResponseTime *prometheus.HistogramVec
type Metrics struct{}

// Method for creation new custom Prometheus metrics
// Example:
// pm := &Metrics{
// Counter: prometheus.NewCounterVec(
// prometheus.CounterOpts{
// Name: "servicename_requests_total",
// Help: "Description",
// ConstLabels: map[string]string{
// "version": version,
// "hash": hash,
// "buildTime": buildTime,
// },
// },
// []string{"endpoint"},
// ),
// ResponseTime: prometheus.NewHistogramVec(
// prometheus.HistogramOpts{
// Name: "servicename_response_time_seconds",
// Help: "Description",
// ConstLabels: map[string]string{
// "version": version,
// "hash": hash,
// "buildTime": buildTime,
// },
// },
// []string{"endpoint"},
// ),
// }
// prometheus.Register(pm.Counter)
// prometheus.Register(pm.ResponseTime)
func NewMetrics(version, hash, buildTime string) *Metrics {
labels := map[string]string{
"version": version,
"hash": hash,
"buildTime": buildTime,
}
pm := &Metrics{
Counter: prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "sts_requests_total",
Help: "Secure token service requests served per endpoint",
ConstLabels: labels,
},
[]string{"endpoint"},
),
ResponseTime: prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "sts_response_time_seconds",
Help: "Secure token service response time served per endpoint",
ConstLabels: labels,
},
[]string{"endpoint"},
),
}
prometheus.Register(pm.Counter)
prometheus.Register(pm.ResponseTime)
pm := &Metrics{}
return pm
}
12 changes: 8 additions & 4 deletions metrics/prometheus/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package prometheus

import (
"net/http"
"time"
)

type MetricsManager struct {
Expand All @@ -15,9 +14,14 @@ func NewMetricsManager(version, hash, buildTime string) *MetricsManager {
}
}

// Main middleware method to collect metrics for Prometheus.
// Example:
// start := time.Now()
// next(rw, r)
// Request counter metric
// pmm.prometheusMetrics.Counter.WithLabelValues(r.URL.Path).Inc()
// Response time metric
// pmm.prometheusMetrics.ResponseTime.WithLabelValues(r.URL.Path).Observe(time.Since(start).Seconds())
func (pmm *MetricsManager) ServeHTTP(rw http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
start := time.Now()
next(rw, r)
pmm.prometheusMetrics.Counter.WithLabelValues(r.URL.Path).Inc()
pmm.prometheusMetrics.ResponseTime.WithLabelValues(r.URL.Path).Observe(time.Since(start).Seconds())
}

0 comments on commit 3bf6a11

Please sign in to comment.