Skip to content

Commit

Permalink
resolve comment: reduce signature change
Browse files Browse the repository at this point in the history
  • Loading branch information
yuchen-db committed Dec 13, 2024
1 parent 3658a51 commit 0177f7a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 60 deletions.
3 changes: 1 addition & 2 deletions pkg/store/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,10 @@ func ScanGRPCCurlProtoStreamMessages(data []byte, atEOF bool) (advance int, toke
// Series returns all series for a requested time range and label matcher. The returned data may
// exceed the requested time bounds.
func (s *LocalStore) Series(r *storepb.SeriesRequest, srv storepb.Store_SeriesServer) error {
matchers, err := storepb.MatchersToPromMatchers(r.Matchers...)
match, matchers, err := matchesExternalLabels(r.Matchers, s.extLabels, nil)
if err != nil {
return status.Error(codes.InvalidArgument, err.Error())
}
match, matchers := matchesExternalLabels(matchers, s.extLabels)
if !match {
return nil
}
Expand Down
28 changes: 18 additions & 10 deletions pkg/store/prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,10 @@ func (p *PrometheusStore) Series(r *storepb.SeriesRequest, seriesSrv storepb.Sto

extLset := p.externalLabelsFn()

matchers, err := storepb.MatchersToPromMatchers(r.Matchers...)
match, matchers, err := matchesExternalLabels(r.Matchers, extLset, nil)
if err != nil {
return status.Error(codes.InvalidArgument, err.Error())
}
match, matchers := matchesExternalLabels(matchers, extLset)
if !match {
return nil
}
Expand Down Expand Up @@ -489,9 +488,20 @@ func (p *PrometheusStore) startPromRemoteRead(ctx context.Context, q *prompb.Que

// matchesExternalLabels returns false if given matchers are not matching external labels.
// If true, matchesExternalLabels also returns Prometheus matchers without those matching external labels.
func matchesExternalLabels(tms []*labels.Matcher, externalLabels labels.Labels) (bool, []*labels.Matcher) {
func matchesExternalLabels(ms []storepb.LabelMatcher, externalLabels labels.Labels, mc *storepb.MatcherConverter) (bool, []*labels.Matcher, error) {
var tms []*labels.Matcher
var err error
if mc != nil {
tms, err = mc.MatchersToPromMatchers(ms...)
} else {
tms, err = storepb.MatchersToPromMatchers(ms...)
}
if err != nil {
return false, nil, err
}

if externalLabels.IsEmpty() {
return true, tms
return true, tms, nil
}

var newMatchers []*labels.Matcher
Expand All @@ -508,10 +518,10 @@ func matchesExternalLabels(tms []*labels.Matcher, externalLabels labels.Labels)
if !tm.Matches(extValue) {
// External label does not match. This should not happen - it should be filtered out on query node,
// but let's do that anyway here.
return false, nil
return false, nil, nil
}
}
return true, newMatchers
return true, newMatchers, nil
}

// encodeChunk translates the sample pairs into a chunk.
Expand All @@ -533,11 +543,10 @@ func (p *PrometheusStore) encodeChunk(ss []prompb.Sample) (storepb.Chunk_Encodin
func (p *PrometheusStore) LabelNames(ctx context.Context, r *storepb.LabelNamesRequest) (*storepb.LabelNamesResponse, error) {
extLset := p.externalLabelsFn()

matchers, err := storepb.MatchersToPromMatchers(r.Matchers...)
match, matchers, err := matchesExternalLabels(r.Matchers, extLset, nil)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
match, matchers := matchesExternalLabels(matchers, extLset)
if !match {
return &storepb.LabelNamesResponse{Names: nil}, nil
}
Expand Down Expand Up @@ -597,11 +606,10 @@ func (p *PrometheusStore) LabelValues(ctx context.Context, r *storepb.LabelValue

extLset := p.externalLabelsFn()

matchers, err := storepb.MatchersToPromMatchers(r.Matchers...)
match, matchers, err := matchesExternalLabels(r.Matchers, extLset, nil)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
match, matchers := matchesExternalLabels(matchers, extLset)
if !match {
return &storepb.LabelValuesResponse{}, nil
}
Expand Down
27 changes: 3 additions & 24 deletions pkg/store/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,24 +273,6 @@ func (s *ProxyStore) TSDBInfos() []infopb.TSDBInfo {
return infos
}

// MatchersToPromMatchers converts storepb label matchers to prometheus label matchers. It goes to cache if matcherConverter is set.
func (s *ProxyStore) MatchersToPromMatchers(ms ...storepb.LabelMatcher) ([]*labels.Matcher, error) {
var tms []*labels.Matcher
var err error
if s.matcherConverter == nil {
tms, err = storepb.MatchersToPromMatchers(ms...)
if err != nil {
return nil, err
}
return tms, nil
}
tms, err = s.matcherConverter.MatchersToPromMatchers(ms...)
if err != nil {
return nil, err
}
return tms, nil
}

func (s *ProxyStore) Series(originalRequest *storepb.SeriesRequest, srv storepb.Store_SeriesServer) error {
// TODO(bwplotka): This should be part of request logger, otherwise it does not make much sense. Also, could be
// triggered by tracing span to reduce cognitive load.
Expand All @@ -299,11 +281,10 @@ func (s *ProxyStore) Series(originalRequest *storepb.SeriesRequest, srv storepb.
reqLogger = log.With(reqLogger, "request", originalRequest.String())
}

matchers, err := s.MatchersToPromMatchers(originalRequest.Matchers...)
match, matchers, err := matchesExternalLabels(originalRequest.Matchers, s.selectorLabels, s.matcherConverter)
if err != nil {
return status.Error(codes.InvalidArgument, err.Error())
}
match, matchers := matchesExternalLabels(matchers, s.selectorLabels)
if !match {
return nil
}
Expand Down Expand Up @@ -499,11 +480,10 @@ func (s *ProxyStore) LabelNames(ctx context.Context, originalRequest *storepb.La
if s.debugLogging {
reqLogger = log.With(reqLogger, "request", originalRequest.String())
}
matchers, err := s.MatchersToPromMatchers(originalRequest.Matchers...)
match, matchers, err := matchesExternalLabels(originalRequest.Matchers, s.selectorLabels, s.matcherConverter)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
match, matchers := matchesExternalLabels(matchers, s.selectorLabels)
if !match {
return &storepb.LabelNamesResponse{}, nil
}
Expand Down Expand Up @@ -603,11 +583,10 @@ func (s *ProxyStore) LabelValues(ctx context.Context, originalRequest *storepb.L
return nil, status.Error(codes.InvalidArgument, "label name parameter cannot be empty")
}

matchers, err := s.MatchersToPromMatchers(originalRequest.Matchers...)
match, matchers, err := matchesExternalLabels(originalRequest.Matchers, s.selectorLabels, s.matcherConverter)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
match, matchers := matchesExternalLabels(matchers, s.selectorLabels)
if !match {
return &storepb.LabelValuesResponse{}, nil
}
Expand Down
27 changes: 3 additions & 24 deletions pkg/store/tsdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,24 +245,6 @@ func (s *TSDBStore) SeriesLocal(ctx context.Context, r *storepb.SeriesRequest) (
return rs.series, nil
}

// MatchersToPromMatchers converts storepb label matchers to prometheus label matchers. It goes to cache if matcherConverter is set.
func (s *TSDBStore) MatchersToPromMatchers(ms ...storepb.LabelMatcher) ([]*labels.Matcher, error) {
var tms []*labels.Matcher
var err error
if s.matcherConverter == nil {
tms, err = storepb.MatchersToPromMatchers(ms...)
if err != nil {
return nil, err
}
return tms, nil
}
tms, err = s.matcherConverter.MatchersToPromMatchers(ms...)
if err != nil {
return nil, err
}
return tms, nil
}

// Series returns all series for a requested time range and label matcher. The returned data may
// exceed the requested time bounds.
func (s *TSDBStore) Series(r *storepb.SeriesRequest, seriesSrv storepb.Store_SeriesServer) error {
Expand All @@ -273,11 +255,10 @@ func (s *TSDBStore) Series(r *storepb.SeriesRequest, seriesSrv storepb.Store_Ser
srv = fs
}

matchers, err := s.MatchersToPromMatchers(r.Matchers...)
match, matchers, err := matchesExternalLabels(r.Matchers, s.getExtLset(), s.matcherConverter)
if err != nil {
return status.Error(codes.InvalidArgument, err.Error())
}
match, matchers := matchesExternalLabels(matchers, s.getExtLset())

if !match {
return nil
Expand Down Expand Up @@ -401,11 +382,10 @@ func (s *TSDBStore) Series(r *storepb.SeriesRequest, seriesSrv storepb.Store_Ser
func (s *TSDBStore) LabelNames(ctx context.Context, r *storepb.LabelNamesRequest) (
*storepb.LabelNamesResponse, error,
) {
matchers, err := s.MatchersToPromMatchers(r.Matchers...)
match, matchers, err := matchesExternalLabels(r.Matchers, s.getExtLset(), s.matcherConverter)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
match, matchers := matchesExternalLabels(matchers, s.getExtLset())

if !match {
return &storepb.LabelNamesResponse{Names: nil}, nil
Expand Down Expand Up @@ -464,11 +444,10 @@ func (s *TSDBStore) LabelValues(ctx context.Context, r *storepb.LabelValuesReque
}
}

matchers, err := s.MatchersToPromMatchers(r.Matchers...)
match, matchers, err := matchesExternalLabels(r.Matchers, s.getExtLset(), s.matcherConverter)
if err != nil {
return nil, status.Error(codes.InvalidArgument, err.Error())
}
match, matchers := matchesExternalLabels(matchers, s.getExtLset())
if !match {
return &storepb.LabelValuesResponse{}, nil
}
Expand Down

0 comments on commit 0177f7a

Please sign in to comment.