-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IDM-410: Prometheus Metrics for the Secure token Service (#2)
* health: Adds new prometheus metrics and metrics endpoint (ory#827) Signed-off-by: Dmitry Dolbik <dolbik@gmail.com> * IDM-410 Prometheus Metrics for the Secure token Service
- Loading branch information
1 parent
f08f107
commit b761372
Showing
11 changed files
with
297 additions
and
138 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package prometheus | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
) | ||
|
||
// Metrics prototypes | ||
// Example: | ||
// Counter *prometheus.CounterVec | ||
// ResponseTime *prometheus.HistogramVec | ||
type Metrics struct { | ||
Counter *prometheus.CounterVec | ||
ResponseTime *prometheus.HistogramVec | ||
} | ||
|
||
// 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", | ||
ConstLabels: labels, | ||
}, | ||
[]string{"endpoint"}, | ||
), | ||
} | ||
prometheus.Register(pm.Counter) | ||
prometheus.Register(pm.ResponseTime) | ||
return pm | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package prometheus | ||
|
||
import ( | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type MetricsManager struct { | ||
prometheusMetrics *Metrics | ||
} | ||
|
||
func NewMetricsManager(version, hash, buildTime string) *MetricsManager { | ||
return &MetricsManager{ | ||
prometheusMetrics: NewMetrics(version, hash, buildTime), | ||
} | ||
} | ||
|
||
// 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) | ||
// 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()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters