The only tool you need to know what is happening and how to fix it.
Go alertingexception-trackerissue-trackinglogsmetricsmonitoringobservability-platformopen-telemetryotelsession-replaytelemetrytraces
add: logs search with contains and equals
narrow-adventure8a68be60f6bf
diff --git a/backend/app/controllers/log.controller.go b/backend/app/controllers/log.controller.goindex 326f9bc2..1cc072f3 100644--- a/backend/app/controllers/log.controller.go+++ b/backend/app/controllers/log.controller.go@@ -20,10 +20,11 @@ type logController struct{}var LogController = logController{}type LogAttributeFilterRequest struct {- Scope string `json:"scope"` // "resource" | "scope" | "log"- Key string `json:"key"`- Value string `json:"value"`- Exclude bool `json:"exclude"`+ Scope string `json:"scope"` // "resource" | "scope" | "log"+ Key string `json:"key"`+ Value string `json:"value"`+ Exclude bool `json:"exclude"`+ Contains bool `json:"contains"`}type LogPaginationParams struct {@@ -101,10 +102,11 @@ func (l logController) List(c *gin.Context) {continue}attrFilters = append(attrFilters, telemetry.LogAttributeFilter{- Scope: f.Scope,- Key: f.Key,- Value: f.Value,- Exclude: f.Exclude,+ Scope: f.Scope,+ Key: f.Key,+ Value: f.Value,+ Exclude: f.Exclude,+ Contains: f.Contains,})}diff --git a/backend/app/repositories/telemetry/attribute_filter_contains_test.go b/backend/app/repositories/telemetry/attribute_filter_contains_test.gonew file mode 100644index 00000000..3e8cec57--- /dev/null+++ b/backend/app/repositories/telemetry/attribute_filter_contains_test.go@@ -0,0 +1,71 @@+//go:build !telemetry_ch++package telemetry++import (+ "context"+ "testing"+ "time"++ "github.com/google/uuid"+ "github.com/tracewayapp/traceway/backend/app/models"+)++func TestLogRecordRepository_Search_ContainsAttributeFilter(t *testing.T) {+ setupTestDB(t)+ ctx := context.Background()+ projectId := uuid.New()+ base := time.Date(2026, 1, 1, 12, 0, 0, 0, time.UTC)++ record := func(attrs map[string]string) models.LogRecord {+ return models.LogRecord{+ Id: uuid.New(),+ ProjectId: projectId,+ Timestamp: base,+ ServiceName: "checkout",+ Body: "request handled",+ LogAttributes: attrs,+ }+ }+ if err := LogRecordRepository.InsertAsync(ctx, []models.LogRecord{+ record(map[string]string{"http.route": "GET /checkout"}),+ record(map[string]string{"http.route": "GET /cart"}),+ record(map[string]string{"other.key": "x"}),+ }); err != nil {+ t.Fatalf("InsertAsync: %v", err)+ }++ search := func(filter LogAttributeFilter) ([]models.LogRecord, int64) {+ records, total, err := LogRecordRepository.Search(ctx, LogSearchParams{+ ProjectId: projectId,+ FromDate: base.Add(-time.Hour),+ ToDate: base.Add(time.Hour),+ AttributeFilters: []LogAttributeFilter{filter},+ })+ if err != nil {+ t.Fatalf("Search (%+v): %v", filter, err)+ }+ return records, total+ }++ // Case-insensitive substring match.+ records, total := search(LogAttributeFilter{Scope: "log", Key: "http.route", Value: "Checkout", Contains: true})+ if total != 1 || len(records) != 1 {+ t.Fatalf("contains attribute filter returned %d rows (total=%d), want 1", len(records), total)+ }+ if records[0].LogAttributes["http.route"] != "GET /checkout" {+ t.Errorf("contains attribute filter returned http.route=%q, want GET /checkout", records[0].LogAttributes["http.route"])+ }++ // Negated form drops the match; the row that doesn't carry the attribute at+ // all must survive.+ records, total = search(LogAttributeFilter{Scope: "log", Key: "http.route", Value: "Checkout", Contains: true, Exclude: true})+ if total != 2 || len(records) != 2 {+ t.Fatalf("not-contains attribute filter returned %d rows (total=%d), want 2", len(records), total)+ }+ for _, r := range records {+ if r.LogAttributes["http.route"] == "GET /checkout" {+ t.Errorf("excluded record (http.route=GET /checkout) was returned")+ }+ }+}diff --git a/backend/app/repositories/telemetry/clickhouse/log_record.repository.go b/backend/app/repositories/telemetry/clickhouse/log_record.repository.goindex 1d257a77..7f867a8f 100644--- a/backend/app/repositories/telemetry/clickhouse/log_record.repository.go+++ b/backend/app/repositories/telemetry/clickhouse/log_record.repository.go@@ -226,13 +226,24 @@ func (r *logRecordRepository) buildWhere(params shared.LogSearchParams) (string,if col == "" {continue}- op := "="- if f.Exclude {- // Map columns default missing keys to '', so != also keeps rows- // that don't carry the attribute at all.- op = "!="+ if f.Contains {+ // Case-insensitive substring match. Map columns default missing+ // keys to '', which contains nothing, so the negated form keeps+ // rows that don't carry the attribute at all.+ op := "> 0"+ if f.Exclude {+ op = "= 0"+ }+ clauses = append(clauses, "positionCaseInsensitive("+col+"[?], ?) "+op)+ } else {+ op := "="+ if f.Exclude {+ // Map columns default missing keys to '', so != also keeps rows+ // that don't carry the attribute at all.+ op = "!="+ }+ clauses = append(clauses, col+"[?] "+op+" ?")}- clauses = append(clauses, col+"[?] "+op+" ?")args = append(args, f.Key, f.Value)}diff --git a/backend/app/repositories/telemetry/duckdb/log_record.repository.go b/backend/app/repositories/telemetry/duckdb/log_record.repository.goindex 58997459..91b7a512 100644--- a/backend/app/repositories/telemetry/duckdb/log_record.repository.go+++ b/backend/app/repositories/telemetry/duckdb/log_record.repository.go@@ -255,7 +255,17 @@ func (r *logRecordRepository) buildWhere(params shared.LogSearchParams) (string,}keyPH := fmt.Sprintf("attr_k%d", i)valPH := fmt.Sprintf("attr_v%d", i)- if f.Exclude {+ if f.Contains {+ // Case-insensitive substring match. COALESCE keeps the negated+ // form true for rows that don't carry the attribute at all+ // (json_extract_string yields NULL there, and INSTR on NULL is NULL).+ expr := fmt.Sprintf("INSTR(LOWER(COALESCE(json_extract_string(%s, '$.\"' || :%s || '\"'), '')), LOWER(:%s))", col, keyPH, valPH)+ if f.Exclude {+ clauses = append(clauses, expr+" = 0")+ } else {+ clauses = append(clauses, expr+" > 0")+ }+ } else if f.Exclude {// COALESCE keeps rows that don't carry the attribute at all// (json_extract_string yields NULL there, and NULL != value is NULL).clauses = append(clauses,diff --git a/backend/app/repositories/telemetry/shared/types.go b/backend/app/repositories/telemetry/shared/types.goindex cf71dadf..d45b9c67 100644--- a/backend/app/repositories/telemetry/shared/types.go+++ b/backend/app/repositories/telemetry/shared/types.go@@ -23,15 +23,18 @@ type FiredNotification struct {FiredAt time.Time}-// LogAttributeFilter selects logs by an exact attribute value. Scope is one of+// LogAttributeFilter selects logs by attribute value. Scope is one of// "resource", "scope", or "log"; picks which of the three attribute maps to// query (Map columns with bloom-filter indexes on ClickHouse, JSON on-// SQLite/DuckDB).+// SQLite/DuckDB). Contains switches from exact match to case-insensitive+// substring match; Exclude negates either mode. Negated filters keep rows+// that don't carry the attribute at all.type LogAttributeFilter struct {- Scope string- Key string- Value string- Exclude bool+ Scope string+ Key string+ Value string+ Exclude bool+ Contains bool}type LogSearchParams struct {diff --git a/backend/app/repositories/telemetry/sqlite/log_record.repository.go b/backend/app/repositories/telemetry/sqlite/log_record.repository.goindex b9f825d1..dcdbafc7 100644--- a/backend/app/repositories/telemetry/sqlite/log_record.repository.go+++ b/backend/app/repositories/telemetry/sqlite/log_record.repository.go@@ -240,7 +240,17 @@ func (r *logRecordRepository) buildWhere(params shared.LogSearchParams) (string,}keyPH := fmt.Sprintf("attr_k%d", i)valPH := fmt.Sprintf("attr_v%d", i)- if f.Exclude {+ if f.Contains {+ // Case-insensitive substring match. COALESCE keeps the negated+ // form true for rows that don't carry the attribute at all+ // (json_extract yields NULL there, and INSTR on NULL is NULL).+ expr := fmt.Sprintf("INSTR(LOWER(COALESCE(json_extract(%s, '$.\"' || :%s || '\"'), '')), LOWER(:%s))", col, keyPH, valPH)+ if f.Exclude {+ clauses = append(clauses, expr+" = 0")+ } else {+ clauses = append(clauses, expr+" > 0")+ }+ } else if f.Exclude {// COALESCE keeps rows that don't carry the attribute at all// (json_extract yields NULL there, and NULL != value is NULL).clauses = append(clauses,diff --git a/frontend/src/routes/logs/+page.svelte b/frontend/src/routes/logs/+page.svelteindex da4d0f0b..8d7e55c7 100644--- a/frontend/src/routes/logs/+page.svelte+++ b/frontend/src/routes/logs/+page.svelte@@ -185,14 +185,22 @@key: string;value: string;exclude: boolean;+ contains: boolean;};+ type FilterOperator = '=' | '!=' | '~=' | '!~=';++ function filterOperator(f: AttributeFilter): FilterOperator {+ if (f.contains) return f.exclude ? '!~=' : '~=';+ return f.exclude ? '!=' : '=';+ }+// Parse `resource.service.name=backend-service` or `log.method!=Enter`- // (scope.*/log.* likewise) into a structured filter. Returns null if the- // input doesn't match the shape.+ // (scope.*/log.* likewise; `~=` / `!~=` for substring match) into a+ // structured filter. Returns null if the input doesn't match the shape.function parseAttributeFilter(input: string): AttributeFilter | null {const trimmed = input.trim();- const m = trimmed.match(/^(resource|scope|log)\.([^=]+?)(!=|=)(.*)$/);+ const m = trimmed.match(/^(resource|scope|log)\.(.+?)(!~=|~=|!=|=)(.*)$/);if (!m) return null;const key = m[2].trim();if (!key) return null;@@ -200,12 +208,13 @@scope: m[1] as AttributeFilter['scope'],key,value: m[4],- exclude: m[3] === '!='+ exclude: m[3].startsWith('!'),+ contains: m[3].includes('~')};}function formatAttributeFilter(f: AttributeFilter): string {- return `${f.scope}.${f.key}${f.exclude ? '!=' : '='}${f.value}`;+ return `${f.scope}.${f.key}${filterOperator(f)}${f.value}`;}function parseLogsUrlParams() {@@ -270,15 +279,25 @@let addFilterOpen = $state(false);let dialogKey = $state('');let dialogValue = $state('');- let dialogExclude = $state(false);+ let dialogOperator = $state<FilterOperator>('=');let dialogError = $state('');// Index into attributeFilters when editing an existing pill, null when adding.let dialogEditIndex = $state<number | null>(null);+ const operatorOptions: { value: FilterOperator; symbol: string; label: string }[] = [+ { value: '=', symbol: '=', label: 'Equals' },+ { value: '!=', symbol: '≠', label: 'Not equals' },+ { value: '~=', symbol: '~', label: 'Contains' },+ { value: '!~=', symbol: '!~', label: 'Not contains' }+ ];+ const dialogOperatorOption = $derived(+ operatorOptions.find((o) => o.value === dialogOperator) ?? operatorOptions[0]+ );+function openAddFilterDialog() {dialogKey = '';dialogValue = '';- dialogExclude = false;+ dialogOperator = '=';dialogError = '';dialogEditIndex = null;addFilterOpen = true;@@ -288,14 +307,14 @@const f = attributeFilters[index];dialogKey = `${f.scope}.${f.key}`;dialogValue = f.value;- dialogExclude = f.exclude;+ dialogOperator = filterOperator(f);dialogError = '';dialogEditIndex = index;addFilterOpen = true;}function submitDialogFilter() {- const composed = `${dialogKey.trim()}${dialogExclude ? '!=' : '='}${dialogValue}`;+ const composed = `${dialogKey.trim()}${dialogOperator}${dialogValue}`;const parsed = parseAttributeFilter(composed);if (!parsed) {dialogError = 'Key must start with resource., scope., or log.';@@ -316,7 +335,8 @@f.scope === filter.scope &&f.key === filter.key &&f.value === filter.value &&- f.exclude === filter.exclude+ f.exclude === filter.exclude &&+ f.contains === filter.contains);attributeFilters = dup? attributeFilters.filter((_, i) => i !== index)@@ -330,7 +350,8 @@f.scope === filter.scope &&f.key === filter.key &&f.value === filter.value &&- f.exclude === filter.exclude+ f.exclude === filter.exclude &&+ f.contains === filter.contains);if (!dup) {attributeFilters = [...attributeFilters, filter];@@ -355,8 +376,10 @@value: string): 'none' | 'include' | 'exclude' {const { scope, key } = displayKeyToFilter(displayKey);+ // Contains filters are ignored: an attribute tile reflects (and toggles)+ // only exact-match filters for its value.const f = attributeFilters.find(- (f) => f.scope === scope && f.key === key && f.value === value+ (f) => !f.contains && f.scope === scope && f.key === key && f.value === value);if (!f) return 'none';return f.exclude ? 'exclude' : 'include';@@ -367,10 +390,10 @@function toggleAttributeFilter(displayKey: string, value: string) {const { scope, key } = displayKeyToFilter(displayKey);const existing = attributeFilters.findIndex(- (f) => f.scope === scope && f.key === key && f.value === value+ (f) => !f.contains && f.scope === scope && f.key === key && f.value === value);if (existing === -1) {- attributeFilters = [...attributeFilters, { scope, key, value, exclude: false }];+ attributeFilters = [...attributeFilters, { scope, key, value, exclude: false, contains: false }];} else {attributeFilters = attributeFilters.filter((_, i) => i !== existing);}@@ -898,7 +921,7 @@onclick={() => openEditFilterDialog(i)}><span class="text-muted-foreground">{f.scope}.{f.key}</span>- <span class={f.exclude ? 'text-red-500' : ''}>{f.exclude ? '!=' : '='}</span>+ <span class={f.exclude ? 'text-red-500' : ''}>{filterOperator(f)}</span><span>{f.value}</span></button><button@@ -935,28 +958,26 @@</label><div class="flex flex-col gap-1 text-sm"><span class="font-medium">Operator</span>- <div class="flex">- <button- type="button"- aria-pressed={!dialogExclude}- class="flex-1 rounded-l-md border px-3 py-2 text-sm transition-colors {!dialogExclude- ? 'z-10 border-primary bg-primary font-semibold text-primary-foreground'- : 'border-input text-muted-foreground hover:text-foreground'}"- onclick={() => (dialogExclude = false)}- >- <span class="font-mono">=</span> Equals- </button>- <button- type="button"- aria-pressed={dialogExclude}- class="-ml-px flex-1 rounded-r-md border px-3 py-2 text-sm transition-colors {dialogExclude- ? 'z-10 border-primary bg-primary font-semibold text-primary-foreground'- : 'border-input text-muted-foreground hover:text-foreground'}"- onclick={() => (dialogExclude = true)}- >- <span class="font-mono">≠</span> Not equals- </button>- </div>+ <Select.Root+ type="single"+ value={dialogOperator}+ onValueChange={(v) => (dialogOperator = v as FilterOperator)}+ >+ <Select.Trigger class="w-full">+ <span>+ <span class="font-mono text-muted-foreground">{dialogOperatorOption.symbol}</span>+ {dialogOperatorOption.label}+ </span>+ </Select.Trigger>+ <Select.Content>+ {#each operatorOptions as opt (opt.value)}+ <Select.Item value={opt.value} label={opt.label}>+ <span class="font-mono text-muted-foreground">{opt.symbol}</span>+ {opt.label}+ </Select.Item>+ {/each}+ </Select.Content>+ </Select.Root></div><label class="flex flex-col gap-1 text-sm"><span class="font-medium">Value</span>