-
Notifications
You must be signed in to change notification settings - Fork 184
/
metrics.go
68 lines (60 loc) · 1.83 KB
/
metrics.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
package proxy
import (
"fmt"
"net"
"net/http"
"strconv"
"time"
"github.com/datadog/datadog-go/statsd"
)
// newStatsdClient creates and returns a statsd client on a host and port that is namespaced to 'sso_proxy'
func newStatsdClient(opts *Options) (*statsd.Client, error) {
client, err := statsd.New(net.JoinHostPort(opts.StatsdHost, strconv.Itoa(opts.StatsdPort)))
if err != nil {
return nil, err
}
client.Namespace = "sso_proxy."
client.Tags = []string{
"service:sso_proxy",
}
return client, nil
}
// GetActionTag returns the action triggered by an http.Request .
func GetActionTag(req *http.Request) string {
// only log metrics for these paths and actions
pathToAction := map[string]string{
"/favicon.ico": "favicon",
"/oauth2/sign_out": "sign_out",
"/oauth2/callback": "callback",
"/oauth2/auth": "auth",
"/ping": "ping",
"/robots.txt": "robots",
}
// get the action from the url path
path := req.URL.Path
if action, ok := pathToAction[path]; ok {
return action
}
return "proxy"
}
// logMetrics logs all metrics surrounding a given request to the metricsWriter
func logRequestMetrics(req *http.Request, requestDuration time.Duration, status int, StatsdClient *statsd.Client) {
// Normalize proxyHost for a) invalid requests or b) LB health checks to
// avoid polluting the proxy_host tag's value space
proxyHost := req.Host
if status == statusInvalidHost {
proxyHost = "_unknown"
}
if req.URL.Path == "/ping" {
proxyHost = "_healthcheck"
}
tags := []string{
fmt.Sprintf("method:%s", req.Method),
fmt.Sprintf("status_code:%d", status),
fmt.Sprintf("status_category:%dxx", status/100),
fmt.Sprintf("action:%s", GetActionTag(req)),
fmt.Sprintf("proxy_host:%s", proxyHost),
}
// TODO: eventually make rates configurable
StatsdClient.Timing("request", requestDuration, tags, 1.0)
}