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

Prometheus support on v1/sys/metrics endpoint #5308

Merged
merged 12 commits into from
Feb 14, 2019
47 changes: 24 additions & 23 deletions command/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,6 @@ func (c *ServerCommand) Run(args []string) int {
c.UI.Error(fmt.Sprintf("Error initializing telemetry: %s", err))
return 1
}
prometheusEnabled := config.Telemetry != nil && config.Telemetry.PrometheusRetentionTime > 0

// Initialize the backend
factory, exists := c.PhysicalBackends[config.Storage.Type]
Expand Down Expand Up @@ -565,7 +564,6 @@ func (c *ServerCommand) Run(args []string) int {
BuiltinRegistry: builtinplugins.Registry,
DisableKeyEncodingChecks: config.DisablePrintableCheck,
InMemSink: inMemMetrics,
PrometheusEnabled: prometheusEnabled,
}
if c.flagDev {
coreConfig.DevToken = c.flagDevRootTokenID
Expand Down Expand Up @@ -1692,6 +1690,23 @@ func (c *ServerCommand) setupTelemetry(config *server.Config) (*metrics.InmemSin

// Configure the statsite sink
var fanout metrics.FanoutSink

// Configure the Prometheus sink
if telConfig.PrometheusRetentionTime == 0 {
return nil, fmt.Errorf("telemetry.prometheus_retention_time must be > 0")
}

prometheusOpts := prometheus.PrometheusOpts{
Expiration: telConfig.PrometheusRetentionTime,
}

sink, err := prometheus.NewPrometheusSinkFrom(prometheusOpts)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't you only want to do this if the retention time is not zero? That way it'd gate it based on configuration existing, like the other types.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I may have misunderstood your previous comment then :(
You mentioned it might be better if users have all formats available with no gates, so right now, specifying a 0 value for Prometheus retention is return an error, with the default being 24h retention

Would you prefer that the behavior is still disabled if an explicit 0 is used ?

if err != nil {
return nil, err
}

fanout = append(fanout, sink)

if telConfig.StatsiteAddr != "" {
sink, err := metrics.NewStatsiteSink(telConfig.StatsiteAddr)
if err != nil {
Expand Down Expand Up @@ -1746,23 +1761,6 @@ func (c *ServerCommand) setupTelemetry(config *server.Config) (*metrics.InmemSin
fanout = append(fanout, sink)
}

// Configure the Prometheus sink
if telConfig.PrometheusRetentionTime.Nanoseconds() > 0 {
prometheusOpts := prometheus.PrometheusOpts{
Expiration: telConfig.PrometheusRetentionTime,
}
sink, err := prometheus.NewPrometheusSinkFrom(prometheusOpts)
if err != nil {
return nil, err
}
// Hostname enabled will create poor quality metrics name
if !telConfig.DisableHostname {
c.UI.Warn("telemetry.disable_hostname has been set to false. Recommended setting is true for Prometheus to avoid poorly named metrics.")

}
fanout = append(fanout, sink)
}

if telConfig.DogStatsDAddr != "" {
var tags []string

Expand All @@ -1779,13 +1777,16 @@ func (c *ServerCommand) setupTelemetry(config *server.Config) (*metrics.InmemSin
}

// Initialize the global sink
if len(fanout) > 0 {
fanout = append(fanout, inm)
metrics.NewGlobal(metricsConf, fanout)
if len(fanout) > 1 {
// Hostname enabled will create poor quality metrics name for prometheus
if !telConfig.DisableHostname {
c.UI.Warn("telemetry.disable_hostname has been set to false. Recommended setting is true for Prometheus to avoid poorly named metrics.")
}
} else {
metricsConf.EnableHostname = false
metrics.NewGlobal(metricsConf, inm)
}
fanout = append(fanout, inm)
metrics.NewGlobal(metricsConf, fanout)
return inm, nil
}

Expand Down
11 changes: 8 additions & 3 deletions command/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ import (
"github.com/hashicorp/vault/helper/parseutil"
)

const (
prometheusDefaultRetentionTime = 24 * time.Hour
)

// Config is the configuration for the vault server.
type Config struct {
Listeners []*Listener `hcl:"-"`
Expand Down Expand Up @@ -99,7 +103,7 @@ func DevConfig(ha, transactional bool) *Config {
EnableUI: true,

Telemetry: &Telemetry{
PrometheusRetentionTime: 24 * time.Hour,
PrometheusRetentionTime: prometheusDefaultRetentionTime,
DisableHostname: true,
},
}
Expand Down Expand Up @@ -239,8 +243,7 @@ type Telemetry struct {

// Prometheus:
// PrometheusRetentionTime is the retention time for prometheus metrics if greater than 0.
// A value of 0 disable Prometheus support.
// Default: 0
// Default: 24h
PrometheusRetentionTime time.Duration `hcl:-`
PrometheusRetentionTimeRaw interface{} `hcl:"prometheus_retention_time"`
}
Expand Down Expand Up @@ -832,6 +835,8 @@ func parseTelemetry(result *Config, list *ast.ObjectList) error {
if result.Telemetry.PrometheusRetentionTime, err = parseutil.ParseDurationSecond(result.Telemetry.PrometheusRetentionTimeRaw); err != nil {
return err
}
} else {
result.Telemetry.PrometheusRetentionTime = prometheusDefaultRetentionTime
}

return nil
Expand Down
3 changes: 0 additions & 3 deletions vault/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,6 @@ type Core struct {

// Telemetry objects
inMemSink *metrics.InmemSink
prometheusEnabled bool
}

// CoreConfig is used to parameterize a core
Expand Down Expand Up @@ -477,7 +476,6 @@ type CoreConfig struct {

// Telemetry objects
InMemSink *metrics.InmemSink
PrometheusEnabled bool
}

func (c *CoreConfig) Clone() *CoreConfig {
Expand Down Expand Up @@ -585,7 +583,6 @@ func NewCore(conf *CoreConfig) (*Core, error) {
allLoggers: conf.AllLoggers,
builtinRegistry: conf.BuiltinRegistry,
inMemSink: conf.InMemSink,
prometheusEnabled: conf.PrometheusEnabled,
}

atomic.StoreUint32(c.sealed, 1)
Expand Down
4 changes: 0 additions & 4 deletions vault/logical_system.go
Original file line number Diff line number Diff line change
Expand Up @@ -2484,10 +2484,6 @@ func (b *SystemBackend) handleMetrics(ctx context.Context, req *logical.Request,

acceptHeaders := req.Headers["Accept"]
if format == "prometheus" || (len(acceptHeaders) > 0 && strings.HasPrefix(acceptHeaders[0], "application/openmetrics-text")) {
if !b.Core.prometheusEnabled {
err := "prometheus support is not enabled"
return nil, fmt.Errorf(err)
}

metricsFamilies, err := prometheus.DefaultGatherer.Gather()
if err != nil && len(metricsFamilies) == 0 {
Expand Down