Skip to main content
ImmutableLog logo
← Back
MicronautHttpServerFilter

Micronaut: ImmutableLog

Integrate ImmutableLog into Micronaut applications using HttpServerFilter. Zero reflection overhead, compatible with GraalVM native compilation and cloud-native deployments.

Installation

The filter uses only Micronaut core APIs. Add the dependencies below if you don't have them:

xml
<!-- pom.xml -->
<dependency>
  <groupId>io.micronaut</groupId>
  <artifactId>micronaut-http-server-netty</artifactId>
</dependency>
<dependency>
  <groupId>io.micronaut.serde</groupId>
  <artifactId>micronaut-serde-jackson</artifactId>
</dependency>

HttpServerFilter

Create the class below. The @Filter("/**") annotation registers the filter globally. Use Publishers.map() to transform the response reactively without blocking the thread.

java
import com.fasterxml.jackson.databind.ObjectMapper;
import io.micronaut.context.annotation.Value;
import io.micronaut.core.async.publisher.Publishers;
import io.micronaut.http.*;
import io.micronaut.http.annotation.Filter;
import io.micronaut.http.filter.*;
import jakarta.inject.Singleton;
import org.reactivestreams.Publisher;

import java.net.InetAddress;
import java.net.URI;
import java.net.http.*;
import java.security.MessageDigest;
import java.time.Instant;
import java.util.*;
import java.util.regex.Pattern;

@Filter("/**")
@Singleton
public class ImmutableLogFilter implements HttpServerFilter {

    @Value("${immutablelog.api-key}")
    String apiKey;

    @Value("${immutablelog.service-name:my-service}")
    String serviceName;

    @Value("${immutablelog.env:production}")
    String env;

    @Value("${immutablelog.url:https://api.immutablelog.com}")
    String apiUrl;

    private static final Set<String> SKIP_PATHS = Set.of("/health", "/ping");
    private final ObjectMapper mapper = new ObjectMapper();
    private final HttpClient http = HttpClient.newHttpClient();

    @Override
    public int getOrder() {
        return ServerFilterPhase.FIRST.order();
    }

    @Override
    public Publisher<MutableHttpResponse<?>> doFilter(
        HttpRequest<?> request,
        ServerFilterChain chain
    ) {
        String path = request.getPath();

        if (SKIP_PATHS.stream().anyMatch(path::startsWith)) {
            return chain.proceed(request);
        }

        long startedAt = System.currentTimeMillis();
        String requestId = UUID.randomUUID().toString();

        return Publishers.map(chain.proceed(request), response -> {
            long latencyMs = System.currentTimeMillis() - startedAt;
            emit(request, response, requestId, latencyMs, null);
            return response;
        });
    }

    public void emitError(HttpRequest<?> request, String requestId,
                          long latencyMs, Throwable ex) {
        emit(request, null, requestId, latencyMs, ex);
    }

    private void emit(HttpRequest<?> request, MutableHttpResponse<?> response,
                      String requestId, long latencyMs, Throwable ex) {
        try {
            String method = request.getMethodName();
            String path = request.getPath();
            int status = response != null ? response.getStatus().getCode() : 500;

            // Custom event name via request attribute
            String customEvent = request.getAttribute("imtbl.eventName", String.class).orElse(null);
            String eventName = customEvent != null
                ? customEvent
                : method.toLowerCase() + "." + path.replace("/", ".").replaceAll("^\\.+", "");

            String type = status >= 500 ? "error"
                : status >= 400 ? "error"
                : status >= 300 ? "info"
                : "success";

            // Bare IP (no port) or null; when null, client_ip is left out of payload and meta.
            String clientIp = getClientIp(request);

            Map<String, Object> payloadMap = new LinkedHashMap<>();
            payloadMap.put("method", method);
            payloadMap.put("path", path);
            payloadMap.put("status", status);
            payloadMap.put("latency_ms", latencyMs);
            if (clientIp != null) payloadMap.put("client_ip", clientIp);
            payloadMap.put("user_agent", request.getHeaders().get("User-Agent"));

            request.getBody(byte[].class).ifPresent(body -> {
                if (body.length > 0) {
                    payloadMap.put("request_body_hash", sha256Hex(body));
                }
            });

            if (ex != null) {
                Map<String, Object> errMap = new LinkedHashMap<>();
                errMap.put("type", ex.getClass().getName());
                errMap.put("message", ex.getMessage());
                errMap.put("retryable", !(ex instanceof IllegalArgumentException));
                payloadMap.put("error", errMap);
            }

            // Trilha imutavel: atributo imtbl.trail -> header X-Imtbl-Trail.
            String trail = sanitizeTrail(request.getAttribute("imtbl.trail", String.class).orElse(null));
            if (trail == null) trail = sanitizeTrail(request.getHeaders().get("X-Imtbl-Trail"));

            // Todos os valores de meta precisam ser strings (http_status inclusive).
            Map<String, String> meta = new LinkedHashMap<>();
            meta.put("type", type);
            meta.put("event_name", eventName);
            meta.put("service", serviceName);
            meta.put("request_id", requestId);
            // "environment" e o nome canonico; "env" e mantido por compatibilidade.
            meta.put("env", env);
            meta.put("environment", env);
            meta.put("http_method", method);
            meta.put("http_status", String.valueOf(status));
            // Rota concreta; se o framework expuser o template (ex: /users/{id}), prefira-o.
            meta.put("http_route", path);
            meta.put("log_level", type.equals("error") ? "error" : "info");
            if (trail != null) meta.put("immutable_trail", trail);

            // client_ip no meta (o SIEM so promove client_ip a source.ip): IP puro, sem porta.
            // So confie no X-Forwarded-For atras de proxy/ALB confiavel.
            if (clientIp != null) meta.put("client_ip", clientIp);
            if (ex != null) {
                meta.put("error_type", ex.getClass().getName());
                if (ex.getMessage() != null && !ex.getMessage().isBlank()) {
                    String m = ex.getMessage();
                    meta.put("error_message", m.length() > 500 ? m.substring(0, 500) : m);
                }
            }

            Map<String, Object> bodyMap = Map.of(
                "payload", mapper.writeValueAsString(payloadMap),
                "meta", meta
            );

            String json = mapper.writeValueAsString(bodyMap);

            var builder = HttpRequest.newBuilder()
                .uri(URI.create(apiUrl + "/v1/events"))
                .header("Content-Type", "application/json")
                .header("Authorization", "Bearer " + apiKey)
                // Idempotency-Key e OBRIGATORIO (sem ele a API responde 400).
                .header("Idempotency-Key", requestId)
                .header("Request-Id", requestId)
                .header("X-Client-TZ", "America/Sao_Paulo")
                .header("X-Client-Offset-Minutes", "-180");
            if (trail != null) builder.header("X-Imtbl-Trail", trail);

            var httpReq = builder
                .POST(HttpRequest.BodyPublishers.ofString(json))
                .build();

            http.sendAsync(httpReq, HttpResponse.BodyHandlers.discarding());

        } catch (Exception ignored) {}
    }

    /**
     * Client IP for client_ip: the first X-Forwarded-For entry, else X-Real-IP, else the IP of
     * the connection (getRemoteAddress()), taking the first one that is a valid IP. Returns null
     * when none is. X-Forwarded-For and X-Real-IP are only trustworthy behind a proxy/ALB that
     * overwrites them: without one, any caller can forge them.
     */
    private String getClientIp(HttpRequest<?> request) {
        String xff = request.getHeaders().get("X-Forwarded-For");
        if (xff != null) {
            String ip = bareIp(xff.split(",")[0]);
            if (ip != null) return ip;
        }
        String xri = bareIp(request.getHeaders().get("X-Real-IP"));
        if (xri != null) return xri;
        // Without a resolved remote address (null or unresolved InetSocketAddress) there is
        // no IP to send: return null instead of throwing, otherwise the whole event is lost.
        var remote = request.getRemoteAddress();
        if (remote == null || remote.getAddress() == null) return null;
        return bareIp(remote.getAddress().getHostAddress());
    }

    private static final Pattern IPV4 = Pattern.compile(
        "(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)(\\.(25[0-5]|2[0-4]\\d|1\\d\\d|[1-9]?\\d)){3}");
    private static final Pattern IPV6_CHARS = Pattern.compile("[0-9A-Fa-f:.]+");

    /**
     * Trims v, strips a port ("10.0.0.1:8080", "[2001:db8::1]:8080") or brackets, and returns
     * the bare IP, or null if it is not one. Never resolves a host name: IPv4 is matched by
     * pattern, and InetAddress only sees strings with ':' made of hex digits, which it parses
     * as an IPv6 literal (or rejects) without DNS.
     */
    static String bareIp(String v) {
        if (v == null) return null;
        v = v.trim();
        if (v.startsWith("[")) {
            int end = v.indexOf(']');
            String rest = end < 0 ? null : v.substring(end + 1);
            if (rest == null || !(rest.isEmpty() || rest.matches(":\\d+"))) return null;
            v = v.substring(1, end);
        } else if (v.indexOf(':') > 0 && v.indexOf(':') == v.lastIndexOf(':')) {
            v = v.substring(0, v.indexOf(':'));
        }
        if (IPV4.matcher(v).matches()) return v;
        if (v.indexOf(':') < 0 || !IPV6_CHARS.matcher(v).matches()) return null;
        try {
            InetAddress.getByName(v);
            return v;
        } catch (Exception e) {
            return null;
        }
    }

    // Normaliza o immutable_trail (trim, max 256, sem ':').
    private String sanitizeTrail(String v) {
        if (v == null) return null;
        v = v.trim();
        if (v.isEmpty()) return null;
        v = v.replace(":", "-");
        if (v.length() > 256) v = v.substring(0, 256);
        return v;
    }

    private String sha256Hex(byte[] data) {
        try {
            var digest = MessageDigest.getInstance("SHA-256");
            byte[] hash = digest.digest(data);
            StringBuilder sb = new StringBuilder();
            for (byte b : hash) sb.append(String.format("%02x", b));
            return sb.toString();
        } catch (Exception e) {
            return "hash-error";
        }
    }
}

Configuration

Add the properties to application.yml:

yaml
# src/main/resources/application.yml
immutablelog:
  api-key: ${IMTBL_API_KEY}
  service-name: ${IMTBL_SERVICE_NAME:my-service}
  env: ${IMTBL_ENV:production}
  url: https://api.immutablelog.com

How it works

1. ServerFilterPhase.FIRST

getOrder() returns the FIRST phase to ensure the filter runs before any other application filter, capturing even requests that fail in subsequent filters.

2. Reactive Publishers.map()

Publishers.map() is used to intercept the response without blocking. Micronaut uses Netty internally. Never block the event loop I/O thread.

3. request.getAttribute()

Request attributes are thread-safe and propagated by the reactive context. Use them to pass the custom event name from the controller to the filter.

4. HttpClient.sendAsync (Java 11+)

Sending to ImmutableLog is fire-and-forget via HttpClient.sendAsync(). Never blocks the Netty thread. The client response was already sent before the POST.

ExceptionHandler

To capture unhandled exceptions, implement ExceptionHandler. It is called by Micronaut before returning the error response, allowing you to enrich the event with exception details.

java
import io.micronaut.context.annotation.Requires;
import io.micronaut.http.*;
import io.micronaut.http.annotation.Produces;
import io.micronaut.http.server.exceptions.*;
import jakarta.inject.Singleton;

/**
 * ExceptionHandler to capture errors and include them in the audit event.
 * Register alongside the filter to capture unhandled exceptions.
 */
@Produces
@Singleton
@Requires(classes = {Exception.class, ExceptionHandler.class})
public class ImmutableLogExceptionHandler
    implements ExceptionHandler<Exception, HttpResponse<?>> {

    private final ImmutableLogFilter filter;

    public ImmutableLogExceptionHandler(ImmutableLogFilter filter) {
        this.filter = filter;
    }

    @Override
    public HttpResponse<?> handle(HttpRequest request, Exception ex) {
        String requestId = request.getAttribute("imtbl.requestId", String.class)
            .orElse(UUID.randomUUID().toString());
        long startedAt = request.getAttribute("imtbl.startedAt", Long.class)
            .orElse(System.currentTimeMillis());
        long latencyMs = System.currentTimeMillis() - startedAt;

        filter.emitError(request, requestId, latencyMs, ex);

        return HttpResponse.serverError(Map.of("error", ex.getMessage()));
    }
}

Custom event

Inject HttpRequest into the controller and set the imtbl.eventName attribute to override the name auto-generated by the filter.

java
import io.micronaut.http.HttpRequest;
import io.micronaut.http.annotation.*;

@Controller("/payments")
public class PaymentController {

    @Post
    public Payment create(HttpRequest<?> request, @Body PaymentDto dto) {
        // Override the auto-generated event name
        request.setAttribute("imtbl.eventName", "payment.created");

        // ... process payment ...
        return payment;
    }
}

Immutable trail (immutable_trail)

A trail groups related events into a single auditable timeline. Set the imtbl.trail request attribute, or propagate it across services with the X-Imtbl-Trail header.

java
@Post("/flows/{id}/run")
public HttpResponse<?> runFlow(String id, HttpRequest<?> request) {
    request.setAttribute("imtbl.trail", "flow-" + id);
    // ... business logic ...
    return HttpResponse.ok(Map.of("status", "ok"));
}

The trail cannot be empty, exceed 256 characters, or contain ":", violations return 400 invalid_immutable_trail. sanitizeTrail fixes the value before sending.

Reactive filter (Reactor)

Alternative using Reactor (Mono/Flux) for more expressive reactive composition. Useful when your stack uses Micronaut + Reactor instead of RxJava.

java
import io.micronaut.http.annotation.Filter;
import io.micronaut.http.filter.*;
import reactor.core.publisher.Mono;

/**
 * Alternative: Reactive filter using Reactor for non-blocking chains.
 * Use when your handlers are reactive (Mono/Flux from Reactor).
 */
@Filter("/**")
public class ImmutableLogReactiveFilter implements HttpServerFilter {

    @Override
    public Publisher<MutableHttpResponse<?>> doFilter(
        HttpRequest<?> request, ServerFilterChain chain
    ) {
        long startedAt = System.currentTimeMillis();
        String requestId = UUID.randomUUID().toString();

        return Mono.from(chain.proceed(request))
            .doOnSuccess(response -> {
                long latencyMs = System.currentTimeMillis() - startedAt;
                emitAsync(request, response, requestId, latencyMs, null);
            })
            .doOnError(ex -> {
                long latencyMs = System.currentTimeMillis() - startedAt;
                emitAsync(request, null, requestId, latencyMs, ex);
            });
    }

    private void emitAsync(/* ... */) {
        // same emit logic as above
    }
}

Native Image (GraalVM)

Micronaut processes annotations at compile time: no runtime reflection. The filter is fully compatible with native compilation. If needed, add manual hints:

json
// src/main/resources/META-INF/native-image/reflect-config.json
// For GraalVM native image compilation, add reflection hints if needed:
[
  {
    "name": "com.example.ImmutableLogFilter",
    "allPublicConstructors": true,
    "allPublicMethods": true
  }
]

// With Micronaut's @Singleton + @Filter, AOT processing handles most cases automatically.
// Run: ./mvnw package -Dpackaging=native-image

