-
Notifications
You must be signed in to change notification settings - Fork 184
/
metrics.go
71 lines (61 loc) · 1.71 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
69
70
71
package auth
import (
"fmt"
"net"
"net/http"
"strconv"
"strings"
"time"
"github.com/datadog/datadog-go/statsd"
)
func newStatsdClient(host string, port int) (*statsd.Client, error) {
client, err := statsd.New(net.JoinHostPort(host, strconv.Itoa(port)))
if err != nil {
return nil, err
}
client.Namespace = "sso_auth."
client.Tags = []string{
"service:sso_auth",
}
return client, nil
}
// GetActionTag returns the tag associated with a route
func GetActionTag(req *http.Request) string {
// only log metrics for these paths and actions
pathToAction := map[string]string{
"/robots.txt": "robots",
"/start": "start",
"/sign_in": "sign_in",
"/sign_out": "sign_out",
"/oauth2/callback": "callback",
"/profile": "profile",
"/validate": "validate",
"/redeem": "redeem",
"/refresh": "refresh",
"/ping": "ping",
}
// get the action from the url path
path := req.URL.Path
if action, ok := pathToAction[path]; ok {
return action
}
if strings.HasPrefix(path, "/static/") {
return "static"
}
return "unknown"
}
// logMetrics logs all metrics surrounding a given request to the metricsWriter
func logRequestMetrics(proxyHost string, req *http.Request, requestDuration time.Duration, status int, StatsdClient *statsd.Client) {
if proxyHost == "" {
proxyHost = "unknown"
}
tags := []string{
fmt.Sprintf("method:%s", req.Method),
fmt.Sprintf("status_code:%d", status),
fmt.Sprintf("status_category:%dxx", status/100),
fmt.Sprintf("proxy_host:%s", proxyHost),
fmt.Sprintf("action:%s", GetActionTag(req)),
}
// TODO: eventually make rates configurable
StatsdClient.Timing("request", requestDuration, tags, 1.0)
}