feat(logs): widen source filter to multi-value OR list
Replaces LogSearchRequest.source (String) with sources (List<String>) and emits 'source IN (...)' when non-empty. LogQueryController parses ?source=a,b,c the same way it parses ?level=a,b,c.
This commit is contained in:
@@ -61,12 +61,20 @@ public class LogQueryController {
|
||||
.toList();
|
||||
}
|
||||
|
||||
List<String> sources = List.of();
|
||||
if (source != null && !source.isEmpty()) {
|
||||
sources = Arrays.stream(source.split(","))
|
||||
.map(String::trim)
|
||||
.filter(s -> !s.isEmpty())
|
||||
.toList();
|
||||
}
|
||||
|
||||
Instant fromInstant = from != null ? Instant.parse(from) : null;
|
||||
Instant toInstant = to != null ? Instant.parse(to) : null;
|
||||
|
||||
LogSearchRequest request = new LogSearchRequest(
|
||||
searchText, levels, application, instanceId, exchangeId,
|
||||
logger, env.slug(), source, fromInstant, toInstant, cursor, limit, sort);
|
||||
logger, env.slug(), sources, fromInstant, toInstant, cursor, limit, sort);
|
||||
|
||||
LogSearchResponse result = logIndex.search(request);
|
||||
|
||||
|
||||
@@ -146,9 +146,12 @@ public class ClickHouseLogStore implements LogIndex {
|
||||
baseParams.add("%" + escapeLike(request.logger()) + "%");
|
||||
}
|
||||
|
||||
if (request.source() != null && !request.source().isEmpty()) {
|
||||
baseConditions.add("source = ?");
|
||||
baseParams.add(request.source());
|
||||
if (request.sources() != null && !request.sources().isEmpty()) {
|
||||
String placeholders = String.join(", ", Collections.nCopies(request.sources().size(), "?"));
|
||||
baseConditions.add("source IN (" + placeholders + ")");
|
||||
for (String s : request.sources()) {
|
||||
baseParams.add(s);
|
||||
}
|
||||
}
|
||||
|
||||
if (request.from() != null) {
|
||||
|
||||
@@ -323,4 +323,52 @@ class ClickHouseLogStoreIT {
|
||||
String.class);
|
||||
assertThat(customVal).isEqualTo("custom-value");
|
||||
}
|
||||
|
||||
@Test
|
||||
void search_bySources_singleValue_filtersCorrectly() {
|
||||
Instant now = Instant.parse("2026-03-31T12:00:00Z");
|
||||
// "source" column is populated by indexBatch via LogEntry.getSource(); default is "app" when null.
|
||||
// Force one row to "container" via a direct insert to avoid coupling to LogEntry constructor.
|
||||
store.indexBatch("agent-1", "my-app", List.of(
|
||||
entry(now, "INFO", "logger", "app msg", "t1", null, null)
|
||||
));
|
||||
jdbc.update("INSERT INTO logs (tenant_id, environment, timestamp, application, instance_id, level, " +
|
||||
"logger_name, message, thread_name, stack_trace, exchange_id, mdc, source) VALUES " +
|
||||
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
"default", "default", java.sql.Timestamp.from(now.plusSeconds(1)), "my-app", "agent-1",
|
||||
"INFO", "logger", "container msg", "t1", "", "", java.util.Map.of(), "container");
|
||||
|
||||
LogSearchResponse result = store.search(new LogSearchRequest(
|
||||
null, null, "my-app", null, null, null, null,
|
||||
List.of("container"), null, null, null, 100, "desc"));
|
||||
|
||||
assertThat(result.data()).hasSize(1);
|
||||
assertThat(result.data().get(0).message()).isEqualTo("container msg");
|
||||
}
|
||||
|
||||
@Test
|
||||
void search_bySources_multiValue_joinsAsOr() {
|
||||
Instant now = Instant.parse("2026-03-31T12:00:00Z");
|
||||
store.indexBatch("agent-1", "my-app", List.of(
|
||||
entry(now, "INFO", "logger", "app msg", "t1", null, null)
|
||||
));
|
||||
jdbc.update("INSERT INTO logs (tenant_id, environment, timestamp, application, instance_id, level, " +
|
||||
"logger_name, message, thread_name, stack_trace, exchange_id, mdc, source) VALUES " +
|
||||
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
"default", "default", java.sql.Timestamp.from(now.plusSeconds(1)), "my-app", "agent-1",
|
||||
"INFO", "logger", "container msg", "t1", "", "", java.util.Map.of(), "container");
|
||||
jdbc.update("INSERT INTO logs (tenant_id, environment, timestamp, application, instance_id, level, " +
|
||||
"logger_name, message, thread_name, stack_trace, exchange_id, mdc, source) VALUES " +
|
||||
"(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
"default", "default", java.sql.Timestamp.from(now.plusSeconds(2)), "my-app", "agent-1",
|
||||
"INFO", "logger", "agent msg", "t1", "", "", java.util.Map.of(), "agent");
|
||||
|
||||
LogSearchResponse result = store.search(new LogSearchRequest(
|
||||
null, null, "my-app", null, null, null, null,
|
||||
List.of("app", "container"), null, null, null, 100, "desc"));
|
||||
|
||||
assertThat(result.data()).hasSize(2);
|
||||
assertThat(result.data()).extracting(LogEntryResult::message)
|
||||
.containsExactlyInAnyOrder("app msg", "container msg");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user