Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[weekly-r317] Add debug logs to PostingsForMatchers #812

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions tsdb/querier.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,18 @@ import (
"errors"
"fmt"
"math"
"os"
"slices"
"time"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/oklog/ulid"
"go.opentelemetry.io/otel/trace"

"github.com/prometheus/prometheus/model/histogram"
"github.com/prometheus/prometheus/model/labels"
"github.com/prometheus/prometheus/promql/parser"
"github.com/prometheus/prometheus/storage"
"github.com/prometheus/prometheus/tsdb/chunkenc"
"github.com/prometheus/prometheus/tsdb/chunks"
Expand Down Expand Up @@ -192,6 +198,20 @@ func selectChunkSeriesSet(ctx context.Context, sortSeries bool, hints *storage.S
// PostingsForMatchers assembles a single postings iterator against the index reader
// based on the given matchers. The resulting postings are not ordered by series.
func PostingsForMatchers(ctx context.Context, ix IndexPostingsReader, ms ...*labels.Matcher) (index.Postings, error) {
shouldLog := false
for _, m := range ms {
if m.Name == labels.MetricName {
shouldLog = m.Type == labels.MatchEqual && m.Value == "tempo_request_duration_seconds_bucket"
break
}
}

var originalMatchers []*labels.Matcher
if shouldLog {
var matchersCopyArr [128]*labels.Matcher
originalMatchers = append(matchersCopyArr[:0], ms...)
}

var its, notIts []index.Postings
// See which label must be non-empty.
// Optimization for case like {l=~".", l!="1"}.
Expand Down Expand Up @@ -317,9 +337,57 @@ func PostingsForMatchers(ctx context.Context, ix IndexPostingsReader, ms ...*lab
it = index.Without(it, n)
}

if shouldLog {
matchersUnchanged := sameMatchers(ms, originalMatchers)
slices.SortStableFunc(originalMatchers, func(i, j *labels.Matcher) int {
if !isSubtractingMatcher(i) && isSubtractingMatcher(j) {
return -1
}

return +1
})
matchersSorted := sameMatchers(ms, originalMatchers)
matchersChanged := !matchersUnchanged && !matchersSorted

traceID := trace.SpanFromContext(ctx).SpanContext().TraceID().String()
level.Info(globalLogger).Log(
"msg", "PostingsForMatchers",
"type", fmt.Sprintf("%T", ix),
"traceID", traceID,
"matchers", formatMatchers(ms),
"original_matchers", formatMatchers(originalMatchers),
"matchersUnchanged", matchersUnchanged,
"matchersSorted", matchersSorted,
"matchersChanged", matchersChanged,
)
}

return it, nil
}

var globalLogger = log.NewSyncLogger(
log.With(log.NewLogfmtLogger(log.NewSyncWriter(os.Stderr)),
"ts", log.Timestamp(time.Now),
"logger_sourrce", "tsdb",
),
)

func sameMatchers(a, b []*labels.Matcher) bool {
if len(a) != len(b) {
return false
}
for i, m := range a {
if m.Type != b[i].Type || m.Name != b[i].Name || m.Value != b[i].Value {
return false
}
}
return true
}

func formatMatchers(ms []*labels.Matcher) string {
return (&parser.VectorSelector{LabelMatchers: ms}).String()
}

func postingsForMatcher(ctx context.Context, ix IndexPostingsReader, m *labels.Matcher) (index.Postings, error) {
// This method will not return postings for missing labels.

Expand Down
Loading