Skip to content

Commit

Permalink
[connector/spanmetricsconnector] fix default metrics initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
LaserPhaser committed Mar 30, 2023
1 parent 95aada5 commit 22543ba
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 9 deletions.
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.
- `override_default_dimensions`: the list of dimensions which will override all 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: 15 additions & 5 deletions connector/spanmetricsconnector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ type Config struct {
// - status.code
// 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"`
Dimensions []Dimension `mapstructure:"dimensions"`
OverrideDefaultDimensions []Dimension `mapstructure:"override_default_dimensions"`

// 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 +87,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.OverrideDefaultDimensions)
if err != nil {
return err
}
Expand Down Expand Up @@ -114,10 +115,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, overrideDefaultDimensions []Dimension) error {
labelNames := make(map[string]struct{})
for _, key := range []string{serviceNameKey, spanKindKey, statusCodeKey, spanNameKey} {
labelNames[key] = struct{}{}
if len(overrideDefaultDimensions) > 0 {
for _, key := range overrideDefaultDimensions {
if _, ok := labelNames[key.Name]; ok {
return fmt.Errorf("duplicate dimension name %s", key.Name)
}
labelNames[key.Name] = struct{}{}
}
} else {
for _, key := range []string{serviceNameKey, spanKindKey, statusCodeKey, spanNameKey} {
labelNames[key] = struct{}{}
}
}

for _, key := range dimensions {
Expand Down
28 changes: 24 additions & 4 deletions connector/spanmetricsconnector/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,10 @@ func TestGetAggregationTemporality(t *testing.T) {

func TestValidateDimensions(t *testing.T) {
for _, tc := range []struct {
name string
dimensions []Dimension
expectedErr string
name string
dimensions []Dimension
overrideDefaultDimensions []Dimension
expectedErr string
}{
{
name: "no additional dimensions",
Expand Down Expand Up @@ -158,9 +159,28 @@ func TestValidateDimensions(t *testing.T) {
},
expectedErr: "duplicate dimension name service_name",
},
{
name: "no duplicate dimensions in overrideDefaultDimensions and dimensions",
dimensions: []Dimension{
{Name: "service_name"},
},
overrideDefaultDimensions: []Dimension{
{Name: "http.service"},
},
},
{
name: "duplicate dimensions in overrideDefaultDimensions and dimensions",
dimensions: []Dimension{
{Name: "service_name"},
},
overrideDefaultDimensions: []Dimension{
{Name: "service_name"},
},
expectedErr: "duplicate dimension name service_name",
},
} {
t.Run(tc.name, func(t *testing.T) {
err := validateDimensions(tc.dimensions)
err := validateDimensions(tc.dimensions, tc.overrideDefaultDimensions)
if tc.expectedErr != "" {
assert.EqualError(t, err, tc.expectedErr)
} else {
Expand Down

0 comments on commit 22543ba

Please sign in to comment.