forge-kit

forge-metrics

Overview

forge-metrics provides consistent, production-ready metrics and observability primitives for backend services.

It standardises how services emit metrics and correlation data, enabling reliable monitoring and troubleshooting.


Key Features


Design Principles


Typical Use Cases


Usage

forge-metrics provides the @ServiceMetrics annotation for automatic metrics collection. To use it, you need to:

  1. Implement a MetricsRecorder - Create a class that implements MetricsRecorder to define how metrics are recorded
  2. Annotate methods - Use @ServiceMetrics(YourRecorder.class) on methods you want to instrument
  3. Return MetricsResultIndicator - Methods should return types that implement MetricsResultIndicator for automatic success/failure detection

Basic Usage

// 1. Implement a MetricsRecorder
@ApplicationScoped
public class AuthMetricsRecorder implements MetricsRecorder
{
    @Inject
    MeterRegistry meterRegistry;

    @Override
    public void recordMetrics(InvocationContext context, MetricsResultIndicator indicator)
    {
        String operation = context.getMethod().getName();
        String status = indicator.success() ? "success" : "failure";

        Counter.builder("auth.operations")
            .tag("operation", operation)
            .tag("status", status)
            .register(meterRegistry)
            .increment();
    }

    @Override
    public void recordException(InvocationContext context, Exception exception)
    {
        // Record exception metrics
    }
}

// 2. Use @ServiceMetrics annotation
@ServiceMetrics(AuthMetricsRecorder.class)
public AuthResponse authenticate(String username, String password)
{
    // Your logic here
    return new AuthResponse(success, token, errorMessage);
}

// 3. Return type implements MetricsResultIndicator
public record AuthResponse(boolean success, String token, String errorMessage)
    implements MetricsResultIndicator
{
}

The interceptor automatically:


Examples

See: examples/forge-metrics for code examples.

See the reference implementation:

This example demonstrates: