Skip to content

Powertools method of identifying request handler method doesn't work with Handler class hierarchies #1056

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

Closed
humanzz opened this issue Feb 20, 2023 · 0 comments · Fixed by #1058
Labels
bug Something isn't working triage

Comments

@humanzz
Copy link
Contributor

humanzz commented Feb 20, 2023

My code base has several Lambda handler classes. I'm implementing an abstract base handler class with some common behaviours. I then implement a lambda specific handler extending the abstract base handler.

...
import software.amazon.codeguruprofilerjavaagent.RequestHandlerWithProfiling;
...

@Log4j2
public abstract class RequestHandler<I, O> extends RequestHandlerWithProfiling<I, O> {

    static {
        // Override the default dimensions to no dimensions by setting the defaultDimensions to an empty list
        MetricsUtils.defaultDimensions();
    }

    @Override
    @Tracing(segmentName = "Handler")
    @Logging(clearState = true)
    @Metrics(captureColdStart = false) // Disable coldStart metric as it requires non-empty dimensions
    public O requestHandler(I i, Context context) {
        try {
            return handle(i, context);
        } catch (Exception e) {
            ....
        }


       public abstract O handle(I i, Context context);
}

What were you trying to accomplish?

In a concrete handler, extending the abstract RequestHandler above, I wanted to use the @Validation utility

public class ConcreteHandler extends RequestHandler<SQSEvent, Void> {
     ...
    @Override
    @Validation(inboundSchema = ..., envelope = ...)
    public Void handle(SQSEvent event, Context context) {
        ...
        return null;
    }
    ...
}

Expected Behavior

The @Validation to work.

Current Behavior

@Validation did not result in validating the request. When debugging, I found the following

  1. ValidationAspect leverage's core's LambdaHandlerProcessor to identify handler methods https://github.com/awslabs/aws-lambda-powertools-java/blob/master/powertools-validation/src/main/java/software/amazon/lambda/powertools/validation/internal/ValidationAspect.java#L58 and skips validation if the method is identified as a handler
  2. LambdaHandlerProcessor makes some strong assumptions about handler method name https://github.com/awslabs/aws-lambda-powertools-java/blob/master/powertools-core/src/main/java/software/amazon/lambda/powertools/core/internal/LambdaHandlerProcessor.java#L39

The end result, is that @Validation under my setup becomes a no-op.

Possible Solution

I'm reporting the above with an example of @Validation annotation, but I believe most other Powertools utilities do share a similar logic in their *Aspect.java classes to leverage LambdaHandlerProcessor for identifying handler methods.

A possible solution is to maybe simply rely on method signature only, rather than forcing a name as already implemented in LambdaHandlerProcessor.placedOnRequestHandler() and LambdaHandlerProcessor.placedOnStreamHandler() and to drop the LambdaHandlerProcessor.isHandlerMethod() check completely.

Steps to Reproduce (for bugs)

Environment

  • Powertools version used: 1.14.0
  • Packaging format (Layers, Maven/Gradle):
  • AWS Lambda function runtime:
  • Debugging logs

How to enable debug mode**

# paste logs here
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working triage
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant