Skip to content

Commit

Permalink
32669 sumconnector config (#33994)
Browse files Browse the repository at this point in the history
**Description:** Fleshes out the config more fully and provides tests
for the config and possible combinations of settings

**Link to tracking Issue:** #32669

**Testing:** Added test cases and testing for config

**Documentation:** No new documentation yet.
  • Loading branch information
greatestusername authored Jul 25, 2024
1 parent 2b29645 commit 358ab68
Show file tree
Hide file tree
Showing 5 changed files with 878 additions and 13 deletions.
96 changes: 96 additions & 0 deletions connector/sumconnector/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@

package sumconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/sumconnector"

import (
"errors"
"fmt"

"go.opentelemetry.io/collector/component"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)

// Config for the connector
type Config struct {
Spans map[string]MetricInfo `mapstructure:"spans"`
Expand All @@ -24,3 +35,88 @@ type AttributeConfig struct {
Key string `mapstructure:"key"`
DefaultValue any `mapstructure:"default_value"`
}

func (c *Config) Validate() (combinedErrors error) {
for name, info := range c.Spans {
if name == "" {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("spans: metric name missing"))
}
if info.SourceAttribute == "" {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("spans: metric source_attribute missing"))
}
if _, err := filterottl.NewBoolExprForSpan(info.Conditions, filterottl.StandardSpanFuncs(), ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()}); err != nil {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("spans condition: metric %q: %w", name, err))
}
if err := info.validateAttributes(); err != nil {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("spans attributes: metric %q: %w", name, err))
}
}
for name, info := range c.SpanEvents {
if name == "" {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("spanevents: metric name missing"))
}
if info.SourceAttribute == "" {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("spanevents: metric source_attribute missing"))
}
if _, err := filterottl.NewBoolExprForSpanEvent(info.Conditions, filterottl.StandardSpanEventFuncs(), ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()}); err != nil {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("spanevents condition: metric %q: %w", name, err))
}
if err := info.validateAttributes(); err != nil {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("spanevents attributes: metric %q: %w", name, err))
}
}
for name, info := range c.Metrics {
if name == "" {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("metrics: metric name missing"))
}
if info.SourceAttribute == "" {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("metrics: metric source_attribute missing"))
}
if _, err := filterottl.NewBoolExprForMetric(info.Conditions, filterottl.StandardMetricFuncs(), ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()}); err != nil {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("metrics condition: metric %q: %w", name, err))
}
if len(info.Attributes) > 0 {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("metrics attributes not supported: metric %q", name))
}
}
for name, info := range c.DataPoints {
if name == "" {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("datapoints: metric name missing"))
}
if info.SourceAttribute == "" {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("datapoints: metric source_attribute missing"))
}
if _, err := filterottl.NewBoolExprForDataPoint(info.Conditions, filterottl.StandardDataPointFuncs(), ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()}); err != nil {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("datapoints condition: metric %q: %w", name, err))
}
if err := info.validateAttributes(); err != nil {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("datapoints attributes: metric %q: %w", name, err))
}
}
for name, info := range c.Logs {
if name == "" {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("logs: metric name missing"))
}
if info.SourceAttribute == "" {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("logs: metric source_attribute missing"))
}
if _, err := filterottl.NewBoolExprForLog(info.Conditions, filterottl.StandardLogFuncs(), ottl.PropagateError, component.TelemetrySettings{Logger: zap.NewNop()}); err != nil {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("logs condition: metric %q: %w", name, err))
}
if err := info.validateAttributes(); err != nil {
combinedErrors = errors.Join(combinedErrors, fmt.Errorf("logs attributes: metric %q: %w", name, err))
}
}
return combinedErrors
}

func (i *MetricInfo) validateAttributes() error {
for _, attr := range i.Attributes {
if attr.Key == "" {
return fmt.Errorf("attribute key missing")
}
}
return nil
}

var _ component.ConfigValidator = (*Config)(nil)
Loading

0 comments on commit 358ab68

Please sign in to comment.