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

aws-lambda-events: Using TracingSqsMessageHandler or TracingSqsEventHandler result in ClassCastException #7362

Closed
tylerbenson opened this issue Dec 6, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@tylerbenson
Copy link
Member

tylerbenson commented Dec 6, 2022

When trying to create a handler that extends one of these classes, the handleRequest method in TracingRequestHandler is erased to a generic Object type. At runtime, Lambda inspects the type signature of this method and falls back to LinkedHashMap if a proper event type is not defined. This is passed into the lambda, but later results in the following exception:

class java.util.LinkedHashMap cannot be cast to class com.amazonaws.services.lambda.runtime.events.SQSEvent (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.amazonaws.services.lambda.runtime.events.SQSEvent is in unnamed module of loader lambdainternal.CustomerClassLoader @30501e60): java.lang.ClassCastException
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.amazonaws.services.lambda.runtime.events.SQSEvent (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.amazonaws.services.lambda.runtime.events.SQSEvent is in unnamed module of loader lambdainternal.CustomerClassLoader @30501e60)
	at io.opentelemetry.instrumentation.awslambdaevents.v2_2.TracingSqsEventHandler.doHandleRequest(TracingSqsEventHandler.java:17)
	at io.opentelemetry.instrumentation.awslambdacore.v1_0.TracingRequestHandler.handleRequest(TracingRequestHandler.java:79)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.base/java.lang.reflect.Method.invoke(Unknown Source)

TracingRequestHandler should not use a generic type.

What version are you using?

    <dependency>
      <groupId>io.opentelemetry.instrumentation</groupId>
      <artifactId>opentelemetry-aws-lambda-events-2.2</artifactId>
      <version>1.20.2-alpha</version>
    </dependency>

Environment
Corretto 11 Lambda Runtime.

@tylerbenson tylerbenson added the bug Something isn't working label Dec 6, 2022
@serkan-ozal
Copy link
Contributor

Hi @tylerbenson,

I think you are specifying handler method name in the handler definition like com.mycompany.MyHandler::handleRequest.
When method name is specified, AWS Lambda Java RIC (Runtime Interface Client) tries to resolve input and output types from the specified method by reflection.

But as you mentioned, because of type erasure in the TracingRequestHandler, input and output types are not able to resolved and treated as raw Object type. So input data is deserialized into default Map implementation, which is LinkedHashMap, for the Jackson serializer.

However, when no method name is specified, input and output types are resolved from class/interface generic type parameters by following the parent classes/interfaces.

So when you specify your handler definition as com.mycompany.MyHandler without method name, input and output types will be able to resolved correctly by the AWS Lambda Java RIC over the TracingSqsEventHandler by using its super class generic type definition (extends TracingRequestHandler<SQSEvent, Void>).

I think, as this is the default and only (no customization is allowed) behavior of AWS Lambda Java RIC, this behavior might be documented without any change in the SDK, WDYT?

@serkan-ozal
Copy link
Contributor

Hi @tylerbenson @cleverchuk, any thoughts on this?

@tylerbenson
Copy link
Member Author

@serkan-ozal this sounds reasonable. I haven't had this come up again for me recently and I haven't had time to investigate further, so I'll go ahead and close this with the assumption that your workaround will solve the problem. Thanks.

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

No branches or pull requests

2 participants