fix: add proper logging to log ingestion endpoint
Previously the endpoint silently returned 202 for all failures: missing agent identity, unregistered agents, empty payloads, and buffer-full drops. Now logs WARN for each failure case with context (instanceId, entry count, reason). Normal ingestion logged at INFO with accepted count. Buffer-full drops tracked individually with accepted/dropped counts. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -45,32 +45,57 @@ public class LogIngestionController {
|
||||
@ApiResponse(responseCode = "202", description = "Logs accepted for indexing")
|
||||
public ResponseEntity<Void> ingestLogs(@RequestBody List<LogEntry> entries) {
|
||||
String instanceId = extractAgentId();
|
||||
String applicationId = resolveApplicationId(instanceId);
|
||||
if (instanceId == null || instanceId.isBlank()) {
|
||||
log.warn("Log ingestion rejected: no agent identity in request (unauthenticated or missing principal)");
|
||||
return ResponseEntity.accepted().build();
|
||||
}
|
||||
|
||||
if (entries != null && !entries.isEmpty()) {
|
||||
log.debug("Received {} log entries from instance={}, app={}", entries.size(), instanceId, applicationId);
|
||||
String environment = resolveEnvironment(instanceId);
|
||||
for (var entry : entries) {
|
||||
logBuffer.offerOrWarn(new BufferedLogEntry(
|
||||
tenantProperties.getId(), environment, instanceId, applicationId, entry));
|
||||
if (entries == null || entries.isEmpty()) {
|
||||
log.warn("Log ingestion from instance={}: empty or null payload", instanceId);
|
||||
return ResponseEntity.accepted().build();
|
||||
}
|
||||
|
||||
AgentInfo agent = registryService.findById(instanceId);
|
||||
if (agent == null) {
|
||||
log.warn("Log ingestion from instance={}: agent not found in registry (not registered or expired). {} entries dropped.",
|
||||
instanceId, entries.size());
|
||||
return ResponseEntity.accepted().build();
|
||||
}
|
||||
|
||||
String applicationId = agent.applicationId();
|
||||
String environment = agent.environmentId() != null ? agent.environmentId() : "default";
|
||||
|
||||
if (applicationId == null || applicationId.isBlank()) {
|
||||
log.warn("Log ingestion from instance={}: agent has no applicationId. {} entries dropped.", instanceId, entries.size());
|
||||
return ResponseEntity.accepted().build();
|
||||
}
|
||||
|
||||
log.debug("Ingesting {} log entries from instance={}, app={}, env={}", entries.size(), instanceId, applicationId, environment);
|
||||
|
||||
int accepted = 0;
|
||||
int dropped = 0;
|
||||
for (var entry : entries) {
|
||||
boolean offered = logBuffer.offer(new BufferedLogEntry(
|
||||
tenantProperties.getId(), environment, instanceId, applicationId, entry));
|
||||
if (offered) {
|
||||
accepted++;
|
||||
} else {
|
||||
dropped++;
|
||||
}
|
||||
}
|
||||
|
||||
if (dropped > 0) {
|
||||
log.warn("Log buffer full: accepted={}, dropped={} from instance={}, app={}",
|
||||
accepted, dropped, instanceId, applicationId);
|
||||
} else {
|
||||
log.info("Accepted {} log entries from instance={}, app={}", accepted, instanceId, applicationId);
|
||||
}
|
||||
|
||||
return ResponseEntity.accepted().build();
|
||||
}
|
||||
|
||||
private String extractAgentId() {
|
||||
Authentication auth = SecurityContextHolder.getContext().getAuthentication();
|
||||
return auth != null ? auth.getName() : "";
|
||||
}
|
||||
|
||||
private String resolveApplicationId(String instanceId) {
|
||||
AgentInfo agent = registryService.findById(instanceId);
|
||||
return agent != null ? agent.applicationId() : "";
|
||||
}
|
||||
|
||||
private String resolveEnvironment(String instanceId) {
|
||||
AgentInfo agent = registryService.findById(instanceId);
|
||||
return agent != null && agent.environmentId() != null ? agent.environmentId() : "default";
|
||||
return auth != null ? auth.getName() : null;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user