-
Notifications
You must be signed in to change notification settings - Fork 38
/
request.go
95 lines (78 loc) · 2.67 KB
/
request.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
package telemetry
import (
"net/http"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/metric"
semconv "go.opentelemetry.io/otel/semconv/v1.20.0"
"go.opentelemetry.io/otel/semconv/v1.20.0/httpconv"
"go.uber.org/zap"
)
type RequestTelemetry struct {
requestCounter metric.Int64Counter
}
const (
RequestTelemetryName = "github.com/transcom/mymove/request"
RequestTelemetryVersion = "0.1"
)
// NewRequestTelemetry provides a way for the request logger to
// provide stats. If we want accurate request counts with dimensions,
// this seems to be the best way to do it
//
// # According to the cloudwatch concepts documentation
//
// https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch_concepts.html
//
// CloudWatch treats each unique combination of dimensions as a
// separate metric, even if the metrics have the same metric name.
//
// This means if we try to use the "Sample count" statistic in cloud
// watch, it will count across all dimensions. It doesn't seem
// possible to get the count without dimensions.
//
// Increment a request count with the same dimensions
func NewRequestTelemetry(logger *zap.Logger) *RequestTelemetry {
meterProvider := otel.GetMeterProvider()
requestMeter := meterProvider.Meter(RequestTelemetryName,
metric.WithInstrumentationVersion(RequestTelemetryVersion))
requestCounter, err := requestMeter.Int64Counter("http.server.request_count",
metric.WithDescription("Count of http requests"),
)
if err != nil {
logger.Error("Error registering request counter", zap.Error(err))
return nil
}
return &RequestTelemetry{
requestCounter: requestCounter,
}
}
var allowedHTTPRequestAttributes = map[attribute.Key]bool{
semconv.HTTPMethodKey: true,
semconv.HTTPRouteKey: true,
semconv.HTTPSchemeKey: true,
semconv.HTTPStatusCodeKey: true,
semconv.NetHostNameKey: true,
}
func allowedHTTPRequestAttributeFilter(kv attribute.KeyValue) bool {
_, ok := allowedHTTPRequestAttributes[kv.Key]
return ok
}
func (rt *RequestTelemetry) IncrementRequestCount(r *http.Request, routePattern string, statusCode int) {
serverAttributes := httpconv.ServerRequest(r.Host, r)
metricAttributes := []attribute.KeyValue{}
for i := range serverAttributes {
attr := serverAttributes[i]
if allowedHTTPRequestAttributeFilter(attr) {
metricAttributes = append(metricAttributes, attr)
}
}
if routePattern != "" {
metricAttributes = append(metricAttributes, semconv.HTTPRoute(routePattern))
}
if statusCode > 0 {
metricAttributes = append(metricAttributes,
semconv.HTTPStatusCode(statusCode))
}
o := metric.WithAttributes(metricAttributes...)
rt.requestCounter.Add(r.Context(), 1, o)
}