Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ContentPreviewingService logs are inserted after custom logs #6132

Open
sedax90 opened this issue Feb 28, 2025 · 2 comments
Open

ContentPreviewingService logs are inserted after custom logs #6132

sedax90 opened this issue Feb 28, 2025 · 2 comments
Labels

Comments

@sedax90
Copy link

sedax90 commented Feb 28, 2025

I've added some decorators to my ServerBuilder:

        sb.decorator(LoggingService.newDecorator());

        // Add request/response preview data to logs
        sb.decorator(ContentPreviewingService.newDecorator(Integer.MAX_VALUE));

        // Add trace ID to every request/response
        sb.decorator(TraceIdDecorator::new);

        return sb.build();

With these decorators I would expect log output like this:

REQUEST LOG
MY CUSTOM LOGS...
RESPONSE LOG

but the output that i optain is:

MY CUSTOM LOGS...
REQUEST LOG
RESPONSE LOG

and request/response do not contain the traceId value that I added as a custom decorator (while all the others do).

How can I get the logs to be written in the correct order?

@ikhoon
Copy link
Contributor

ikhoon commented Mar 4, 2025

request/response do not contain the traceId value that I added as a custom decorator (while all the others do).

Did you set the trace ID using MDC? If so, I recommend using RequestScopedMdc as requests in Armeria are served asynchronously in an event loop.

How can I get the logs to be written in the correct order?

The request and response logs are written together, so it is difficult to inject add MY CUSTOM LOGS using a decorator.

logWriter.logRequest(requestLog);
} catch (Throwable t) {
logException(ctx, "request", t);
}
}
if (requestLog.responseCause() != null ||
requestLog.responseHeaders().status().isServerError() ||
!isTransientService(ctx)) {
try {
logWriter.logResponse(requestLog);

One possible workaround is implementing a custom LogWriter and composing it with the default LogWriter using .andThen().

LogWriter logWriter = LogWriter.of().andThen(new LogWriter() {
    @Override
    public void logRequest(RequestOnlyLog log) {
        logger.info("MY CUSTOM LOGS...");
    }

    @Override
    public void logResponse(RequestLog log) {
        // DO NOTHING
    }
});
LoggingService.builder()
              .logWriter(logWriter)
              .newDecorator();

@ikhoon ikhoon added the question label Mar 4, 2025
@sedax90
Copy link
Author

sedax90 commented Mar 4, 2025

thanks for your response, TraceID is added like this:

public class TraceIdDecorator extends SimpleDecoratingHttpService {

    public TraceIdDecorator(HttpService delegate) {
        super(delegate);
    }

    @Override
    public HttpResponse serve(ServiceRequestContext ctx, HttpRequest req) throws Exception {
        final String traceId = UUID.randomUUID().toString();

        // Salva il traceId nel contesto della richiesta
        ctx.setAttr(AttributeKey.valueOf("traceId"), traceId);

        // Aggiungi il traceId nei log
        MDC.put("traceId", traceId);

        // Esegui la richiesta
        HttpResponse response = unwrap().serve(ctx, req);

        // Rimuovi il traceId dal MDC alla fine della richiesta
        response.whenComplete().thenRun(() -> MDC.remove("traceId"));

        // Propaga il traceId nella response
        return response.mapHeaders(headers -> headers.toBuilder().set("X-Trace-ID", traceId).build());
    }
}

an my custom logs are not added via decorator but directly in my services, so inside my post/get/etc methods... and before the return

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants