|
|
@@ -15,8 +15,10 @@ import org.springframework.stereotype.Service;
|
|
|
import java.time.LocalDateTime;
|
|
|
import java.time.format.DateTimeFormatter;
|
|
|
import java.util.HashMap;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
+import java.util.Set;
|
|
|
import java.util.stream.Collectors;
|
|
|
|
|
|
@Service
|
|
|
@@ -26,6 +28,10 @@ public class LogSearchService {
|
|
|
|
|
|
private final LogEntryRepository logEntryRepository;
|
|
|
private static final Map<String, String> SORT_FIELD_MAPPING = createSortFieldMapping();
|
|
|
+ private static final Set<String> ALLOWED_SORT_PROPERTIES = Set.of(
|
|
|
+ "timestamp", "level", "serviceName", "traceId", "spanId", "bizName",
|
|
|
+ "className", "methodName", "thread", "application", "rawMessage"
|
|
|
+ );
|
|
|
|
|
|
public LogSearchResponse searchLogs(LogSearchRequest request) {
|
|
|
log.info("Searching logs with criteria: {}", request);
|
|
|
@@ -95,7 +101,10 @@ public class LogSearchService {
|
|
|
Sort.Direction.ASC : Sort.Direction.DESC;
|
|
|
|
|
|
String requestedSortBy = request.getSortBy() == null ? "timestamp" : request.getSortBy();
|
|
|
- String actualSortField = SORT_FIELD_MAPPING.getOrDefault(requestedSortBy, requestedSortBy);
|
|
|
+ String normalizedSortProperty = SORT_FIELD_MAPPING.getOrDefault(requestedSortBy, requestedSortBy);
|
|
|
+ String actualSortField = ALLOWED_SORT_PROPERTIES.contains(normalizedSortProperty)
|
|
|
+ ? normalizedSortProperty
|
|
|
+ : "timestamp";
|
|
|
|
|
|
return PageRequest.of(request.getPage(), request.getSize(),
|
|
|
Sort.by(direction, actualSortField));
|
|
|
@@ -103,12 +112,19 @@ public class LogSearchService {
|
|
|
|
|
|
private static Map<String, String> createSortFieldMapping() {
|
|
|
Map<String, String> mapping = new HashMap<>();
|
|
|
- mapping.put("timestamp", "@timestamp");
|
|
|
- mapping.put("level", "log_level");
|
|
|
- mapping.put("serviceName", "service_name");
|
|
|
- mapping.put("traceId", "trace_id");
|
|
|
- mapping.put("spanId", "span_id");
|
|
|
+ // Accept external ES field names and normalize them to entity property names.
|
|
|
+ mapping.put("@timestamp", "timestamp");
|
|
|
+ mapping.put("log_level", "level");
|
|
|
+ mapping.put("service_name", "serviceName");
|
|
|
+ mapping.put("trace_id", "traceId");
|
|
|
+ mapping.put("span_id", "spanId");
|
|
|
+ mapping.put("biz_name", "bizName");
|
|
|
mapping.put("bizName", "bizName");
|
|
|
+ mapping.put("timestamp", "timestamp");
|
|
|
+ mapping.put("level", "level");
|
|
|
+ mapping.put("serviceName", "serviceName");
|
|
|
+ mapping.put("traceId", "traceId");
|
|
|
+ mapping.put("spanId", "spanId");
|
|
|
return mapping;
|
|
|
}
|
|
|
|