Skip to content

Commit

Permalink
Fail on invalid intrinsic dimension override keys
Browse files Browse the repository at this point in the history
  • Loading branch information
stoewer committed Dec 20, 2022
1 parent 45b6e36 commit 9088882
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 7 deletions.
11 changes: 8 additions & 3 deletions modules/generator/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"flag"
"time"

"github.com/pkg/errors"

"github.com/grafana/tempo/modules/generator/processor/servicegraphs"
"github.com/grafana/tempo/modules/generator/processor/spanmetrics"
"github.com/grafana/tempo/modules/generator/registry"
Expand Down Expand Up @@ -50,7 +52,7 @@ func (cfg *ProcessorConfig) RegisterFlagsAndApplyDefaults(prefix string, f *flag
}

// copyWithOverrides creates a copy of the config using values set in the overrides.
func (cfg *ProcessorConfig) copyWithOverrides(o metricsGeneratorOverrides, userID string) ProcessorConfig {
func (cfg *ProcessorConfig) copyWithOverrides(o metricsGeneratorOverrides, userID string) (ProcessorConfig, error) {
copyCfg := *cfg

if buckets := o.MetricsGeneratorProcessorServiceGraphsHistogramBuckets(userID); buckets != nil {
Expand All @@ -66,8 +68,11 @@ func (cfg *ProcessorConfig) copyWithOverrides(o metricsGeneratorOverrides, userI
copyCfg.SpanMetrics.Dimensions = dimensions
}
if dimensions := o.MetricsGeneratorProcessorSpanMetricsIntrinsicDimensions(userID); dimensions != nil {
copyCfg.SpanMetrics.IntrinsicDimensions.ApplyFromMap(dimensions)
err := copyCfg.SpanMetrics.IntrinsicDimensions.ApplyFromMap(dimensions)
if err != nil {
return ProcessorConfig{}, errors.Wrap(err, "fail to apply overrides")
}
}

return copyCfg
return copyCfg, nil
}
16 changes: 14 additions & 2 deletions modules/generator/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/grafana/tempo/modules/generator/processor/servicegraphs"
"github.com/grafana/tempo/modules/generator/processor/spanmetrics"
Expand Down Expand Up @@ -31,7 +32,8 @@ func TestProcessorConfig_copyWithOverrides(t *testing.T) {
spanMetricsIntrinsicDimensions: map[string]bool{"status_code": true},
}

copied := original.copyWithOverrides(o, "tenant")
copied, err := original.copyWithOverrides(o, "tenant")
require.NoError(t, err)

assert.NotEqual(t, *original, copied)

Expand All @@ -53,8 +55,18 @@ func TestProcessorConfig_copyWithOverrides(t *testing.T) {
t.Run("empty overrides", func(t *testing.T) {
o := &mockOverrides{}

copied := original.copyWithOverrides(o, "tenant")
copied, err := original.copyWithOverrides(o, "tenant")
require.NoError(t, err)

assert.Equal(t, *original, copied)
})

t.Run("invalid overrides", func(t *testing.T) {
o := &mockOverrides{
spanMetricsIntrinsicDimensions: map[string]bool{"invalid": true},
}

_, err := original.copyWithOverrides(o, "tenant")
require.Error(t, err)
})
}
5 changes: 4 additions & 1 deletion modules/generator/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ func (i *instance) watchOverrides() {

func (i *instance) updateProcessors() error {
desiredProcessors := i.overrides.MetricsGeneratorProcessors(i.instanceID)
desiredCfg := i.cfg.Processor.copyWithOverrides(i.overrides, i.instanceID)
desiredCfg, err := i.cfg.Processor.copyWithOverrides(i.overrides, i.instanceID)
if err != nil {
return err
}

i.processorsMtx.RLock()
toAdd, toRemove, toReplace, err := i.diffProcessors(desiredProcessors, desiredCfg)
Expand Down
6 changes: 5 additions & 1 deletion modules/generator/processor/spanmetrics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package spanmetrics
import (
"flag"

"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -44,7 +45,7 @@ type IntrinsicDimensions struct {
StatusMessage bool `yaml:"status_message,omitempty"`
}

func (ic *IntrinsicDimensions) ApplyFromMap(dimensions map[string]bool) {
func (ic *IntrinsicDimensions) ApplyFromMap(dimensions map[string]bool) error {
for label, active := range dimensions {
switch label {
case dimService:
Expand All @@ -57,6 +58,9 @@ func (ic *IntrinsicDimensions) ApplyFromMap(dimensions map[string]bool) {
ic.StatusCode = active
case dimStatusMessage:
ic.StatusMessage = active
default:
return errors.Errorf("%s is not a valid intrinsic dimension", label)
}
}
return nil
}

0 comments on commit 9088882

Please sign in to comment.