In most cases Micronaut AOT generates the hints automatically for classes with @Singleton and @Filter. Test with: mvn package -Dpackaging=native-image

This documentation reflects the current integration behavior. For questions or advanced integrations, contact the support team.

Back to Java

meta fields

Beyond the payload, the meta object carries the fields the SIEM uses to normalize (ECS) and enrich the event. All meta values are strings.

Field in metaWhat it does in the SIEM
event_nameThe event label (e.g. http.GET.auth-me-user, Payment error), the identifier shown in the listing. Becomes event.action and derives event.category (e.g. a name with login → authentication).
client_ipBecomes source.ip with maximum precedence and provenance client_asserted. It is the only meta field that becomes source.ip. It must be a plain IP, without a port. See the section below.
immutable_trailBecomes imtbl.immutable_trail, which groups related events for investigation (e.g. one order, one user).
typeEvent severity: error / warning / info / success. Colors the badge in the listing. (event_type is accepted as a synonym.)
http_method, http_status, http_route, log_level, environment, error_type, error_messageSent by the middlewares, always as a string (including http_status, e.g. "200"). error_type and error_message only exist when there is an error.
service, env, request_idMetadata: indexed and queryable in the dashboard (search/filters).

client_ip: end-user IP

The problem it solves

Between the browser and the core there are proxies/ALB. The IP the core sees on the connection is the previous hop (the client's backend or an AWS proxy), not the user's. Without action, source.ip would be the proxy IP, useless for geo, threat and IP-based detection. (Real example: XFF arrived as 44.192.13.3 (AWS) while the user was 179.110.4.205.)

The solution

The client's backend is the only one that sees the browser's real IP, so it forwards that IP in meta.client_ip when calling POST /v1/events.

How the SIEM handles it

  • Validates it is an IP (v4/v6). A non-IP value is ignored, without dropping the event. An IP with a port (e.g. 1.2.3.4:5678) is also silently discarded.
  • Writes to source.ip with maximum precedence (see order below).
  • Stamps imtbl.source_ip_origin = "client_asserted"; the UI shows the “User IP” badge.
  • Feeds GeoIP, threat intel and IP-based detection rules.
source.ip precedence:client_ipX-Forwarded-For (1º hop)X-Real-IPconnection IP

Provenance and trust

client_ip is asserted by the tenant: the client's backend claims the IP, and it can be forged by whoever controls that backend. However it stays contained within the tenant (it never crosses another tenant's boundary). A threat/geo hit on a client_asserted value is a claim, not an edge observation.

How to obtain the IP in your backend

Try the candidates in order: the first IP from X-Forwarded-For (closest to the user), then X-Real-IP (if your proxy sets it) and finally the connection IP. Each candidate goes through a function that strips the port (1.2.3.4:5678, [2001:db8::1]:8080) and the brackets and checks that it is a real IP, without any DNS lookup. An invalid value (garbage, localhost, an IPv6 zone such as fe80::1%eth0) is ignored and the next candidate is tried. Send a plain IP, without a port, and if no candidate is a valid IP omit the client_ip key (meta does not accept null, and never send “unknown”).

Only behind a trusted proxy: reading the first IP from X-Forwarded-For is only safe when a trusted proxy or ALB sits in front of the application and overwrites or appends the header. Without a proxy, the client controls the header and can forge the IP, and the SIEM will promote that value to source.ip.

The getClientIp (clientIP, get_client_ip) code of this page's middleware implements this rule, in the candidate order it shows.

Payload example

json
{
  "payload": "{\"user\":\"bob\",\"action\":\"login\"}",
  "meta": {
    "event_name": "user.login",
    "client_ip": "179.110.4.205",
    "immutable_trail": "user-bob"
  }
}

PII note: client_ip is PII and gets sealed into the immutable block (permanent). Decide consciously between forensic evidence and “right to be forgotten” before sending it.