-
Notifications
You must be signed in to change notification settings - Fork 25
/
config.go
176 lines (152 loc) · 4.94 KB
/
config.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package otelcollector
import (
"context"
"sort"
"time"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/processor/batchprocessor"
)
// OTELConfigUnmarshaller can be used as an OTEL config map provider.
type OTELConfigUnmarshaller struct {
config map[string]interface{}
}
// NewOTELConfigUnmarshaler creates a new OTELConfigUnmarshaler instance.
func NewOTELConfigUnmarshaler(config map[string]interface{}) *OTELConfigUnmarshaller {
return &OTELConfigUnmarshaller{config: config}
}
// Implements MapProvider interface
// Retrieve returns the value to be injected in the configuration and the corresponding watcher.
func (u *OTELConfigUnmarshaller) Retrieve(_ context.Context, _ string, _ confmap.WatcherFunc) (*confmap.Retrieved, error) {
return confmap.NewRetrieved(u.config)
}
// Shutdown indicates the provider should close.
func (u *OTELConfigUnmarshaller) Shutdown(ctx context.Context) error {
return nil
}
// Scheme returns the scheme name, location scheme used by Retrieve.
func (u *OTELConfigUnmarshaller) Scheme() string {
return schemeName
}
// OTELConfig represents OTEL Collector configuration.
type OTELConfig struct {
Extensions map[string]interface{} `json:"extensions,omitempty"`
Receivers map[string]interface{} `json:"receivers,omitempty"`
Processors map[string]interface{} `json:"processors,omitempty"`
Exporters map[string]interface{} `json:"exporters,omitempty"`
Service *OTELService `json:"service"`
}
// NewOTELConfig creates new empty OTELConfig.
func NewOTELConfig() *OTELConfig {
return &OTELConfig{
Extensions: map[string]interface{}{},
Receivers: map[string]interface{}{},
Processors: map[string]interface{}{},
Exporters: map[string]interface{}{},
Service: NewOTELService(),
}
}
// AsMap returns map representation of OTELConfig.
func (o *OTELConfig) AsMap() map[string]interface{} {
return map[string]interface{}{
"extensions": o.Extensions,
"receivers": o.Receivers,
"processors": o.Processors,
"exporters": o.Exporters,
"service": o.Service.AsMap(),
}
}
// AddExtension adds given extension and enables it in service.
func (o *OTELConfig) AddExtension(name string, value interface{}) {
if value == nil {
value = map[string]interface{}{}
}
o.Extensions[name] = value
extensions := make([]string, 0, len(o.Extensions))
for extension := range o.Extensions {
extensions = append(extensions, extension)
}
sort.Strings(extensions)
o.Service.Extensions = extensions
}
// AddReceiver adds receiver to OTEL config.
func (o *OTELConfig) AddReceiver(name string, value interface{}) {
o.Receivers[name] = value
}
// AddProcessor adds receiver to OTEL config.
func (o *OTELConfig) AddProcessor(name string, value interface{}) {
o.Processors[name] = value
}
// AddExporter adds receiver to OTEL config.
func (o *OTELConfig) AddExporter(name string, value interface{}) {
o.Exporters[name] = value
}
// AddDebugExtensions adds common debug extextions and enables them.
func (o *OTELConfig) AddDebugExtensions() {
o.AddExtension("health_check", nil)
o.AddExtension("pprof", map[string]interface{}{
"endpoint": "localhost:1777",
})
o.AddExtension("zpages", map[string]interface{}{
"endpoint": "localhost:55679",
})
}
// AddBatchProcessor is a helper function for adding batch processor.
func (o *OTELConfig) AddBatchProcessor(name string, timeout time.Duration, sendBatchSize uint32) {
o.AddProcessor(name, batchprocessor.Config{
Timeout: timeout,
SendBatchSize: sendBatchSize,
})
}
// OTELService represents service in OTEL Config.
type OTELService struct {
Telemetry map[string]interface{}
Pipelines map[string]Pipeline
Extensions []string
}
// NewOTELService returns new empty OTEL Service.
func NewOTELService() *OTELService {
return &OTELService{
Telemetry: map[string]interface{}{
"logs": map[string]interface{}{
"level": "INFO",
},
},
Pipelines: map[string]Pipeline{},
Extensions: []string{},
}
}
// AsMap returns map representation of OTELService.
func (o *OTELService) AsMap() map[string]interface{} {
pipelines := map[string]interface{}{}
for name, pipe := range o.Pipelines {
pipelines[name] = pipe.AsMap()
}
return map[string]interface{}{
"telemetry": o.Telemetry,
"extensions": o.Extensions,
"pipelines": pipelines,
}
}
// AddPipeline adds pipeline to OTEL Service.
func (o *OTELService) AddPipeline(name string, pipeline Pipeline) {
o.Pipelines[name] = pipeline
}
// Pipeline gets pipeline with given name from OTEL Service together with `exists` bool.
func (o *OTELService) Pipeline(name string) (Pipeline, bool) {
pipeline, exists := o.Pipelines[name]
return pipeline, exists
}
// Pipeline represents OTEL Config pipeline.
type Pipeline struct {
Receivers []string
Processors []string
Exporters []string
}
// AsMap returns map representation of Pipeline.
func (p *Pipeline) AsMap() map[string]interface{} {
return map[string]interface{}{
"receivers": p.Receivers,
"processors": p.Processors,
"exporters": p.Exporters,
}
}