diff --git a/doc/telemetry/telemetry_config.md b/doc/telemetry/telemetry_config.md index 54500bd5093..85cc99d16ed 100644 --- a/doc/telemetry/telemetry_config.md +++ b/doc/telemetry/telemetry_config.md @@ -24,7 +24,6 @@ You may use all, some, or none of the collectors. The following collectors suppo | `Statsd` | `[]Statsd` | List of Statsd configurations | | | `M3` | `[]M3` | List of M3 configurations | | | `MetricPrefix` | `string` | Prefix to add to all emitted metrics | spire_server/spire_agent | -| `TrustDomain` | `string` | Optional label value for all metrics | | | `EnableTrustDomainLabel` | `bool` | Enable optional trust domain label for all metrics | false | | `EnableHostnameLabel` | `bool` | Enable adding hostname to labels | true | | `AllowedPrefixes` | `[]string` | A list of metric prefixes to allow, with '.' as the separator | | @@ -81,8 +80,6 @@ telemetry { ] InMem {} - TrustDomain = "example.org" - EnaEnableTrustDomainLabel = true AllowedLabels = [] BlockedLabels = [] AllowedPrefixes = [] diff --git a/pkg/agent/agent.go b/pkg/agent/agent.go index 550369201f9..9907e01944e 100644 --- a/pkg/agent/agent.go +++ b/pkg/agent/agent.go @@ -77,6 +77,7 @@ func (a *Agent) Run(ctx context.Context) error { FileConfig: a.c.Telemetry, Logger: a.c.Log.WithField(telemetry.SubsystemName, telemetry.Telemetry), ServiceName: telemetry.SpireAgent, + TrustDomain: a.c.TrustDomain.Name(), }) if err != nil { return err diff --git a/pkg/common/telemetry/config.go b/pkg/common/telemetry/config.go index 7d6909d8162..13327bc7e5d 100644 --- a/pkg/common/telemetry/config.go +++ b/pkg/common/telemetry/config.go @@ -10,6 +10,7 @@ type MetricsConfig struct { Logger logrus.FieldLogger ServiceName string Sinks []Sink + TrustDomain string } type FileConfig struct { @@ -20,7 +21,6 @@ type FileConfig struct { InMem *InMem `hcl:"InMem"` MetricPrefix string `hcl:"MetricPrefix"` - TrustDomain *string `hcl:"TrustDomain"` EnableTrustDomainLabel *bool `hcl:"EnableTrustDomainLabel"` EnableHostnameLabel *bool `hcl:"EnableHostnameLabel"` AllowedPrefixes []string `hcl:"AllowedPrefixes"` // A list of metric prefixes to allow, with '.' as the separator diff --git a/pkg/common/telemetry/dogstatsd_test.go b/pkg/common/telemetry/dogstatsd_test.go index ed44e69e6e2..a498c2bf17f 100644 --- a/pkg/common/telemetry/dogstatsd_test.go +++ b/pkg/common/telemetry/dogstatsd_test.go @@ -58,6 +58,7 @@ func testDogStatsdConfig() *MetricsConfig { return &MetricsConfig{ Logger: l, ServiceName: "foo", + TrustDomain: "test.org", FileConfig: FileConfig{ DogStatsd: []DogStatsdConfig{ { diff --git a/pkg/common/telemetry/inmem_test.go b/pkg/common/telemetry/inmem_test.go index 1b6f8c781f3..e9dcb5f25f2 100644 --- a/pkg/common/telemetry/inmem_test.go +++ b/pkg/common/telemetry/inmem_test.go @@ -80,6 +80,7 @@ func testInmemConfig() *MetricsConfig { return &MetricsConfig{ Logger: logger, ServiceName: "foo", + TrustDomain: "test.org", FileConfig: FileConfig{InMem: &InMem{}}, } } diff --git a/pkg/common/telemetry/m3_test.go b/pkg/common/telemetry/m3_test.go index 4823b2e7c00..41a9d418557 100644 --- a/pkg/common/telemetry/m3_test.go +++ b/pkg/common/telemetry/m3_test.go @@ -124,6 +124,7 @@ func testM3Config() *MetricsConfig { return &MetricsConfig{ Logger: l, ServiceName: "foo", + TrustDomain: "test.org", FileConfig: FileConfig{ M3: []M3Config{ { diff --git a/pkg/common/telemetry/metrics.go b/pkg/common/telemetry/metrics.go index f6078c9d4f3..ecea4947f70 100644 --- a/pkg/common/telemetry/metrics.go +++ b/pkg/common/telemetry/metrics.go @@ -11,9 +11,6 @@ import ( const timerGranularity = time.Millisecond -// EnableTrustDomainLabel is the value for the custom TrustDomain label/tag for a metric -var trustDomain = "" - // Label is a label/tag for a metric type Label = metrics.Label @@ -48,7 +45,8 @@ type MetricsImpl struct { c *MetricsConfig runners []sinkRunner // Each instance of metrics.Metrics in the slice corresponds to one metrics sink type - metricsSinks []*metrics.Metrics + metricsSinks []*metrics.Metrics + enableTrustDomainLabel bool } var _ Metrics = (*MetricsImpl)(nil) @@ -87,16 +85,18 @@ func NewMetrics(c *MetricsConfig) (*MetricsImpl, error) { conf.EnableHostnameLabel = true } - if c.FileConfig.EnableTrustDomainLabel != nil && c.FileConfig.TrustDomain != nil && *c.FileConfig.EnableTrustDomainLabel { - trustDomain = *c.FileConfig.TrustDomain - } - conf.EnableTypePrefix = runner.requiresTypePrefix() conf.AllowedLabels = c.FileConfig.AllowedLabels conf.BlockedLabels = c.FileConfig.BlockedLabels conf.AllowedPrefixes = c.FileConfig.AllowedPrefixes conf.BlockedPrefixes = c.FileConfig.BlockedPrefixes + if c.FileConfig.EnableTrustDomainLabel != nil { + impl.enableTrustDomainLabel = *c.FileConfig.EnableTrustDomainLabel + } else { + impl.enableTrustDomainLabel = false + } + metricsSink, err := metrics.New(conf, fanout) if err != nil { return nil, err @@ -120,7 +120,7 @@ func (m *MetricsImpl) ListenAndServe(ctx context.Context) error { } func (m *MetricsImpl) SetGauge(key []string, val float32) { - if trustDomain != "" { + if m.enableTrustDomainLabel { m.SetGaugeWithLabels(key, val, []Label{}) } else { for _, s := range m.metricsSinks { @@ -131,16 +131,10 @@ func (m *MetricsImpl) SetGauge(key []string, val float32) { // SetGaugeWithLabels delegates to embedded metrics, sanitizing labels func (m *MetricsImpl) SetGaugeWithLabels(key []string, val float32, labels []Label) { - if trustDomain != "" { - for i, label := range labels { - if label.Name == TrustDomainID { - label.Value = trustDomain - } - } + if m.enableTrustDomainLabel { + labels = append(labels, Label{Name: TrustDomain, Value: m.c.TrustDomain}) } - // or optionally, the label could have another name instead of TrustDomainID and just append that to the list of exsiting labels. - // labels = append(labels, Label{Name: TrustDomain, Value: trustDomain}) sanitizedLabels := SanitizeLabels(labels) for _, s := range m.metricsSinks { s.SetGaugeWithLabels(key, val, sanitizedLabels) @@ -154,7 +148,7 @@ func (m *MetricsImpl) EmitKey(key []string, val float32) { } func (m *MetricsImpl) IncrCounter(key []string, val float32) { - if trustDomain != "" { + if m.enableTrustDomainLabel { m.IncrCounterWithLabels(key, val, []Label{}) } else { for _, s := range m.metricsSinks { @@ -165,12 +159,8 @@ func (m *MetricsImpl) IncrCounter(key []string, val float32) { // IncrCounterWithLabels delegates to embedded metrics, sanitizing labels func (m *MetricsImpl) IncrCounterWithLabels(key []string, val float32, labels []Label) { - if trustDomain != "" { - for i, label := range labels { - if label.Name == TrustDomainID { - label.Value = trustDomain - } - } + if m.enableTrustDomainLabel { + labels = append(labels, Label{Name: TrustDomain, Value: m.c.TrustDomain}) } sanitizedLabels := SanitizeLabels(labels) @@ -180,7 +170,7 @@ func (m *MetricsImpl) IncrCounterWithLabels(key []string, val float32, labels [] } func (m *MetricsImpl) AddSample(key []string, val float32) { - if trustDomain != "" { + if m.enableTrustDomainLabel { m.AddSampleWithLabels(key, val, []Label{}) } else { for _, s := range m.metricsSinks { @@ -191,12 +181,8 @@ func (m *MetricsImpl) AddSample(key []string, val float32) { // AddSampleWithLabels delegates to embedded metrics, sanitizing labels func (m *MetricsImpl) AddSampleWithLabels(key []string, val float32, labels []Label) { - if trustDomain != "" { - for i, label := range labels { - if label.Name == TrustDomainID { - label.Value = trustDomain - } - } + if m.enableTrustDomainLabel { + labels = append(labels, Label{Name: TrustDomain, Value: m.c.TrustDomain}) } sanitizedLabels := SanitizeLabels(labels) @@ -206,7 +192,7 @@ func (m *MetricsImpl) AddSampleWithLabels(key []string, val float32, labels []La } func (m *MetricsImpl) MeasureSince(key []string, start time.Time) { - if trustDomain != "" { + if m.enableTrustDomainLabel { m.MeasureSinceWithLabels(key, start, []Label{}) } else { for _, s := range m.metricsSinks { @@ -217,12 +203,8 @@ func (m *MetricsImpl) MeasureSince(key []string, start time.Time) { // MeasureSinceWithLabels delegates to embedded metrics, sanitizing labels func (m *MetricsImpl) MeasureSinceWithLabels(key []string, start time.Time, labels []Label) { - if trustDomain != "" { - for i, label := range labels { - if label.Name == TrustDomainID { - label.Value = trustDomain - } - } + if m.enableTrustDomainLabel { + labels = append(labels, Label{Name: TrustDomain, Value: m.c.TrustDomain}) } sanitizedLabels := SanitizeLabels(labels) diff --git a/pkg/common/telemetry/names.go b/pkg/common/telemetry/names.go index fe5be2af7a4..2ee2dfc43ec 100644 --- a/pkg/common/telemetry/names.go +++ b/pkg/common/telemetry/names.go @@ -604,9 +604,6 @@ const ( // TrustDomainID tags the ID of some trust domain TrustDomainID = "trust_domain_id" - // TrustDomainName tags the custom trust domain name provided for telemetry - TrustDomainName = "trust_domain_name" - // Unknown tags some unknown caller, entity, or status Unknown = "unknown" diff --git a/pkg/common/telemetry/prometheus_test.go b/pkg/common/telemetry/prometheus_test.go index 9a58b21d044..659d59ca47d 100644 --- a/pkg/common/telemetry/prometheus_test.go +++ b/pkg/common/telemetry/prometheus_test.go @@ -82,6 +82,7 @@ func testPrometheusConfig() *MetricsConfig { return &MetricsConfig{ Logger: l, ServiceName: "foo", + TrustDomain: "test.org", FileConfig: FileConfig{ // Let prometheus listen on a random port Prometheus: &PrometheusConfig{}, diff --git a/pkg/common/telemetry/statsd_test.go b/pkg/common/telemetry/statsd_test.go index d7cde716df6..96d216d3cf3 100644 --- a/pkg/common/telemetry/statsd_test.go +++ b/pkg/common/telemetry/statsd_test.go @@ -64,6 +64,7 @@ func testStatsdConfigWithPort(port int) *MetricsConfig { return &MetricsConfig{ Logger: l, ServiceName: "foo", + TrustDomain: "test.org", FileConfig: FileConfig{ Statsd: []StatsdConfig{ { diff --git a/pkg/server/server.go b/pkg/server/server.go index 6693e2fd2e1..115ed5e4af5 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -92,6 +92,7 @@ func (s *Server) run(ctx context.Context) (err error) { FileConfig: s.config.Telemetry, Logger: s.config.Log.WithField(telemetry.SubsystemName, telemetry.Telemetry), ServiceName: telemetry.SpireServer, + TrustDomain: s.config.TrustDomain.Name(), }) if err != nil { return err