Skip to content

Commit 3c90820

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

File tree

1 file changed

+128
-0
lines changed

1 file changed

+128
-0
lines changed

pkg/util/push/otlp_test.go

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

0 commit comments

Comments
 (0)