Added support exposing metrics in Prometheus format
This commit is contained in:
parent
e1c64f70df
commit
583744d8c5
|
@ -19,6 +19,8 @@ import (
|
||||||
"github.com/hashicorp/logutils"
|
"github.com/hashicorp/logutils"
|
||||||
"github.com/hashicorp/serf/coordinate"
|
"github.com/hashicorp/serf/coordinate"
|
||||||
"github.com/hashicorp/serf/serf"
|
"github.com/hashicorp/serf/serf"
|
||||||
|
"github.com/prometheus/client_golang/prometheus"
|
||||||
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Self struct {
|
type Self struct {
|
||||||
|
@ -86,7 +88,17 @@ 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" {
|
||||||
|
handlerOptions := promhttp.HandlerOpts{
|
||||||
|
ErrorLog: s.agent.logger,
|
||||||
|
ErrorHandling: promhttp.ContinueOnError,
|
||||||
|
DisableCompression: true,
|
||||||
|
}
|
||||||
|
|
||||||
|
handler := promhttp.HandlerFor(prometheus.DefaultGatherer, handlerOptions)
|
||||||
|
handler.ServeHTTP(resp, req)
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
return s.agent.MemSink.DisplayMetrics(resp, req)
|
return s.agent.MemSink.DisplayMetrics(resp, req)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -626,6 +626,7 @@ func (b *Builder) Build() (rt RuntimeConfig, err error) {
|
||||||
TelemetryDisableHostname: b.boolVal(c.Telemetry.DisableHostname),
|
TelemetryDisableHostname: b.boolVal(c.Telemetry.DisableHostname),
|
||||||
TelemetryDogstatsdAddr: b.stringVal(c.Telemetry.DogstatsdAddr),
|
TelemetryDogstatsdAddr: b.stringVal(c.Telemetry.DogstatsdAddr),
|
||||||
TelemetryDogstatsdTags: c.Telemetry.DogstatsdTags,
|
TelemetryDogstatsdTags: c.Telemetry.DogstatsdTags,
|
||||||
|
TelemetryPrometheusDisable: b.boolVal(c.Telemetry.PrometheusDisable),
|
||||||
TelemetryFilterDefault: b.boolVal(c.Telemetry.FilterDefault),
|
TelemetryFilterDefault: b.boolVal(c.Telemetry.FilterDefault),
|
||||||
TelemetryAllowedPrefixes: telemetryAllowedPrefixes,
|
TelemetryAllowedPrefixes: telemetryAllowedPrefixes,
|
||||||
TelemetryBlockedPrefixes: telemetryBlockedPrefixes,
|
TelemetryBlockedPrefixes: telemetryBlockedPrefixes,
|
||||||
|
|
|
@ -394,6 +394,7 @@ type Telemetry struct {
|
||||||
FilterDefault *bool `json:"filter_default,omitempty" hcl:"filter_default" mapstructure:"filter_default"`
|
FilterDefault *bool `json:"filter_default,omitempty" hcl:"filter_default" mapstructure:"filter_default"`
|
||||||
PrefixFilter []string `json:"prefix_filter,omitempty" hcl:"prefix_filter" mapstructure:"prefix_filter"`
|
PrefixFilter []string `json:"prefix_filter,omitempty" hcl:"prefix_filter" mapstructure:"prefix_filter"`
|
||||||
MetricsPrefix *string `json:"metrics_prefix,omitempty" hcl:"metrics_prefix" mapstructure:"metrics_prefix"`
|
MetricsPrefix *string `json:"metrics_prefix,omitempty" hcl:"metrics_prefix" mapstructure:"metrics_prefix"`
|
||||||
|
PrometheusDisable *bool `json:"prometheus_disable,omitempty" hcl:"prometheus_disable" mapstructure:"prometheus_disable"`
|
||||||
StatsdAddr *string `json:"statsd_address,omitempty" hcl:"statsd_address" mapstructure:"statsd_address"`
|
StatsdAddr *string `json:"statsd_address,omitempty" hcl:"statsd_address" mapstructure:"statsd_address"`
|
||||||
StatsiteAddr *string `json:"statsite_address,omitempty" hcl:"statsite_address" mapstructure:"statsite_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"`
|
EnableDeprecatedNames *bool `json:"enable_deprecated_names" hcl:"enable_deprecated_names" mapstructure:"enable_deprecated_names"`
|
||||||
|
|
|
@ -425,6 +425,11 @@ type RuntimeConfig struct {
|
||||||
// hcl: telemetry { dogstatsd_tags = []string }
|
// hcl: telemetry { dogstatsd_tags = []string }
|
||||||
TelemetryDogstatsdTags []string
|
TelemetryDogstatsdTags []string
|
||||||
|
|
||||||
|
// TelemetryPrometheusDisable enables prometheus Support for metrics
|
||||||
|
//
|
||||||
|
// hcl: telemetry { prometheus_disable = (true|false) }
|
||||||
|
TelemetryPrometheusDisable bool
|
||||||
|
|
||||||
// TelemetryFilterDefault is the default for whether to allow a metric that's not
|
// TelemetryFilterDefault is the default for whether to allow a metric that's not
|
||||||
// covered by the filter.
|
// covered by the filter.
|
||||||
//
|
//
|
||||||
|
|
|
@ -2589,6 +2589,7 @@ func TestFullConfig(t *testing.T) {
|
||||||
"prefix_filter": [ "+oJotS8XJ","-cazlEhGn" ],
|
"prefix_filter": [ "+oJotS8XJ","-cazlEhGn" ],
|
||||||
"enable_deprecated_names": true,
|
"enable_deprecated_names": true,
|
||||||
"metrics_prefix": "ftO6DySn",
|
"metrics_prefix": "ftO6DySn",
|
||||||
|
"prometheus_disable": true,
|
||||||
"statsd_address": "drce87cy",
|
"statsd_address": "drce87cy",
|
||||||
"statsite_address": "HpFwKB8R"
|
"statsite_address": "HpFwKB8R"
|
||||||
},
|
},
|
||||||
|
@ -3026,6 +3027,7 @@ func TestFullConfig(t *testing.T) {
|
||||||
prefix_filter = [ "+oJotS8XJ","-cazlEhGn" ]
|
prefix_filter = [ "+oJotS8XJ","-cazlEhGn" ]
|
||||||
enable_deprecated_names = true
|
enable_deprecated_names = true
|
||||||
metrics_prefix = "ftO6DySn"
|
metrics_prefix = "ftO6DySn"
|
||||||
|
prometheus_disable = true
|
||||||
statsd_address = "drce87cy"
|
statsd_address = "drce87cy"
|
||||||
statsite_address = "HpFwKB8R"
|
statsite_address = "HpFwKB8R"
|
||||||
}
|
}
|
||||||
|
@ -3588,6 +3590,7 @@ func TestFullConfig(t *testing.T) {
|
||||||
TelemetryAllowedPrefixes: []string{"oJotS8XJ", "consul.consul"},
|
TelemetryAllowedPrefixes: []string{"oJotS8XJ", "consul.consul"},
|
||||||
TelemetryBlockedPrefixes: []string{"cazlEhGn"},
|
TelemetryBlockedPrefixes: []string{"cazlEhGn"},
|
||||||
TelemetryMetricsPrefix: "ftO6DySn",
|
TelemetryMetricsPrefix: "ftO6DySn",
|
||||||
|
TelemetryPrometheusDisable: true,
|
||||||
TelemetryStatsdAddr: "drce87cy",
|
TelemetryStatsdAddr: "drce87cy",
|
||||||
TelemetryStatsiteAddr: "HpFwKB8R",
|
TelemetryStatsiteAddr: "HpFwKB8R",
|
||||||
TLSCipherSuites: []uint16{tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
|
TLSCipherSuites: []uint16{tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384},
|
||||||
|
@ -4140,6 +4143,7 @@ func TestSanitize(t *testing.T) {
|
||||||
"TelemetryDogstatsdTags": [],
|
"TelemetryDogstatsdTags": [],
|
||||||
"TelemetryFilterDefault": false,
|
"TelemetryFilterDefault": false,
|
||||||
"TelemetryMetricsPrefix": "",
|
"TelemetryMetricsPrefix": "",
|
||||||
|
"TelemetryPrometheusDisable": false,
|
||||||
"TelemetryStatsdAddr": "",
|
"TelemetryStatsdAddr": "",
|
||||||
"TelemetryStatsiteAddr": "",
|
"TelemetryStatsiteAddr": "",
|
||||||
"TranslateWANAddrs": false,
|
"TranslateWANAddrs": false,
|
||||||
|
|
|
@ -15,6 +15,7 @@ import (
|
||||||
"github.com/armon/go-metrics"
|
"github.com/armon/go-metrics"
|
||||||
"github.com/armon/go-metrics/circonus"
|
"github.com/armon/go-metrics/circonus"
|
||||||
"github.com/armon/go-metrics/datadog"
|
"github.com/armon/go-metrics/datadog"
|
||||||
|
"github.com/armon/go-metrics/prometheus"
|
||||||
"github.com/hashicorp/consul/agent"
|
"github.com/hashicorp/consul/agent"
|
||||||
"github.com/hashicorp/consul/agent/config"
|
"github.com/hashicorp/consul/agent/config"
|
||||||
"github.com/hashicorp/consul/command/flags"
|
"github.com/hashicorp/consul/command/flags"
|
||||||
|
@ -208,6 +209,17 @@ func dogstatdSink(config *config.RuntimeConfig, hostname string) (metrics.Metric
|
||||||
return sink, nil
|
return sink, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func prometheusSink(config *config.RuntimeConfig, hostname string) (metrics.MetricSink, error) {
|
||||||
|
if config.TelemetryPrometheusDisable {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
sink, err := prometheus.NewPrometheusSink()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return sink, nil
|
||||||
|
}
|
||||||
|
|
||||||
func circonusSink(config *config.RuntimeConfig, hostname string) (metrics.MetricSink, error) {
|
func circonusSink(config *config.RuntimeConfig, hostname string) (metrics.MetricSink, error) {
|
||||||
if config.TelemetryCirconusAPIToken == "" && config.TelemetryCirconusSubmissionURL == "" {
|
if config.TelemetryCirconusAPIToken == "" && config.TelemetryCirconusSubmissionURL == "" {
|
||||||
return nil, nil
|
return nil, nil
|
||||||
|
@ -284,6 +296,9 @@ func startupTelemetry(conf *config.RuntimeConfig) (*metrics.InmemSink, error) {
|
||||||
if err := addSink("circonus", circonusSink); err != nil {
|
if err := addSink("circonus", circonusSink); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
if err := addSink("prometheus", prometheusSink); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
if len(sinks) > 0 {
|
if len(sinks) > 0 {
|
||||||
sinks = append(sinks, memSink)
|
sinks = append(sinks, memSink)
|
||||||
|
|
Loading…
Reference in New Issue