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.

templates/AiMetrics.java

View on GitHub
package com.example.ai.observability;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.function.Supplier;

/**
 * Centralized AI metrics using Micrometer.
 * Records latency, token usage, errors, and cost estimates.
 */
@Component
@RequiredArgsConstructor
public class AiMetrics {

    private final MeterRegistry registry;

    /**
     * Time an AI operation and record success/failure.
     */
    public <T> T track(String operation, String model, Supplier<T> action) {
        Timer.Sample sample = Timer.start(registry);
        try {
            T result = action.get();
            sample.stop(timer(operation, model, "success"));
            return result;
        } catch (Exception e) {
            sample.stop(timer(operation, model, "error"));
            errorCounter(operation, model).increment();
            throw e;
        }
    }

    /**
     * Record token usage from a completed AI response.
     */
    public void recordTokens(String model, long inputTokens, long outputTokens) {
        counter("ai.tokens", "model", model, "type", "input").increment(inputTokens);
        counter("ai.tokens", "model", model, "type", "output").increment(outputTokens);
        counter("ai.tokens.total", "model", model).increment(inputTokens + outputTokens);
    }

    /**
     * Record estimated cost (pass rate from configuration, not hardcoded).
     */
    public void recordCost(String model, double estimatedCost) {
        registry.counter("ai.cost.estimated", "model", model).increment(estimatedCost);
    }

    private Timer timer(String operation, String model, String status) {
        return Timer.builder("ai.request.duration")
            .tag("operation", operation)
            .tag("model", model)
            .tag("status", status)
            .description("AI request latency")
            .register(registry);
    }

    private Counter errorCounter(String operation, String model) {
        return counter("ai.request.errors", "operation", operation, "model", model);
    }

    private Counter counter(String name, String... tags) {
        return Counter.builder(name).tags(tags).register(registry);
    }
}
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