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
24 changes: 24 additions & 0 deletions handlers/backend_handler.go
Expand Up @@ -11,6 +11,8 @@ import (
"strings"
"time"

"github.com/prometheus/client_golang/prometheus"

"github.com/alphagov/router/logger"
)

Expand Down Expand Up @@ -126,8 +128,27 @@ func closeBody(resp *http.Response) {
}

func (bt *backendTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
var (
responseCode int
startTime = time.Now()
)

BackendHandlerRequestCountMetric.With(prometheus.Labels{
"backend_id": bt.backendID,
}).Inc()

defer func() {
durationSeconds := time.Since(startTime).Seconds()

BackendHandlerResponseDurationSecondsMetric.With(prometheus.Labels{
"backend_id": bt.backendID,
"response_code": fmt.Sprintf("%d", responseCode),
}).Add(durationSeconds)
}()

resp, err = bt.wrapped.RoundTrip(req)
if err == nil {
responseCode = resp.StatusCode
populateViaHeader(resp.Header, fmt.Sprintf("%d.%d", resp.ProtoMajor, resp.ProtoMinor))
} else {
// Log the error (deferred to allow special case error handling to add/change details)
Expand All @@ -140,15 +161,18 @@ func (bt *backendTransport) RoundTrip(req *http.Request) (resp *http.Response, e
if netErr, ok := err.(net.Error); ok {
if netErr.Timeout() {
logDetails["status"] = 504
responseCode = 504
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a shame we're not making use of the constants provided in the http package here. Would be a nice change for another PR.

Copy link
Contributor Author

@tlwr tlwr Mar 20, 2020

Choose a reason for hiding this comment

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

I think this could be within the purview of this PR

Edit: This is done in cac6eec

return newErrorResponse(504), nil
}
}
if strings.Contains(err.Error(), "connection refused") {
logDetails["status"] = 502
responseCode = 502
return newErrorResponse(502), nil
}

// 500 for all other errors
responseCode = 500
return newErrorResponse(500), nil
}
return
Expand Down
94 changes: 94 additions & 0 deletions handlers/backend_handler_test.go
Expand Up @@ -11,6 +11,9 @@ import (
. "github.com/onsi/gomega"
"github.com/onsi/gomega/ghttp"

"github.com/prometheus/client_golang/prometheus"
promtest "github.com/prometheus/client_golang/prometheus/testutil"

"github.com/alphagov/router/handlers"
log "github.com/alphagov/router/logger"
)
Expand Down Expand Up @@ -134,4 +137,95 @@ var _ = Describe("Backend handler", func() {
})
})
})

Context("metrics", func() {
var (
beforeRequestCountMetric float64
beforeResponseDurationSecondsMetric float64
)

var measureRequestCount = func() float64 {
return promtest.ToFloat64(
handlers.BackendHandlerRequestCountMetric.With(prometheus.Labels{
"backend_id": "backend-metrics",
}),
)
}

var measureResponseDurationSeconds = func(responseCode string) float64 {
return promtest.ToFloat64(
handlers.BackendHandlerResponseDurationSecondsMetric.With(prometheus.Labels{
"backend_id": "backend-metrics",
"response_code": responseCode,
}),
)
}

BeforeEach(func() {
router = handlers.NewBackendHandler(
"backend-metrics",
backendURL,
timeout, timeout,
logger,
)

beforeRequestCountMetric = measureRequestCount()
})

Context("when the request/response succeeds", func() {
BeforeEach(func() {
backend.AppendHandlers(func(rw http.ResponseWriter, r *http.Request) {
time.Sleep(200 * time.Millisecond)
rw.WriteHeader(http.StatusOK)
})

beforeResponseDurationSecondsMetric = measureResponseDurationSeconds("200")

router.ServeHTTP(
rw,
httptest.NewRequest("GET", backendURL.String(), nil),
)
})

It("should count the number of requests", func() {
Expect(
measureRequestCount() - beforeRequestCountMetric,
).To(Equal(float64(1)))
})

It("should record the duration of proxied responses", func() {
Expect(
measureResponseDurationSeconds("200") - beforeResponseDurationSecondsMetric,
).To(BeNumerically("~", 0.2, 0.1))
})
})

Context("when the request times out", func() {
BeforeEach(func() {
backend.AppendHandlers(func(rw http.ResponseWriter, r *http.Request) {
time.Sleep(timeout * 2)
rw.WriteHeader(http.StatusOK)
})

beforeResponseDurationSecondsMetric = measureResponseDurationSeconds("504")

router.ServeHTTP(
rw,
httptest.NewRequest("GET", backendURL.String(), nil),
)
})

It("should count the number of requests", func() {
Expect(
measureRequestCount() - beforeRequestCountMetric,
).To(Equal(float64(1)))
})

It("should record the duration of proxied responses", func() {
Expect(
measureResponseDurationSeconds("504") - beforeResponseDurationSecondsMetric,
).To(BeNumerically("~", 1.0, 0.1))
})
})
})
})