Skip to content
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

[connector/spanmetricsconnector] fix default metrics initialization #20525

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions connector/spanmetricsconnector/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ The following settings can be optionally configured:
buckets: `[2ms, 4ms, 6ms, 8ms, 10ms, 50ms, 100ms, 200ms, 400ms, 800ms, 1s, 1400ms, 2s, 5s, 10s, 15s]`
- `exponential`:
- `max_size` (default: 160) the maximum number of buckets per positive or negative number range.
- `delete_keys`: the list of dimensions which will be removed from default dimensions defined above.
- `dimensions`: the list of dimensions to add together with the default dimensions defined above.

Each additional dimension is defined with a `name` which is looked up in the span's collection of attributes or
Expand Down
20 changes: 17 additions & 3 deletions connector/spanmetricsconnector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ var defaultHistogramBucketsMs = []float64{
}

// Dimension defines the dimension name and optional default value if the Dimension is missing from a span attribute.

type Keys struct {
Name string `mapstructure:"name"`
}
type Dimension struct {
Name string `mapstructure:"name"`
Default *string `mapstructure:"default"`
Expand All @@ -50,6 +54,7 @@ type Config struct {
// The dimensions will be fetched from the span's attributes. Examples of some conventionally used attributes:
// https://github.com/open-telemetry/opentelemetry-collector/blob/main/model/semconv/opentelemetry.go.
Dimensions []Dimension `mapstructure:"dimensions"`
DeleteKeys []Keys `mapstructure:"delete_keys"`

// DimensionsCacheSize defines the size of cache for storing Dimensions, which helps to avoid cache memory growing
// indefinitely over the lifetime of the collector.
Expand Down Expand Up @@ -86,7 +91,7 @@ var _ component.ConfigValidator = (*Config)(nil)

// Validate checks if the processor configuration is valid
func (c Config) Validate() error {
err := validateDimensions(c.Dimensions)
err := validateDimensions(c.Dimensions, c.DeleteKeys)
if err != nil {
return err
}
Expand Down Expand Up @@ -114,10 +119,19 @@ func (c Config) GetAggregationTemporality() pmetric.AggregationTemporality {
}

// validateDimensions checks duplicates for reserved dimensions and additional dimensions.
func validateDimensions(dimensions []Dimension) error {
func validateDimensions(dimensions []Dimension, deleteKeys []Keys) error {
labelNames := make(map[string]struct{})

// Making map of elements for o(1) complexity
set := make(map[string]struct{})
for _, item := range deleteKeys {
set[item.Name] = struct{}{}
}

for _, key := range []string{serviceNameKey, spanKindKey, statusCodeKey, spanNameKey} {
labelNames[key] = struct{}{}
if _, exists := set[key]; !exists {
labelNames[key] = struct{}{}
}
}

for _, key := range dimensions {
Expand Down
21 changes: 20 additions & 1 deletion connector/spanmetricsconnector/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func TestValidateDimensions(t *testing.T) {
for _, tc := range []struct {
name string
dimensions []Dimension
deleteKeys []Keys
expectedErr string
}{
{
Expand Down Expand Up @@ -158,9 +159,27 @@ func TestValidateDimensions(t *testing.T) {
},
expectedErr: "duplicate dimension name service_name",
},
{
name: "Wrong key name to delete",
dimensions: []Dimension{
{Name: "service_name"},
},
deleteKeys: []Keys{
{Name: "http.service"},
},
},
{
name: "Correct key to delete",
dimensions: []Dimension{
{Name: "service_name"},
},
deleteKeys: []Keys{
{Name: "service_name"},
},
},
} {
t.Run(tc.name, func(t *testing.T) {
err := validateDimensions(tc.dimensions)
err := validateDimensions(tc.dimensions, tc.deleteKeys)
if tc.expectedErr != "" {
assert.EqualError(t, err, tc.expectedErr)
} else {
Expand Down