Skip to content

Commit

Permalink
[querier] promql append regex head and tail
Browse files Browse the repository at this point in the history
  • Loading branch information
taloric committed Sep 5, 2023
1 parent 3742c2c commit 9686f87
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions server/querier/app/prometheus/service/converters.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,19 +277,19 @@ func (p *prometheusReader) promReaderTransToSQL(ctx context.Context, req *prompb
if matcher.Name == PROMETHEUS_METRICS_NAME {
continue
}
operation := getLabelMatcherType(matcher.Type)
operation, value := getLabelMatcher(matcher.Type, matcher.Value)
if operation == "" {
return ctx, "", "", "", "", fmt.Errorf("unknown match type %v", matcher.Type)
}

tagName, tagAlias, isDeepFlowTag := p.parsePromQLTag(prefixType, db, matcher.Name)
if prefixType != prefixNone && isDeepFlowTag && tagAlias != "" {
// for Prometheus metrics, query DeepFlow enum tag can only use tag alias(x_enum) in filter clause
filters = append(filters, fmt.Sprintf("%s %s '%s'", tagAlias, operation, matcher.Value))
filters = append(filters, fmt.Sprintf("%s %s '%s'", tagAlias, operation, value))
} else {
// for normal query
// for DeepFlow metrics, query enum tag can only use tag name(Enum(x)) in filter clause
filters = append(filters, fmt.Sprintf("%s %s '%s'", tagName, operation, matcher.Value))
filters = append(filters, fmt.Sprintf("%s %s '%s'", tagName, operation, value))
}

if db == "" || db == chCommon.DB_NAME_PROMETHEUS || db == chCommon.DB_NAME_EXT_METRICS {
Expand Down Expand Up @@ -731,19 +731,32 @@ func (p *prometheusReader) respTransToProm(ctx context.Context, metricsName stri
}

// match prometheus lable matcher type
func getLabelMatcherType(t prompb.LabelMatcher_Type) string {
func getLabelMatcher(t prompb.LabelMatcher_Type, v string) (string, string) {
switch t {
case prompb.LabelMatcher_EQ:
return "="
return "=", v
case prompb.LabelMatcher_NEQ:
return "!="
return "!=", v
case prompb.LabelMatcher_RE:
return "REGEXP"
return "REGEXP", appendRegexRules(v)
case prompb.LabelMatcher_NRE:
return "NOT REGEXP"
return "NOT REGEXP", appendRegexRules(v)
default:
return ""
return "", v
}
}

func appendRegexRules(v string) string {
if len(v) > 0 {
if !strings.HasPrefix(v, "^") {
v = "^" + v
}
if !strings.HasSuffix(v, "$") {
v = v + "$"
}
return v
}
return v
}

func getValue(value interface{}) string {
Expand Down

0 comments on commit 9686f87

Please sign in to comment.