/
config.go
114 lines (106 loc) · 3.85 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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package ecsobserver // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/observer/ecsobserver"
import (
"fmt"
"os"
"time"
)
const (
defaultRefreshInterval = 30 * time.Second
defaultJobLabelName = "prometheus_job"
awsRegionEnvKey = "AWS_REGION"
defaultDockerLabelMatcherPortLabel = "ECS_PROMETHEUS_EXPORTER_PORT"
)
type Config struct {
// ClusterName is the target ECS cluster name for service discovery.
ClusterName string `mapstructure:"cluster_name" yaml:"cluster_name"`
// ClusterRegion is the target ECS cluster's AWS region.
ClusterRegion string `mapstructure:"cluster_region" yaml:"cluster_region"`
// RefreshInterval determines how frequency at which the observer
// needs to poll for collecting information about new processes.
RefreshInterval time.Duration `mapstructure:"refresh_interval" yaml:"refresh_interval"`
// ResultFile is the output path of the discovered targets YAML file (optional).
// This is mainly used in conjunction with the Prometheus receiver.
ResultFile string `mapstructure:"result_file" yaml:"result_file"`
// JobLabelName is the override for prometheus job label, using `job` literal will cause error
// in otel prometheus receiver. See https://github.com/open-telemetry/opentelemetry-collector/issues/575
JobLabelName string `mapstructure:"job_label_name" yaml:"job_label_name"`
// Services is a list of service name patterns for filtering tasks.
Services []ServiceConfig `mapstructure:"services" yaml:"services"`
// TaskDefinitions is a list of task definition arn patterns for filtering tasks.
TaskDefinitions []TaskDefinitionConfig `mapstructure:"task_definitions" yaml:"task_definitions"`
// DockerLabels is a list of docker labels for filtering containers within tasks.
DockerLabels []DockerLabelConfig `mapstructure:"docker_labels" yaml:"docker_labels"`
}
// Validate overrides the embedded noop validation so that load config can trigger
// our own validation logic.
func (c *Config) Validate() error {
if c.ClusterName == "" {
// TODO: https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/3188
// would allow auto detect cluster name in extension
return fmt.Errorf("must specify ECS cluster name directly")
}
for _, s := range c.Services {
if err := s.validate(); err != nil {
return err
}
}
for _, t := range c.TaskDefinitions {
if err := t.validate(); err != nil {
return err
}
}
for _, d := range c.DockerLabels {
if err := d.validate(); err != nil {
return err
}
}
return nil
}
// defaultConfig only applies docker label
func defaultConfig() Config {
return Config{
ClusterName: "default",
ClusterRegion: os.Getenv(awsRegionEnvKey),
ResultFile: "/etc/ecs_sd_targets.yaml",
RefreshInterval: defaultRefreshInterval,
JobLabelName: defaultJobLabelName,
DockerLabels: []DockerLabelConfig{
{
PortLabel: defaultDockerLabelMatcherPortLabel,
},
},
}
}
// exampleConfig returns an example instance that matches testdata/config_example.yaml.
// It can be used to validate if the struct tags like mapstructure, yaml are working properly.
func exampleConfig() *Config {
return &Config{
ClusterName: "ecs-sd-test-1",
ClusterRegion: "us-west-2",
ResultFile: "/etc/ecs_sd_targets.yaml",
RefreshInterval: 15 * time.Second,
JobLabelName: defaultJobLabelName,
Services: []ServiceConfig{
{
NamePattern: "^retail-.*$",
},
},
TaskDefinitions: []TaskDefinitionConfig{
{
CommonExporterConfig: CommonExporterConfig{
JobName: "task_def_1",
MetricsPath: "/not/metrics",
MetricsPorts: []int{9113, 9090},
},
ArnPattern: ".*:task-definition/nginx:[0-9]+",
},
},
DockerLabels: []DockerLabelConfig{
{
PortLabel: "ECS_PROMETHEUS_EXPORTER_PORT",
},
},
}
}