Skip to content

Commit

Permalink
add tests, some of which should fail currently
Browse files Browse the repository at this point in the history
Signed-off-by: Callum Styan <[email protected]>
  • Loading branch information
cstyan committed Mar 28, 2024
1 parent 523b5de commit 03ff9b7
Show file tree
Hide file tree
Showing 8 changed files with 787 additions and 0 deletions.
52 changes: 52 additions & 0 deletions pkg/logproto/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package logproto
import (
"encoding/json"
"fmt"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/timestamp"
"math"
"testing"
"time"
"unsafe"

jsoniter "github.com/json-iterator/go"
Expand Down Expand Up @@ -338,6 +342,54 @@ func TestFilterChunkRefRequestGetQuery(t *testing.T) {
}
}

func TestIndexStatsRequestSpanLogging(t *testing.T) {
now := time.Now()
end := now.Add(time.Duration(1000 * time.Second))
req := IndexStatsRequest{
From: model.Time(now.UnixMilli()),
Through: model.Time(end.UnixMilli()),
}

span := mocktracer.MockSpan{}
req.LogToSpan(&span)

for _, l := range span.Logs() {
for _, field := range l.Fields {
if field.Key == "start" {
require.Equal(t, timestamp.Time(now.UnixMilli()).String(), field.ValueString)
}
if field.Key == "end" {
require.Equal(t, timestamp.Time(end.UnixMilli()).String(), field.ValueString)

}
}
}
}

func TestVolumeRequest(t *testing.T) {
now := time.Now()
end := now.Add(time.Duration(1000 * time.Second))
req := VolumeRequest{
From: model.Time(now.UnixMilli()),
Through: model.Time(end.UnixMilli()),
}

span := mocktracer.MockSpan{}
req.LogToSpan(&span)

for _, l := range span.Logs() {
for _, field := range l.Fields {
if field.Key == "start" {
require.Equal(t, timestamp.Time(now.UnixMilli()).String(), field.ValueString)
}
if field.Key == "end" {
require.Equal(t, timestamp.Time(end.UnixMilli()).String(), field.ValueString)

}
}
}
}

func benchmarkMergeLabelResponses(b *testing.B, responses []*LabelResponse) {
b.ReportAllocs()
for n := 0; n < b.N; n++ {
Expand Down
93 changes: 93 additions & 0 deletions pkg/querier/queryrange/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"context"
"errors"
"fmt"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/prometheus/prometheus/model/timestamp"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -424,6 +426,97 @@ func Test_codec_DecodeResponse(t *testing.T) {
}
}

func TestLokiRequestSpanLogging(t *testing.T) {
now := time.Now()
end := now.Add(time.Duration(1000 * time.Second))
req := LokiRequest{
StartTs: now,
EndTs: end,
}

span := mocktracer.MockSpan{}
req.LogToSpan(&span)

for _, l := range span.Logs() {
for _, field := range l.Fields {
if field.Key == "start" {
require.Equal(t, timestamp.Time(now.UnixMilli()).String(), field.ValueString)
}
if field.Key == "end" {
require.Equal(t, timestamp.Time(end.UnixMilli()).String(), field.ValueString)
}
}
}
}

func TestLokiInstantRequestSpanLogging(t *testing.T) {
now := time.Now()
req := LokiInstantRequest{
TimeTs: now,
}

span := mocktracer.MockSpan{}
req.LogToSpan(&span)

for _, l := range span.Logs() {
for _, field := range l.Fields {
if field.Key == "ts" {
require.Equal(t, timestamp.Time(now.UnixMilli()).String(), field.ValueString)
}
}
}
}

func TestLokiSeriesRequestSpanLogging(t *testing.T) {
now := time.Now()
end := now.Add(time.Duration(1000 * time.Second))
req := LokiSeriesRequest{
StartTs: now,
EndTs: end,
}

span := mocktracer.MockSpan{}
req.LogToSpan(&span)

for _, l := range span.Logs() {
for _, field := range l.Fields {
if field.Key == "start" {
require.Equal(t, timestamp.Time(now.UnixMilli()).String(), field.ValueString)
}
if field.Key == "end" {
require.Equal(t, timestamp.Time(end.UnixMilli()).String(), field.ValueString)

}
}
}
}

func TestLabelRequestSpanLogging(t *testing.T) {
now := time.Now()
end := now.Add(time.Duration(1000 * time.Second))
req := LabelRequest{
LabelRequest: logproto.LabelRequest{
Start: &now,
End: &end,
},
}

span := mocktracer.MockSpan{}
req.LogToSpan(&span)

for _, l := range span.Logs() {
for _, field := range l.Fields {
if field.Key == "start" {
require.Equal(t, timestamp.Time(now.UnixMilli()).String(), field.ValueString)
}
if field.Key == "end" {
require.Equal(t, timestamp.Time(end.UnixMilli()).String(), field.ValueString)

}
}
}
}

func Test_codec_DecodeProtobufResponseParity(t *testing.T) {
// test fixtures from pkg/util/marshal_test
var queryTests = []struct {
Expand Down
27 changes: 27 additions & 0 deletions pkg/querier/queryrange/queryrangebase/query_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ package queryrangebase
import (
"bytes"
"context"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/prometheus/prometheus/model/timestamp"
"io"
"net/http"
"strconv"
"testing"
"time"

jsoniter "github.com/json-iterator/go"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -269,6 +272,30 @@ func TestMergeAPIResponses(t *testing.T) {
}
}

func TestPrometheusRequestSpanLogging(t *testing.T) {
now := time.Now()
end := now.Add(time.Duration(1000 * time.Second))
req := PrometheusRequest{
Start: now,
End: end,
}

span := mocktracer.MockSpan{}
req.LogToSpan(&span)

for _, l := range span.Logs() {
for _, field := range l.Fields {
if field.Key == "start" {
require.Equal(t, timestamp.Time(now.UnixMilli()).String(), field.ValueString)
}
if field.Key == "end" {
require.Equal(t, timestamp.Time(end.UnixMilli()).String(), field.ValueString)

}
}
}
}

func mustParse(t *testing.T, response string) Response {
var resp PrometheusResponse
// Needed as goimports automatically add a json import otherwise.
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 03ff9b7

Please sign in to comment.