33
44package sumconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/sumconnector"
55
6+ import (
7+ "errors"
8+ "fmt"
9+
10+ "go.opentelemetry.io/collector/component"
11+ "go.uber.org/zap"
12+
13+ "github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterottl"
14+ "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
15+ )
16+
617// Config for the connector
718type Config struct {
819 Spans map [string ]MetricInfo `mapstructure:"spans"`
@@ -24,3 +35,88 @@ type AttributeConfig struct {
2435 Key string `mapstructure:"key"`
2536 DefaultValue any `mapstructure:"default_value"`
2637}
38+
39+ func (c * Config ) Validate () (combinedErrors error ) {
40+ for name , info := range c .Spans {
41+ if name == "" {
42+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("spans: metric name missing" ))
43+ }
44+ if info .SourceAttribute == "" {
45+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("spans: metric source_attribute missing" ))
46+ }
47+ if _ , err := filterottl .NewBoolExprForSpan (info .Conditions , filterottl .StandardSpanFuncs (), ottl .PropagateError , component.TelemetrySettings {Logger : zap .NewNop ()}); err != nil {
48+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("spans condition: metric %q: %w" , name , err ))
49+ }
50+ if err := info .validateAttributes (); err != nil {
51+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("spans attributes: metric %q: %w" , name , err ))
52+ }
53+ }
54+ for name , info := range c .SpanEvents {
55+ if name == "" {
56+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("spanevents: metric name missing" ))
57+ }
58+ if info .SourceAttribute == "" {
59+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("spanevents: metric source_attribute missing" ))
60+ }
61+ if _ , err := filterottl .NewBoolExprForSpanEvent (info .Conditions , filterottl .StandardSpanEventFuncs (), ottl .PropagateError , component.TelemetrySettings {Logger : zap .NewNop ()}); err != nil {
62+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("spanevents condition: metric %q: %w" , name , err ))
63+ }
64+ if err := info .validateAttributes (); err != nil {
65+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("spanevents attributes: metric %q: %w" , name , err ))
66+ }
67+ }
68+ for name , info := range c .Metrics {
69+ if name == "" {
70+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("metrics: metric name missing" ))
71+ }
72+ if info .SourceAttribute == "" {
73+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("metrics: metric source_attribute missing" ))
74+ }
75+ if _ , err := filterottl .NewBoolExprForMetric (info .Conditions , filterottl .StandardMetricFuncs (), ottl .PropagateError , component.TelemetrySettings {Logger : zap .NewNop ()}); err != nil {
76+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("metrics condition: metric %q: %w" , name , err ))
77+ }
78+ if len (info .Attributes ) > 0 {
79+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("metrics attributes not supported: metric %q" , name ))
80+ }
81+ }
82+ for name , info := range c .DataPoints {
83+ if name == "" {
84+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("datapoints: metric name missing" ))
85+ }
86+ if info .SourceAttribute == "" {
87+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("datapoints: metric source_attribute missing" ))
88+ }
89+ if _ , err := filterottl .NewBoolExprForDataPoint (info .Conditions , filterottl .StandardDataPointFuncs (), ottl .PropagateError , component.TelemetrySettings {Logger : zap .NewNop ()}); err != nil {
90+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("datapoints condition: metric %q: %w" , name , err ))
91+ }
92+ if err := info .validateAttributes (); err != nil {
93+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("datapoints attributes: metric %q: %w" , name , err ))
94+ }
95+ }
96+ for name , info := range c .Logs {
97+ if name == "" {
98+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("logs: metric name missing" ))
99+ }
100+ if info .SourceAttribute == "" {
101+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("logs: metric source_attribute missing" ))
102+ }
103+ if _ , err := filterottl .NewBoolExprForLog (info .Conditions , filterottl .StandardLogFuncs (), ottl .PropagateError , component.TelemetrySettings {Logger : zap .NewNop ()}); err != nil {
104+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("logs condition: metric %q: %w" , name , err ))
105+ }
106+ if err := info .validateAttributes (); err != nil {
107+ combinedErrors = errors .Join (combinedErrors , fmt .Errorf ("logs attributes: metric %q: %w" , name , err ))
108+ }
109+ }
110+ return combinedErrors
111+ }
112+
113+ func (i * MetricInfo ) validateAttributes () error {
114+ for _ , attr := range i .Attributes {
115+ if attr .Key == "" {
116+ return fmt .Errorf ("attribute key missing" )
117+ }
118+ }
119+ return nil
120+ }
121+
122+ var _ component.ConfigValidator = (* Config )(nil )
0 commit comments