|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package datadogextension // import "github.com/open-telemetry/opentelemetry-collector-contrib/extension/datadogextension" |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "go.opentelemetry.io/collector/component" |
| 11 | + "go.opentelemetry.io/collector/config/confighttp" |
| 12 | + |
| 13 | + "github.com/open-telemetry/opentelemetry-collector-contrib/extension/datadogextension/internal/httpserver" |
| 14 | + datadogconfig "github.com/open-telemetry/opentelemetry-collector-contrib/pkg/datadog/config" |
| 15 | +) |
| 16 | + |
| 17 | +var _ component.Config = (*Config)(nil) |
| 18 | + |
| 19 | +// Config contains the information necessary for enabling the Datadog Extension. |
| 20 | +type Config struct { |
| 21 | + confighttp.ClientConfig `mapstructure:",squash"` |
| 22 | + // Define the site and API key (and whether to fail on invalid API key) in API. |
| 23 | + API datadogconfig.APIConfig `mapstructure:"api"` |
| 24 | + // If Hostname is empty extension will use available system APIs and cloud provider endpoints. |
| 25 | + Hostname string `mapstructure:"hostname"` |
| 26 | + // HTTPConfig is v2 config for the http metadata service. |
| 27 | + HTTPConfig *httpserver.Config `mapstructure:"http"` |
| 28 | +} |
| 29 | + |
| 30 | +// Validate ensures that the configuration is valid. |
| 31 | +func (c *Config) Validate() error { |
| 32 | + if c.API.Site == "" { |
| 33 | + return datadogconfig.ErrEmptyEndpoint |
| 34 | + } |
| 35 | + if c.API.Key == "" { |
| 36 | + return datadogconfig.ErrUnsetAPIKey |
| 37 | + } |
| 38 | + invalidAPIKeyChars := datadogconfig.NonHexRegex.FindAllString(string(c.API.Key), -1) |
| 39 | + if len(invalidAPIKeyChars) > 0 { |
| 40 | + return fmt.Errorf("%w: invalid characters: %s", datadogconfig.ErrAPIKeyFormat, strings.Join(invalidAPIKeyChars, ", ")) |
| 41 | + } |
| 42 | + return nil |
| 43 | +} |
0 commit comments