diff --git a/pkg/util/push/otlp_test.go b/pkg/util/push/otlp_test.go new file mode 100644 index 00000000000..15035c55175 --- /dev/null +++ b/pkg/util/push/otlp_test.go @@ -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 + } +}