Skip to content

Commit

Permalink
enable prometheus sink by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Conraux committed Feb 4, 2019
1 parent 743ef90 commit cc45124
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
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)
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

0 comments on commit cc45124

Please sign in to comment.