Skip to content

Commit

Permalink
Merge pull request #18 from keratin/metrics
Browse files Browse the repository at this point in the history
add: `GET /metrics` (authenticated)
  • Loading branch information
cainlevy authored Nov 10, 2017
2 parents 5a39206 + c19ed2c commit f2c1bbb
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 9 deletions.
4 changes: 4 additions & 0 deletions api/meta/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package meta
import (
"github.com/keratin/authn-server/api"
"github.com/keratin/authn-server/lib/route"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

func Routes(app *api.App) []*route.HandledRoute {
Expand All @@ -24,5 +25,8 @@ func Routes(app *api.App) []*route.HandledRoute {
route.Get("/stats").
SecuredWith(authentication).
Handle(getStats(app)),
route.Get("/metrics").
SecuredWith(authentication).
Handle(promhttp.Handler()),
}
}
3 changes: 2 additions & 1 deletion api/views/root.ego
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ func Root(w io.Writer) {
<div style="flex: 1 0 auto;">
<ul>
<li><a href="health">Health</a></li>
<li><a href="stats">Stats</a></li>
<li><a href="stats">Active Users</a></li>
<li><a href="metrics">Server Metrics</a></li>
<li><a href="jwks">Keys</a></li>
<li><a href="https://github.com/keratin/authn-server">GitHub</a></li>
<li><a href="https://github.com/keratin/authn-server/blob/master/docs/README.md">Docs</a></li>
Expand Down
22 changes: 21 additions & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ This endpoint is primarily used by backend client libraries to fetch the public

### Service Stats

Visibility: Public
Visibility: Private

`GET /stats`

Expand All @@ -506,3 +506,23 @@ Time periods are labeled in ISO8601 formats:
"monthly": {}
}
}

### Server Stats

Visibility: Private

`GET /metrics`

Returns server stats (memory usage) and traffic stats (counts, timings) that may be used to monitor and alert on server health. The data is formatted for consumption by Prometheus-compatible collectors.

#### Success:

200 Ok

# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 10
[...]
# HELP http_requests_total How many HTTP requests processed, partitioned by name and status code
# TYPE http_requests_total counter
http_requests_total{code="200",name="GET /health"} 97
45 changes: 39 additions & 6 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions glide.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@ import:
- package: github.com/getsentry/raven-go
- package: github.com/sirupsen/logrus
version: ^1.0.3
- package: github.com/prometheus/client_golang
version: ^0.9.0-pre1
subpackages:
- prometheus
- package: github.com/felixge/httpsnoop
41 changes: 41 additions & 0 deletions lib/route/instrument.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package route

import (
"net/http"
"strconv"

"github.com/felixge/httpsnoop"

"github.com/prometheus/client_golang/prometheus"
)

var (
httpRequests = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "http_requests_total",
Help: "How many HTTP requests processed, partitioned by name and status code",
},
[]string{"name", "code"},
)
httpTimings = prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Name: "http_request_times",
Help: "The duration of HTTP requests, partitioned by name",
Buckets: prometheus.ExponentialBuckets(0.001, 10, 5),
},
[]string{"name"},
)
)

func init() {
prometheus.MustRegister(httpRequests)
prometheus.MustRegister(httpTimings)
}

func instrumentRoute(name string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
metrics := httpsnoop.CaptureMetrics(next, w, r)
httpRequests.WithLabelValues(name, strconv.Itoa(metrics.Code)).Inc()
httpTimings.WithLabelValues(name).Observe(float64(metrics.Duration.Seconds()))
})
}
2 changes: 1 addition & 1 deletion lib/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,6 @@ func Attach(router *mux.Router, pathPrefix string, routes ...*HandledRoute) {
PathPrefix(pathPrefix).
Methods(r.verb).
Path(r.tpl).
Handler(r.security(r.handler))
Handler(instrumentRoute(r.verb+" "+r.tpl, r.security(r.handler)))
}
}

0 comments on commit f2c1bbb

Please sign in to comment.