Skip to content

Commit b8fd660

Browse files
committed
Tests
Signed-off-by: Friedrich Gonzalez <[email protected]>
1 parent db6c565 commit b8fd660

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed

pkg/util/push/otlp_test.go

+129
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
package push
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
"time"
10+
11+
"github.com/stretchr/testify/assert"
12+
"github.com/stretchr/testify/require"
13+
"go.opentelemetry.io/collector/pdata/pcommon"
14+
"go.opentelemetry.io/collector/pdata/pmetric"
15+
"go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp"
16+
17+
"github.com/cortexproject/cortex/pkg/cortexpb"
18+
)
19+
20+
func TestOTLPWriteHandler(t *testing.T) {
21+
exportRequest := generateOTLPWriteRequest(t)
22+
23+
buf, err := exportRequest.MarshalProto()
24+
require.NoError(t, err)
25+
26+
req, err := http.NewRequest("", "", bytes.NewReader(buf))
27+
require.NoError(t, err)
28+
req.Header.Set("Content-Type", "application/x-protobuf")
29+
30+
push := verifyOTLPWriteRequestHandler(t, cortexpb.API)
31+
handler := OTLPHandler(nil, push)
32+
33+
recorder := httptest.NewRecorder()
34+
handler.ServeHTTP(recorder, req)
35+
36+
resp := recorder.Result()
37+
require.Equal(t, http.StatusOK, resp.StatusCode)
38+
}
39+
40+
func generateOTLPWriteRequest(t *testing.T) pmetricotlp.ExportRequest {
41+
d := pmetric.NewMetrics()
42+
43+
// Generate One Counter, One Gauge, One Histogram, One Exponential-Histogram
44+
// with resource attributes: service.name="test-service", service.instance.id="test-instance", host.name="test-host"
45+
// with metric attibute: foo.bar="baz"
46+
47+
timestamp := time.Now()
48+
49+
resourceMetric := d.ResourceMetrics().AppendEmpty()
50+
resourceMetric.Resource().Attributes().PutStr("service.name", "test-service")
51+
resourceMetric.Resource().Attributes().PutStr("service.instance.id", "test-instance")
52+
resourceMetric.Resource().Attributes().PutStr("host.name", "test-host")
53+
54+
scopeMetric := resourceMetric.ScopeMetrics().AppendEmpty()
55+
56+
// Generate One Counter
57+
counterMetric := scopeMetric.Metrics().AppendEmpty()
58+
counterMetric.SetName("test-counter")
59+
counterMetric.SetDescription("test-counter-description")
60+
counterMetric.SetEmptySum()
61+
counterMetric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
62+
counterMetric.Sum().SetIsMonotonic(true)
63+
64+
counterDataPoint := counterMetric.Sum().DataPoints().AppendEmpty()
65+
counterDataPoint.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
66+
counterDataPoint.SetDoubleValue(10.0)
67+
counterDataPoint.Attributes().PutStr("foo.bar", "baz")
68+
69+
counterExemplar := counterDataPoint.Exemplars().AppendEmpty()
70+
counterExemplar.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
71+
counterExemplar.SetDoubleValue(10.0)
72+
counterExemplar.SetSpanID(pcommon.SpanID{0, 1, 2, 3, 4, 5, 6, 7})
73+
counterExemplar.SetTraceID(pcommon.TraceID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15})
74+
75+
// Generate One Gauge
76+
gaugeMetric := scopeMetric.Metrics().AppendEmpty()
77+
gaugeMetric.SetName("test-gauge")
78+
gaugeMetric.SetDescription("test-gauge-description")
79+
gaugeMetric.SetEmptyGauge()
80+
81+
gaugeDataPoint := gaugeMetric.Gauge().DataPoints().AppendEmpty()
82+
gaugeDataPoint.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
83+
gaugeDataPoint.SetDoubleValue(10.0)
84+
gaugeDataPoint.Attributes().PutStr("foo.bar", "baz")
85+
86+
// Generate One Histogram
87+
histogramMetric := scopeMetric.Metrics().AppendEmpty()
88+
histogramMetric.SetName("test-histogram")
89+
histogramMetric.SetDescription("test-histogram-description")
90+
histogramMetric.SetEmptyHistogram()
91+
histogramMetric.Histogram().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
92+
93+
histogramDataPoint := histogramMetric.Histogram().DataPoints().AppendEmpty()
94+
histogramDataPoint.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
95+
histogramDataPoint.ExplicitBounds().FromRaw([]float64{0.0, 1.0, 2.0, 3.0, 4.0, 5.0})
96+
histogramDataPoint.BucketCounts().FromRaw([]uint64{2, 2, 2, 2, 2, 2})
97+
histogramDataPoint.SetCount(10)
98+
histogramDataPoint.SetSum(30.0)
99+
histogramDataPoint.Attributes().PutStr("foo.bar", "baz")
100+
101+
// Generate One Exponential-Histogram
102+
exponentialHistogramMetric := scopeMetric.Metrics().AppendEmpty()
103+
exponentialHistogramMetric.SetName("test-exponential-histogram")
104+
exponentialHistogramMetric.SetDescription("test-exponential-histogram-description")
105+
exponentialHistogramMetric.SetEmptyExponentialHistogram()
106+
exponentialHistogramMetric.ExponentialHistogram().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative)
107+
108+
exponentialHistogramDataPoint := exponentialHistogramMetric.ExponentialHistogram().DataPoints().AppendEmpty()
109+
exponentialHistogramDataPoint.SetTimestamp(pcommon.NewTimestampFromTime(timestamp))
110+
exponentialHistogramDataPoint.SetScale(2.0)
111+
exponentialHistogramDataPoint.Positive().BucketCounts().FromRaw([]uint64{2, 2, 2, 2, 2})
112+
exponentialHistogramDataPoint.SetZeroCount(2)
113+
exponentialHistogramDataPoint.SetCount(10)
114+
exponentialHistogramDataPoint.SetSum(30.0)
115+
exponentialHistogramDataPoint.Attributes().PutStr("foo.bar", "baz")
116+
117+
return pmetricotlp.NewExportRequestFromMetrics(d)
118+
}
119+
120+
func verifyOTLPWriteRequestHandler(t *testing.T, expectSource cortexpb.WriteRequest_SourceEnum) func(ctx context.Context, request *cortexpb.WriteRequest) (response *cortexpb.WriteResponse, err error) {
121+
t.Helper()
122+
return func(ctx context.Context, request *cortexpb.WriteRequest) (response *cortexpb.WriteResponse, err error) {
123+
assert.Len(t, request.Timeseries, 12) // 1 (counter) + 1 (gauge) + 7 (hist_bucket) + 2 (hist_sum, hist_count) + 1 (exponential histogram)
124+
// TODO: test more things
125+
assert.Equal(t, expectSource, request.Source)
126+
assert.False(t, request.SkipLabelNameValidation)
127+
return &cortexpb.WriteResponse{}, nil
128+
}
129+
}

0 commit comments

Comments
 (0)