Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

fix: allow multiple spaces between a filter expression (#1897)

Nityananda Gohain9c80ba6b786e
diff --git a/pkg/query-service/app/logs/parser.go b/pkg/query-service/app/logs/parser.go
index 2b2ebc0015..5e4a195817 100644
--- a/pkg/query-service/app/logs/parser.go
+++ b/pkg/query-service/app/logs/parser.go
@@ -36,7 +36,7 @@ const (
DESC = "desc"
)
-var tokenRegex, _ = regexp.Compile(`(?i)(and( )*?|or( )*?)?(([\w.-]+ (in|nin) \([^(]+\))|([\w.]+ (gt|lt|gte|lte) (')?[\S]+(')?)|([\w.]+ (contains|ncontains)) [^\\]?'(.*?[^\\])')`)
+var tokenRegex, _ = regexp.Compile(`(?i)(and( )*?|or( )*?)?(([\w.-]+( )+(in|nin)( )+\([^(]+\))|([\w.]+( )+(gt|lt|gte|lte)( )+(')?[\S]+(')?)|([\w.]+( )+(contains|ncontains))( )+[^\\]?'(.*?[^\\])')`)
var operatorRegex, _ = regexp.Compile(`(?i)(?: )(in|nin|gt|lt|gte|lte|contains|ncontains)(?: )`)
func ParseLogFilterParams(r *http.Request) (*model.LogsFilterParams, error) {
@@ -152,6 +152,7 @@ func ParseLogAggregateParams(r *http.Request) (*model.LogsAggregateParams, error
func parseLogQuery(query string) ([]string, error) {
sqlQueryTokens := []string{}
+
filterTokens := tokenRegex.FindAllString(query, -1)
if len(filterTokens) == 0 {
@@ -190,7 +191,13 @@ func parseLogQuery(query string) ([]string, error) {
sqlQueryTokens = append(sqlQueryTokens, f)
} else {
symbol := operatorMapping[strings.ToLower(op)]
- sqlQueryTokens = append(sqlQueryTokens, strings.Replace(v, " "+op+" ", " "+symbol+" ", 1)+" ")
+ sqlExpr := strings.Replace(v, " "+op+" ", " "+symbol+" ", 1)
+ splittedExpr := strings.Split(sqlExpr, symbol)
+ if len(splittedExpr) != 2 {
+ return nil, fmt.Errorf("error while splitting expression: %s", sqlExpr)
+ }
+ trimmedSqlExpr := fmt.Sprintf("%s %s %s ", strings.Join(strings.Fields(splittedExpr[0]), " "), symbol, strings.TrimSpace(splittedExpr[1]))
+ sqlQueryTokens = append(sqlQueryTokens, trimmedSqlExpr)
}
}
diff --git a/pkg/query-service/app/logs/parser_test.go b/pkg/query-service/app/logs/parser_test.go
index a305fb6be2..d51f5b554f 100644
--- a/pkg/query-service/app/logs/parser_test.go
+++ b/pkg/query-service/app/logs/parser_test.go
@@ -80,7 +80,17 @@ var correctQueriesTest = []struct {
{
`filters with extra spaces`,
`service IN ('name > 100') AND length gt 100`,
- []string{`service IN ('name > 100') `, `AND length > 100 `},
+ []string{`service IN ('name > 100') `, `AND length > 100 `},
+ },
+ {
+ `Extra space within a filter expression`,
+ `service IN ('name > 100')`,
+ []string{`service IN ('name > 100') `},
+ },
+ {
+ `Extra space between a query filter`,
+ `data contains 'hello world .'`,
+ []string{`data ILIKE '%hello world .%' `},
},
{
`filters with special characters in key name`,