-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[spanmetricsprocessor] Validate duplicate dimensions at start #2844
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |
"strings" | ||
"sync" | ||
"time" | ||
"unicode" | ||
|
||
"go.opentelemetry.io/collector/component" | ||
"go.opentelemetry.io/collector/config/configmodels" | ||
|
@@ -82,7 +83,7 @@ type processorImp struct { | |
metricKeyToDimensions map[metricKey]dimKV | ||
} | ||
|
||
func newProcessor(logger *zap.Logger, config configmodels.Exporter, nextConsumer consumer.Traces) *processorImp { | ||
func newProcessor(logger *zap.Logger, config configmodels.Exporter, nextConsumer consumer.Traces) (*processorImp, error) { | ||
logger.Info("Building spanmetricsprocessor") | ||
pConfig := config.(*Config) | ||
|
||
|
@@ -98,6 +99,10 @@ func newProcessor(logger *zap.Logger, config configmodels.Exporter, nextConsumer | |
} | ||
} | ||
|
||
if err := validateDimensions(pConfig.Dimensions); err != nil { | ||
return nil, err | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a test to cover this line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
} | ||
|
||
return &processorImp{ | ||
logger: logger, | ||
config: *pConfig, | ||
|
@@ -110,7 +115,7 @@ func newProcessor(logger *zap.Logger, config configmodels.Exporter, nextConsumer | |
nextConsumer: nextConsumer, | ||
dimensions: pConfig.Dimensions, | ||
metricKeyToDimensions: make(map[metricKey]dimKV), | ||
} | ||
}, nil | ||
} | ||
|
||
func mapDurationsToMillis(vs []time.Duration, f func(duration time.Duration) float64) []float64 { | ||
|
@@ -121,6 +126,35 @@ func mapDurationsToMillis(vs []time.Duration, f func(duration time.Duration) flo | |
return vsm | ||
} | ||
|
||
// validateDimensions checks duplicates for reserved dimensions and additional dimensions. Considering | ||
// the usage of Prometheus related exporters, we also validate the dimensions after sanitization. | ||
func validateDimensions(dimensions []Dimension) error { | ||
labelNames := make(map[string]struct{}) | ||
for _, key := range []string{serviceNameKey, spanKindKey, statusCodeKey} { | ||
labelNames[key] = struct{}{} | ||
labelNames[sanitize(key)] = struct{}{} | ||
} | ||
labelNames[operationKey] = struct{}{} | ||
|
||
for _, key := range dimensions { | ||
if _, ok := labelNames[key.Name]; ok { | ||
return fmt.Errorf("duplicate dimension name %s", key.Name) | ||
} | ||
labelNames[key.Name] = struct{}{} | ||
|
||
sanitizedName := sanitize(key.Name) | ||
if sanitizedName == key.Name { | ||
continue | ||
} | ||
if _, ok := labelNames[sanitizedName]; ok { | ||
return fmt.Errorf("duplicate dimension name %s after sanitization", sanitizedName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add a test to cover this line. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
labelNames[sanitizedName] = struct{}{} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// Start implements the component.Component interface. | ||
func (p *processorImp) Start(ctx context.Context, host component.Host) error { | ||
p.logger.Info("Starting spanmetricsprocessor") | ||
|
@@ -379,3 +413,33 @@ func (p *processorImp) cache(serviceName string, span pdata.Span, k metricKey) { | |
p.metricKeyToDimensions[k] = buildDimensionKVs(serviceName, span, p.dimensions) | ||
} | ||
} | ||
|
||
// copied from prometheus-go-metric-exporter | ||
// sanitize replaces non-alphanumeric characters with underscores in s. | ||
func sanitize(s string) string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably worth copying the tests from prometheusexporter too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
if len(s) == 0 { | ||
return s | ||
} | ||
|
||
// Note: No length limit for label keys because Prometheus doesn't | ||
// define a length limit, thus we should NOT be truncating label keys. | ||
// See https://github.com/orijtech/prometheus-go-metrics-exporter/issues/4. | ||
s = strings.Map(sanitizeRune, s) | ||
if unicode.IsDigit(rune(s[0])) { | ||
s = "key_" + s | ||
} | ||
if s[0] == '_' { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here |
||
s = "key" + s | ||
} | ||
return s | ||
} | ||
|
||
// copied from prometheus-go-metric-exporter | ||
// sanitizeRune converts anything that is not a letter or digit to an underscore | ||
func sanitizeRune(r rune) rune { | ||
if unicode.IsLetter(r) || unicode.IsDigit(r) { | ||
return r | ||
} | ||
// Everything else turns into an underscore | ||
return '_' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ... and here as well? |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks!