-
Notifications
You must be signed in to change notification settings - Fork 2k
/
metrics_endpoint.go
43 lines (35 loc) · 1.11 KB
/
metrics_endpoint.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
package agent
import (
"net/http"
"sync"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var (
// Only create the prometheus handler once
promHandler http.Handler
promOnce sync.Once
)
// MetricsRequest returns metrics for the agent. Metrics are JSON by default
// but Prometheus is an optional format.
func (s *HTTPServer) MetricsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
if req.Method != "GET" {
return nil, CodedError(405, ErrInvalidMethod)
}
if format := req.URL.Query().Get("format"); format == "prometheus" {
s.prometheusHandler().ServeHTTP(resp, req)
return nil, nil
}
return s.agent.InmemSink.DisplayMetrics(resp, req)
}
func (s *HTTPServer) prometheusHandler() http.Handler {
promOnce.Do(func() {
handlerOptions := promhttp.HandlerOpts{
ErrorLog: s.logger.Named("prometheus_handler").StandardLogger(nil),
ErrorHandling: promhttp.ContinueOnError,
DisableCompression: true,
}
promHandler = promhttp.HandlerFor(prometheus.DefaultGatherer, handlerOptions)
})
return promHandler
}