use the MetricsPrefix to set the service name and provide as slice literal to avoid bugs from append modifying its first arg

This commit is contained in:
Kit Patella 2020-11-16 14:01:12 -08:00
parent 464d13d80b
commit 6290be054a
2 changed files with 16 additions and 9 deletions

View File

@ -78,7 +78,7 @@ func NewBaseDeps(configLoader ConfigLoader, logOut io.Writer) (BaseDeps, error)
return d, fmt.Errorf("failed to setup node ID: %w", err) return d, fmt.Errorf("failed to setup node ID: %w", err)
} }
gauges, counters, summaries := getPrometheusDefs() gauges, counters, summaries := getPrometheusDefs(cfg.Telemetry)
cfg.Telemetry.PrometheusOpts.GaugeDefinitions = gauges cfg.Telemetry.PrometheusOpts.GaugeDefinitions = gauges
cfg.Telemetry.PrometheusOpts.CounterDefinitions = counters cfg.Telemetry.PrometheusOpts.CounterDefinitions = counters
cfg.Telemetry.PrometheusOpts.SummaryDefinitions = summaries cfg.Telemetry.PrometheusOpts.SummaryDefinitions = summaries
@ -190,8 +190,8 @@ func registerWithGRPC(b grpcresolver.Builder) {
// getPrometheusDefs reaches into every slice of prometheus defs we've defined in each part of the agent, and appends // getPrometheusDefs reaches into every slice of prometheus defs we've defined in each part of the agent, and appends
// all of our slices into one nice slice of definitions per metric type for the Consul agent to pass to go-metrics. // all of our slices into one nice slice of definitions per metric type for the Consul agent to pass to go-metrics.
func getPrometheusDefs() ([]prometheus.GaugeDefinition, []prometheus.CounterDefinition, []prometheus.SummaryDefinition) { func getPrometheusDefs(cfg lib.TelemetryConfig) ([]prometheus.GaugeDefinition, []prometheus.CounterDefinition, []prometheus.SummaryDefinition) {
serviceName := []string{"consul"} // Build slice of slices for all gauge definitions
var gauges = [][]prometheus.GaugeDefinition{ var gauges = [][]prometheus.GaugeDefinition{
cache.Gauges, cache.Gauges,
consul.AutopilotGauges, consul.AutopilotGauges,
@ -200,12 +200,15 @@ func getPrometheusDefs() ([]prometheus.GaugeDefinition, []prometheus.CounterDefi
grpc.StatsGauges, grpc.StatsGauges,
usagemetrics.Gauges, usagemetrics.Gauges,
} }
// Flatten definitions
// NOTE(kit): Do we actually want to create a set here so we can ensure definition names are unique?
var gaugeDefs []prometheus.GaugeDefinition var gaugeDefs []prometheus.GaugeDefinition
for _, g := range gauges { for _, g := range gauges {
// Set Consul to each definition's namespace // Set Consul to each definition's namespace
// TODO(kit): Prepending the service to each definition should be handled by go-metrics
var withService []prometheus.GaugeDefinition var withService []prometheus.GaugeDefinition
for _, gauge := range g { for _, gauge := range g {
gauge.Name = append(serviceName, gauge.Name...) gauge.Name = append([]string{cfg.MetricsPrefix}, gauge.Name...)
withService = append(withService, gauge) withService = append(withService, gauge)
} }
gaugeDefs = append(gaugeDefs, withService...) gaugeDefs = append(gaugeDefs, withService...)
@ -239,12 +242,14 @@ func getPrometheusDefs() ([]prometheus.GaugeDefinition, []prometheus.CounterDefi
local.StateCounters, local.StateCounters,
raftCounters, raftCounters,
} }
// Flatten definitions
// NOTE(kit): Do we actually want to create a set here so we can ensure definition names are unique?
var counterDefs []prometheus.CounterDefinition var counterDefs []prometheus.CounterDefinition
for _, c := range counters { for _, c := range counters {
// Set Consul to each definition's namespace // TODO(kit): Prepending the service to each definition should be handled by go-metrics
var withService []prometheus.CounterDefinition var withService []prometheus.CounterDefinition
for _, counter := range c { for _, counter := range c {
counter.Name = append(serviceName, counter.Name...) counter.Name = append([]string{cfg.MetricsPrefix}, counter.Name...)
withService = append(withService, counter) withService = append(withService, counter)
} }
counterDefs = append(counterDefs, withService...) counterDefs = append(counterDefs, withService...)
@ -283,12 +288,14 @@ func getPrometheusDefs() ([]prometheus.GaugeDefinition, []prometheus.CounterDefi
fsm.SnapshotSummaries, fsm.SnapshotSummaries,
raftSummaries, raftSummaries,
} }
// Flatten definitions
// NOTE(kit): Do we actually want to create a set here so we can ensure definition names are unique?
var summaryDefs []prometheus.SummaryDefinition var summaryDefs []prometheus.SummaryDefinition
for _, s := range summaries { for _, s := range summaries {
// Set Consul to each definition's namespace // TODO(kit): Prepending the service to each definition should be handled by go-metrics
var withService []prometheus.SummaryDefinition var withService []prometheus.SummaryDefinition
for _, summary := range s { for _, summary := range s {
summary.Name = append(serviceName, summary.Name...) summary.Name = append([]string{cfg.MetricsPrefix}, summary.Name...)
withService = append(withService, summary) withService = append(withService, summary)
} }
summaryDefs = append(summaryDefs, withService...) summaryDefs = append(summaryDefs, withService...)

View File

@ -4,7 +4,7 @@ import (
"reflect" "reflect"
"time" "time"
metrics "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/armon/go-metrics/prometheus"