rrezartprebreza/spring-boot-skills / skills/spring-boot-4/ai-observability
ai-observability
VerifiedUse when adding Spring AI-specific model observations, token usage, latency, externally configured cost attribution, advisor telemetry, or protected prompt and completion logging. Use production-observability for general service metrics, health, logs, and OTLP setup.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 7.2 KB |
| agents/openai.yaml | config | 200 B |
| examples/bad-ai-audit-advisor.java | other | 2.1 KB |
| examples/good-ai-audit-advisor.java | other | 2.6 KB |
| templates/AiAuditAdvisor.java | other | 4.9 KB |
| templates/AiMetrics.java | other | 2.3 KB |
templates/AiAuditAdvisor.java
View on GitHubpackage com.example.ai.observability;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClientRequest;
import org.springframework.ai.chat.client.ChatClientResponse;
import org.springframework.ai.chat.client.advisor.api.CallAdvisor;
import org.springframework.ai.chat.client.advisor.api.CallAdvisorChain;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.ChatOptions;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import java.time.Instant;
import java.util.UUID;
/**
* Production audit advisor for Spring AI 2.0.
* Tracks latency, token usage, and errors without logging PII.
*
* API notes:
* - CallAroundAdvisor / AdvisedRequest / AdvisedResponse are pre-GA APIs.
* The current interface is CallAdvisor with adviseCall(ChatClientRequest, CallAdvisorChain).
* - Use Usage.getCompletionTokens(); do not call the old getGenerationTokens().
*/
@Slf4j
@Component
@RequiredArgsConstructor
public class AiAuditAdvisor implements CallAdvisor {
private final AiMetrics metrics;
private final AiAuditRecorder auditRecorder; // separate bean — see note on @Async below
@Override
public int getOrder() {
return 0; // runs first in advisor chain
}
@Override
public String getName() {
return "AiAuditAdvisor";
}
@Override
public ChatClientResponse adviseCall(ChatClientRequest request, CallAdvisorChain chain) {
String requestId = UUID.randomUUID().toString();
Instant startTime = Instant.now();
String requestedModel = requestedModel(request);
try {
ChatClientResponse response = metrics.track("chat", requestedModel, () -> chain.nextCall(request));
ChatResponse chatResponse = response.chatResponse();
// Extract token usage if available
if (chatResponse != null && chatResponse.getMetadata() != null
&& chatResponse.getMetadata().getUsage() != null) {
var usage = chatResponse.getMetadata().getUsage();
long inputTokens = usage.getPromptTokens();
long outputTokens = usage.getCompletionTokens();
// The response reports the model that actually served the call.
String servedModel = chatResponse.getMetadata().getModel();
metrics.recordTokens(servedModel != null ? servedModel : requestedModel, inputTokens, outputTokens);
// Log metadata only — never log prompt content (PII risk)
log.info("AI request completed: requestId={}, inputTokens={}, outputTokens={}",
requestId, inputTokens, outputTokens);
}
// Persist audit asynchronously — don't block the response
recordBestEffort(requestId, "SUCCESS", null, startTime);
return response;
} catch (Exception e) {
log.error("AI request failed: requestId={}, failureType={}", requestId, e.getClass().getSimpleName());
recordBestEffort(requestId, "ERROR", e.getClass().getSimpleName(), startTime);
throw e;
}
}
private void recordBestEffort(String requestId, String status, String errorType, Instant startTime) {
try {
auditRecorder.record(requestId, status, errorType, startTime);
} catch (RuntimeException failure) {
// Submission can fail before the @Async method body runs.
log.warn("AI audit submission failed: requestId={}, failureType={}",
requestId, failure.getClass().getSimpleName());
}
}
private static String requestedModel(ChatClientRequest request) {
ChatOptions options = request.prompt().getOptions();
return options != null && options.getModel() != null ? options.getModel() : "default";
}
}
/**
* Async persistence lives in its OWN bean on purpose: an @Async method invoked on
* `this` bypasses the Spring proxy and runs synchronously (self-invocation pitfall,
* same as @Transactional). A separate component goes through the proxy.
*/
@Component
@RequiredArgsConstructor
class AiAuditRecorder {
private final AiAuditLogRepository auditRepository;
@Async
public void record(String requestId, String status, String errorMessage, Instant startTime) {
try {
auditRepository.save(new AiAuditLog(
UUID.fromString(requestId),
status,
errorMessage,
startTime,
Instant.now()
));
} catch (Exception e) {
// Never let audit failures break the AI call path
org.slf4j.LoggerFactory.getLogger(AiAuditRecorder.class)
.warn("Failed to persist AI audit log: requestId={}, failureType={}",
requestId, e.getClass().getSimpleName());
}
}
}
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis. | Stars: 472k | Updated | Claude CodeCodex | Verified | Install |
| diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness. | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependencies | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is available | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |