Skip to content

Commit

Permalink
Revert "refactoring prometheus endpoint"
Browse files Browse the repository at this point in the history
This reverts commit 2f852e9.
  • Loading branch information
chelseakomlo committed Sep 12, 2017
1 parent bd3cfbd commit ac8c80b
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 19 deletions.
27 changes: 27 additions & 0 deletions command/agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import (
"github.com/NYTimes/gziphandler"
"github.com/hashicorp/nomad/helper/tlsutil"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
"github.com/ugorji/go/codec"
)

Expand Down Expand Up @@ -83,6 +85,12 @@ func NewHTTPServer(agent *Agent, config *Config) (*HTTPServer, error) {
// Handle requests with gzip compression
go http.Serve(ln, gziphandler.GzipHandler(mux))

// Register Prometheus separately as this requires uncompressed responses
srv.registerPrometheusHandler()

// Handle requests without gzip compression
go http.Serve(ln, mux)

return srv, nil
}

Expand All @@ -105,6 +113,12 @@ func newScadaHttp(agent *Agent, list net.Listener) *HTTPServer {
// Handle requests with gzip compression
go http.Serve(list, gziphandler.GzipHandler(mux))

// Register Prometheus separately as this requires uncompressed responses
srv.registerPrometheusHandler()

// Handle requests without gzip compression
go http.Serve(list, mux)

return srv
}

Expand Down Expand Up @@ -133,6 +147,19 @@ func (s *HTTPServer) Shutdown() {
}
}

// Create a prometheus handler and register it separately, as other requests
// use gzip compression
func (s *HTTPServer) registerPrometheusHandler() {
handlerOptions := promhttp.HandlerOpts{
ErrorLog: s.logger,
ErrorHandling: promhttp.ContinueOnError,
DisableCompression: true,
}

handler := promhttp.HandlerFor(prometheus.DefaultGatherer, handlerOptions)
s.mux.Handle("/metrics", handler)
}

// registerHandlers is used to attach our handlers to the mux
func (s *HTTPServer) registerHandlers(enableDebug bool) {
s.mux.HandleFunc("/v1/jobs", s.wrap(s.JobsRequest))
Expand Down
22 changes: 3 additions & 19 deletions command/agent/metrics_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,13 @@ package agent

import (
"net/http"

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)

// 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 req.Method == "GET" {
return s.agent.InmemSink.DisplayMetrics(resp, req)
}

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)
return nil, CodedError(405, ErrInvalidMethod)
}

0 comments on commit ac8c80b

Please sign in to comment.