From ce97e9233b79fa815949b6edfad02682b91633eb Mon Sep 17 00:00:00 2001 From: Chelsea Holland Komlo Date: Sun, 10 Sep 2017 14:43:36 +0000 Subject: [PATCH] refactoring prometheus endpoint --- command/agent/metrics_endpoint.go | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/command/agent/metrics_endpoint.go b/command/agent/metrics_endpoint.go index 8f390236cdfd..d8ae838d55f6 100644 --- a/command/agent/metrics_endpoint.go +++ b/command/agent/metrics_endpoint.go @@ -2,12 +2,28 @@ package agent import ( "net/http" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/promhttp" ) // MetricsRequest returns metrics in JSON format func (s *HTTPServer) MetricsRequest(resp http.ResponseWriter, req *http.Request) (interface{}, error) { - if req.Method == "GET" { - return s.agent.InmemSink.DisplayMetrics(resp, req) + if req.Method != "GET" { + return nil, CodedError(405, ErrInvalidMethod) } - return nil, CodedError(405, ErrInvalidMethod) + + if format := req.URL.Query().Get("format"); format == "prometheus" { + handlerOptions := promhttp.HandlerOpts{ + ErrorLog: s.logger, + ErrorHandling: promhttp.ContinueOnError, + DisableCompression: true, + } + + handler := promhttp.HandlerFor(prometheus.DefaultGatherer, handlerOptions) + handler.ServeHTTP(resp, req) + return nil, nil + } + + return s.agent.InmemSink.DisplayMetrics(resp, req) }