Debian Codegraph @

otelcol/config.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package otelcol // import "go.opentelemetry.io/collector/otelcol"
5
6 import (
7 "errors"
8 "fmt"
9
10 "go.opentelemetry.io/collector/component"
11 "go.opentelemetry.io/collector/service"
12 "go.opentelemetry.io/collector/service/pipelines"
13 )
14
15 var (
16 errMissingExporters = errors.New("no exporter configuration specified in config")
17 errMissingReceivers = errors.New("no receiver configuration specified in config")
18 errEmptyConfigurationFile = errors.New("empty configuration file")
19 )
20
21 // Config defines the configuration for the various elements of collector or agent.
22 type Config struct {
23 // Receivers is a map of ComponentID to Receivers.
24 Receivers map[component.ID]component.Config `mapstructure:"receivers"`
25
26 // Exporters is a map of ComponentID to Exporters.
27 Exporters map[component.ID]component.Config `mapstructure:"exporters"`
28
29 // Processors is a map of ComponentID to Processors.
30 Processors map[component.ID]component.Config `mapstructure:"processors"`
31
32 // Connectors is a map of ComponentID to connectors.
33 Connectors map[component.ID]component.Config `mapstructure:"connectors"`
34
35 // Extensions is a map of ComponentID to extensions.
36 Extensions map[component.ID]component.Config `mapstructure:"extensions"`
37
38 Service service.Config `mapstructure:"service"`
39
40 // prevent unkeyed literal initialization
41 _ struct{}
42 }
43
44 // Validate returns an error if the config is invalid.
45 //
46 // This function performs basic validation of configuration. There may be more subtle
47 // invalid cases that we currently don't check for but which we may want to add in
48 // the future (e.g. disallowing receiving and exporting on the same endpoint).
49 func (cfg *Config) Validate() error {
50 // There must be at least one property set in the configuration file.
51 if len(cfg.Receivers) == 0 && len(cfg.Exporters) == 0 && len(cfg.Processors) == 0 && len(cfg.Connectors) == 0 && len(cfg.Extensions) == 0 {
52 return errEmptyConfigurationFile
53 }
54
55 // Currently, there is no default receiver enabled.
56 // The configuration must specify at least one receiver to be valid.
57 if !pipelines.AllowNoPipelines.IsEnabled() && len(cfg.Receivers) == 0 {
58 return errMissingReceivers
59 }
60
61 // Currently, there is no default exporter enabled.
62 // The configuration must specify at least one exporter to be valid.
63 if !pipelines.AllowNoPipelines.IsEnabled() && len(cfg.Exporters) == 0 {
64 return errMissingExporters
65 }
66
67 // Validate the connector configuration.
68 for connID := range cfg.Connectors {
69 if _, ok := cfg.Exporters[connID]; ok {
70 return fmt.Errorf("connectors::%s: ambiguous ID: Found both %q exporter and %q connector. "+
71 "Change one of the components' IDs to eliminate ambiguity (e.g. rename %q connector to %q)",
72 connID, connID, connID, connID, connID.String()+"/connector")
73 }
74 if _, ok := cfg.Receivers[connID]; ok {
75 return fmt.Errorf("connectors::%s: ambiguous ID: Found both %q receiver and %q connector. "+
76 "Change one of the components' IDs to eliminate ambiguity (e.g. rename %q connector to %q)",
77 connID, connID, connID, connID, connID.String()+"/connector")
78 }
79 }
80
81 // Check that all enabled extensions in the service are configured.
82 for _, ref := range cfg.Service.Extensions {
83 // Check that the name referenced in the Service extensions exists in the top-level extensions.
84 if cfg.Extensions[ref] == nil {
85 return fmt.Errorf("service::extensions: references extension %q which is not configured", ref)
86 }
87 }
88
89 // Check that all pipelines reference only configured components.
90 for pipelineID, pipeline := range cfg.Service.Pipelines {
91 // Validate pipeline receiver name references.
92 for _, ref := range pipeline.Receivers {
93 // Check that the name referenced in the pipeline's receivers exists in the top-level receivers.
94 if _, ok := cfg.Receivers[ref]; ok {
95 continue
96 }
97
98 if _, ok := cfg.Connectors[ref]; ok {
99 continue
100 }
101 return fmt.Errorf("service::pipelines::%s: references receiver %q which is not configured", pipelineID.String(), ref)
102 }
103
104 // Validate pipeline processor name references.
105 for _, ref := range pipeline.Processors {
106 // Check that the name referenced in the pipeline's processors exists in the top-level processors.
107 if cfg.Processors[ref] == nil {
108 return fmt.Errorf("service::pipelines::%s: references processor %q which is not configured", pipelineID.String(), ref)
109 }
110 }
111
112 // Validate pipeline exporter name references.
113 for _, ref := range pipeline.Exporters {
114 // Check that the name referenced in the pipeline's Exporters exists in the top-level Exporters.
115 if _, ok := cfg.Exporters[ref]; ok {
116 continue
117 }
118 if _, ok := cfg.Connectors[ref]; ok {
119 continue
120 }
121 return fmt.Errorf("service::pipelines::%s: references exporter %q which is not configured", pipelineID.String(), ref)
122 }
123 }
124 return nil
125 }