-
Notifications
You must be signed in to change notification settings - Fork 7
Address type erasure/reflection issue #36
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
src/main/java/com/newrelic/opentracing/aws/LambdaTracing.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package com.newrelic.opentracing.aws; | ||
|
|
||
| import com.amazonaws.services.lambda.runtime.Context; | ||
| import io.opentracing.Scope; | ||
| import io.opentracing.Span; | ||
| import io.opentracing.SpanContext; | ||
| import io.opentracing.Tracer; | ||
| import io.opentracing.util.GlobalTracer; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.function.BiFunction; | ||
|
|
||
| /** | ||
| * Trace calls to lambda functions, for arbitrary Input and Output types. | ||
| * | ||
| * <p>For flexibility, applications may extend this class to enhance the root span or handle novel | ||
| * invocation event types. | ||
| * | ||
| * @param <Input> The invocation payload type for your lambda function. | ||
| * @param <Output> The result type for your lambda function. | ||
| */ | ||
| public class LambdaTracing<Input, Output> { | ||
| protected static final AtomicBoolean isColdStart = new AtomicBoolean(true); | ||
|
|
||
| /** | ||
| * One-line instrumentation convenience method. | ||
| * | ||
| * @param input The invocation event | ||
| * @param context The invocation context | ||
| * @param realHandler The callback that implements the business logic for this event handler | ||
| * @param <Input> The type of the invocation event | ||
| * @param <Output> The type of the response | ||
| * @return The invocation response (the return value of the realHandler callback) | ||
| */ | ||
| public static <Input, Output> Output instrument( | ||
| Input input, Context context, BiFunction<Input, Context, Output> realHandler) { | ||
| return new LambdaTracing<Input, Output>().instrumentRequest(input, context, realHandler); | ||
| } | ||
|
|
||
| /** | ||
| * Instrument a Lambda invocation | ||
| * | ||
| * @param input The invocation event | ||
| * @param context The invocation context | ||
| * @param realHandler The function that implements the business logic. Will be invoked with the | ||
| * input and context parameters, from within the instrumentation scope. | ||
| * @return the return value from realHandler | ||
| */ | ||
| public Output instrumentRequest( | ||
| Input input, Context context, BiFunction<Input, Context, Output> realHandler) { | ||
| final Tracer tracer = GlobalTracer.get(); | ||
| final SpanContext spanContext = extractContext(tracer, input); | ||
|
|
||
| Span span = buildRootSpan(input, context, tracer, spanContext); | ||
| try (Scope scope = tracer.activateSpan(span)) { | ||
| Output output = realHandler.apply(input, context); | ||
| parseResponse(span, output); | ||
| return output; | ||
| } catch (Throwable throwable) { | ||
| span.log(SpanUtil.createErrorAttributes(throwable)); | ||
| throw throwable; | ||
| } finally { | ||
MattWhelan marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| span.finish(); | ||
| } | ||
| } | ||
|
|
||
| protected SpanContext extractContext(Tracer tracer, Object input) { | ||
jasonjkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return HeadersParser.parseAndExtract(tracer, input); | ||
| } | ||
|
|
||
| protected Span buildRootSpan( | ||
jasonjkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Input input, Context context, Tracer tracer, SpanContext spanContext) { | ||
| return SpanUtil.buildSpan(input, context, tracer, spanContext, isColdStart); | ||
| } | ||
|
|
||
| protected void parseResponse(Span span, Output output) { | ||
jasonjkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ResponseParser.parseResponse(output, span); | ||
| } | ||
| } | ||
68 changes: 68 additions & 0 deletions
68
src/main/java/com/newrelic/opentracing/aws/StreamLambdaTracing.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| package com.newrelic.opentracing.aws; | ||
|
|
||
| import com.amazonaws.services.lambda.runtime.Context; | ||
| import com.amazonaws.services.lambda.runtime.RequestStreamHandler; | ||
| import io.opentracing.Scope; | ||
| import io.opentracing.Span; | ||
| import io.opentracing.SpanContext; | ||
| import io.opentracing.Tracer; | ||
| import io.opentracing.util.GlobalTracer; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
|
|
||
| /** | ||
| * Trace calls to lambda functions, implementing manual JSON serialization. | ||
| * | ||
| * <p>For flexibility, applications may extend this class to enhance the root span. | ||
| */ | ||
| public class StreamLambdaTracing { | ||
jasonjkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| /** | ||
| * One-line instrumentation convenience method. | ||
| * | ||
| * @param input The invocation event's input stream | ||
| * @param output The invocation response output stream | ||
| * @param context The invocation context | ||
| * @param realHandler The callback that implements the business logic for this event handler | ||
| */ | ||
| public static void instrument( | ||
| InputStream input, OutputStream output, Context context, RequestStreamHandler realHandler) | ||
| throws IOException { | ||
| new StreamLambdaTracing().instrumentRequest(input, output, context, realHandler); | ||
| } | ||
|
|
||
| /** | ||
| * Instrument a Lambda invocation | ||
| * | ||
| * @param input The invocation event's input stream | ||
| * @param output The invocation response output stream | ||
| * @param context The invocation context | ||
| * @param realHandler The function that implements the business logic. Will be invoked with the | ||
| * input and context parameters, from within the instrumentation scope. | ||
| */ | ||
| public void instrumentRequest( | ||
| InputStream input, OutputStream output, Context context, RequestStreamHandler realHandler) | ||
| throws IOException { | ||
| final Tracer tracer = GlobalTracer.get(); | ||
| final SpanContext spanContext = extractContext(tracer, input); | ||
|
|
||
| Span span = buildRootSpan(input, context, tracer, spanContext); | ||
| try (Scope scope = tracer.activateSpan(span)) { | ||
| realHandler.handleRequest(input, output, context); | ||
| } catch (Throwable throwable) { | ||
| span.log(SpanUtil.createErrorAttributes(throwable)); | ||
| throw throwable; | ||
| } finally { | ||
| span.finish(); | ||
| } | ||
| } | ||
|
|
||
| protected Span buildRootSpan( | ||
jasonjkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| InputStream input, Context context, Tracer tracer, SpanContext spanContext) { | ||
| return SpanUtil.buildSpan(input, context, tracer, spanContext, LambdaTracing.isColdStart); | ||
| } | ||
|
|
||
| protected SpanContext extractContext(Tracer tracer, InputStream input) { | ||
| return null; | ||
jasonjkeller marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/test/java/com/newrelic/opentracing/aws/ReflectionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| package com.newrelic.opentracing.aws; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| import com.amazonaws.services.lambda.runtime.Context; | ||
| import com.amazonaws.services.lambda.runtime.RequestHandler; | ||
| import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; | ||
| import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; | ||
| import java.lang.reflect.Method; | ||
| import java.util.Arrays; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
|
|
||
| public class ReflectionTest { | ||
| private static final int SYNTHETIC_MODIFIER = 0x1000; | ||
|
|
||
| private Object handler; | ||
|
|
||
| @Before | ||
| public void setup() { | ||
| handler = new TestHandler(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testInputReflection() { | ||
| // Ignoring synthetics, we expect handleRequest to take the declared type as its first arg. | ||
| // This is necessary for correct payload deserialization. | ||
| final Method handleRequest = | ||
| Arrays.stream(handler.getClass().getMethods()) | ||
| .filter( | ||
| m -> | ||
| ((m.getModifiers() & SYNTHETIC_MODIFIER) == 0) | ||
| && m.getName().equals("handleRequest")) | ||
| .findFirst() | ||
| .orElseThrow(AssertionError::new); | ||
|
|
||
| assertEquals(APIGatewayProxyRequestEvent.class, handleRequest.getParameterTypes()[0]); | ||
| } | ||
|
|
||
| public static class TestHandler | ||
| implements RequestHandler<APIGatewayProxyRequestEvent, APIGatewayProxyResponseEvent> { | ||
| @Override | ||
| public APIGatewayProxyResponseEvent handleRequest( | ||
| APIGatewayProxyRequestEvent apiGatewayProxyRequestEvent, Context context) { | ||
| return LambdaTracing.instrument( | ||
| apiGatewayProxyRequestEvent, context, (event, c) -> new APIGatewayProxyResponseEvent()); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.