Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 0 additions & 28 deletions exporter/datadogexporter/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ package datadogexporter

import (
"context"
"errors"
"net/http"
"net/http/httptest"
"path"
"testing"

Expand Down Expand Up @@ -63,38 +60,13 @@ func TestCreateAPIMetricsExporter(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, cfg)

// Test with invalid API key

// Mock http server
tsInvalid := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{\"valid\": false}"))
}))
defer tsInvalid.Close()
cfg.Exporters["datadog/api"].(*Config).Metrics.Endpoint = tsInvalid.URL
ctx := context.Background()
exp, err := factory.CreateMetricsExporter(
ctx,
component.ExporterCreateParams{Logger: logger},
cfg.Exporters["datadog/api"],
)

assert.Equal(t, errors.New("provided Datadog API key is invalid: ***************************aaaaa"), err)
assert.Nil(t, exp)

// Override endpoint to return that the API key is valid
tsValid := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{\"valid\": true}"))
}))
defer tsValid.Close()
cfg.Exporters["datadog/api"].(*Config).Metrics.Endpoint = tsValid.URL

ctx = context.Background()
exp, err = factory.CreateMetricsExporter(
ctx,
component.ExporterCreateParams{Logger: logger},
cfg.Exporters["datadog/api"],
)

assert.Nil(t, err)
assert.NotNil(t, exp)
}
Expand Down
10 changes: 0 additions & 10 deletions exporter/datadogexporter/metrics_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package datadogexporter

import (
"context"
"fmt"

"go.opentelemetry.io/collector/consumer/pdata"
"go.opentelemetry.io/collector/translator/internaldata"
Expand All @@ -35,19 +34,10 @@ func newMetricsExporter(logger *zap.Logger, cfg *Config) (*metricsExporter, erro
client := datadog.NewClient(cfg.API.Key, "")
client.SetBaseUrl(cfg.Metrics.TCPAddr.Endpoint)

if ok, err := client.Validate(); err != nil {
logger.Warn("Error when validating API key", zap.Error(err))
} else if ok {
logger.Info("Provided Datadog API key is valid")
} else {
return nil, fmt.Errorf("provided Datadog API key is invalid: %s", cfg.API.GetCensoredKey())
}

// Calculate tags at startup
tags := cfg.TagsConfig.GetTags(false)

return &metricsExporter{logger, cfg, client, tags}, nil

}

func (exp *metricsExporter) processMetrics(series *Series) {
Expand Down
52 changes: 1 addition & 51 deletions exporter/datadogexporter/metrics_exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,82 +15,32 @@
package datadogexporter

import (
"errors"
"net/http"
"net/http/httptest"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/config/confignet"
"go.uber.org/zap"
)

func TestNewExporterValid(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{\"valid\": true}"))
}))
defer ts.Close()

cfg := &Config{}
cfg.API.Key = "ddog_32_characters_long_api_key1"
cfg.Metrics.TCPAddr.Endpoint = ts.URL
logger := zap.NewNop()

// The client should have been created correctly
exp, err := newMetricsExporter(logger, cfg)
require.NoError(t, err)
assert.NotNil(t, exp)
}

func TestNewExporterInvalid(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("{\"valid\": false}"))
}))
defer ts.Close()

func TestNewExporter(t *testing.T) {
cfg := &Config{}
cfg.API.Key = "ddog_32_characters_long_api_key1"
cfg.Metrics.TCPAddr.Endpoint = ts.URL
logger := zap.NewNop()

// An error should be raised
exp, err := newMetricsExporter(logger, cfg)
assert.Equal(t,
errors.New("provided Datadog API key is invalid: ***************************_key1"),
err,
)
assert.Nil(t, exp)
}

func TestNewExporterValidateError(t *testing.T) {
ts := httptest.NewServer(http.NotFoundHandler())
defer ts.Close()

cfg := &Config{}
cfg.API.Key = "ddog_32_characters_long_api_key1"
cfg.Metrics.TCPAddr.Endpoint = ts.URL
logger := zap.NewNop()

// The client should have been created correctly
// with the error being ignored
exp, err := newMetricsExporter(logger, cfg)
require.NoError(t, err)
assert.NotNil(t, exp)
}

func TestProcessMetrics(t *testing.T) {
ts := httptest.NewServer(http.NotFoundHandler())
defer ts.Close()

cfg := &Config{
TagsConfig: TagsConfig{
Hostname: "test_host",
Env: "test_env",
Tags: []string{"key:val"},
},
Metrics: MetricsConfig{
TCPAddr: confignet.TCPAddr{Endpoint: ts.URL},
Namespace: "test.",
},
}
Expand Down