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

Micronaut 4 integration based on @ClientFilter & @ServerFilter #1677

Closed
s-af opened this issue Nov 15, 2023 · 2 comments
Closed

Micronaut 4 integration based on @ClientFilter & @ServerFilter #1677

s-af opened this issue Nov 15, 2023 · 2 comments

Comments

@s-af
Copy link

s-af commented Nov 15, 2023

Detailed Description

I am currently trying to integrate Logbook with my Micronaut project. In my first attempt I was following this guide. This approach was working fine until I tried to retrieve @RequestScope bean value for conditional logging. Micronut currently doesn't provide options how to get request context inside Netty channel handlers (see discussion).

To avoid this issue I would suggest to implement Micronaut integration module based on @ClientFilter & @ServerFilter.

Possible Implementation

There is an initial idea which I was playing around for @ClientFilter:

@RequestFilter
Publisher<HttpResponse<?>> filter(
        HttpRequest<?> request,
        FilterContinuation<Publisher<HttpResponse<?>>> continuation
) {
    return Mono.zip(
            Mono.from(continuation.proceed()),
            Mono.defer(() -> {
                try {
                    return Mono.just(
                            Optional.of(
                                    logbook.process(LogbookRequest.clientRequest(request))
                                            .write()
                            )
                    );
                } catch (IOException e) {
                    return Mono.just(Optional.<Logbook.ResponseProcessingStage>empty());
                }
            })
    ).map(tuple -> {
            HttpResponse<?> response = tuple.getT1();
            tuple.getT2().ifPresent(stage -> {
                try {
                    stage.process(LogbookResponse.clientResponse(response))
                            .write();
                } catch (IOException e) { }
            });
            return response;
        }
    );
}

Similar for @ServerFilter:

private record LogbookServerFilterContext(Logbook.ResponseProcessingStage stage)
        implements PropagatedContextElement {}

@RequestFilter
void filter(
        HttpRequest<?> request,
        MutablePropagatedContext context
) {       
    try {
        context.add(
                new LogbookServerFilterContext(
                        logbook.process(LogbookRequest.serverRequest(request))
                                .write()
                )
        );
    } catch (Exception e) { }
}

@ResponseFilter
void filter(
        HttpResponse<?> response,
        MutablePropagatedContext context
) {
    try {
        Objects.requireNonNull(context.getContext())
                .get(LogbookServerFilterContext.class)
                .stage
                .process(LogbookResponse.serverResponse(response))
                .write();
    } catch (Exception e) { }
}

And Logbook bean which use RequestScope bean to decide if Logbook should log request/responses:

Logbook.builder()
        .condition(ignore-> requestScopedBean.shouldLog())
        .build();

Your Environment

  • Version used: Micronaut v4.0.5, Logbook v3.6.0
Copy link
Contributor

In order to prioritize the support for Logbook, we would like to check whether the old issues are still relevant.
This issue has not been updated for over six months.

  • Please check if it is still relevant in latest version of the Logbook.
  • If so, please add a descriptive comment to keep the issue open.
  • Otherwise, the issue will automatically be closed after a week.

@github-actions github-actions bot added the stale label May 20, 2024
Copy link
Contributor

This issue has automatically been closed due to no activities. If the issue still exists in the latest version of the Logbook, please feel free to re-open it.

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

No branches or pull requests

2 participants