mirror of
https://github.com/kemko/nomad.git
synced 2026-01-08 11:25:41 +03:00
Merge pull request #10140 from hashicorp/b-gh-10070
agent: return req error if prometheus metrics are disabled.
This commit is contained in:
@@ -22,6 +22,12 @@ func (s *HTTPServer) MetricsRequest(resp http.ResponseWriter, req *http.Request)
|
||||
}
|
||||
|
||||
if format := req.URL.Query().Get("format"); format == "prometheus" {
|
||||
|
||||
// Only return Prometheus formatted metrics if the user has enabled
|
||||
// this functionality.
|
||||
if !s.agent.config.Telemetry.PrometheusMetrics {
|
||||
return nil, CodedError(http.StatusUnsupportedMediaType, "Prometheus is not enabled")
|
||||
}
|
||||
s.prometheusHandler().ServeHTTP(resp, req)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
@@ -29,6 +29,40 @@ func TestHTTP_MetricsWithIllegalMethod(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestHTTP_MetricsPrometheusDisabled(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
t.Parallel()
|
||||
httpTest(t, func(c *Config) { c.Telemetry.PrometheusMetrics = false }, func(s *TestAgent) {
|
||||
req, err := http.NewRequest("GET", "/v1/metrics?format=prometheus", nil)
|
||||
assert.Nil(err)
|
||||
|
||||
resp, err := s.Server.MetricsRequest(nil, req)
|
||||
assert.Nil(resp)
|
||||
assert.Error(err, "Prometheus is not enabled")
|
||||
})
|
||||
}
|
||||
|
||||
func TestHTTP_MetricsPrometheusEnabled(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
t.Parallel()
|
||||
httpTest(t, nil, func(s *TestAgent) {
|
||||
req, err := http.NewRequest("GET", "/v1/metrics?format=prometheus", nil)
|
||||
assert.Nil(err)
|
||||
respW := httptest.NewRecorder()
|
||||
|
||||
resp, err := s.Server.MetricsRequest(respW, req)
|
||||
assert.Nil(resp)
|
||||
assert.Nil(err)
|
||||
|
||||
// Ensure the response body is not empty and that it contains something
|
||||
// that looks like a metric we expect.
|
||||
assert.NotNil(respW.Body)
|
||||
assert.Contains(respW.Body.String(), "HELP go_gc_duration_seconds")
|
||||
})
|
||||
}
|
||||
|
||||
func TestHTTP_Metrics(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user