Skip to content

rrezartprebreza/spring-boot-skills / skills/spring-boot-3/ai-observability

ai-observability

Verified

Use 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.

templates/AiAuditAdvisor.java

View on GitHub
package 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 1.0 GA.
 * Tracks latency, token usage, and errors without logging PII.
 *
 * GA API notes:
 * - CallAroundAdvisor / AdvisedRequest / AdvisedResponse were removed at 1.0 GA.
 *   The interface is CallAdvisor with adviseCall(ChatClientRequest, CallAdvisorChain).
 * - Usage.getGenerationTokens() was renamed to getCompletionTokens().
 */
@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());
        }
    }
}
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: 472kUpdated Claude CodeCodexVerifiedInstall
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: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
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 availableStars: 322kUpdated Claude CodeCodexVerifiedInstall