Skip to content

Commit 0878fda

Browse files
committed
tracing: report series.matchers for series requests
Add a series.matchers attribute to trace spans for Series gRPC requests. Signed-off-by: Craig Ringer <craig.ringer@enterprisedb.com>
1 parent 4c3f6b6 commit 0878fda

6 files changed

Lines changed: 93 additions & 15 deletions

File tree

api/grpc/thanos.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ import (
3333

3434
"github.com/thanos-io/thanos-parquet-gateway/db"
3535
"github.com/thanos-io/thanos-parquet-gateway/internal/limits"
36+
"github.com/thanos-io/thanos-parquet-gateway/internal/matchers"
3637
"github.com/thanos-io/thanos-parquet-gateway/internal/tracing"
3738
"github.com/thanos-io/thanos-parquet-gateway/internal/warnings"
3839
"github.com/thanos-io/thanos-parquet-gateway/schema"
@@ -418,6 +419,8 @@ func (qs *QueryServer) Series(request *storepb.SeriesRequest, srv storepb.Store_
418419
return status.Error(codes.Internal, err.Error())
419420
}
420421

422+
span.SetAttributes(attribute.StringSlice("series.matchers", matchers.ToStringSlice(ms)))
423+
421424
hints := &storage.SelectHints{
422425
Start: request.MinTime,
423426
End: request.MaxTime,

db/block.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"golang.org/x/sync/errgroup"
1919

2020
"github.com/thanos-io/thanos-parquet-gateway/internal/limits"
21+
matcherspkg "github.com/thanos-io/thanos-parquet-gateway/internal/matchers"
2122
"github.com/thanos-io/thanos-parquet-gateway/internal/tracing"
2223
"github.com/thanos-io/thanos-parquet-gateway/internal/util"
2324
"github.com/thanos-io/thanos-parquet-gateway/internal/warnings"
@@ -208,7 +209,7 @@ func (q BlockQuerier) selectFn(ctx context.Context, _ bool, hints *storage.Selec
208209
defer span.End()
209210

210211
span.SetAttributes(attribute.Bool("sorted", true))
211-
span.SetAttributes(attribute.StringSlice("matchers", matchersToStringSlice(matchers)))
212+
span.SetAttributes(attribute.StringSlice("matchers", matcherspkg.ToStringSlice(matchers)))
212213
span.SetAttributes(attribute.Int("block.shards", len(q.shards)))
213214
span.SetAttributes(attribute.String("block.mint", time.UnixMilli(q.mint).String()))
214215
span.SetAttributes(attribute.String("block.maxt", time.UnixMilli(q.maxt).String()))
@@ -239,7 +240,7 @@ func (q *BlockChunkQuerier) selectChunksFn(ctx context.Context, _ bool, hints *s
239240
defer span.End()
240241

241242
span.SetAttributes(attribute.Bool("sorted", true))
242-
span.SetAttributes(attribute.StringSlice("matchers", matchersToStringSlice(matchers)))
243+
span.SetAttributes(attribute.StringSlice("matchers", matcherspkg.ToStringSlice(matchers)))
243244
span.SetAttributes(attribute.Int("block.shards", len(q.shards)))
244245

245246
sss := make([]storage.ChunkSeriesSet, 0, len(q.shards))

db/db.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"golang.org/x/sync/errgroup"
2121

2222
"github.com/thanos-io/thanos-parquet-gateway/internal/limits"
23+
matcherspkg "github.com/thanos-io/thanos-parquet-gateway/internal/matchers"
2324
"github.com/thanos-io/thanos-parquet-gateway/internal/tracing"
2425
"github.com/thanos-io/thanos-parquet-gateway/internal/util"
2526
"github.com/thanos-io/thanos-parquet-gateway/internal/warnings"
@@ -391,7 +392,7 @@ func (q DBQuerier) selectFn(ctx context.Context, sorted bool, hints *storage.Sel
391392
defer span.End()
392393

393394
span.SetAttributes(attribute.Bool("sorted", sorted))
394-
span.SetAttributes(attribute.StringSlice("matchers", matchersToStringSlice(matchers)))
395+
span.SetAttributes(attribute.StringSlice("matchers", matcherspkg.ToStringSlice(matchers)))
395396
span.SetAttributes(attribute.Int("block.shards", len(q.blocks)))
396397

397398
// If we need to merge multiple series sets vertically we need them sorted
@@ -431,7 +432,7 @@ func (q *DBChunkQuerier) selectChunksFn(ctx context.Context, sorted bool, hints
431432
defer span.End()
432433

433434
span.SetAttributes(attribute.Bool("sorted", sorted))
434-
span.SetAttributes(attribute.StringSlice("matchers", matchersToStringSlice(matchers)))
435+
span.SetAttributes(attribute.StringSlice("matchers", matcherspkg.ToStringSlice(matchers)))
435436
span.SetAttributes(attribute.Int("block.shards", len(q.blocks)))
436437

437438
// If we need to merge multiple series sets vertically we need them sorted

db/shard.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"go.opentelemetry.io/otel/attribute"
1919

2020
"github.com/thanos-io/thanos-parquet-gateway/internal/limits"
21+
matcherspkg "github.com/thanos-io/thanos-parquet-gateway/internal/matchers"
2122
"github.com/thanos-io/thanos-parquet-gateway/internal/tracing"
2223
"github.com/thanos-io/thanos-parquet-gateway/internal/warnings"
2324
"github.com/thanos-io/thanos-parquet-gateway/schema"
@@ -130,7 +131,7 @@ func (q ShardQuerier) LabelValues(ctx context.Context, name string, hints *stora
130131
ctx, span := tracing.Tracer().Start(ctx, "Label Values Shard")
131132
defer span.End()
132133

133-
span.SetAttributes(attribute.StringSlice("matchers", matchersToStringSlice(matchers)))
134+
span.SetAttributes(attribute.StringSlice("matchers", matcherspkg.ToStringSlice(matchers)))
134135
span.SetAttributes(attribute.StringSlice("shard.replica_labels", q.replicaLabelNames))
135136
span.SetAttributes(attribute.String("shard.external_labels", q.extlabels.String()))
136137

@@ -164,7 +165,7 @@ func (q ShardQuerier) LabelNames(ctx context.Context, hints *storage.LabelHints,
164165
ctx, span := tracing.Tracer().Start(ctx, "Label Names Shard")
165166
defer span.End()
166167

167-
span.SetAttributes(attribute.StringSlice("matchers", matchersToStringSlice(matchers)))
168+
span.SetAttributes(attribute.StringSlice("matchers", matcherspkg.ToStringSlice(matchers)))
168169
span.SetAttributes(attribute.StringSlice("shard.replica_labels", q.replicaLabelNames))
169170
span.SetAttributes(attribute.String("shard.external_labels", q.extlabels.String()))
170171

@@ -198,20 +199,12 @@ func (q ShardQuerier) Select(ctx context.Context, sorted bool, hints *storage.Se
198199
return newLazySeriesSet(ctx, q.selectFn, sorted, hints, matchers...)
199200
}
200201

201-
func matchersToStringSlice(matchers []*labels.Matcher) []string {
202-
res := make([]string, len(matchers))
203-
for i := range matchers {
204-
res[i] = matchers[i].String()
205-
}
206-
return res
207-
}
208-
209202
func (q ShardQuerier) selectCore(ctx context.Context, spanName string, sorted bool, hints *storage.SelectHints, matchers ...*labels.Matcher) ([]search.SeriesChunks, annotations.Annotations, error) {
210203
ctx, span := tracing.Tracer().Start(ctx, spanName)
211204
defer span.End()
212205

213206
span.SetAttributes(attribute.Bool("sorted", sorted))
214-
span.SetAttributes(attribute.StringSlice("matchers", matchersToStringSlice(matchers)))
207+
span.SetAttributes(attribute.StringSlice("matchers", matcherspkg.ToStringSlice(matchers)))
215208
span.SetAttributes(attribute.StringSlice("shard.replica_labels", q.replicaLabelNames))
216209
span.SetAttributes(attribute.String("shard.external_labels", q.extlabels.String()))
217210

internal/matchers/matchers.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Copyright (c) The Thanos Authors.
2+
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3+
// https://opensource.org/licenses/Apache-2.0
4+
5+
package matchers
6+
7+
import "github.com/prometheus/prometheus/model/labels"
8+
9+
// ToStringSlice converts a slice of Prometheus matchers to a slice of strings.
10+
func ToStringSlice(matchers []*labels.Matcher) []string {
11+
res := make([]string, len(matchers))
12+
for i := range matchers {
13+
res[i] = matchers[i].String()
14+
}
15+
return res
16+
}

internal/matchers/matchers_test.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// Copyright (c) The Thanos Authors.
2+
// Licensed under the Apache 2.0 license found in the LICENSE file or at:
3+
// https://opensource.org/licenses/Apache-2.0
4+
5+
package matchers
6+
7+
import (
8+
"testing"
9+
10+
"github.com/prometheus/prometheus/model/labels"
11+
)
12+
13+
func TestToStringSlice(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
matchers []*labels.Matcher
17+
want []string
18+
}{
19+
{
20+
name: "nil input",
21+
matchers: nil,
22+
want: []string{},
23+
},
24+
{
25+
name: "empty input",
26+
matchers: []*labels.Matcher{},
27+
want: []string{},
28+
},
29+
{
30+
name: "single matcher",
31+
matchers: []*labels.Matcher{
32+
labels.MustNewMatcher(labels.MatchEqual, "__name__", "up"),
33+
},
34+
want: []string{`__name__="up"`},
35+
},
36+
{
37+
name: "multiple matchers with different types",
38+
matchers: []*labels.Matcher{
39+
labels.MustNewMatcher(labels.MatchEqual, "__name__", "http_requests_total"),
40+
labels.MustNewMatcher(labels.MatchNotEqual, "job", "test"),
41+
labels.MustNewMatcher(labels.MatchRegexp, "instance", "localhost.*"),
42+
},
43+
want: []string{
44+
`__name__="http_requests_total"`,
45+
`job!="test"`,
46+
`instance=~"localhost.*"`,
47+
},
48+
},
49+
}
50+
51+
for _, tt := range tests {
52+
t.Run(tt.name, func(t *testing.T) {
53+
got := ToStringSlice(tt.matchers)
54+
if len(got) != len(tt.want) {
55+
t.Fatalf("len(ToStringSlice()) = %d, want %d", len(got), len(tt.want))
56+
}
57+
for i := range got {
58+
if got[i] != tt.want[i] {
59+
t.Errorf("ToStringSlice()[%d] = %q, want %q", i, got[i], tt.want[i])
60+
}
61+
}
62+
})
63+
}
64+
}

0 commit comments

Comments
 (0)