6
6
package sumconnector // import "github.com/open-telemetry/opentelemetry-collector-contrib/connector/sumconnector"
7
7
8
8
import (
9
+ "context"
10
+
9
11
"go.opentelemetry.io/collector/component"
10
12
"go.opentelemetry.io/collector/connector"
13
+ "go.opentelemetry.io/collector/consumer"
11
14
12
15
"github.com/open-telemetry/opentelemetry-collector-contrib/connector/sumconnector/internal/metadata"
16
+ "github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/expr"
17
+ "github.com/open-telemetry/opentelemetry-collector-contrib/internal/filter/filterottl"
18
+ "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
19
+ "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottldatapoint"
20
+ "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottllog"
21
+ "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlmetric"
22
+ "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlspan"
23
+ "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl/contexts/ottlspanevent"
13
24
)
14
25
15
26
// NewFactory returns a ConnectorFactory.
16
27
func NewFactory () connector.Factory {
17
28
return connector .NewFactory (
18
29
metadata .Type ,
19
30
createDefaultConfig ,
20
- connector .WithTracesToMetrics (nil , metadata .TracesToMetricsStability ),
21
- connector .WithMetricsToMetrics (nil , metadata .MetricsToMetricsStability ),
22
- connector .WithLogsToMetrics (nil , metadata .LogsToMetricsStability ),
31
+ connector .WithTracesToMetrics (createTracesToMetrics , metadata .TracesToMetricsStability ),
32
+ connector .WithMetricsToMetrics (createMetricsToMetrics , metadata .MetricsToMetricsStability ),
33
+ connector .WithLogsToMetrics (createLogsToMetrics , metadata .LogsToMetricsStability ),
23
34
)
24
35
}
25
36
@@ -28,3 +39,124 @@ func createDefaultConfig() component.Config {
28
39
return & Config {}
29
40
}
30
41
42
+ // createTracesToMetrics creates a traces to metrics connector based on provided config.
43
+ func createTracesToMetrics (
44
+ _ context.Context ,
45
+ set connector.Settings ,
46
+ cfg component.Config ,
47
+ nextConsumer consumer.Metrics ,
48
+ ) (connector.Traces , error ) {
49
+ c := cfg .(* Config )
50
+
51
+ spanMetricDefs := make (map [string ]metricDef [ottlspan.TransformContext ], len (c .Spans ))
52
+ for name , info := range c .Spans {
53
+ md := metricDef [ottlspan.TransformContext ]{
54
+ desc : info .Description ,
55
+ attrs : info .Attributes ,
56
+ }
57
+ if len (info .Conditions ) > 0 {
58
+ // Error checked in Config.Validate()
59
+ condition , _ := filterottl .NewBoolExprForSpan (info .Conditions , filterottl .StandardSpanFuncs (), ottl .PropagateError , set .TelemetrySettings )
60
+ md .condition = condition
61
+ }
62
+ spanMetricDefs [name ] = md
63
+ }
64
+
65
+ spanEventMetricDefs := make (map [string ]metricDef [ottlspanevent.TransformContext ], len (c .SpanEvents ))
66
+ for name , info := range c .SpanEvents {
67
+ md := metricDef [ottlspanevent.TransformContext ]{
68
+ desc : info .Description ,
69
+ attrs : info .Attributes ,
70
+ }
71
+ if len (info .Conditions ) > 0 {
72
+ // Error checked in Config.Validate()
73
+ condition , _ := filterottl .NewBoolExprForSpanEvent (info .Conditions , filterottl .StandardSpanEventFuncs (), ottl .PropagateError , set .TelemetrySettings )
74
+ md .condition = condition
75
+ }
76
+ spanEventMetricDefs [name ] = md
77
+ }
78
+
79
+ return & count {
80
+ metricsConsumer : nextConsumer ,
81
+ spansMetricDefs : spanMetricDefs ,
82
+ spanEventsMetricDefs : spanEventMetricDefs ,
83
+ }, nil
84
+ }
85
+
86
+ // createMetricsToMetrics creates a metricds to metrics connector based on provided config.
87
+ func createMetricsToMetrics (
88
+ _ context.Context ,
89
+ set connector.Settings ,
90
+ cfg component.Config ,
91
+ nextConsumer consumer.Metrics ,
92
+ ) (connector.Metrics , error ) {
93
+ c := cfg .(* Config )
94
+
95
+ metricMetricDefs := make (map [string ]metricDef [ottlmetric.TransformContext ], len (c .Metrics ))
96
+ for name , info := range c .Metrics {
97
+ md := metricDef [ottlmetric.TransformContext ]{
98
+ desc : info .Description ,
99
+ }
100
+ if len (info .Conditions ) > 0 {
101
+ // Error checked in Config.Validate()
102
+ condition , _ := filterottl .NewBoolExprForMetric (info .Conditions , filterottl .StandardMetricFuncs (), ottl .PropagateError , set .TelemetrySettings )
103
+ md .condition = condition
104
+ }
105
+ metricMetricDefs [name ] = md
106
+ }
107
+
108
+ dataPointMetricDefs := make (map [string ]metricDef [ottldatapoint.TransformContext ], len (c .DataPoints ))
109
+ for name , info := range c .DataPoints {
110
+ md := metricDef [ottldatapoint.TransformContext ]{
111
+ desc : info .Description ,
112
+ attrs : info .Attributes ,
113
+ }
114
+ if len (info .Conditions ) > 0 {
115
+ // Error checked in Config.Validate()
116
+ condition , _ := filterottl .NewBoolExprForDataPoint (info .Conditions , filterottl .StandardDataPointFuncs (), ottl .PropagateError , set .TelemetrySettings )
117
+ md .condition = condition
118
+ }
119
+ dataPointMetricDefs [name ] = md
120
+ }
121
+
122
+ return & count {
123
+ metricsConsumer : nextConsumer ,
124
+ metricsMetricDefs : metricMetricDefs ,
125
+ dataPointsMetricDefs : dataPointMetricDefs ,
126
+ }, nil
127
+ }
128
+
129
+ // createLogsToMetrics creates a logs to metrics connector based on provided config.
130
+ func createLogsToMetrics (
131
+ _ context.Context ,
132
+ set connector.Settings ,
133
+ cfg component.Config ,
134
+ nextConsumer consumer.Metrics ,
135
+ ) (connector.Logs , error ) {
136
+ c := cfg .(* Config )
137
+
138
+ metricDefs := make (map [string ]metricDef [ottllog.TransformContext ], len (c .Logs ))
139
+ for name , info := range c .Logs {
140
+ md := metricDef [ottllog.TransformContext ]{
141
+ desc : info .Description ,
142
+ attrs : info .Attributes ,
143
+ }
144
+ if len (info .Conditions ) > 0 {
145
+ // Error checked in Config.Validate()
146
+ condition , _ := filterottl .NewBoolExprForLog (info .Conditions , filterottl .StandardLogFuncs (), ottl .PropagateError , set .TelemetrySettings )
147
+ md .condition = condition
148
+ }
149
+ metricDefs [name ] = md
150
+ }
151
+
152
+ return & count {
153
+ metricsConsumer : nextConsumer ,
154
+ logsMetricDefs : metricDefs ,
155
+ }, nil
156
+ }
157
+
158
+ type metricDef [K any ] struct {
159
+ condition expr.BoolExpr [K ]
160
+ desc string
161
+ attrs []AttributeConfig
162
+ }
0 commit comments