forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmetrics_exporter_test.go
More file actions
121 lines (101 loc) · 3.03 KB
/
Copy pathmetrics_exporter_test.go
File metadata and controls
121 lines (101 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
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,
)
}