-
Notifications
You must be signed in to change notification settings - Fork 945
Use aws-lambda-java-serialization
library, which is available by default, while deserializing input and serializing output
#11868
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
Changes from all commits
57ae14b
94a6111
9cd1ddc
b2c2c17
898a5d0
a542ee8
c6c0767
4da5b50
4fa294c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,32 +5,90 @@ | |
|
||
package io.opentelemetry.instrumentation.awslambdaevents.v2_2; | ||
|
||
import com.amazonaws.services.lambda.runtime.Context; | ||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; | ||
import io.opentelemetry.instrumentation.awslambdacore.v1_0.AwsLambdaRequest; | ||
import io.opentelemetry.instrumentation.awslambdacore.v1_0.TracingRequestStreamWrapper; | ||
import io.opentelemetry.instrumentation.awslambdacore.v1_0.internal.ApiGatewayProxyRequest; | ||
import io.opentelemetry.instrumentation.awslambdacore.v1_0.internal.MapUtils; | ||
import io.opentelemetry.instrumentation.awslambdacore.v1_0.internal.WrappedLambda; | ||
import io.opentelemetry.instrumentation.awslambdaevents.v2_2.internal.SerializationUtil; | ||
import io.opentelemetry.sdk.OpenTelemetrySdk; | ||
import java.util.function.BiFunction; | ||
import java.io.ByteArrayInputStream; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.io.OutputStream; | ||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
|
||
/** | ||
* Wrapper for {@link io.opentelemetry.instrumentation.awslambdacore.v1_0.TracingRequestHandler}. | ||
* Allows for wrapping a regular lambda, not proxied through API Gateway. Therefore, HTTP headers | ||
* propagation is not supported. | ||
* Wrapper for {@link com.amazonaws.services.lambda.runtime.RequestHandler} based Lambda handlers. | ||
*/ | ||
public class TracingRequestWrapper extends TracingRequestWrapperBase<Object, Object> { | ||
public class TracingRequestWrapper extends TracingRequestStreamWrapper { | ||
public TracingRequestWrapper() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the purpose of this constructor? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tylerbenson default constructors are used by the AWS Lambda runtime while instantiating our wrapper handlers. Other constructors with arguments are used by the tests. |
||
super(TracingRequestWrapper::map); | ||
super(); | ||
} | ||
|
||
// Visible for testing | ||
TracingRequestWrapper( | ||
OpenTelemetrySdk openTelemetrySdk, | ||
WrappedLambda wrappedLambda, | ||
BiFunction<Object, Class<?>, Object> mapper) { | ||
super(openTelemetrySdk, wrappedLambda, mapper); | ||
TracingRequestWrapper(OpenTelemetrySdk openTelemetrySdk, WrappedLambda wrappedLambda) { | ||
super(openTelemetrySdk, wrappedLambda); | ||
} | ||
|
||
@Override | ||
protected final AwsLambdaRequest createRequest( | ||
InputStream inputStream, Context context, ApiGatewayProxyRequest proxyRequest) { | ||
Method targetMethod = wrappedLambda.getRequestTargetMethod(); | ||
Object input = LambdaParameters.toInput(targetMethod, inputStream, TracingRequestWrapper::map); | ||
return AwsLambdaRequest.create(context, input, extractHeaders(input)); | ||
} | ||
|
||
protected Map<String, String> extractHeaders(Object input) { | ||
if (input instanceof APIGatewayProxyRequestEvent) { | ||
return MapUtils.emptyIfNull(((APIGatewayProxyRequestEvent) input).getHeaders()); | ||
} | ||
return Collections.emptyMap(); | ||
} | ||
|
||
@Override | ||
protected final void doHandleRequest( | ||
InputStream input, OutputStream output, Context context, AwsLambdaRequest request) { | ||
Method targetMethod = wrappedLambda.getRequestTargetMethod(); | ||
Object[] parameters = LambdaParameters.toParameters(targetMethod, request.getInput(), context); | ||
try { | ||
Object result = targetMethod.invoke(wrappedLambda.getTargetObject(), parameters); | ||
SerializationUtil.toJson(output, result); | ||
} catch (IllegalAccessException e) { | ||
throw new IllegalStateException("Method is inaccessible", e); | ||
} catch (InvocationTargetException e) { | ||
throw (e.getCause() instanceof RuntimeException | ||
? (RuntimeException) e.getCause() | ||
: new IllegalStateException(e.getTargetException())); | ||
Comment on lines
+66
to
+68
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we stripping off the top layer of the exception chain? Is it to remove the instrumentation itself or something? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is the same exception handling logic borrowed from |
||
} | ||
} | ||
|
||
@SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"}) | ||
// Used for testing | ||
<INPUT, OUTPUT> OUTPUT handleRequest(INPUT input, Context context) throws IOException { | ||
byte[] inputJsonData = SerializationUtil.toJsonData(input); | ||
ByteArrayInputStream inputStream = new ByteArrayInputStream(inputJsonData); | ||
ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); | ||
|
||
super.handleRequest(inputStream, outputStream, context); | ||
|
||
byte[] outputJsonData = outputStream.toByteArray(); | ||
return (OUTPUT) | ||
SerializationUtil.fromJson( | ||
new ByteArrayInputStream(outputJsonData), | ||
wrappedLambda.getRequestTargetMethod().getReturnType()); | ||
} | ||
|
||
// Visible for testing | ||
static <T> T map(Object jsonMap, Class<T> clazz) { | ||
static <T> T map(InputStream inputStream, Class<T> clazz) { | ||
try { | ||
return OBJECT_MAPPER.convertValue(jsonMap, clazz); | ||
return SerializationUtil.fromJson(inputStream, clazz); | ||
} catch (IllegalArgumentException e) { | ||
throw new IllegalStateException( | ||
"Could not map input to requested parameter type: " + clazz, e); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a comment here about this library being available during runtime natively in lambda?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@rapphil done