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

Properly capture lambda payloads for all handler types. #8264

Merged
merged 4 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import static net.bytebuddy.asm.Advice.OnMethodExit;
import static net.bytebuddy.asm.Advice.Origin;
import static net.bytebuddy.asm.Advice.This;
import static net.bytebuddy.implementation.bytecode.assign.Assigner.Typing.DYNAMIC;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

Expand Down Expand Up @@ -66,20 +65,14 @@ protected boolean defaultEnabled() {

@Override
public void methodAdvice(MethodTransformer transformer) {
// two args
transformer.applyAdvice(
isMethod()
.and(named("handleRequest"))
.and(takesArgument(1, named("com.amazonaws.services.lambda.runtime.Context"))),
getClass().getName() + "$ExtensionCommunicationAdvice");
// three args (streaming)
// lambda under the hood converts all handlers to streaming handlers via
// lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest
// full spec here : https://docs.aws.amazon.com/lambda/latest/dg/java-handler.html
transformer.applyAdvice(
isMethod()
.and(named("handleRequest"))
.and(takesArgument(2, named("com.amazonaws.services.lambda.runtime.Context"))),
getClass().getName() + "$ExtensionCommunicationAdvice");
// full spec here : https://docs.aws.amazon.com/lambda/latest/dg/java-handler.html

}

public static class ExtensionCommunicationAdvice {
Expand Down Expand Up @@ -108,7 +101,7 @@ static AgentScope enter(
static void exit(
@Origin String method,
@Enter final AgentScope scope,
@Advice.Return(typing = DYNAMIC) final Object result,
@Advice.Argument(1) final Object result,
@Advice.Thrown final Throwable throwable) {

if (scope == null) {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,6 @@ abstract class LambdaHandlerInstrumentationTest extends VersionedNamingTestBase
null
}

def "test lambda handler"() {
when:
new Handler().handleRequest(null, null)

then:
assertTraces(1) {
trace(1) {
span {
operationName operation()
errored false
}
}
}
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the lambdainternal.EventHandlerLoader$PojoHandlerAsStreamHandler.handleRequest is not published publicly, there is no way to test non-streaming handlers.

def "test lambda streaming handler"() {
when:
def input = new ByteArrayInputStream(StandardCharsets.UTF_8.encode("Hello").array())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import datadog.trace.bootstrap.instrumentation.api.AgentSpanContext;
import datadog.trace.core.CoreTracer;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
import okhttp3.ConnectionPool;
Expand Down Expand Up @@ -63,6 +64,7 @@ public class LambdaHandler {
private static final JsonAdapter<Object> adapter =
new Moshi.Builder()
.add(ByteArrayInputStream.class, new ReadFromInputStreamJsonAdapter())
.add(ByteArrayOutputStream.class, new ReadFromOutputStreamJsonAdapter())
.add(SkipUnsupportedTypeJsonAdapter.newFactory())
.build()
.adapter(Object.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package datadog.trace.lambda;

import com.squareup.moshi.JsonAdapter;
import com.squareup.moshi.JsonReader;
import com.squareup.moshi.JsonWriter;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import okio.BufferedSink;

public final class ReadFromOutputStreamJsonAdapter extends JsonAdapter<ByteArrayOutputStream> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is another failure in the tests related to this:

groovy.lang.GroovyRuntimeException: Could not find matching constructor for: java.io.ByteArrayOutputStream([B)
	at datadog.trace.lambda.LambdaHandlerTest.test moshi toJson OutputStream(LambdaHandlerTest.groovy:315)


@Override
public ByteArrayOutputStream fromJson(JsonReader reader) throws IOException {
throw new UnsupportedOperationException();
}

@Override
public void toJson(JsonWriter writer, ByteArrayOutputStream outputStream) throws IOException {
if (outputStream != null) {
BufferedSink sink = writer.valueSink();
byte[] bytes = outputStream.toByteArray();
sink.write(bytes);
sink.flush();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -308,4 +308,17 @@ class LambdaHandlerTest extends DDCoreSpecification {
then:
result == body
}

def "test moshi toJson OutputStream"() {
given:
def body = "{\"body\":\"bababango\",\"statusCode\":\"200\"}"
def myEvent = new ByteArrayOutputStream()
myEvent.write(body.getBytes(), 0, body.length())

when:
def result = LambdaHandler.writeValueAsString(myEvent)

then:
result == body
}
}
Loading