/
converter_k8sattributesprocessor.go
149 lines (120 loc) · 3.97 KB
/
converter_k8sattributesprocessor.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package otelcolconvert
import (
"fmt"
"github.com/grafana/alloy/internal/component/otelcol"
"github.com/grafana/alloy/internal/component/otelcol/processor/k8sattributes"
"github.com/grafana/alloy/internal/converter/diag"
"github.com/grafana/alloy/internal/converter/internal/common"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/k8sattributesprocessor"
"go.opentelemetry.io/collector/component"
)
func init() {
converters = append(converters, k8sAttributesProcessorConverter{})
}
type k8sAttributesProcessorConverter struct{}
func (k8sAttributesProcessorConverter) Factory() component.Factory {
return k8sattributesprocessor.NewFactory()
}
func (k8sAttributesProcessorConverter) InputComponentName() string {
return "otelcol.processor.k8sattributes"
}
func (k8sAttributesProcessorConverter) ConvertAndAppend(state *State, id component.InstanceID, cfg component.Config) diag.Diagnostics {
var diags diag.Diagnostics
label := state.AlloyComponentLabel()
args := toK8SAttributesProcessor(state, id, cfg.(*k8sattributesprocessor.Config))
block := common.NewBlockWithOverride([]string{"otelcol", "processor", "k8sattributes"}, label, args)
diags.Add(
diag.SeverityLevelInfo,
fmt.Sprintf("Converted %s into %s", StringifyInstanceID(id), StringifyBlock(block)),
)
state.Body().AppendBlock(block)
return diags
}
func toK8SAttributesProcessor(state *State, id component.InstanceID, cfg *k8sattributesprocessor.Config) *k8sattributes.Arguments {
var (
nextMetrics = state.Next(id, component.DataTypeMetrics)
nextLogs = state.Next(id, component.DataTypeLogs)
nextTraces = state.Next(id, component.DataTypeTraces)
)
return &k8sattributes.Arguments{
AuthType: string(cfg.AuthType),
Passthrough: cfg.Passthrough,
ExtractConfig: k8sattributes.ExtractConfig{
Metadata: cfg.Extract.Metadata,
Annotations: toFilterExtract(cfg.Extract.Annotations),
Labels: toFilterExtract(cfg.Extract.Labels),
},
Filter: k8sattributes.FilterConfig{
Node: cfg.Filter.Node,
Namespace: cfg.Filter.Namespace,
Fields: toFilterFields(cfg.Filter.Fields),
Labels: toFilterFields(cfg.Filter.Labels),
},
PodAssociations: toPodAssociations(cfg.Association),
Exclude: toExclude(cfg.Exclude),
Output: &otelcol.ConsumerArguments{
Metrics: ToTokenizedConsumers(nextMetrics),
Logs: ToTokenizedConsumers(nextLogs),
Traces: ToTokenizedConsumers(nextTraces),
},
}
}
func toExclude(cfg k8sattributesprocessor.ExcludeConfig) k8sattributes.ExcludeConfig {
res := k8sattributes.ExcludeConfig{
Pods: []k8sattributes.ExcludePodConfig{},
}
for _, c := range cfg.Pods {
res.Pods = append(res.Pods, k8sattributes.ExcludePodConfig{
Name: c.Name,
})
}
return res
}
func toPodAssociations(cfg []k8sattributesprocessor.PodAssociationConfig) []k8sattributes.PodAssociation {
if len(cfg) == 0 {
return nil
}
res := make([]k8sattributes.PodAssociation, 0, len(cfg))
for i, c := range cfg {
res = append(res, k8sattributes.PodAssociation{
Sources: []k8sattributes.PodAssociationSource{},
})
for _, c2 := range c.Sources {
res[i].Sources = append(res[i].Sources, k8sattributes.PodAssociationSource{
From: c2.From,
Name: c2.Name,
})
}
}
return res
}
func toFilterExtract(cfg []k8sattributesprocessor.FieldExtractConfig) []k8sattributes.FieldExtractConfig {
if len(cfg) == 0 {
return nil
}
res := make([]k8sattributes.FieldExtractConfig, 0, len(cfg))
for _, c := range cfg {
res = append(res, k8sattributes.FieldExtractConfig{
TagName: c.TagName,
Key: c.Key,
KeyRegex: c.KeyRegex,
Regex: c.Regex,
From: c.From,
})
}
return res
}
func toFilterFields(cfg []k8sattributesprocessor.FieldFilterConfig) []k8sattributes.FieldFilterConfig {
if len(cfg) == 0 {
return nil
}
res := make([]k8sattributes.FieldFilterConfig, 0, len(cfg))
for _, c := range cfg {
res = append(res, k8sattributes.FieldFilterConfig{
Key: c.Key,
Value: c.Value,
Op: c.Op,
})
}
return res
}