From 2e495ec8a63d9a5264b46c5473e7e59960eb3c2d Mon Sep 17 00:00:00 2001
From: Pierre Souchay
Date: Fri, 6 Apr 2018 14:21:05 +0200
Subject: [PATCH] Now use prometheus_retention_time > 0 to enable prometheus
support
---
agent/agent_endpoint.go | 5 +++++
agent/config/builder.go | 2 +-
agent/config/config.go | 2 +-
agent/config/runtime.go | 6 +++---
agent/config/runtime_test.go | 8 ++++----
command/agent/agent.go | 7 +++++--
6 files changed, 19 insertions(+), 11 deletions(-)
diff --git a/agent/agent_endpoint.go b/agent/agent_endpoint.go
index 5d8970fab..16472cf0f 100644
--- a/agent/agent_endpoint.go
+++ b/agent/agent_endpoint.go
@@ -89,6 +89,11 @@ func (s *HTTPServer) AgentMetrics(resp http.ResponseWriter, req *http.Request) (
return nil, acl.ErrPermissionDenied
}
if format := req.URL.Query().Get("format"); format == "prometheus" {
+ if s.agent.config.TelemetryPrometheusRetentionTime.Nanoseconds() < 1 {
+ resp.WriteHeader(http.StatusUnsupportedMediaType)
+ fmt.Fprint(resp, "Prometheus is not enable since its retention time is not positive")
+ return nil, nil
+ }
handlerOptions := promhttp.HandlerOpts{
ErrorLog: s.agent.logger,
ErrorHandling: promhttp.ContinueOnError,
diff --git a/agent/config/builder.go b/agent/config/builder.go
index 765918011..bedec32f8 100644
--- a/agent/config/builder.go
+++ b/agent/config/builder.go
@@ -626,7 +626,7 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
TelemetryDisableHostname: b.boolVal(c.Telemetry.DisableHostname),
TelemetryDogstatsdAddr: b.stringVal(c.Telemetry.DogstatsdAddr),
TelemetryDogstatsdTags: c.Telemetry.DogstatsdTags,
- TelemetryPrometheusDisable: b.boolVal(c.Telemetry.PrometheusDisable),
+ TelemetryPrometheusRetentionTime: b.durationVal("prometheus_retention_time", c.Telemetry.PrometheusRetentionTime),
TelemetryFilterDefault: b.boolVal(c.Telemetry.FilterDefault),
TelemetryAllowedPrefixes: telemetryAllowedPrefixes,
TelemetryBlockedPrefixes: telemetryBlockedPrefixes,
diff --git a/agent/config/config.go b/agent/config/config.go
index a481bbf4c..6c9af93ff 100644
--- a/agent/config/config.go
+++ b/agent/config/config.go
@@ -394,7 +394,7 @@ type Telemetry struct {
FilterDefault *bool `json:"filter_default,omitempty" hcl:"filter_default" mapstructure:"filter_default"`
PrefixFilter []string `json:"prefix_filter,omitempty" hcl:"prefix_filter" mapstructure:"prefix_filter"`
MetricsPrefix *string `json:"metrics_prefix,omitempty" hcl:"metrics_prefix" mapstructure:"metrics_prefix"`
- PrometheusDisable *bool `json:"prometheus_disable,omitempty" hcl:"prometheus_disable" mapstructure:"prometheus_disable"`
+ PrometheusRetentionTime *string `json:"prometheus_retention_time,omitempty" hcl:"prometheus_retention_time" mapstructure:"prometheus_retention_time"`
StatsdAddr *string `json:"statsd_address,omitempty" hcl:"statsd_address" mapstructure:"statsd_address"`
StatsiteAddr *string `json:"statsite_address,omitempty" hcl:"statsite_address" mapstructure:"statsite_address"`
EnableDeprecatedNames *bool `json:"enable_deprecated_names" hcl:"enable_deprecated_names" mapstructure:"enable_deprecated_names"`
diff --git a/agent/config/runtime.go b/agent/config/runtime.go
index e1aa1b2eb..ac8a27f5b 100644
--- a/agent/config/runtime.go
+++ b/agent/config/runtime.go
@@ -425,10 +425,10 @@ type RuntimeConfig struct {
// hcl: telemetry { dogstatsd_tags = []string }
TelemetryDogstatsdTags []string
- // TelemetryPrometheusDisable enables prometheus Support for metrics
+ // PrometheusRetentionTime enables prometheus Support for metrics if greater than 0
//
- // hcl: telemetry { prometheus_disable = (true|false) }
- TelemetryPrometheusDisable bool
+ // hcl: telemetry { prometheus_retention_time = "duration" }
+ TelemetryPrometheusRetentionTime time.Duration
// TelemetryFilterDefault is the default for whether to allow a metric that's not
// covered by the filter.
diff --git a/agent/config/runtime_test.go b/agent/config/runtime_test.go
index e923a14f6..f21ac1012 100644
--- a/agent/config/runtime_test.go
+++ b/agent/config/runtime_test.go
@@ -2589,7 +2589,7 @@ func TestFullConfig(t *testing.T) {
"prefix_filter": [ "+oJotS8XJ","-cazlEhGn" ],
"enable_deprecated_names": true,
"metrics_prefix": "ftO6DySn",
- "prometheus_disable": true,
+ "prometheus_retention_time": "15s",
"statsd_address": "drce87cy",
"statsite_address": "HpFwKB8R"
},
@@ -3027,7 +3027,7 @@ func TestFullConfig(t *testing.T) {
prefix_filter = [ "+oJotS8XJ","-cazlEhGn" ]
enable_deprecated_names = true
metrics_prefix = "ftO6DySn"
- prometheus_disable = true
+ prometheus_retention_time = "15s"
statsd_address = "drce87cy"
statsite_address = "HpFwKB8R"
}
@@ -3590,7 +3590,7 @@ func TestFullConfig(t *testing.T) {
TelemetryAllowedPrefixes: []string{"oJotS8XJ", "consul.consul"},
TelemetryBlockedPrefixes: []string{"cazlEhGn"},
TelemetryMetricsPrefix: "ftO6DySn",
- TelemetryPrometheusDisable: true,
+ TelemetryPrometheusRetentionTime: 15 * time.Second,
TelemetryStatsdAddr: "drce87cy",
TelemetryStatsiteAddr: "HpFwKB8R",
TLSCipherSuites: []uint16{tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
@@ -4143,7 +4143,7 @@ func TestSanitize(t *testing.T) {
"TelemetryDogstatsdTags": [],
"TelemetryFilterDefault": false,
"TelemetryMetricsPrefix": "",
- "TelemetryPrometheusDisable": false,
+ "TelemetryPrometheusRetentionTime": "0s",
"TelemetryStatsdAddr": "",
"TelemetryStatsiteAddr": "",
"TranslateWANAddrs": false,
diff --git a/command/agent/agent.go b/command/agent/agent.go
index 9e7867c67..ef44073a6 100644
--- a/command/agent/agent.go
+++ b/command/agent/agent.go
@@ -210,10 +210,13 @@ func dogstatdSink(config *config.RuntimeConfig, hostname string) (metrics.Metric
}
func prometheusSink(config *config.RuntimeConfig, hostname string) (metrics.MetricSink, error) {
- if config.TelemetryPrometheusDisable {
+ if config.TelemetryPrometheusRetentionTime.Nanoseconds() < 1 {
return nil, nil
}
- sink, err := prometheus.NewPrometheusSink()
+ prometheusOpts := prometheus.PrometheusOpts{
+ Expiration: config.TelemetryPrometheusRetentionTime,
+ }
+ sink, err := prometheus.NewPrometheusSinkFrom(prometheusOpts)
if err != nil {
return nil, err
}