Skip to content

Commit

Permalink
fix: fix span logging based on changes to request types timestamps (#…
Browse files Browse the repository at this point in the history
…12393)

Signed-off-by: Callum Styan <[email protected]>
  • Loading branch information
cstyan authored Apr 1, 2024
1 parent 5190dda commit 018856c
Show file tree
Hide file tree
Showing 9 changed files with 791 additions and 6 deletions.
52 changes: 52 additions & 0 deletions pkg/logproto/compat_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,14 @@ import (
"fmt"
"math"
"testing"
"time"
"unsafe"

jsoniter "github.com/json-iterator/go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/timestamp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -338,6 +342,54 @@ func TestFilterChunkRefRequestGetQuery(t *testing.T) {
}
}

func TestIndexStatsRequestSpanLogging(t *testing.T) {
now := time.Now()
end := now.Add(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(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
12 changes: 6 additions & 6 deletions pkg/querier/queryrange/codec.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ func (r *LokiRequest) WithShards(shards logql.Shards) *LokiRequest {
func (r *LokiRequest) LogToSpan(sp opentracing.Span) {
sp.LogFields(
otlog.String("query", r.GetQuery()),
otlog.String("start", timestamp.Time(r.GetStart().UnixNano()).String()),
otlog.String("end", timestamp.Time(r.GetEnd().UnixNano()).String()),
otlog.String("start", timestamp.Time(r.GetStart().UnixMilli()).String()),
otlog.String("end", timestamp.Time(r.GetEnd().UnixMilli()).String()),
otlog.Int64("step (ms)", r.GetStep()),
otlog.Int64("interval (ms)", r.GetInterval()),
otlog.Int64("limit", int64(r.GetLimit())),
Expand Down Expand Up @@ -179,8 +179,8 @@ func (r *LokiSeriesRequest) GetStep() int64 {
func (r *LokiSeriesRequest) LogToSpan(sp opentracing.Span) {
sp.LogFields(
otlog.String("matchers", strings.Join(r.GetMatch(), ",")),
otlog.String("start", timestamp.Time(r.GetStart().UnixNano()).String()),
otlog.String("end", timestamp.Time(r.GetEnd().UnixNano()).String()),
otlog.String("start", timestamp.Time(r.GetStart().UnixMilli()).String()),
otlog.String("end", timestamp.Time(r.GetEnd().UnixMilli()).String()),
otlog.String("shards", strings.Join(r.GetShards(), ",")),
)
}
Expand Down Expand Up @@ -250,8 +250,8 @@ func (r *LabelRequest) WithQuery(query string) queryrangebase.Request {

func (r *LabelRequest) LogToSpan(sp opentracing.Span) {
sp.LogFields(
otlog.String("start", timestamp.Time(r.GetStart().UnixNano()).String()),
otlog.String("end", timestamp.Time(r.GetEnd().UnixNano()).String()),
otlog.String("start", timestamp.Time(r.GetStart().UnixMilli()).String()),
otlog.String("end", timestamp.Time(r.GetEnd().UnixMilli()).String()),
)
}

Expand Down
92 changes: 92 additions & 0 deletions pkg/querier/queryrange/codec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ import (

"github.com/gorilla/mux"
"github.com/grafana/dskit/user"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/prometheus/common/model"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/model/timestamp"
"github.com/prometheus/prometheus/promql"
"github.com/prometheus/prometheus/promql/parser"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -424,6 +426,96 @@ func Test_codec_DecodeResponse(t *testing.T) {
}
}

func TestLokiRequestSpanLogging(t *testing.T) {
now := time.Now()
end := now.Add(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(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(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
26 changes: 26 additions & 0 deletions pkg/querier/queryrange/queryrangebase/query_range_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ import (
"net/http"
"strconv"
"testing"
"time"

jsoniter "github.com/json-iterator/go"
"github.com/opentracing/opentracing-go/mocktracer"
"github.com/prometheus/prometheus/model/timestamp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand Down Expand Up @@ -269,6 +272,29 @@ func TestMergeAPIResponses(t *testing.T) {
}
}

func TestPrometheusRequestSpanLogging(t *testing.T) {
now := time.Now()
end := now.Add(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 018856c

Please sign in to comment.