Skip to content

Commit

Permalink
fix: pipelines filter: check if attrib is not nil before running stri…
Browse files Browse the repository at this point in the history
…ng operator
  • Loading branch information
raj-k-singh committed Nov 23, 2023
1 parent 6e8abdc commit 2b09630
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 45 deletions.
96 changes: 51 additions & 45 deletions pkg/query-service/app/logparsingpipeline/pipelineBuilder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,55 +424,61 @@ func TestResourceFiltersWork(t *testing.T) {
func TestPipelineFilterWithStringOpsShouldNotSpamWarningsIfAttributeIsMissing(t *testing.T) {
require := require.New(t)

testPipeline := Pipeline{
OrderId: 1,
Name: "pipeline1",
Alias: "pipeline1",
Enabled: true,
Filter: &v3.FilterSet{
Operator: "AND",
Items: []v3.FilterItem{
{
Key: v3.AttributeKey{
Key: "service",
DataType: v3.AttributeKeyDataTypeString,
Type: v3.AttributeKeyTypeResource,
for _, operator := range []v3.FilterOperator{
v3.FilterOperatorContains,
v3.FilterOperatorNotContains,
v3.FilterOperatorRegex,
v3.FilterOperatorNotRegex,
} {
testPipeline := Pipeline{
OrderId: 1,
Name: "pipeline1",
Alias: "pipeline1",
Enabled: true,
Filter: &v3.FilterSet{
Operator: "AND",
Items: []v3.FilterItem{
{
Key: v3.AttributeKey{
Key: "service",
DataType: v3.AttributeKeyDataTypeString,
Type: v3.AttributeKeyTypeResource,
},
Operator: operator,
Value: "nginx",
},
Operator: "contains",
Value: "nginx",
},
},
},
Config: []PipelineOperator{
{
ID: "add",
Type: "add",
Enabled: true,
Name: "add",
Field: "attributes.test",
Value: "test-value",
Config: []PipelineOperator{
{
ID: "add",
Type: "add",
Enabled: true,
Name: "add",
Field: "attributes.test",
Value: "test-value",
},
},
},
}

testLog := model.SignozLog{
Timestamp: uint64(time.Now().UnixNano()),
Body: "test log",
Attributes_string: map[string]string{},
Resources_string: map[string]string{},
SeverityText: entry.Info.String(),
SeverityNumber: uint8(entry.Info),
SpanID: "",
TraceID: "",
}
}

result, collectorWarnAndErrorLogs, err := SimulatePipelinesProcessing(
context.Background(),
[]Pipeline{testPipeline},
[]model.SignozLog{testLog},
)
require.Nil(err)
require.Equal(0, len(collectorWarnAndErrorLogs), strings.Join(collectorWarnAndErrorLogs, "\n"))
require.Equal(1, len(result))
testLog := model.SignozLog{
Timestamp: uint64(time.Now().UnixNano()),
Body: "test log",
Attributes_string: map[string]string{},
Resources_string: map[string]string{},
SeverityText: entry.Info.String(),
SeverityNumber: uint8(entry.Info),
SpanID: "",
TraceID: "",
}

result, collectorWarnAndErrorLogs, err := SimulatePipelinesProcessing(
context.Background(),
[]Pipeline{testPipeline},
[]model.SignozLog{testLog},
)
require.Nil(err)
require.Equal(0, len(collectorWarnAndErrorLogs), strings.Join(collectorWarnAndErrorLogs, "\n"))
require.Equal(1, len(result))
}
}
13 changes: 13 additions & 0 deletions pkg/query-service/queryBuilderToExpr/queryBuilderToExpr.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
expr "github.com/antonmedv/expr"
v3 "go.signoz.io/signoz/pkg/query-service/model/v3"
"go.uber.org/zap"
"golang.org/x/exp/slices"
)

var logOperatorsToExpr = map[v3.FilterOperator]string{
Expand Down Expand Up @@ -66,6 +67,18 @@ func Parse(filters *v3.FilterSet) (string, error) {
filter = fmt.Sprintf("%s %s %s", name, logOperatorsToExpr[v.Operator], exprFormattedValue(v.Value))
}

if slices.Contains(
[]v3.FilterOperator{
v3.FilterOperatorContains,
v3.FilterOperatorNotContains,
v3.FilterOperatorRegex,
v3.FilterOperatorNotRegex,
},
v.Operator,
) {
filter = fmt.Sprintf("%s != nil && %s", name, filter)
}

// check if the filter is a correct expression language
_, err := expr.Compile(filter)
if err != nil {
Expand Down

0 comments on commit 2b09630

Please sign in to comment.