-
Notifications
You must be signed in to change notification settings - Fork 804
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Friedrich Gonzalez <friedrichg@gmail.com>
- Loading branch information
1 parent
db6c565
commit 3c90820
Showing
1 changed file
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package push | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/cortexproject/cortex/pkg/cortexpb" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
"go.opentelemetry.io/collector/pdata/pcommon" | ||
"go.opentelemetry.io/collector/pdata/pmetric" | ||
"go.opentelemetry.io/collector/pdata/pmetric/pmetricotlp" | ||
) | ||
|
||
func TestOTLPWriteHandler(t *testing.T) { | ||
exportRequest := generateOTLPWriteRequest(t) | ||
|
||
buf, err := exportRequest.MarshalProto() | ||
require.NoError(t, err) | ||
|
||
req, err := http.NewRequest("", "", bytes.NewReader(buf)) | ||
require.NoError(t, err) | ||
req.Header.Set("Content-Type", "application/x-protobuf") | ||
|
||
push := verifyOTLPWriteRequestHandler(t, cortexpb.API) | ||
handler := OTLPHandler(nil, push) | ||
|
||
recorder := httptest.NewRecorder() | ||
handler.ServeHTTP(recorder, req) | ||
|
||
resp := recorder.Result() | ||
require.Equal(t, http.StatusOK, resp.StatusCode) | ||
} | ||
|
||
func generateOTLPWriteRequest(t *testing.T) pmetricotlp.ExportRequest { | ||
d := pmetric.NewMetrics() | ||
|
||
// Generate One Counter, One Gauge, One Histogram, One Exponential-Histogram | ||
// with resource attributes: service.name="test-service", service.instance.id="test-instance", host.name="test-host" | ||
// with metric attibute: foo.bar="baz" | ||
|
||
timestamp := time.Now() | ||
|
||
resourceMetric := d.ResourceMetrics().AppendEmpty() | ||
resourceMetric.Resource().Attributes().PutStr("service.name", "test-service") | ||
resourceMetric.Resource().Attributes().PutStr("service.instance.id", "test-instance") | ||
resourceMetric.Resource().Attributes().PutStr("host.name", "test-host") | ||
|
||
scopeMetric := resourceMetric.ScopeMetrics().AppendEmpty() | ||
|
||
// Generate One Counter | ||
counterMetric := scopeMetric.Metrics().AppendEmpty() | ||
counterMetric.SetName("test-counter") | ||
counterMetric.SetDescription("test-counter-description") | ||
counterMetric.SetEmptySum() | ||
counterMetric.Sum().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative) | ||
counterMetric.Sum().SetIsMonotonic(true) | ||
|
||
counterDataPoint := counterMetric.Sum().DataPoints().AppendEmpty() | ||
counterDataPoint.SetTimestamp(pcommon.NewTimestampFromTime(timestamp)) | ||
counterDataPoint.SetDoubleValue(10.0) | ||
counterDataPoint.Attributes().PutStr("foo.bar", "baz") | ||
|
||
counterExemplar := counterDataPoint.Exemplars().AppendEmpty() | ||
counterExemplar.SetTimestamp(pcommon.NewTimestampFromTime(timestamp)) | ||
counterExemplar.SetDoubleValue(10.0) | ||
counterExemplar.SetSpanID(pcommon.SpanID{0, 1, 2, 3, 4, 5, 6, 7}) | ||
counterExemplar.SetTraceID(pcommon.TraceID{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}) | ||
|
||
// Generate One Gauge | ||
gaugeMetric := scopeMetric.Metrics().AppendEmpty() | ||
gaugeMetric.SetName("test-gauge") | ||
gaugeMetric.SetDescription("test-gauge-description") | ||
gaugeMetric.SetEmptyGauge() | ||
|
||
gaugeDataPoint := gaugeMetric.Gauge().DataPoints().AppendEmpty() | ||
gaugeDataPoint.SetTimestamp(pcommon.NewTimestampFromTime(timestamp)) | ||
gaugeDataPoint.SetDoubleValue(10.0) | ||
gaugeDataPoint.Attributes().PutStr("foo.bar", "baz") | ||
|
||
// Generate One Histogram | ||
histogramMetric := scopeMetric.Metrics().AppendEmpty() | ||
histogramMetric.SetName("test-histogram") | ||
histogramMetric.SetDescription("test-histogram-description") | ||
histogramMetric.SetEmptyHistogram() | ||
histogramMetric.Histogram().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative) | ||
|
||
histogramDataPoint := histogramMetric.Histogram().DataPoints().AppendEmpty() | ||
histogramDataPoint.SetTimestamp(pcommon.NewTimestampFromTime(timestamp)) | ||
histogramDataPoint.ExplicitBounds().FromRaw([]float64{0.0, 1.0, 2.0, 3.0, 4.0, 5.0}) | ||
histogramDataPoint.BucketCounts().FromRaw([]uint64{2, 2, 2, 2, 2, 2}) | ||
histogramDataPoint.SetCount(10) | ||
histogramDataPoint.SetSum(30.0) | ||
histogramDataPoint.Attributes().PutStr("foo.bar", "baz") | ||
|
||
// Generate One Exponential-Histogram | ||
exponentialHistogramMetric := scopeMetric.Metrics().AppendEmpty() | ||
exponentialHistogramMetric.SetName("test-exponential-histogram") | ||
exponentialHistogramMetric.SetDescription("test-exponential-histogram-description") | ||
exponentialHistogramMetric.SetEmptyExponentialHistogram() | ||
exponentialHistogramMetric.ExponentialHistogram().SetAggregationTemporality(pmetric.AggregationTemporalityCumulative) | ||
|
||
exponentialHistogramDataPoint := exponentialHistogramMetric.ExponentialHistogram().DataPoints().AppendEmpty() | ||
exponentialHistogramDataPoint.SetTimestamp(pcommon.NewTimestampFromTime(timestamp)) | ||
exponentialHistogramDataPoint.SetScale(2.0) | ||
exponentialHistogramDataPoint.Positive().BucketCounts().FromRaw([]uint64{2, 2, 2, 2, 2}) | ||
exponentialHistogramDataPoint.SetZeroCount(2) | ||
exponentialHistogramDataPoint.SetCount(10) | ||
exponentialHistogramDataPoint.SetSum(30.0) | ||
exponentialHistogramDataPoint.Attributes().PutStr("foo.bar", "baz") | ||
|
||
return pmetricotlp.NewExportRequestFromMetrics(d) | ||
} | ||
|
||
func verifyOTLPWriteRequestHandler(t *testing.T, expectSource cortexpb.WriteRequest_SourceEnum) func(ctx context.Context, request *cortexpb.WriteRequest) (response *cortexpb.WriteResponse, err error) { | ||
t.Helper() | ||
return func(ctx context.Context, request *cortexpb.WriteRequest) (response *cortexpb.WriteResponse, err error) { | ||
assert.Len(t, request.Timeseries, 12) // 1 (counter) + 1 (gauge) + 7 (hist_bucket) + 2 (hist_sum, hist_count) + 1 (exponential histogram) | ||
// TODO: test more things | ||
assert.Equal(t, expectSource, request.Source) | ||
assert.False(t, request.SkipLabelNameValidation) | ||
return &cortexpb.WriteResponse{}, nil | ||
} | ||
} |