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.
TypeScript apmapplication-monitoringdistributed-tracinggogood-first-issuejaegerloglogsmetricsmonitoringnextjsobservabilityopen-sourceopentelemetryprometheusreactreactjsself-hostedtracingtypescript
package telemetrymetadata
import (
"context"
"fmt"
schema "github.com/SigNoz/signoz-otel-collector/cmd/signozschemamigrator/schema_migrator"
"github.com/SigNoz/signoz/pkg/clickhousesql"
"github.com/SigNoz/signoz/pkg/querybuilder"
qbtypes "github.com/SigNoz/signoz/pkg/types/querybuildertypes/querybuildertypesv5"
"github.com/SigNoz/signoz/pkg/types/telemetrytypes"
"github.com/huandu/go-sqlbuilder"
)
// Compile keeps the related-values polarity form for a single key: the key
// must exist to apply the filter, and a row without it answers with the
// operator's polarity. A family compiles through the shared condition.
func (s *storage) Compile(ctx context.Context, q qbtypes.QueryInfo, logical *telemetrytypes.LogicalField, operator qbtypes.FilterOperator, value any, sb *sqlbuilder.SelectBuilder) (qbtypes.Compiled, error) {
if logical.IsFamily() {
return querybuilder.SharedCondition(ctx, q, s, logical, operator, value, sb)
}
condition, err := s.conditionForKey(ctx, q, logical.Single(), operator, value, sb)
if err != nil {
return qbtypes.Compiled{}, err
}
return qbtypes.Compiled{Condition: condition}, nil
}
func (s *storage) conditionForKey(
ctx context.Context,
q qbtypes.QueryInfo,
key *telemetrytypes.TelemetryFieldKey,
operator qbtypes.FilterOperator,
value any,
sb *sqlbuilder.SelectBuilder,
) (string, error) {
switch operator {
case qbtypes.FilterOperatorContains,
qbtypes.FilterOperatorNotContains,
qbtypes.FilterOperatorILike,
qbtypes.FilterOperatorNotILike,
qbtypes.FilterOperatorLike,
qbtypes.FilterOperatorNotLike:
value = querybuilder.FormatValueForContains(value)
}
columns, err := s.getColumn(ctx, q.StartNs, q.EndNs, key)
if err != nil {
// if we don't have a column, we can't build a condition for related values
return "", nil
}
fieldExpression, err := s.read(ctx, q, key)
if err != nil {
// if we don't have a table field name, we can't build a condition for related values
return "", nil
}
if key.FieldDataType != telemetrytypes.FieldDataTypeString &&
key.FieldDataType != telemetrytypes.FieldDataTypeUnspecified {
// if the field data type is not string, we can't build a condition for related values
return "", nil
}
fieldExpression, value = querybuilder.DataTypeCollisionHandledFieldName(key, value, fieldExpression, operator)
// key must exist to apply the main filter. for positive operators the
// absent-key rows are excluded (fallback false); for negative operators
// they are kept (fallback true) so rows legitimately lacking the key match.
keyMissingFallback := operator.IsNegativeOperator()
expr := `if(mapContains(%s, %s), %s, %t)`
var cond string
// regular operators
switch operator {
// regular operators
case qbtypes.FilterOperatorEqual:
cond = sb.E(fieldExpression, value)
case qbtypes.FilterOperatorNotEqual:
cond = sb.NE(fieldExpression, value)
// like and not like
case qbtypes.FilterOperatorLike:
cond = sb.Like(fieldExpression, value)
case qbtypes.FilterOperatorNotLike:
cond = sb.NotLike(fieldExpression, value)
case qbtypes.FilterOperatorILike:
cond = sb.ILike(fieldExpression, value)
case qbtypes.FilterOperatorNotILike:
cond = sb.NotILike(fieldExpression, value)
case qbtypes.FilterOperatorContains:
cond = sb.ILike(fieldExpression, fmt.Sprintf("%%%s%%", value))
case qbtypes.FilterOperatorNotContains:
cond = sb.NotILike(fieldExpression, fmt.Sprintf("%%%s%%", value))
case qbtypes.FilterOperatorRegexp:
cond = fmt.Sprintf(`match(%s, %s)`, sqlbuilder.Escape(fieldExpression), sb.Var(value))
case qbtypes.FilterOperatorNotRegexp:
cond = fmt.Sprintf(`NOT match(%s, %s)`, sqlbuilder.Escape(fieldExpression), sb.Var(value))
// in and not in
case qbtypes.FilterOperatorIn:
values, ok := value.([]any)
if !ok {
return "", qbtypes.ErrInValues
}
// instead of using IN, we use `=` + `OR` to make use of index
conditions := []string{}
for _, value := range values {
conditions = append(conditions, sb.E(fieldExpression, value))
}
cond = sb.Or(conditions...)
case qbtypes.FilterOperatorNotIn:
values, ok := value.([]any)
if !ok {
return "", qbtypes.ErrInValues
}
// instead of using NOT IN, we use `!=` + `AND` to make use of index
conditions := []string{}
for _, value := range values {
conditions = append(conditions, sb.NE(fieldExpression, value))
}
cond = sb.And(conditions...)
// exists and not exists
// in the query builder, `exists` and `not exists` are used for
// key membership checks, so depending on the column type, the condition changes
case qbtypes.FilterOperatorExists, qbtypes.FilterOperatorNotExists:
switch columns[0].Type {
case schema.MapColumnType{
KeyType: schema.LowCardinalityColumnType{ElementType: schema.ColumnTypeString},
ValueType: schema.ColumnTypeString,
}:
leftOperand := fmt.Sprintf("mapContains(%s, %s)", columns[0].Name, clickhousesql.StringLiteral(key.Name))
if operator == qbtypes.FilterOperatorExists {
cond = sb.E(leftOperand, true)
} else {
cond = sb.NE(leftOperand, true)
}
}
}
return fmt.Sprintf(expr, columns[0].Name, sb.Var(key.Name), cond, keyMissingFallback), nil
}