diff --git a/examples/sam/java/NewRelicExampleJava/pom.xml b/examples/sam/java/NewRelicExampleJava/pom.xml index afda3b1..ad9ca68 100644 --- a/examples/sam/java/NewRelicExampleJava/pom.xml +++ b/examples/sam/java/NewRelicExampleJava/pom.xml @@ -22,22 +22,16 @@ aws-lambda-java-events 3.1.0 - - - com.newrelic.opentracing - newrelic-java-lambda - 2.2.1 - - - com.newrelic.opentracing - java-aws-lambda - 2.1.0 - io.opentracing opentracing-util 0.33.0 + + com.google.code.gson + gson + 2.8.6 + com.googlecode.json-simple json-simple diff --git a/examples/sam/java/NewRelicExampleJava/src/main/java/com.newrelic.lambda.example/App.java b/examples/sam/java/NewRelicExampleJava/src/main/java/com.newrelic.lambda.example/App.java index de0a6f3..af409ec 100644 --- a/examples/sam/java/NewRelicExampleJava/src/main/java/com.newrelic.lambda.example/App.java +++ b/examples/sam/java/NewRelicExampleJava/src/main/java/com.newrelic.lambda.example/App.java @@ -2,8 +2,6 @@ import com.amazonaws.services.lambda.runtime.Context; import com.amazonaws.services.lambda.runtime.RequestHandler; -import com.newrelic.opentracing.LambdaTracer; -import com.newrelic.opentracing.aws.LambdaTracing; import io.opentracing.Scope; import io.opentracing.Span; import io.opentracing.Tracer; @@ -11,20 +9,9 @@ import java.util.Map; -/** - * Handler for requests to Lambda function. - */ public class App implements RequestHandler, String> { - static { - // Register the New Relic OpenTracing LambdaTracer as the Global Tracer - GlobalTracer.registerIfAbsent(LambdaTracer.INSTANCE); - } public String handleRequest(final Map input, final Context context) { - return LambdaTracing.instrument(input, context, (event, ctx) -> handleInvocation()); - } - - private String handleInvocation() { final Tracer tracer = GlobalTracer.get(); // This is an example of a custom span. `FROM Span SELECT * WHERE name='MyJavaSpan'` in New Relic will find this event. diff --git a/examples/sam/java/README.md b/examples/sam/java/README.md index 242f439..6cc1aba 100644 --- a/examples/sam/java/README.md +++ b/examples/sam/java/README.md @@ -48,7 +48,7 @@ tell CloudFormation where to find lambda function code, what layers to use, and what IAM policies to add to the Lambda function's execution role. We also set environment variables that are available to the handler function. -### App.java +### App.java and Stream.java Lambda functions written in Java are Java classes. The runtime loads them just like any Java class, and then invokes the handler function for each @@ -62,13 +62,13 @@ and sending them to the New Relic collector. If you're familiar with the logging ecosystem in Java, the idea behind OpenTracing and OpenTelemetry is a similar one: standardize the API that libraries use, and let applications choose the concrete implementation -responsible for managing the data itself. +responsible for managing the data itself. + +We have two examples, implementing the AWS RequestHandler and the AWS RequestStreamHandler. +Both examples are instrumented with the published New Relic Lambda Layer that wraps your handler +function, and initializes the New Relic agent, allowing us to collect telemetry. + +Additionally, there are a couple examples in App.java demonstrating how you might use the OpenTracing API in +your own code to create custom span data. -For all that to work, two things happen here. First, we register the New Relic -`LambdaTracer` as the concrete `Tracer` implementation in the static initializer. -Second, we need to wrap your request handler's business logic so that the trace -begins and ends correctly, and errors are handled appropriately. That's the call -to `LambdaTracing.instrument` on the first line of our request handler. -There are a couple examples here of how you might use the OpenTracing API in -your own code. diff --git a/examples/sam/java/template.yaml b/examples/sam/java/template.yaml index 7d5fe33..beb3d85 100644 --- a/examples/sam/java/template.yaml +++ b/examples/sam/java/template.yaml @@ -16,23 +16,43 @@ Resources: CodeUri: NewRelicExampleJava/ Description: A simple Lambda, with New Relic telemetry FunctionName: newrelic-example-java - Handler: com.newrelic.lambda.example.App::handleRequest + Handler: com.newrelic.java.HandlerWrapper::handleRequest # Lambda Extensions are not supported in the "java8" runtime. - Runtime: java8.al2 + Runtime: java11 MemorySize: 512 Environment: Variables: + NEW_RELIC_LAMBDA_HANDLER: com.newrelic.lambda.example.App::handleRequest NEW_RELIC_ACCOUNT_ID: !Sub ${NRAccountId} # NEW_RELIC_EXTENSION_SEND_FUNCTION_LOGS: true # NEW_RELIC_EXTENSION_LOG_LEVEL: DEBUG Layers: - # This layer includes the New Relic Lambda Extension, a sidecar process that sends telemetry, - - !Sub arn:${AWS::Partition}:lambda:${AWS::Region}:451483290750:layer:NewRelicLambdaExtension:7 + # This layer includes the New Relic Java agent and Lambda Extension, a sidecar process that sends telemetry, + # as well as the New Relic Agent for java, and a handler wrapper that makes integration easy. + - !Sub arn:${AWS::Partition}:lambda:${AWS::Region}:451483290750:layer:NewRelicJava11:2 Policies: # This policy allows the lambda to know the value of the New Relic licence key. We need this so # that we can send telemetry back to New Relic - AWSSecretsManagerGetSecretValuePolicy: SecretArn: !ImportValue NewRelicLicenseKeySecret-NewRelic-LicenseKeySecretARN + NewRelicExampleStream: + Type: AWS::Serverless::Function + Properties: + CodeUri: NewRelicExampleJava/ + Description: A simple Lambda, with New Relic telemetry + FunctionName: newrelic-example-java-stream + Handler: com.newrelic.java.HandlerWrapper::handleStreamsRequest + Runtime: java11 + MemorySize: 512 + Environment: + Variables: + NEW_RELIC_LAMBDA_HANDLER: com.newrelic.lambda.example.Stream::handleRequest + NEW_RELIC_ACCOUNT_ID: !Sub ${NRAccountId} + Layers: + - !Sub arn:${AWS::Partition}:lambda:${AWS::Region}:451483290750:layer:NewRelicJava11:2 + Policies: + - AWSSecretsManagerGetSecretValuePolicy: + SecretArn: !ImportValue NewRelicLicenseKeySecret-NewRelic-LicenseKeySecretARN Logs: Type: AWS::Logs::LogGroup Properties: