@@ -24,61 +24,143 @@ import (
2424
2525var (
2626 errUnsetAPIKey = errors .New ("the Datadog API key is unset" )
27- errConflict = errors .New ("'site' and 'api_key' must not be set with agent mode" )
2827)
2928
30- // Config defines configuration for the Datadog exporter.
31- type Config struct {
32- configmodels.ExporterSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
29+ const (
30+ NoneMode = "none"
31+ AgentlessMode = "agentless"
32+ DogStatsDMode = "dogstatsd"
33+ )
3334
34- // ApiKey is the Datadog API key to associate your Agent's data with your organization.
35+ // APIConfig defines the API configuration options
36+ type APIConfig struct {
37+ // Key is the Datadog API key to associate your Agent's data with your organization.
3538 // Create a new API key here: https://app.datadoghq.com/account/settings
36- APIKey string `mapstructure:"api_key "`
39+ Key string `mapstructure:"key "`
3740
3841 // Site is the site of the Datadog intake to send data to.
3942 // The default value is "datadoghq.com".
4043 Site string `mapstructure:"site"`
44+ }
45+
46+ // DogStatsDConfig defines the DogStatsd related configuration
47+ type DogStatsDConfig struct {
48+ // Endpoint is the DogStatsD address.
49+ // The default value is 127.0.0.1:8125
50+ // A Unix address is supported
51+ Endpoint string `mapstructure:"endpoint"`
52+
53+ // Telemetry states whether to send metrics
54+ Telemetry bool `mapstructure:"telemetry"`
55+ }
56+
57+ // AgentlessConfig defines the Agentless related configuration
58+ type AgentlessConfig struct {
59+ // Endpoint is the host of the Datadog intake server to send metrics to.
60+ // If unset, the value is obtained from the Site.
61+ Endpoint string `mapstructure:"endpoint"`
62+ }
63+
64+ // MetricsConfig defines the metrics exporter specific configuration options
65+ type MetricsConfig struct {
66+ // Namespace is the namespace under which the metrics are sent
67+ // By default metrics are not namespaced
68+ Namespace string `mapstructure:"namespace"`
4169
42- // MetricsURL is the host of the Datadog intake server or Dogstatsd server to send metrics to.
43- // If not set, the value is obtained from the Site and the sending method.
44- MetricsURL string `mapstructure:"metrics_url"`
70+ // Mode is the metrics sending mode: either 'dogstatsd' or 'agentless'
71+ Mode string `mapstructure:"mode"`
4572
46- // Tags is the list of default tags to add to every metric or trace
73+ // DogStatsD defines the DogStatsD configuration options.
74+ DogStatsD DogStatsDConfig `mapstructure:"dogstatsd"`
75+
76+ // Agentless defines the Agentless configuration options.
77+ Agentless AgentlessConfig `mapstructure:"agentless"`
78+ }
79+
80+ // TagsConfig defines the tag-related configuration
81+ // It is embedded in the configuration
82+ type TagsConfig struct {
83+ // Hostname is the host name for unified service tagging.
84+ // If unset, it is determined automatically.
85+ // See https://docs.datadoghq.com/agent/faq/how-datadog-agent-determines-the-hostname
86+ // for more details.
87+ Hostname string `mapstructure:"hostname"`
88+
89+ // Env is the environment for unified service tagging.
90+ // It can also be set through the `DD_ENV` environment variable.
91+ Env string `mapstructure:"env"`
92+
93+ // Service is the service for unified service tagging.
94+ // It can also be set through the `DD_SERVICE` environment variable.
95+ Service string `mapstructure:"service"`
96+
97+ // Version is the version for unified service tagging.
98+ // It can also be set through the `DD_VERSION` version variable.
99+ Version string `mapstructure:"version"`
100+
101+ // Tags is the list of default tags to add to every metric or trace.
47102 Tags []string `mapstructure:"tags"`
103+ }
104+
105+ // GetTags gets the default tags extracted from the configuration
106+ func (t * TagsConfig ) GetTags () []string {
107+ tags := make ([]string , 0 , 4 )
108+
109+ if t .Hostname != "" {
110+ tags = append (tags , fmt .Sprintf ("host:%s" , t .Hostname ))
111+ }
48112
49- // Mode states the mode for sending metrics and traces.
50- // The possible values are "api" and "agent".
51- // The default value is "agent".
52- Mode string `mapstructure:"sending_method"`
113+ if t .Env != "" {
114+ tags = append (tags , fmt .Sprintf ("env:%s" , t .Env ))
115+ }
116+
117+ if t .Service != "" {
118+ tags = append (tags , fmt .Sprintf ("service:%s" , t .Service ))
119+ }
120+
121+ if t .Version != "" {
122+ tags = append (tags , fmt .Sprintf ("version:%s" , t .Version ))
123+ }
124+
125+ if len (t .Tags ) > 0 {
126+ tags = append (tags , t .Tags ... )
127+ }
128+
129+ return tags
53130}
54131
55- // Sanitize tries to sanitize a given configuration
56- func ( c * Config ) Sanitize () error {
57- if c . Mode == AgentMode {
132+ // Config defines configuration for the Datadog exporter.
133+ type Config struct {
134+ configmodels. ExporterSettings `mapstructure:",squash"` // squash ensures fields are correctly decoded in embedded struct.
58135
59- if c .APIKey != "" || c .Site != DefaultSite {
60- return errConflict
61- }
136+ TagsConfig `mapstructure:",squash"`
62137
63- if c .MetricsURL == "" {
64- c .MetricsURL = "127.0.0.1:8125"
65- }
138+ // API defines the Datadog API configuration.
139+ API APIConfig `mapstructure:"api"`
66140
67- } else if c .Mode == APIMode {
141+ // Metrics defines the Metrics exporter specific configuration
142+ Metrics MetricsConfig `mapstructure:"metrics"`
143+ }
68144
69- if c .APIKey == "" {
145+ // Sanitize tries to sanitize a given configuration
146+ func (c * Config ) Sanitize () error {
147+
148+ if c .Metrics .Mode != AgentlessMode && c .Metrics .Mode != DogStatsDMode {
149+ return fmt .Errorf ("Metrics mode '%s' is not recognized" , c .Metrics .Mode )
150+ }
151+
152+ // Exactly one configuration for metrics must be set
153+ if c .Metrics .Mode == AgentlessMode {
154+ if c .API .Key == "" {
70155 return errUnsetAPIKey
71156 }
72157
73- // Sanitize API key
74- c .APIKey = strings .TrimSpace (c .APIKey )
158+ c .API .Key = strings .TrimSpace (c .API .Key )
75159
76- if c .MetricsURL == "" {
77- c .MetricsURL = fmt .Sprintf ("https://api.%s" , c .Site )
160+ // Set the endpoint based on the Site
161+ if c .Metrics .Agentless .Endpoint == "" {
162+ c .Metrics .Agentless .Endpoint = fmt .Sprintf ("https://api.%s" , c .API .Site )
78163 }
79-
80- } else {
81- return fmt .Errorf ("Selected mode '%s' is invalid" , c .Mode )
82164 }
83165
84166 return nil
0 commit comments