Skip to content

Commit

Permalink
Merge pull request #70 from newrelic/kwest/lambda-1236
Browse files Browse the repository at this point in the history
Java layer example update
  • Loading branch information
Katie West authored May 7, 2021
2 parents bdd2d2c + 9a43640 commit fcdd844
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 37 deletions.
16 changes: 5 additions & 11 deletions examples/sam/java/NewRelicExampleJava/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,16 @@
<artifactId>aws-lambda-java-events</artifactId>
<version>3.1.0</version>
</dependency>

<dependency>
<groupId>com.newrelic.opentracing</groupId>
<artifactId>newrelic-java-lambda</artifactId>
<version>2.2.1</version>
</dependency>
<dependency>
<groupId>com.newrelic.opentracing</groupId>
<artifactId>java-aws-lambda</artifactId>
<version>2.1.0</version>
</dependency>
<dependency>
<groupId>io.opentracing</groupId>
<artifactId>opentracing-util</artifactId>
<version>0.33.0</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,16 @@

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;
import io.opentracing.util.GlobalTracer;

import java.util.Map;

/**
* Handler for requests to Lambda function.
*/
public class App implements RequestHandler<Map<String, Object>, String> {
static {
// Register the New Relic OpenTracing LambdaTracer as the Global Tracer
GlobalTracer.registerIfAbsent(LambdaTracer.INSTANCE);
}

public String handleRequest(final Map<String, Object> 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.
Expand Down
18 changes: 9 additions & 9 deletions examples/sam/java/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
28 changes: 24 additions & 4 deletions examples/sam/java/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit fcdd844

Please sign in to comment.