Skip to content

Commit

Permalink
add new fail_on_invalid_key option
Browse files Browse the repository at this point in the history
  • Loading branch information
keisku committed Apr 25, 2022
1 parent ab4ed72 commit 14f929f
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
- `attributesprocessor`: Support filter by severity (#9132)
- `processor/transform`: Add transformation of logs (#9368)
- `datadogexporter`: Add `metrics::summaries::mode` to specify export mode for summaries (#8846)
- `datadogexporter`: Add `api.fail_on_invalid_key` to fail fast if api key is invalid (#9426)

### 🧰 Bug fixes 🧰

Expand Down
7 changes: 5 additions & 2 deletions exporter/datadogexporter/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,11 @@ import (
"regexp"
"strings"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata/valid"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/config/confignet"
"go.opentelemetry.io/collector/exporter/exporterhelper"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/exporter/datadogexporter/internal/metadata/valid"
)

var (
Expand Down Expand Up @@ -58,6 +57,10 @@ type APIConfig struct {
// It can also be set through the `DD_SITE` environment variable (Deprecated: [v0.47.0] set environment variable explicitly on configuration instead).
// The default value is "datadoghq.com".
Site string `mapstructure:"site"`

// FailOnInvalidKey states whether to exit on invalid api key.
// The default value is false.
FailOnInvalidKey bool `mapstructure:"fail_on_invalid_key"`
}

// MetricsConfig defines the metrics exporter specific configuration options
Expand Down
5 changes: 5 additions & 0 deletions exporter/datadogexporter/example/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ exporters:
#
# site: datadoghq.com

## @param fail_on_invalid_key - boolean - optional - default: false
## Whether to exit on invalid api key.
#
# fail_on_invalid_key: false

## @param tls - custom object - optional
# TLS settings for HTTPS communications.
# tls:
Expand Down
24 changes: 15 additions & 9 deletions exporter/datadogexporter/internal/utils/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,22 @@ func CreateClient(APIKey string, endpoint string) *datadog.Client {
}

// ValidateAPIKey checks that the provided client was given a correct API key.
func ValidateAPIKey(logger *zap.Logger, client *datadog.Client) {
// If `api.fail_on_invalid_key` is enabled,
func ValidateAPIKey(logger *zap.Logger, client *datadog.Client, failOnInvalidKey bool) {
logger.Info("Validating API key.")
res, err := client.Validate()
if err != nil {
logger.Warn("Error while validating API key.", zap.Error(err))
}

if res {
valid, err := client.Validate()
if err == nil && valid {
logger.Info("API key validation successful.")
} else {
logger.Warn("API key validation failed.")
return
}
switch {
case err != nil && failOnInvalidKey:
logger.Fatal("Error while validating API key.", zap.Error(err))
case err != nil && !failOnInvalidKey:
logger.Warn("Error while validating API key.", zap.Error(err))
case !valid && failOnInvalidKey:
logger.Fatal("API Key validation failed.")
case !valid && !failOnInvalidKey:
logger.Warn("API Key validation failed.")
}
}
2 changes: 1 addition & 1 deletion exporter/datadogexporter/metrics_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func newMetricsExporter(ctx context.Context, params component.ExporterCreateSett
client.ExtraHeader["User-Agent"] = utils.UserAgent(params.BuildInfo)
client.HttpClient = utils.NewHTTPClient(cfg.TimeoutSettings, cfg.LimitedHTTPClientSettings.TLSSetting.InsecureSkipVerify)

utils.ValidateAPIKey(params.Logger, client)
utils.ValidateAPIKey(params.Logger, client, cfg.API.FailOnInvalidKey)

tr, err := translatorFromConfig(params.Logger, cfg)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion exporter/datadogexporter/traces_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ var (
func newTracesExporter(ctx context.Context, params component.ExporterCreateSettings, cfg *config.Config, onceMetadata *sync.Once) *traceExporter {
// client to send running metric to the backend & perform API key validation
client := utils.CreateClient(cfg.API.Key, cfg.Metrics.TCPAddr.Endpoint)
utils.ValidateAPIKey(params.Logger, client)
utils.ValidateAPIKey(params.Logger, client, cfg.API.FailOnInvalidKey)

// removes potentially sensitive info and PII, approach taken from serverless approach
// https://github.com/DataDog/datadog-serverless-functions/blob/11f170eac105d66be30f18eda09eca791bc0d31b/aws/logs_monitoring/trace_forwarder/cmd/trace/main.go#L43
Expand Down

0 comments on commit 14f929f

Please sign in to comment.