-
Notifications
You must be signed in to change notification settings - Fork 489
/
Copy pathconfig.go
714 lines (624 loc) · 24.6 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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
package traces
import (
"encoding/base64"
"errors"
"fmt"
"io/ioutil"
"net"
"sort"
"time"
"github.com/grafana/agent/pkg/logs"
"github.com/grafana/agent/pkg/traces/automaticloggingprocessor"
"github.com/grafana/agent/pkg/traces/noopreceiver"
"github.com/grafana/agent/pkg/traces/promsdprocessor"
"github.com/grafana/agent/pkg/traces/remotewriteexporter"
"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/loadbalancingexporter"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/spanmetricsprocessor"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/tailsamplingprocessor"
"github.com/prometheus/client_golang/prometheus"
prom_config "github.com/prometheus/common/config"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/configloader"
"go.opentelemetry.io/collector/config/configparser"
"go.opentelemetry.io/collector/exporter/otlpexporter"
"go.opentelemetry.io/collector/exporter/otlphttpexporter"
"go.opentelemetry.io/collector/exporter/prometheusexporter"
"go.opentelemetry.io/collector/processor/attributesprocessor"
"go.opentelemetry.io/collector/processor/batchprocessor"
"go.opentelemetry.io/collector/receiver/jaegerreceiver"
"go.opentelemetry.io/collector/receiver/kafkareceiver"
"go.opentelemetry.io/collector/receiver/opencensusreceiver"
"go.opentelemetry.io/collector/receiver/otlpreceiver"
"go.opentelemetry.io/collector/receiver/zipkinreceiver"
)
const (
spanMetricsPipelineName = "metrics/spanmetrics"
// defaultDecisionWait is the default time to wait for a trace before making a sampling decision
defaultDecisionWait = time.Second * 5
// defaultLoadBalancingPort is the default port the agent uses for internal load balancing
defaultLoadBalancingPort = "4318"
// agent's load balancing options
dnsTagName = "dns"
staticTagName = "static"
// sampling policies
alwaysSamplePolicy = "always_sample"
stringAttributePolicy = "string_attribute"
numericAttributePolicy = "numeric_attribute"
rateLimitingPolicy = "rate_limiting"
latencyPolicy = "latency"
statusCodePolicy = "status_code"
)
// Config controls the configuration of Traces trace pipelines.
type Config struct {
Configs []InstanceConfig `yaml:"configs,omitempty"`
// Unmarshaled is true when the Config was unmarshaled from YAML.
Unmarshaled bool `yaml:"-"`
}
// UnmarshalYAML implements yaml.Unmarshaler.
func (c *Config) UnmarshalYAML(unmarshal func(interface{}) error) error {
c.Unmarshaled = true
type plain Config
return unmarshal((*plain)(c))
}
// Validate ensures that the Config is valid.
func (c *Config) Validate(logsConfig *logs.Config) error {
names := make(map[string]struct{}, len(c.Configs))
for idx, c := range c.Configs {
if c.Name == "" {
return fmt.Errorf("traces config at index %d is missing a name", idx)
}
if _, exist := names[c.Name]; exist {
return fmt.Errorf("found multiple traces configs with name %s", c.Name)
}
names[c.Name] = struct{}{}
}
for _, inst := range c.Configs {
if inst.AutomaticLogging != nil {
if err := inst.AutomaticLogging.Validate(logsConfig); err != nil {
return fmt.Errorf("failed to validate automatic_logging for traces config %s: %w", inst.Name, err)
}
}
}
return nil
}
// InstanceConfig configures an individual Traces trace pipeline.
type InstanceConfig struct {
Name string `yaml:"name"`
// Deprecated in favor of RemoteWrite and Batch.
PushConfig PushConfig `yaml:"push_config,omitempty"`
// RemoteWrite defines one or multiple backends that can receive the pipeline's traffic.
RemoteWrite []RemoteWriteConfig `yaml:"remote_write,omitempty"`
// Receivers: https://github.com/open-telemetry/opentelemetry-collector/blob/7d7ae2eb34b5d387627875c498d7f43619f37ee3/receiver/README.md
Receivers map[string]interface{} `yaml:"receivers,omitempty"`
// Batch: https://github.com/open-telemetry/opentelemetry-collector/blob/7d7ae2eb34b5d387627875c498d7f43619f37ee3/processor/batchprocessor/config.go#L24
Batch map[string]interface{} `yaml:"batch,omitempty"`
// Attributes: https://github.com/open-telemetry/opentelemetry-collector/blob/7d7ae2eb34b5d387627875c498d7f43619f37ee3/processor/attributesprocessor/config.go#L30
Attributes map[string]interface{} `yaml:"attributes,omitempty"`
// prom service discovery config
ScrapeConfigs []interface{} `yaml:"scrape_configs,omitempty"`
OperationType string `yaml:"prom_sd_operation_type,omitempty"`
// SpanMetricsProcessor: https://github.com/open-telemetry/opentelemetry-collector-contrib/blob/main/processor/spanmetricsprocessor/README.md
SpanMetrics *SpanMetricsConfig `yaml:"spanmetrics,omitempty"`
// AutomaticLogging
AutomaticLogging *automaticloggingprocessor.AutomaticLoggingConfig `yaml:"automatic_logging,omitempty"`
// TailSampling defines a sampling strategy for the pipeline
TailSampling *tailSamplingConfig `yaml:"tail_sampling"`
// LoadBalancing is used to distribute spans of the same trace to the same agent instance
LoadBalancing *loadBalancingConfig `yaml:"load_balancing"`
}
const (
compressionNone = "none"
compressionGzip = "gzip"
protocolGRPC = "grpc"
protocolHTTP = "http"
)
// DefaultPushConfig holds the default settings for a PushConfig.
var DefaultPushConfig = PushConfig{
Compression: compressionGzip,
}
// PushConfig controls the configuration of exporting to Grafana Cloud
type PushConfig struct {
Endpoint string `yaml:"endpoint,omitempty"`
Compression string `yaml:"compression,omitempty"`
Insecure bool `yaml:"insecure,omitempty"`
InsecureSkipVerify bool `yaml:"insecure_skip_verify,omitempty"`
BasicAuth *prom_config.BasicAuth `yaml:"basic_auth,omitempty,omitempty"`
Batch map[string]interface{} `yaml:"batch,omitempty"` // https://github.com/open-telemetry/opentelemetry-collector/blob/7d7ae2eb34b5d387627875c498d7f43619f37ee3/processor/batchprocessor/config.go#L24
SendingQueue map[string]interface{} `yaml:"sending_queue,omitempty"` // https://github.com/open-telemetry/opentelemetry-collector/blob/7d7ae2eb34b5d387627875c498d7f43619f37ee3/exporter/exporterhelper/queued_retry.go#L30
RetryOnFailure map[string]interface{} `yaml:"retry_on_failure,omitempty"` // https://github.com/open-telemetry/opentelemetry-collector/blob/7d7ae2eb34b5d387627875c498d7f43619f37ee3/exporter/exporterhelper/queued_retry.go#L54
}
// UnmarshalYAML implements yaml.Unmarshaler.
func (c *PushConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultPushConfig
type plain PushConfig
if err := unmarshal((*plain)(c)); err != nil {
return err
}
if c.Compression != compressionGzip && c.Compression != compressionNone {
return fmt.Errorf("unsupported compression '%s', expected 'gzip' or 'none'", c.Compression)
}
return nil
}
// DefaultRemoteWriteConfig holds the default settings for a PushConfig.
var DefaultRemoteWriteConfig = RemoteWriteConfig{
Compression: compressionGzip,
Protocol: protocolGRPC,
}
// RemoteWriteConfig controls the configuration of an exporter
type RemoteWriteConfig struct {
Endpoint string `yaml:"endpoint,omitempty"`
Compression string `yaml:"compression,omitempty"`
Protocol string `yaml:"protocol,omitempty"`
Insecure bool `yaml:"insecure,omitempty"`
// Deprecated
InsecureSkipVerify bool `yaml:"insecure_skip_verify,omitempty"`
TLSConfig *prom_config.TLSConfig `yaml:"tls_config,omitempty"`
BasicAuth *prom_config.BasicAuth `yaml:"basic_auth,omitempty"`
Headers map[string]string `yaml:"headers,omitempty"`
SendingQueue map[string]interface{} `yaml:"sending_queue,omitempty"` // https://github.com/open-telemetry/opentelemetry-collector/blob/7d7ae2eb34b5d387627875c498d7f43619f37ee3/exporter/exporterhelper/queued_retry.go#L30
RetryOnFailure map[string]interface{} `yaml:"retry_on_failure,omitempty"` // https://github.com/open-telemetry/opentelemetry-collector/blob/7d7ae2eb34b5d387627875c498d7f43619f37ee3/exporter/exporterhelper/queued_retry.go#L54
}
// UnmarshalYAML implements yaml.Unmarshaler.
func (c *RemoteWriteConfig) UnmarshalYAML(unmarshal func(interface{}) error) error {
*c = DefaultRemoteWriteConfig
type plain RemoteWriteConfig
if err := unmarshal((*plain)(c)); err != nil {
return err
}
if c.Compression != compressionGzip && c.Compression != compressionNone {
return fmt.Errorf("unsupported compression '%s', expected 'gzip' or 'none'", c.Compression)
}
return nil
}
// SpanMetricsConfig controls the configuration of spanmetricsprocessor and the related metrics exporter.
type SpanMetricsConfig struct {
LatencyHistogramBuckets []time.Duration `yaml:"latency_histogram_buckets,omitempty"`
Dimensions []spanmetricsprocessor.Dimension `yaml:"dimensions,omitempty"`
// Namespace if set, exports metrics under the provided value.
Namespace string `yaml:"namespace,omitempty"`
// ConstLabels are values that are applied for every exported metric.
ConstLabels *prometheus.Labels `yaml:"const_labels,omitempty"`
// MetricsInstance is the Agent's metrics instance that will be used to push metrics
MetricsInstance string `yaml:"metrics_instance"`
// HandlerEndpoint is the address where a prometheus exporter will be exposed
HandlerEndpoint string `yaml:"handler_endpoint"`
}
// tailSamplingConfig is the configuration for tail-based sampling
type tailSamplingConfig struct {
// Policies are the strategies used for sampling. Multiple policies can be used in the same pipeline.
// For more information, refer to https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/processor/tailsamplingprocessor
Policies []map[string]interface{} `yaml:"policies"`
// DecisionWait defines the time to wait for a complete trace before making a decision
DecisionWait time.Duration `yaml:"decision_wait,omitempty"`
}
// loadBalancingConfig defines the configuration for load balancing spans between agent instances
// loadBalancingConfig is an OTel exporter's config with extra resolver config
type loadBalancingConfig struct {
Exporter exporterConfig `yaml:"exporter"`
Resolver map[string]interface{} `yaml:"resolver"`
// ReceiverPort is the port the instance will use to receive load balanced traces
ReceiverPort string `yaml:"receiver_port"`
}
// exporterConfig defined the config for a otlp exporter for load balancing
type exporterConfig struct {
Compression string `yaml:"compression,omitempty"`
Insecure bool `yaml:"insecure,omitempty"`
InsecureSkipVerify bool `yaml:"insecure_skip_verify,omitempty"`
BasicAuth *prom_config.BasicAuth `yaml:"basic_auth,omitempty"`
}
// exporter builds an OTel exporter from RemoteWriteConfig
func exporter(rwCfg RemoteWriteConfig) (map[string]interface{}, error) {
if len(rwCfg.Endpoint) == 0 {
return nil, errors.New("must have a configured a backend endpoint")
}
headers := map[string]string{}
if rwCfg.Headers != nil {
headers = rwCfg.Headers
}
if rwCfg.BasicAuth != nil {
password := string(rwCfg.BasicAuth.Password)
if len(rwCfg.BasicAuth.PasswordFile) > 0 {
buff, err := ioutil.ReadFile(rwCfg.BasicAuth.PasswordFile)
if err != nil {
return nil, fmt.Errorf("unable to load password file %s: %w", rwCfg.BasicAuth.PasswordFile, err)
}
password = string(buff)
}
encodedAuth := base64.StdEncoding.EncodeToString([]byte(rwCfg.BasicAuth.Username + ":" + password))
headers["authorization"] = "Basic " + encodedAuth
}
compression := rwCfg.Compression
if compression == compressionNone {
compression = ""
}
otlpExporter := map[string]interface{}{
"endpoint": rwCfg.Endpoint,
"compression": compression,
"headers": headers,
"insecure": rwCfg.Insecure,
"sending_queue": rwCfg.SendingQueue,
"retry_on_failure": rwCfg.RetryOnFailure,
}
if !rwCfg.Insecure {
// If there is a TLSConfig use it
if rwCfg.TLSConfig != nil {
otlpExporter["ca_file"] = rwCfg.TLSConfig.CAFile
otlpExporter["cert_file"] = rwCfg.TLSConfig.CertFile
otlpExporter["key_file"] = rwCfg.TLSConfig.KeyFile
otlpExporter["insecure_skip_verify"] = rwCfg.TLSConfig.InsecureSkipVerify
} else {
// If not, set whatever value is specified in the old config.
otlpExporter["insecure_skip_verify"] = rwCfg.InsecureSkipVerify
}
}
// Apply some sane defaults to the exporter. The
// sending_queue.retry_on_failure default is 300s which prevents any
// sending-related errors to not be logged for 5 minutes. We'll lower that
// to 60s.
if retryConfig := otlpExporter["retry_on_failure"].(map[string]interface{}); retryConfig == nil {
otlpExporter["retry_on_failure"] = map[string]interface{}{
"max_elapsed_time": "60s",
}
} else if retryConfig["max_elapsed_time"] == nil {
retryConfig["max_elapsed_time"] = "60s"
}
return otlpExporter, nil
}
// exporters builds one or multiple exporters from a remote_write block.
// It also supports building an exporter from push_config.
func (c *InstanceConfig) exporters() (map[string]interface{}, error) {
if len(c.RemoteWrite) == 0 {
otlpExporter, err := exporter(RemoteWriteConfig{
Endpoint: c.PushConfig.Endpoint,
Compression: c.PushConfig.Compression,
Insecure: c.PushConfig.Insecure,
TLSConfig: &prom_config.TLSConfig{
InsecureSkipVerify: c.PushConfig.InsecureSkipVerify,
},
BasicAuth: c.PushConfig.BasicAuth,
SendingQueue: c.PushConfig.SendingQueue,
RetryOnFailure: c.PushConfig.RetryOnFailure,
})
return map[string]interface{}{
"otlp": otlpExporter,
}, err
}
exporters := map[string]interface{}{}
for i, remoteWriteConfig := range c.RemoteWrite {
exporter, err := exporter(remoteWriteConfig)
if err != nil {
return nil, err
}
var exporterName string
switch remoteWriteConfig.Protocol {
case protocolGRPC:
exporterName = fmt.Sprintf("otlp/%d", i)
case protocolHTTP:
exporterName = fmt.Sprintf("otlphttp/%d", i)
}
exporters[exporterName] = exporter
}
return exporters, nil
}
func resolver(config map[string]interface{}) (map[string]interface{}, error) {
if len(config) == 0 {
return nil, fmt.Errorf("must configure one resolver (dns or static)")
}
resolverCfg := make(map[string]interface{})
for typ, cfg := range config {
switch typ {
case dnsTagName, staticTagName:
resolverCfg[typ] = cfg
default:
return nil, fmt.Errorf("unsupported resolver config type: %s", typ)
}
}
return resolverCfg, nil
}
func (c *InstanceConfig) loadBalancingExporter() (map[string]interface{}, error) {
exporter, err := exporter(RemoteWriteConfig{
// Endpoint is omitted in OTel load balancing exporter
Endpoint: "noop",
Compression: c.LoadBalancing.Exporter.Compression,
Insecure: c.LoadBalancing.Exporter.Insecure,
TLSConfig: &prom_config.TLSConfig{InsecureSkipVerify: c.LoadBalancing.Exporter.InsecureSkipVerify},
BasicAuth: c.LoadBalancing.Exporter.BasicAuth,
})
if err != nil {
return nil, err
}
resolverCfg, err := resolver(c.LoadBalancing.Resolver)
if err != nil {
return nil, err
}
return map[string]interface{}{
"protocol": map[string]interface{}{
"otlp": exporter,
},
"resolver": resolverCfg,
}, nil
}
// formatPolicies creates sampling policies (i.e. rules) compatible with OTel's tail sampling processor
// https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/v0.21.0/processor/tailsamplingprocessor
func formatPolicies(cfg []map[string]interface{}) ([]map[string]interface{}, error) {
policies := make([]map[string]interface{}, 0, len(cfg))
for i, policy := range cfg {
if len(policy) != 1 {
return nil, errors.New("malformed sampling policy")
}
for typ, rules := range policy {
switch typ {
case alwaysSamplePolicy:
policies = append(policies, map[string]interface{}{
"name": fmt.Sprintf("%s/%d", typ, i),
"type": typ,
})
case stringAttributePolicy, rateLimitingPolicy, numericAttributePolicy, latencyPolicy, statusCodePolicy:
policies = append(policies, map[string]interface{}{
"name": fmt.Sprintf("%s/%d", typ, i),
"type": typ,
typ: rules,
})
default:
return nil, fmt.Errorf("unsupported policy type %s", typ)
}
}
}
return policies, nil
}
func (c *InstanceConfig) otelConfig() (*config.Config, error) {
otelMapStructure := map[string]interface{}{}
if len(c.Receivers) == 0 {
return nil, errors.New("must have at least one configured receiver")
}
if len(c.RemoteWrite) != 0 && len(c.PushConfig.Endpoint) != 0 {
return nil, errors.New("must not configure push_config and remote_write. push_config is deprecated in favor of remote_write")
}
if c.Batch != nil && c.PushConfig.Batch != nil {
return nil, errors.New("must not configure push_config.batch and batch. push_config.batch is deprecated in favor of batch")
}
exporters, err := c.exporters()
if err != nil {
return nil, err
}
exportersNames := make([]string, 0, len(exporters))
for name := range exporters {
exportersNames = append(exportersNames, name)
}
// processors
processors := map[string]interface{}{}
processorNames := []string{}
if c.ScrapeConfigs != nil {
opType := promsdprocessor.OperationTypeUpsert
if c.OperationType != "" {
opType = c.OperationType
}
processorNames = append(processorNames, promsdprocessor.TypeStr)
processors[promsdprocessor.TypeStr] = map[string]interface{}{
"scrape_configs": c.ScrapeConfigs,
"operation_type": opType,
}
}
if c.AutomaticLogging != nil {
processorNames = append(processorNames, automaticloggingprocessor.TypeStr)
processors[automaticloggingprocessor.TypeStr] = map[string]interface{}{
"automatic_logging": c.AutomaticLogging,
}
}
if c.Attributes != nil {
processors["attributes"] = c.Attributes
processorNames = append(processorNames, "attributes")
}
if c.Batch != nil {
processors["batch"] = c.Batch
processorNames = append(processorNames, "batch")
} else if c.PushConfig.Batch != nil {
processors["batch"] = c.PushConfig.Batch
processorNames = append(processorNames, "batch")
}
pipelines := make(map[string]interface{})
if c.SpanMetrics != nil {
// Configure the metrics exporter.
namespace := "traces_spanmetrics"
if len(c.SpanMetrics.Namespace) != 0 {
namespace = fmt.Sprintf("%s_%s", c.SpanMetrics.Namespace, namespace)
}
var exporterName string
if len(c.SpanMetrics.MetricsInstance) != 0 && len(c.SpanMetrics.HandlerEndpoint) == 0 {
exporterName = remotewriteexporter.TypeStr
exporters[remotewriteexporter.TypeStr] = map[string]interface{}{
"namespace": namespace,
"const_labels": c.SpanMetrics.ConstLabels,
"metrics_instance": c.SpanMetrics.MetricsInstance,
}
} else if len(c.SpanMetrics.MetricsInstance) == 0 && len(c.SpanMetrics.HandlerEndpoint) != 0 {
exporterName = "prometheus"
exporters[exporterName] = map[string]interface{}{
"endpoint": c.SpanMetrics.HandlerEndpoint,
"namespace": namespace,
"const_labels": c.SpanMetrics.ConstLabels,
}
} else {
return nil, fmt.Errorf("must specify a prometheus instance or a metrics handler endpoint to export the metrics")
}
processorNames = append(processorNames, "spanmetrics")
processors["spanmetrics"] = map[string]interface{}{
"metrics_exporter": exporterName,
"latency_histogram_buckets": c.SpanMetrics.LatencyHistogramBuckets,
"dimensions": c.SpanMetrics.Dimensions,
}
pipelines[spanMetricsPipelineName] = map[string]interface{}{
"receivers": []string{noopreceiver.TypeStr},
"exporters": []string{exporterName},
}
}
// receivers
receiverNames := []string{}
for name := range c.Receivers {
receiverNames = append(receiverNames, name)
}
if c.TailSampling != nil {
wait := defaultDecisionWait
if c.TailSampling.DecisionWait != 0 {
wait = c.TailSampling.DecisionWait
}
policies, err := formatPolicies(c.TailSampling.Policies)
if err != nil {
return nil, err
}
// tail_sampling should be executed before the batch processor
// TODO(mario.rodriguez): put attributes processor before tail_sampling. Maybe we want to sample on mutated spans
processorNames = append([]string{"tail_sampling"}, processorNames...)
processors["tail_sampling"] = map[string]interface{}{
"policies": policies,
"decision_wait": wait,
}
}
if c.LoadBalancing != nil {
internalExporter, err := c.loadBalancingExporter()
if err != nil {
return nil, err
}
exporters["loadbalancing"] = internalExporter
receiverPort := defaultLoadBalancingPort
if c.LoadBalancing.ReceiverPort != "" {
receiverPort = c.LoadBalancing.ReceiverPort
}
c.Receivers["otlp/lb"] = map[string]interface{}{
"protocols": map[string]interface{}{
"grpc": map[string]interface{}{
"endpoint": net.JoinHostPort("0.0.0.0", receiverPort),
},
},
}
}
// Build Pipelines
splitPipeline := c.LoadBalancing != nil
orderedSplitProcessors := orderProcessors(processorNames, splitPipeline)
if splitPipeline {
// load balancing pipeline
pipelines["traces/0"] = map[string]interface{}{
"receivers": receiverNames,
"processors": orderedSplitProcessors[0],
"exporters": []string{"loadbalancing"},
}
// processing pipeline
pipelines["traces/1"] = map[string]interface{}{
"exporters": exportersNames,
"processors": orderedSplitProcessors[1],
"receivers": []string{"otlp/lb"},
}
} else {
pipelines["traces"] = map[string]interface{}{
"exporters": exportersNames,
"processors": orderedSplitProcessors[0],
"receivers": receiverNames,
}
}
if c.SpanMetrics != nil {
// Insert a noop receiver in the metrics pipeline.
// Added to pass validation requiring at least one receiver in a pipeline.
c.Receivers[noopreceiver.TypeStr] = nil
}
otelMapStructure["exporters"] = exporters
otelMapStructure["processors"] = processors
otelMapStructure["receivers"] = c.Receivers
// pipelines
otelMapStructure["service"] = map[string]interface{}{
"pipelines": pipelines,
}
factories, err := tracingFactories()
if err != nil {
return nil, fmt.Errorf("failed to create factories: %w", err)
}
parser := configparser.NewParserFromStringMap(otelMapStructure)
otelCfg, err := configloader.Load(parser, factories)
if err != nil {
return nil, fmt.Errorf("failed to load OTel config: %w", err)
}
return otelCfg, nil
}
// tracingFactories() only creates the needed factories. if we decide to add support for a new
// processor, exporter, receiver we need to add it here
func tracingFactories() (component.Factories, error) {
extensions, err := component.MakeExtensionFactoryMap()
if err != nil {
return component.Factories{}, err
}
receivers, err := component.MakeReceiverFactoryMap(
jaegerreceiver.NewFactory(),
zipkinreceiver.NewFactory(),
otlpreceiver.NewFactory(),
opencensusreceiver.NewFactory(),
kafkareceiver.NewFactory(),
noopreceiver.NewFactory(),
)
if err != nil {
return component.Factories{}, err
}
exporters, err := component.MakeExporterFactoryMap(
otlpexporter.NewFactory(),
otlphttpexporter.NewFactory(),
loadbalancingexporter.NewFactory(),
prometheusexporter.NewFactory(),
remotewriteexporter.NewFactory(),
)
if err != nil {
return component.Factories{}, err
}
processors, err := component.MakeProcessorFactoryMap(
batchprocessor.NewFactory(),
attributesprocessor.NewFactory(),
promsdprocessor.NewFactory(),
spanmetricsprocessor.NewFactory(),
automaticloggingprocessor.NewFactory(),
tailsamplingprocessor.NewFactory(),
)
if err != nil {
return component.Factories{}, err
}
return component.Factories{
Extensions: extensions,
Receivers: receivers,
Processors: processors,
Exporters: exporters,
}, nil
}
// orders the passed processors into their preferred order in a tracing pipeline. pass
// true to splitPipelines if this function should split the input pipelines into two
// sets: before and after load balancing
func orderProcessors(processors []string, splitPipelines bool) [][]string {
order := map[string]int{
"attributes": 0,
"spanmetrics": 1,
"tail_sampling": 2,
"automatic_logging": 3,
"batch": 4,
}
sort.Slice(processors, func(i, j int) bool {
iVal := order[processors[i]]
jVal := order[processors[j]]
return iVal < jVal
})
if !splitPipelines {
return [][]string{
processors,
}
}
// if we're splitting pipelines we have to look for the first processor that belongs in the second
// stage and split on that. if nothing belongs in the second stage just leave them all in the first
foundAt := len(processors)
for i, processor := range processors {
if processor == "batch" ||
processor == "tail_sampling" ||
processor == "automatic_logging" {
foundAt = i
break
}
}
return [][]string{
processors[:foundAt],
processors[foundAt:],
}
}