forked from SumoLogic/sumologic-otel-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus_formatter_test.go
221 lines (189 loc) · 7.99 KB
/
prometheus_formatter_test.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
// Copyright 2020, 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 sumologicexporter
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/pdata/pcommon"
"go.opentelemetry.io/collector/pdata/pmetric"
)
func TestSanitizeKey(t *testing.T) {
f, err := newPrometheusFormatter()
require.NoError(t, err)
key := "&^*123-abc-ABC!./?_:\n\r"
expected := "___123-abc-ABC_./__:__"
assert.EqualValues(t, expected, f.sanitizeKeyBytes([]byte(key)))
}
func TestSanitizeValue(t *testing.T) {
f, err := newPrometheusFormatter()
require.NoError(t, err)
// `\`, `"` and `\n` should be escaped, everything else should be left as-is
// see: https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md#line-format
value := `&^*123-abc-ABC!?./"\` + "\n\r"
expected := `&^*123-abc-ABC!?./\"\\\n` + "\r"
assert.Equal(t, expected, f.sanitizeValue(value))
}
func TestTags2StringNoLabels(t *testing.T) {
f, err := newPrometheusFormatter()
require.NoError(t, err)
_, attributes := exampleIntMetric()
attributes.Clear()
assert.Equal(t, prometheusTags(""), f.tags2String(attributes, pcommon.NewMap()))
}
func TestTags2String(t *testing.T) {
f, err := newPrometheusFormatter()
require.NoError(t, err)
_, attributes := exampleIntMetric()
assert.Equal(
t,
prometheusTags(`{test="test_value",test2="second_value"}`),
f.tags2String(attributes, pcommon.NewMap()),
)
}
func TestTags2StringNoAttributes(t *testing.T) {
f, err := newPrometheusFormatter()
require.NoError(t, err)
_, attributes := exampleIntMetric()
attributes.Clear()
assert.Equal(t, prometheusTags(""), f.tags2String(pcommon.NewMap(), pcommon.NewMap()))
}
func TestPrometheusMetricDataTypeIntGauge(t *testing.T) {
f, err := newPrometheusFormatter()
require.NoError(t, err)
metric, attributes := exampleIntGaugeMetric()
result := f.metric2String(metric, attributes)
expected := `gauge_metric_name{foo="bar",remote_name="156920",url="http://example_url"} 124 1608124661166
gauge_metric_name{foo="bar",remote_name="156955",url="http://another_url"} 245 1608124662166`
assert.Equal(t, expected, result)
}
func TestPrometheusMetricDataTypeDoubleGauge(t *testing.T) {
f, err := newPrometheusFormatter()
require.NoError(t, err)
metric, attributes := exampleDoubleGaugeMetric()
result := f.metric2String(metric, attributes)
expected := `gauge_metric_name_double_test{foo="bar",local_name="156720",endpoint="http://example_url"} 33.4 1608124661169
gauge_metric_name_double_test{foo="bar",local_name="156155",endpoint="http://another_url"} 56.8 1608124662186`
assert.Equal(t, expected, result)
}
func TestPrometheusMetricDataTypeIntSum(t *testing.T) {
f, err := newPrometheusFormatter()
require.NoError(t, err)
metric, attributes := exampleIntSumMetric()
result := f.metric2String(metric, attributes)
expected := `sum_metric_int_test{foo="bar",name="156720",address="http://example_url"} 45 1608124444169
sum_metric_int_test{foo="bar",name="156155",address="http://another_url"} 1238 1608124699186`
assert.Equal(t, expected, result)
}
func TestPrometheusMetricDataTypeDoubleSum(t *testing.T) {
f, err := newPrometheusFormatter()
require.NoError(t, err)
metric, attributes := exampleDoubleSumMetric()
result := f.metric2String(metric, attributes)
expected := `sum_metric_double_test{foo="bar",pod_name="lorem",namespace="default"} 45.6 1618124444169
sum_metric_double_test{foo="bar",pod_name="opsum",namespace="kube-config"} 1238.1 1608424699186`
assert.Equal(t, expected, result)
}
func TestPrometheusMetricDataTypeSummary(t *testing.T) {
f, err := newPrometheusFormatter()
require.NoError(t, err)
metric, attributes := exampleSummaryMetric()
result := f.metric2String(metric, attributes)
expected := `summary_metric_double_test{foo="bar",quantile="0.6",pod_name="dolor",namespace="sumologic"} 0.7 1618124444169
summary_metric_double_test{foo="bar",quantile="2.6",pod_name="dolor",namespace="sumologic"} 4 1618124444169
summary_metric_double_test_sum{foo="bar",pod_name="dolor",namespace="sumologic"} 45.6 1618124444169
summary_metric_double_test_count{foo="bar",pod_name="dolor",namespace="sumologic"} 3 1618124444169
summary_metric_double_test_sum{foo="bar",pod_name="sit",namespace="main"} 1238.1 1608424699186
summary_metric_double_test_count{foo="bar",pod_name="sit",namespace="main"} 7 1608424699186`
assert.Equal(t, expected, result)
}
func TestPrometheusMetricDataTypeHistogram(t *testing.T) {
f, err := newPrometheusFormatter()
require.NoError(t, err)
metric, attributes := exampleHistogramMetric()
result := f.metric2String(metric, attributes)
expected := `histogram_metric_double_test{bar="foo",le="0.1",container="dolor",branch="sumologic"} 0 1618124444169
histogram_metric_double_test{bar="foo",le="0.2",container="dolor",branch="sumologic"} 12 1618124444169
histogram_metric_double_test{bar="foo",le="0.5",container="dolor",branch="sumologic"} 19 1618124444169
histogram_metric_double_test{bar="foo",le="0.8",container="dolor",branch="sumologic"} 24 1618124444169
histogram_metric_double_test{bar="foo",le="1",container="dolor",branch="sumologic"} 32 1618124444169
histogram_metric_double_test{bar="foo",le="+Inf",container="dolor",branch="sumologic"} 45 1618124444169
histogram_metric_double_test_sum{bar="foo",container="dolor",branch="sumologic"} 45.6 1618124444169
histogram_metric_double_test_count{bar="foo",container="dolor",branch="sumologic"} 7 1618124444169
histogram_metric_double_test{bar="foo",le="0.1",container="sit",branch="main"} 0 1608424699186
histogram_metric_double_test{bar="foo",le="0.2",container="sit",branch="main"} 10 1608424699186
histogram_metric_double_test{bar="foo",le="0.5",container="sit",branch="main"} 11 1608424699186
histogram_metric_double_test{bar="foo",le="0.8",container="sit",branch="main"} 12 1608424699186
histogram_metric_double_test{bar="foo",le="1",container="sit",branch="main"} 16 1608424699186
histogram_metric_double_test{bar="foo",le="+Inf",container="sit",branch="main"} 22 1608424699186
histogram_metric_double_test_sum{bar="foo",container="sit",branch="main"} 54.1 1608424699186
histogram_metric_double_test_count{bar="foo",container="sit",branch="main"} 98 1608424699186`
assert.Equal(t, expected, result)
}
func TestPrometheusMetrics(t *testing.T) {
type testCase struct {
name string
metricFunc func(fillData bool) (pmetric.Metric, pcommon.Map)
expected string
}
tests := []testCase{
{
name: "empty int gauge",
metricFunc: buildExampleIntGaugeMetric,
expected: "",
},
{
name: "empty double gauge",
metricFunc: buildExampleDoubleGaugeMetric,
expected: "",
},
{
name: "empty int sum",
metricFunc: buildExampleIntSumMetric,
expected: "",
},
{
name: "empty double sum",
metricFunc: buildExampleDoubleSumMetric,
expected: "",
},
{
name: "empty summary",
metricFunc: buildExampleSummaryMetric,
expected: "",
},
{
name: "empty histogram",
metricFunc: buildExampleHistogramMetric,
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f, err := newPrometheusFormatter()
require.NoError(t, err)
result := f.metric2String(tt.metricFunc(false))
assert.Equal(t, tt.expected, result)
})
}
}
func Benchmark_PrometheusFormatter_Metric2String(b *testing.B) {
f, err := newPrometheusFormatter()
require.NoError(b, err)
metric, attributes := buildExampleHistogramMetric(true)
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = f.metric2String(metric, attributes)
}
}