Skip to content

Commit

Permalink
Added enableTrustDomainLabel to MetricsConfig and updated tests --sig…
Browse files Browse the repository at this point in the history
…noff

Signed-off-by: gajibade <[email protected]>
  • Loading branch information
gajibade committed Nov 29, 2024
1 parent 5b28ed5 commit 23697ed
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 45 deletions.
3 changes: 0 additions & 3 deletions doc/telemetry/telemetry_config.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 | |
Expand Down Expand Up @@ -81,8 +80,6 @@ telemetry {
]
InMem {}
TrustDomain = "example.org"
EnaEnableTrustDomainLabel = true
AllowedLabels = []
BlockedLabels = []
AllowedPrefixes = []
Expand Down
1 change: 1 addition & 0 deletions pkg/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion pkg/common/telemetry/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ type MetricsConfig struct {
Logger logrus.FieldLogger
ServiceName string
Sinks []Sink
TrustDomain string
}

type FileConfig struct {
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions pkg/common/telemetry/dogstatsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ func testDogStatsdConfig() *MetricsConfig {
return &MetricsConfig{
Logger: l,
ServiceName: "foo",
TrustDomain: "test.org",
FileConfig: FileConfig{
DogStatsd: []DogStatsdConfig{
{
Expand Down
1 change: 1 addition & 0 deletions pkg/common/telemetry/inmem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func testInmemConfig() *MetricsConfig {
return &MetricsConfig{
Logger: logger,
ServiceName: "foo",
TrustDomain: "test.org",
FileConfig: FileConfig{InMem: &InMem{}},
}
}
Expand Down
1 change: 1 addition & 0 deletions pkg/common/telemetry/m3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ func testM3Config() *MetricsConfig {
return &MetricsConfig{
Logger: l,
ServiceName: "foo",
TrustDomain: "test.org",
FileConfig: FileConfig{
M3: []M3Config{
{
Expand Down
58 changes: 20 additions & 38 deletions pkg/common/telemetry/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions pkg/common/telemetry/names.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
1 change: 1 addition & 0 deletions pkg/common/telemetry/prometheus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{},
Expand Down
1 change: 1 addition & 0 deletions pkg/common/telemetry/statsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ func testStatsdConfigWithPort(port int) *MetricsConfig {
return &MetricsConfig{
Logger: l,
ServiceName: "foo",
TrustDomain: "test.org",
FileConfig: FileConfig{
Statsd: []StatsdConfig{
{
Expand Down
1 change: 1 addition & 0 deletions pkg/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 23697ed

Please sign in to comment.