Enable compression / automatic Mime-Type detection for Prometheus endpoint

This commit is contained in:
Pierre Souchay 2018-04-09 13:16:03 +02:00
parent 2cccb8f36a
commit c164ee7dbd
1 changed files with 14 additions and 4 deletions

View File

@ -77,6 +77,17 @@ func (s *HTTPServer) AgentSelf(resp http.ResponseWriter, req *http.Request) (int
}, nil }, nil
} }
// enablePrometheusOutput will look for Prometheus mime-type or format Query parameter the same way as Nomad
func enablePrometheusOutput(req *http.Request) bool {
if contentType := req.Header.Get("Accept"); contentType == "text/plain; version=0.0.4; charset=utf-8" {
return true
}
if format := req.URL.Query().Get("format"); format == "prometheus" {
return true
}
return false
}
func (s *HTTPServer) AgentMetrics(resp http.ResponseWriter, req *http.Request) (interface{}, error) { func (s *HTTPServer) AgentMetrics(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
// Fetch the ACL token, if any, and enforce agent policy. // Fetch the ACL token, if any, and enforce agent policy.
var token string var token string
@ -88,16 +99,15 @@ func (s *HTTPServer) AgentMetrics(resp http.ResponseWriter, req *http.Request) (
if rule != nil && !rule.AgentRead(s.agent.config.NodeName) { if rule != nil && !rule.AgentRead(s.agent.config.NodeName) {
return nil, acl.ErrPermissionDenied return nil, acl.ErrPermissionDenied
} }
if format := req.URL.Query().Get("format"); format == "prometheus" { if enablePrometheusOutput(req) {
if s.agent.config.TelemetryPrometheusRetentionTime.Nanoseconds() < 1 { if s.agent.config.TelemetryPrometheusRetentionTime.Nanoseconds() < 1 {
resp.WriteHeader(http.StatusUnsupportedMediaType) resp.WriteHeader(http.StatusUnsupportedMediaType)
fmt.Fprint(resp, "Prometheus is not enable since its retention time is not positive") fmt.Fprint(resp, "Prometheus is not enable since its retention time is not positive")
return nil, nil return nil, nil
} }
handlerOptions := promhttp.HandlerOpts{ handlerOptions := promhttp.HandlerOpts{
ErrorLog: s.agent.logger, ErrorLog: s.agent.logger,
ErrorHandling: promhttp.ContinueOnError, ErrorHandling: promhttp.ContinueOnError,
DisableCompression: true,
} }
handler := promhttp.HandlerFor(prometheus.DefaultGatherer, handlerOptions) handler := promhttp.HandlerFor(prometheus.DefaultGatherer, handlerOptions)