Skip to content

rrezartprebreza/spring-boot-skills / skills/spring-boot-4/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.

examples/good-ai-audit-advisor.java

View on GitHub
// GOOD — GA CallAdvisor API, Micrometer metrics, async audit via separate bean, no PII in logs

@Component
@RequiredArgsConstructor
@Slf4j
public class AiAuditAdvisor implements CallAdvisor {

    private final MeterRegistry meterRegistry;
    private final AiAuditRecorder auditRecorder;   // separate bean — @Async needs the proxy

    @Override
    public int getOrder() {
        return 0;
    }

    @Override
    public String getName() {
        return "AiAuditAdvisor";
    }

    @Override
    public ChatClientResponse adviseCall(ChatClientRequest request, CallAdvisorChain chain) {
        String requestId = UUID.randomUUID().toString();
        Timer.Sample sample = Timer.start(meterRegistry);

        try {
            ChatClientResponse response = chain.nextCall(request);
            ChatResponse chatResponse = response.chatResponse();

            // Record metrics
            sample.stop(Timer.builder("ai.request.duration")
                .tag("status", "success")
                .register(meterRegistry));

            // Track token usage from response metadata
            if (chatResponse != null && chatResponse.getMetadata() != null
                    && chatResponse.getMetadata().getUsage() != null) {
                var usage = chatResponse.getMetadata().getUsage();
                meterRegistry.counter("ai.tokens.input").increment(usage.getPromptTokens());
                meterRegistry.counter("ai.tokens.output").increment(usage.getCompletionTokens()); // GA name
            }

            // Async audit — separate bean, so the @Async proxy is actually used
            auditRecorder.record(requestId, "SUCCESS", null);

            // Log without PII — only metadata
            log.info("AI request {} completed", requestId);

            return response;
        } catch (Exception e) {
            sample.stop(Timer.builder("ai.request.duration")
                .tag("status", "error")
                .register(meterRegistry));

            meterRegistry.counter("ai.request.errors").increment();
            auditRecorder.record(requestId, "ERROR", e.getMessage());

            throw e;
        }
    }
}

// Separate bean: calling an @Async method on `this` bypasses the Spring proxy and
// runs synchronously — the same self-invocation pitfall as @Transactional.
@Component
@RequiredArgsConstructor
class AiAuditRecorder {

    private final AuditLogRepository auditLogRepository;

    @Async
    public void record(String requestId, String status, String errorMessage) {
        auditLogRepository.save(new AiAuditLog(requestId, status, errorMessage, Instant.now()));
    }
}
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