Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

agent: return req error if prometheus metrics are disabled. #10140

Merged
merged 4 commits into from
Mar 10, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 1.1.0 (Unreleased)

BUG FIXES:
* agent: Only allow querying Prometheus formatted metrics if Prometheus is enabled within the config [[GH-10140](https://github.com/hashicorp/nomad/pull/10140)]
* api: Added missing devices block to AllocatedTaskResources [[GH-10064](https://github.com/hashicorp/nomad/pull/10064)]
* cli: Fixed a bug where non-int proxy port would panic CLI [[GH-10072](https://github.com/hashicorp/nomad/issues/10072)]
* cli: Fixed a bug where `nomad operator debug` incorrectly parsed https Consul API URLs. [[GH-10082](https://github.com/hashicorp/nomad/pull/10082)]
Expand Down
6 changes: 6 additions & 0 deletions api/internal/testutil/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type TestServerConfig struct {
Client *ClientConfig `json:"client,omitempty"`
Vault *VaultConfig `json:"vault,omitempty"`
ACL *ACLConfig `json:"acl,omitempty"`
Telemetry *Telemetry `json:"telemetry,omitempty"`
DevMode bool `json:"-"`
Stdout, Stderr io.Writer `json:"-"`
}
Expand Down Expand Up @@ -90,6 +91,11 @@ type ACLConfig struct {
Enabled bool `json:"enabled"`
}

// Telemetry is used to configure the Nomad telemetry setup.
type Telemetry struct {
PrometheusMetrics bool `json:"prometheus_metrics"`
}

// ServerConfigCallback is a function interface which can be
// passed to NewTestServerConfig to modify the server config.
type ServerConfigCallback func(c *TestServerConfig)
Expand Down
5 changes: 4 additions & 1 deletion api/operator_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"testing"

"github.com/hashicorp/nomad/api/internal/testutil"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -31,7 +32,9 @@ func TestOperator_MetricsSummary(t *testing.T) {

func TestOperator_Metrics_Prometheus(t *testing.T) {
t.Parallel()
c, s := makeClient(t, nil, nil)
c, s := makeClient(t, nil, func(c *testutil.TestServerConfig) {
c.Telemetry = &testutil.Telemetry{PrometheusMetrics: true}
})
defer s.Stop()

operator := c.Operator()
Expand Down
6 changes: 6 additions & 0 deletions command/agent/metrics_endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
34 changes: 34 additions & 0 deletions command/agent/metrics_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
9 changes: 9 additions & 0 deletions website/content/docs/upgrade/upgrade-specific.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,15 @@ upgrade. However, specific versions of Nomad may have more details provided for
their upgrades as a result of new features or changed behavior. This page is
used to document those details separately from the standard upgrade flow.

## Nomad 1.1.0

#### Agent Metrics API

The Nomad agent metrics API now respects the
[prometheus_metrics](https://www.nomadproject.io/docs/configuration/telemetry#prometheus_metrics)
jrasell marked this conversation as resolved.
Show resolved Hide resolved
configuration value. If this value is set to `false`, which is the default value,
calling `/v1/metrics?format=prometheus` will now result in a response error.

## Nomad 1.0.3, 0.12.10

Nomad versions 1.0.3 and 0.12.10 change the behavior of the `exec` and `java` drivers so that
Expand Down