-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
http_monitoring.go
246 lines (213 loc) · 8.55 KB
/
http_monitoring.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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package diagnostics
import (
"context"
"strconv"
"strings"
"time"
"github.com/valyala/fasthttp"
"go.opencensus.io/stats"
"go.opencensus.io/stats/view"
"go.opencensus.io/tag"
diag_utils "github.com/dapr/dapr/pkg/diagnostics/utils"
)
// To track the metrics for fasthttp using opencensus, this implementation is inspired by
// https://github.com/census-instrumentation/opencensus-go/tree/master/plugin/ochttp
// Tag key definitions for http requests.
var (
httpStatusCodeKey = tag.MustNewKey("status")
httpPathKey = tag.MustNewKey("path")
httpMethodKey = tag.MustNewKey("method")
)
// Default distributions.
var (
defaultSizeDistribution = view.Distribution(1024, 2048, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, 67108864, 268435456, 1073741824, 4294967296)
defaultLatencyDistribution = view.Distribution(1, 2, 3, 4, 5, 6, 8, 10, 13, 16, 20, 25, 30, 40, 50, 65, 80, 100, 130, 160, 200, 250, 300, 400, 500, 650, 800, 1000, 2000, 5000, 10000, 20000, 50000, 100000)
)
type httpMetrics struct {
serverRequestCount *stats.Int64Measure
serverRequestBytes *stats.Int64Measure
serverResponseBytes *stats.Int64Measure
serverLatency *stats.Float64Measure
serverResponseCount *stats.Int64Measure
clientSentBytes *stats.Int64Measure
clientReceivedBytes *stats.Int64Measure
clientRoundtripLatency *stats.Float64Measure
clientCompletedCount *stats.Int64Measure
appID string
enabled bool
}
func newHTTPMetrics() *httpMetrics {
return &httpMetrics{
serverRequestCount: stats.Int64(
"http/server/request_count",
"Number of HTTP requests started in server.",
stats.UnitDimensionless),
serverRequestBytes: stats.Int64(
"http/server/request_bytes",
"HTTP request body size if set as ContentLength (uncompressed) in server.",
stats.UnitBytes),
serverResponseBytes: stats.Int64(
"http/server/response_bytes",
"HTTP response body size (uncompressed) in server.",
stats.UnitBytes),
serverLatency: stats.Float64(
"http/server/latency",
"HTTP request end to end latency in server.",
stats.UnitMilliseconds),
serverResponseCount: stats.Int64(
"http/server/response_count",
"The number of HTTP responses",
stats.UnitDimensionless),
clientSentBytes: stats.Int64(
"http/client/sent_bytes",
"Total bytes sent in request body (not including headers)",
stats.UnitBytes),
clientReceivedBytes: stats.Int64(
"http/client/received_bytes",
"Total bytes received in response bodies (not including headers but including error responses with bodies)",
stats.UnitBytes),
clientRoundtripLatency: stats.Float64(
"http/client/roundtrip_latency",
"Time between first byte of request headers sent to last byte of response received, or terminal error",
stats.UnitMilliseconds),
clientCompletedCount: stats.Int64(
"http/client/completed_count",
"Count of completed requests",
stats.UnitDimensionless),
enabled: false,
}
}
func (h *httpMetrics) IsEnabled() bool {
return h.enabled
}
func (h *httpMetrics) ServerRequestReceived(ctx context.Context, method, path string, contentSize int64) {
if h.enabled {
stats.RecordWithTags(
ctx,
diag_utils.WithTags(appIDKey, h.appID, httpPathKey, path, httpMethodKey, method),
h.serverRequestCount.M(1))
stats.RecordWithTags(
ctx, diag_utils.WithTags(appIDKey, h.appID),
h.serverRequestBytes.M(contentSize))
}
}
func (h *httpMetrics) ServerRequestCompleted(ctx context.Context, method, path, status string, contentSize int64, elapsed float64) {
if h.enabled {
stats.RecordWithTags(
ctx,
diag_utils.WithTags(appIDKey, h.appID, httpPathKey, path, httpMethodKey, method, httpStatusCodeKey, status),
h.serverResponseCount.M(1))
stats.RecordWithTags(
ctx,
diag_utils.WithTags(appIDKey, h.appID, httpPathKey, path, httpMethodKey, method, httpStatusCodeKey, status),
h.serverLatency.M(elapsed))
stats.RecordWithTags(
ctx, diag_utils.WithTags(appIDKey, h.appID),
h.serverResponseBytes.M(contentSize))
}
}
func (h *httpMetrics) ClientRequestStarted(ctx context.Context, method, path string, contentSize int64) {
if h.enabled {
stats.RecordWithTags(
ctx,
diag_utils.WithTags(appIDKey, h.appID, httpPathKey, h.convertPathToMetricLabel(path), httpMethodKey, method),
h.clientSentBytes.M(contentSize))
}
}
func (h *httpMetrics) ClientRequestCompleted(ctx context.Context, method, path, status string, contentSize int64, elapsed float64) {
if h.enabled {
stats.RecordWithTags(
ctx,
diag_utils.WithTags(appIDKey, h.appID, httpPathKey, h.convertPathToMetricLabel(path), httpMethodKey, method, httpStatusCodeKey, status),
h.clientCompletedCount.M(1))
stats.RecordWithTags(
ctx,
diag_utils.WithTags(appIDKey, h.appID, httpPathKey, h.convertPathToMetricLabel(path), httpMethodKey, method, httpStatusCodeKey, status),
h.clientRoundtripLatency.M(elapsed))
stats.RecordWithTags(
ctx, diag_utils.WithTags(appIDKey, h.appID),
h.clientReceivedBytes.M(contentSize))
}
}
func (h *httpMetrics) Init(appID string) error {
h.appID = appID
h.enabled = true
tags := []tag.Key{appIDKey}
return view.Register(
diag_utils.NewMeasureView(h.serverRequestCount, []tag.Key{appIDKey, httpPathKey, httpMethodKey}, view.Count()),
diag_utils.NewMeasureView(h.serverRequestBytes, tags, defaultSizeDistribution),
diag_utils.NewMeasureView(h.serverResponseBytes, tags, defaultSizeDistribution),
diag_utils.NewMeasureView(h.serverLatency, []tag.Key{appIDKey, httpMethodKey, httpPathKey, httpStatusCodeKey}, defaultLatencyDistribution),
diag_utils.NewMeasureView(h.serverResponseCount, []tag.Key{appIDKey, httpMethodKey, httpPathKey, httpStatusCodeKey}, view.Count()),
diag_utils.NewMeasureView(h.clientSentBytes, []tag.Key{appIDKey, httpMethodKey, httpPathKey, httpStatusCodeKey}, defaultSizeDistribution),
diag_utils.NewMeasureView(h.clientReceivedBytes, tags, defaultSizeDistribution),
diag_utils.NewMeasureView(h.clientRoundtripLatency, []tag.Key{appIDKey, httpMethodKey, httpPathKey, httpStatusCodeKey}, defaultLatencyDistribution),
diag_utils.NewMeasureView(h.clientCompletedCount, []tag.Key{appIDKey, httpMethodKey, httpPathKey, httpStatusCodeKey}, view.Count()),
)
}
// FastHTTPMiddleware is the middleware to track http server-side requests.
func (h *httpMetrics) FastHTTPMiddleware(next fasthttp.RequestHandler) fasthttp.RequestHandler {
return func(ctx *fasthttp.RequestCtx) {
reqContentSize := ctx.Request.Header.ContentLength()
if reqContentSize < 0 {
reqContentSize = 0
}
method := string(ctx.Method())
path := h.convertPathToMetricLabel(string(ctx.Path()))
h.ServerRequestReceived(ctx, method, path, int64(reqContentSize))
start := time.Now()
next(ctx)
status := strconv.Itoa(ctx.Response.StatusCode())
elapsed := float64(time.Since(start) / time.Millisecond)
respSize := int64(len(ctx.Response.Body()))
h.ServerRequestCompleted(ctx, method, path, status, respSize, elapsed)
}
}
// convertPathToMetricLabel removes the variant parameters in URL path for low cardinality label space
// For example, it removes {keys} param from /v1/state/statestore/{keys}.
func (h *httpMetrics) convertPathToMetricLabel(path string) string {
if path == "" {
return path
}
p := path
if p[0] == '/' {
p = path[1:]
}
// Split up to 6 delimiters in 'v1/actors/DemoActor/1/timer/name'
parsedPath := strings.SplitN(p, "/", 6)
if len(parsedPath) < 3 {
return path
}
// Replace actor id with {id} for appcallback url - 'actors/DemoActor/1/method/method1'
if parsedPath[0] == "actors" {
parsedPath[2] = "{id}"
return strings.Join(parsedPath, "/")
}
switch parsedPath[1] {
case "state", "secrets":
// state api: Concat 3 items(v1, state, statestore) in /v1/state/statestore/key
// secrets api: Concat 3 items(v1, secrets, keyvault) in /v1/secrets/keyvault/name
return "/" + strings.Join(parsedPath[0:3], "/")
case "actors":
if len(parsedPath) < 5 {
return path
}
// ignore id part
parsedPath[3] = "{id}"
// Concat 5 items(v1, actors, DemoActor, {id}, timer) in /v1/actors/DemoActor/1/timer/name
return "/" + strings.Join(parsedPath[0:5], "/")
}
return path
}