-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetrics.go
128 lines (102 loc) · 4.11 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package metrics
import (
"net/http"
"strconv"
"strings"
"time"
"github.com/alexfalkowski/go-service/telemetry/metrics"
ss "github.com/alexfalkowski/go-service/transport/strings"
snoop "github.com/felixge/httpsnoop"
"go.opentelemetry.io/otel/metric"
)
// NewHandler for metrics.
func NewHandler(meter metric.Meter) *Handler {
started := metrics.MustInt64Counter(meter, "http_server_started_total", "Total number of RPCs started on the server.")
received := metrics.MustInt64Counter(meter, "http_server_msg_received_total", "Total number of RPC messages received on the server.")
sent := metrics.MustInt64Counter(meter, "http_server_msg_sent_total", "Total number of RPC messages sent by the server.")
handled := metrics.MustInt64Counter(meter, "http_server_handled_total", "Total number of RPCs completed on the server, regardless of success or failure.")
handledHist := metrics.MustFloat64Histogram(meter, "http_server_handling_seconds",
"Histogram of response latency (seconds) of HTTP that had been application-level handled by the server.")
h := &Handler{
started: started, received: received,
sent: sent, handled: handled,
handledHist: handledHist,
}
return h
}
// Handler for metrics.
type Handler struct {
started metric.Int64Counter
received metric.Int64Counter
sent metric.Int64Counter
handled metric.Int64Counter
handledHist metric.Float64Histogram
}
// ServeHTTP for metrics.
func (h *Handler) ServeHTTP(resp http.ResponseWriter, req *http.Request, next http.HandlerFunc) {
service, method := req.URL.Path, strings.ToLower(req.Method)
if ss.IsHealth(service) {
next(resp, req)
return
}
opts := metric.WithAttributes(
serviceAttribute.String(service),
methodAttribute.String(method),
)
start := time.Now()
ctx := req.Context()
h.started.Add(ctx, 1, opts)
h.received.Add(ctx, 1, opts)
m := snoop.CaptureMetricsFn(resp, func(res http.ResponseWriter) { next(res, req.WithContext(ctx)) })
if m.Code >= 200 && m.Code <= 299 {
h.sent.Add(ctx, 1, opts)
}
h.handled.Add(ctx, 1, opts, metric.WithAttributes(statusCodeAttribute.String(strconv.Itoa(m.Code))))
h.handledHist.Record(ctx, time.Since(start).Seconds(), opts)
}
// NewRoundTripper for metrics.
func NewRoundTripper(meter metric.Meter, r http.RoundTripper) *RoundTripper {
started := metrics.MustInt64Counter(meter, "http_client_started_total", "Total number of RPCs started on the client.")
received := metrics.MustInt64Counter(meter, "http_client_msg_received_total", "Total number of RPC messages received on the client.")
sent := metrics.MustInt64Counter(meter, "http_client_msg_sent_total", "Total number of RPC messages sent by the client.")
handled := metrics.MustInt64Counter(meter, "http_client_handled_total", "Total number of RPCs completed on the client, regardless of success or failure.")
handledHist := metrics.MustFloat64Histogram(meter, "http_client_handling_seconds",
"Histogram of response latency (seconds) of HTTP that had been application-level handled by the client.")
rt := &RoundTripper{
started: started, received: received, sent: sent, handled: handled, handledHist: handledHist,
RoundTripper: r,
}
return rt
}
// RoundTripper for metrics.
type RoundTripper struct {
started metric.Int64Counter
received metric.Int64Counter
sent metric.Int64Counter
handled metric.Int64Counter
handledHist metric.Float64Histogram
http.RoundTripper
}
// RoundTrip for metrics.
func (r *RoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
if ss.IsHealth(req.URL.String()) {
return r.RoundTripper.RoundTrip(req)
}
service, method := req.URL.Hostname(), strings.ToLower(req.Method)
start := time.Now()
ctx := req.Context()
opts := metric.WithAttributes(
serviceAttribute.String(service),
methodAttribute.String(method),
)
r.started.Add(ctx, 1, opts)
r.sent.Add(ctx, 1, opts)
resp, err := r.RoundTripper.RoundTrip(req)
if err != nil {
return nil, err
}
r.received.Add(ctx, 1, opts)
r.handled.Add(ctx, 1, opts, metric.WithAttributes(statusCodeAttribute.String(strconv.Itoa(resp.StatusCode))))
r.handledHist.Record(ctx, time.Since(start).Seconds(), opts)
return resp, nil
}