/
handler_telemetry.go
64 lines (52 loc) · 2.08 KB
/
handler_telemetry.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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package api
import (
"net/http"
"strconv"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/DataDog/datadog-agent/pkg/telemetry"
)
var (
apiRequests = telemetry.NewCounterWithOpts("", "api_requests",
[]string{"handler", "status", "forwarded"}, "Counter of requests made to the cluster agent API.",
telemetry.Options{NoDoubleUnderscoreSep: true})
apiElapsed = telemetry.NewHistogramWithOpts("", "api_elapsed",
[]string{"handler", "status", "forwarded"}, "Poll duration distribution by config provider (in seconds).",
prometheus.DefBuckets,
telemetry.Options{NoDoubleUnderscoreSep: true})
)
// TelemetryHandler provides a http handler and emits requests telemetry for it.
type TelemetryHandler struct {
handlerName string
handler func(w http.ResponseWriter, r *http.Request)
}
// WithTelemetryWrapper returns a http handler function that emits telemetry.
func WithTelemetryWrapper(handlerName string, handler func(w http.ResponseWriter, r *http.Request)) func(w http.ResponseWriter, r *http.Request) {
th := TelemetryHandler{
handlerName: handlerName,
handler: handler,
}
return th.handle
}
func (t *TelemetryHandler) handle(w http.ResponseWriter, r *http.Request) {
t.handler(&telemetryWriterWrapper{ResponseWriter: w, handlerName: t.handlerName, startTime: time.Now()}, r)
}
// Could be made generic, overwite http.ResponseWriter/WriteHeader
type telemetryWriterWrapper struct {
http.ResponseWriter
handlerName string
startTime time.Time
}
func (w *telemetryWriterWrapper) WriteHeader(statusCode int) {
w.ResponseWriter.WriteHeader(statusCode)
forwarded := w.Header().Get(respForwarded)
if forwarded == "" {
forwarded = "false"
}
apiElapsed.Observe(time.Since(w.startTime).Seconds(), w.handlerName, strconv.Itoa(statusCode), forwarded)
apiRequests.Inc(w.handlerName, strconv.Itoa(statusCode), forwarded)
}