Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prometheus metrics #148

Merged
merged 21 commits into from Apr 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 22 additions & 0 deletions integration_tests/metrics_test.go
Expand Up @@ -10,4 +10,26 @@ var _ = Describe("metrics API endpoint", func() {
resp := doRequest(newRequest("GET", routerAPIURL("/metrics")))
Expect(resp.StatusCode).To(Equal(200))
})

Context("when looking at metrics returned in response", func() {
var (
responseBody string
)

BeforeEach(func() {
resp := doRequest(newRequest("GET", routerAPIURL("/metrics")))
Expect(resp.StatusCode).To(Equal(200))
responseBody = readBody(resp)
})

It("should contain router internal server error metrics", func() {
Skip("Metric is a vector so it is not registered by default")
Expect(responseBody).To(ContainSubstring("router_internal_server_error_count"))
})

It("should contain routing table metrics", func() {
Expect(responseBody).To(ContainSubstring("router_route_reload_count"))
Expect(responseBody).To(ContainSubstring("router_route_reload_error_count"))
})
})
})
2 changes: 2 additions & 0 deletions main.go
Expand Up @@ -88,6 +88,8 @@ func main() {
os.Exit(0)
}

initMetrics()

logInfo(fmt.Sprintf("router: using GOMAXPROCS value of %d", runtime.GOMAXPROCS(0)))

if tlsSkipVerify {
Expand Down
36 changes: 36 additions & 0 deletions metrics.go
@@ -0,0 +1,36 @@
package main

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

var (
internalServerErrorCountMetric = prometheus.NewCounterVec(
prometheus.CounterOpts{
Name: "router_internal_server_error_count",
tlwr marked this conversation as resolved.
Show resolved Hide resolved
Help: "Number of internal server errors encountered by router",
},
[]string{"host"},
)

routeReloadCountMetric = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "router_route_reload_count",
Help: "Number of times routing table has been reloaded",
},
)

routeReloadErrorCountMetric = prometheus.NewCounter(
prometheus.CounterOpts{
Name: "router_route_reload_error_count",
Help: "Number of errors encountered by reloading routing table",
},
)
)

func initMetrics() {
prometheus.MustRegister(internalServerErrorCountMetric)

prometheus.MustRegister(routeReloadCountMetric)
prometheus.MustRegister(routeReloadErrorCountMetric)
}
11 changes: 11 additions & 0 deletions router.go
Expand Up @@ -7,6 +7,8 @@ import (
"sync"
"time"

"github.com/prometheus/client_golang/prometheus"

"github.com/alphagov/router/handlers"
"github.com/alphagov/router/logger"
"github.com/alphagov/router/triemux"
Expand Down Expand Up @@ -78,11 +80,16 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
defer func() {
if r := recover(); r != nil {
logWarn("router: recovered from panic in ServeHTTP:", r)

errorMessage := fmt.Sprintf("panic: %v", r)
err := logger.RecoveredError{ErrorMessage: errorMessage}

logger.NotifySentry(logger.ReportableError{Error: err, Request: req})
rt.logger.LogFromClientRequest(map[string]interface{}{"error": errorMessage, "status": 500}, req)

w.WriteHeader(http.StatusInternalServerError)

internalServerErrorCountMetric.With(prometheus.Labels{"host": req.Host}).Inc()
}
}()
rt.lock.RLock()
Expand All @@ -96,13 +103,17 @@ func (rt *Router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
// create a new proxy mux, load applications (backends) and routes into it, and
// then flip the "mux" pointer in the Router.
func (rt *Router) ReloadRoutes() {
routeReloadCountMetric.Inc()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be nice for these calls to be deferred until the function returns. What do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree 4e1bad2


defer func() {
if r := recover(); r != nil {
logWarn("router: recovered from panic in ReloadRoutes:", r)
logInfo("router: original routes have not been modified")
errorMessage := fmt.Sprintf("panic: %v", r)
err := logger.RecoveredError{ErrorMessage: errorMessage}
logger.NotifySentry(logger.ReportableError{Error: err})

routeReloadErrorCountMetric.Inc()
}
}()

Expand Down