/
statsd.go
84 lines (73 loc) · 2.51 KB
/
statsd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// This file is part of graze/golang-service
//
// Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//
// license: https://github.com/graze/golang-service/blob/master/LICENSE
// link: https://github.com/graze/golang-service
package handlers
import (
"net/http"
"net/url"
"strconv"
"time"
"github.com/DataDog/datadog-go/statsd"
"github.com/graze/golang-service/metrics"
)
type statsdHandler struct {
statsd *statsd.Client
handler http.Handler
}
// ServeHTTP does the actual handling of HTTP requests by wrapping the request in a logger
func (h statsdHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
LogServeHTTP(w, req, h.handler, h.writeLog)
}
// writeLog writes the log do the statsd client from a statsdHandler
func (h statsdHandler) writeLog(w LoggingResponseWriter, req *http.Request, url url.URL, ts time.Time, dur time.Duration, status, size int) {
writeStatsdLog(h.statsd, req, url, ts, dur, status, size)
}
// writeStatsdLog send the response time and a counter for each request to statsd
func writeStatsdLog(w *statsd.Client, req *http.Request, url url.URL, ts time.Time, dur time.Duration, status, size int) {
uri := uriPath(req, url)
tags := []string{
"endpoint:" + uri,
"statusCode:" + strconv.Itoa(status),
"method:" + req.Method,
"protocol:" + req.Proto,
}
w.Timing("request.response_time", dur, tags, 1)
w.Incr("request.count", tags, 1)
}
// StatsdIoHandler returns a http.Handler that wraps h and logs request to statsd
//
// Example:
//
// r := mux.NewRouter()
// r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("This is a catch-all route"))
// })
// c, err := statsd.New("127.0.0.1:8125")
// loggedRouter := handlers.StatsdHandler(c, r)
// http.ListenAndServe(":1123", loggedRouter)
//
func StatsdIoHandler(out *statsd.Client, h http.Handler) http.Handler {
return statsdHandler{out, h}
}
// StatsdHandler returns a handlers.StatsdHandler to write request and response informtion to statsd
//
// Usage:
// r := mux.NewRouter()
// r.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// w.Write([]byte("This is a catch-all route"))
// })
// loggedRouter := handlers.StatsdHandler(r)
// http.ListenAndServe(":1123", loggedRouter)
func StatsdHandler(h http.Handler) http.Handler {
client, err := metrics.GetStatsdFromEnv()
if err != nil {
panic(err)
}
return StatsdIoHandler(client, h)
}