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
47 changes: 47 additions & 0 deletions exporter/datadogexporter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package datadogexporter

import (
"os"
"path"
"testing"

Expand Down Expand Up @@ -117,3 +118,49 @@ func TestOverrideMetricsURL(t *testing.T) {
require.NoError(t, err)
assert.Equal(t, cfg.Metrics.Endpoint, DebugEndpoint)
}

func TestAPIKeyUnset(t *testing.T) {
cfg := Config{}
err := cfg.Sanitize()
assert.Equal(t, err, errUnsetAPIKey)
}

func TestCensorAPIKey(t *testing.T) {
cfg := APIConfig{
Key: "ddog_32_characters_long_api_key1",
}

assert.Equal(
t,
"***************************_key1",
cfg.GetCensoredKey(),
)
}

func TestUpdateWithEnv(t *testing.T) {
cfg := TagsConfig{
Hostname: "test_host",
Env: "test_env",
}

// Should be ignored since they were set
// on config
os.Setenv("DD_HOST", "env_host")
os.Setenv("DD_ENV", "env_env")

// Should be picked up
os.Setenv("DD_SERVICE", "env_service")
os.Setenv("DD_VERSION", "env_version")

cfg.UpdateWithEnv()

assert.Equal(t,
TagsConfig{
Hostname: "test_host",
Env: "test_env",
Service: "env_service",
Version: "env_version",
},
cfg,
)
}
36 changes: 36 additions & 0 deletions exporter/datadogexporter/host_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package datadogexporter

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
)

func TestHost(t *testing.T) {

host := GetHost(&Config{TagsConfig: TagsConfig{Hostname: "test_host"}})
assert.Equal(t, *host, "test_host")

host = GetHost(&Config{})
osHostname, err := os.Hostname()
if err == nil {
assert.Equal(t, *host, osHostname)
} else {
assert.Equal(t, *host, "unknown")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,7 @@ func newMetricsExporter(logger *zap.Logger, cfg *Config) (*metricsExporter, erro

}

func (exp *metricsExporter) PushMetricsData(ctx context.Context, md pdata.Metrics) (int, error) {
data := internaldata.MetricsToOC(md)
series, droppedTimeSeries := MapMetrics(exp.logger, exp.cfg.Metrics, data)

func (exp *metricsExporter) processMetrics(series *Series) {
addNamespace := exp.cfg.Metrics.Namespace != ""
overrideHostname := exp.cfg.Hostname != ""
addTags := len(exp.tags) > 0
Expand All @@ -73,6 +70,12 @@ func (exp *metricsExporter) PushMetricsData(ctx context.Context, md pdata.Metric
}

}
}

func (exp *metricsExporter) PushMetricsData(ctx context.Context, md pdata.Metrics) (int, error) {
data := internaldata.MetricsToOC(md)
series, droppedTimeSeries := MapMetrics(exp.logger, exp.cfg.Metrics, data)
exp.processMetrics(&series)

err := exp.client.PostMetrics(series.metrics)
return droppedTimeSeries, err
Expand Down
121 changes: 121 additions & 0 deletions exporter/datadogexporter/metrics_exporter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

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()

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.",
},
}
logger := zap.NewNop()

exp, err := newMetricsExporter(logger, cfg)

require.NoError(t, err)

var series Series
series.Add(NewGauge(
"original_host",
"metric_name",
0,
0,
[]string{"key2:val2"},
))

exp.processMetrics(&series)

assert.Equal(t, "test_host", *series.metrics[0].Host)
assert.Equal(t, "test.metric_name", *series.metrics[0].Metric)
assert.ElementsMatch(t,
[]string{"key:val", "env:test_env", "key2:val2"},
series.metrics[0].Tags,
)

}