SDK based instrumentation requires you to enable New Relic agent as an SDK for your chosen runtime. Additionally, you must configure the New Relic extension or the newrelic-log-ingestion
lambda to send data to New Relic.
Select your runtime below and follow the setup instructions.
To instrument your Go-language Lambda:
Download our Go agent package, and place it in the same directory as your function.
Install the agent by running:
bash$go get -u github.com/newrelic/go-agent/v3/newrelicInstall the
nrlambda
integration by running:bash$go get -u github.com/newrelic/go-agent/v3/integrations/nrlambda
In your Lambda code, import our components, create an application, and update how you start your Lambda. See our instrumentation examples:
Optionally, add custom events that will be associated with your Lambda invocation by using the
RecordCustomEvent
API. For example:func handler(ctx context.Context) {if txn := newrelic.FromContext(ctx); nil != txn {txn.Application().RecordCustomEvent("MyEvent", map[string]interface{}{"zip": "zap",})}fmt.Println("hello world!")}Build and zip your Lambda function and upload it to AWS.
Here are suggestions for zipping and uploading the Lambda:
Build the binary for execution on Linux. This produces a binary file called
main
. You can use:bash$GOOS=linux go build -o mainZip the binary into a deployment package using:
bash$zip deployment.zip mainUpload the zip file to AWS using either the AWS Lambda console or the AWS CLI. Name the handler
main
(to match the name given during the binary build).
To enable distributed tracing and configure environmental variables, refer environment variables documentation.
Invoke the Lambda at least once. This creates a CloudWatch log group, which must be present for the next step to work.
Our wrapper gathers data about the Lambda execution, generates a JSON message, and logs it to CloudWatch Logs. Next, you'll configure CloudWatch to send those logs to New Relic.
Monitoring for AWS Lambda in Java doesn't use our APM Java agent. Instead, it uses these two OpenTracing dependencies:
AWS Lambda OpenTracing Java SDK: OpenTracing instrumentation for AWS Lambda RequestHandler and RequestStreamHandler.
Our AWS Lambda OpenTracing Tracer: An OpenTracing Tracer implementation designed to monitor AWS Lambda. It generates spans, error events, transaction events, error traces, and provides distributed tracing support.
Sugerencia
Supported OpenTracing Versions
- OpenTracing 0.31.0:
- Lambda Tracer: com.newrelic.opentracing:newrelic-java-lambda:1.1.1
- Lambda SDK: com.newrelic.opentracing:java-aws-lambda:1.0.0
- OpenTracing 0.32.0, 0.33.0:
- Lambda Tracer: com.newrelic.opentracing:newrelic-java-lambda:2.2.3
- Lambda SDK: com.newrelic.opentracing:java-aws-lambda:2.1.1
To instrument your Java Lambda:
- OpenTracing 0.31.0:
In your project's
build.gradle
file, include our OpenTracing AWS Lambda Tracer and the AWS Lambda OpenTracing SDK dependencies:dependencies {compile("com.newrelic.opentracing:java-aws-lambda:2.1.1")compile("com.newrelic.opentracing:newrelic-java-lambda:2.2.3")compile("io.opentracing:opentracing-util:0.33.0")}Implement the AWS Lambda
RequestHandler
interface as shown in the Java Lambda example and override thedoHandleRequest
method.In the
doHandleRequest
method, call theLambdaTracing.instrument(...)
API to create a root span to trace the lambda function's execution. This is also where you will define your business logic for the lambda function.Register a
LambdaTracer.INSTANCE
as the OpenTracing Global tracer, as shown in the Java Lambda example.Create a ZIP deployment package and upload it to AWS Lambda. Or deploy it via other means.
In the AWS Lambda console, set the handler. For the example Java Lambda, the handler would be
com.handler.example.MyLambdaHandler::handleRequest
. BecausehandleRequest
is assumed, you could also usecom.handler.example.MyLambdaHandler
.To enable distributed tracing and configure environmental variables, refer environment variables documentation.
Invoke the Lambda at least once. This creates a CloudWatch log group, which must be present for the next step to work.
Our wrapper gathers data about the Lambda execution, generates a JSON message, and logs it to CloudWatch Logs. Next, you'll configure CloudWatch to send those logs to New Relic.
Please see the AWS Lambda distributed tracing example for a complete project that illustrates common use cases such as:
- Distributed tracing between Lambda functions
- Manual span creation (aka custom instrumentation)
- Tracing external calls
- Adding custom attributes (aka Tags) to spans
To instrument your Node.js Lambda:
Download our Node.js agent package and place it in the same directory as your function, ensuring the agent is installed as a dependency in the
node_modules
directory. Use the Node Package Manager:bash$npm install newrelic --saveIn your Lambda code, require the agent module at the top of the file, and wrap the handler function with the newrelic
setLambdaHandler
. For example:- Sample Code for CommonJS:
const newrelic = require('newrelic');// Other module loads go under the require statement abovemodule.exports.handler = newrelic.setLambdaHandler((event, context, callback) => {// This is your handler function codeconsole.log('Lambda executed');callback();});- Sample Code for ES Module:
import newrelic from 'newrelic';// Other module loads go under the require statement aboveexport const handler = newrelic.setLambdaHandler((event, context, callback) => {// This is your handler function codeconsole.log('Lambda executed');callback();});Optional: You can also add custom events to your Lambda using the
recordCustomEvent
API. For example:- Sample code for CommonJS:
module.exports.handler = newrelic.setLambdaHandler((event, context, callback) => {newrelic.recordCustomEvent('MyEventType', { foo: 'bar' });console.log('Lambda executed');callback();});- Sample code for ES Module:
export const lambdaHandler = newrelic.setLambdaHandler((event, context, callback) => {newrelic.recordCustomEvent('MyEventType', { foo: 'bar' });console.log('Lambda executed');callback();});Zip your Lambda function and the Node.js agent folder together. Requirements and recommendations:
- The New Relic files outside the New Relic agent folder don't need to be included.
- If your Lambda function file name is, for example,
lambda_function.node
, we recommend naming your zip filelambda_function.zip
. Do not use a tarball. - Your Lambda and its associated modules must all be in the zip file's root directory. This means that if you zip a folder that contains the files, it won't work.
Upload the zipped file to your AWS Lambda account.
To enable distributed tracing and configure environmental variables, refer environment variables documentation.
To complete the instrumentation, follow one of the following steps to send the telemetry data to New Relic:
- Use the New Relic Lambda
Extension layer
. You can get the latest (NewRelicLambdaExtension) layer arn. You can either use AWS CLI to install the layer or manually add it to your Lambda.
bash$aws lambda update-function-configuration --function-name <your-lambda-function-name> --layers arn:aws:lambda:<aws-region>:451483290750:layer:NewRelicLambdaExtension:<version>- Use the
newrelic-log-ingestion
. You can refer to the CloudWatch fallback for detailed steps.
- Use the New Relic Lambda
To instrument your Python Lambda:
Download both the Python agent and Python lambda wrapper packages and place them in the same directory as your function code. To do this, use pip:
bash$pip install -t . newrelic newrelic-lambdaImportante
If you use Homebrew, you may get this error:
DistutilsOptionError: must supply either home or prefix/exec-prefix -- not both
. For details, see the Homebrew GitHub post.In your Lambda code, import both the Python agent module and the Python lambda wrapper module.
Decorate the handler function using the New Relic decorator. The New Relic package must be imported first in your code. Here's an example:
import newrelic.agentfrom newrelic_lambda.lambda_handler import lambda_handlernewrelic.agent.initialize()@lambda_handler()def handler(event, context):...Optional: You can also add custom events to your Lambda using the
record_custom_event
API. Here's an example:@lambda_handler()def handler(event, context):newrelic.agent.record_custom_event('CustomEvent', {'foo': 'bar'})...Zip your
lambda_function.py
andnewrelic/
folder together using these guidelines:- The New Relic files outside the
newrelic/
folder don't need to be included. - If your Lambda function file name is, for example,
lambda_function.py
, name your zip filelambda_function.zip
. Do not use a tarball. - Your Lambda and its associated modules must all be in the zip file's root directory. This means that if you zip a folder that contains the files, it won't work.
- The New Relic files outside the
Upload the zipped file to your AWS Lambda account.
To enable distributed tracing and configure environmental variables, refer environment variables documentation.
Invoke the Lambda at least once. This creates a CloudWatch log group, which must be present for the next step to work.
The New Relic decorator gathers data about the Lambda execution, generates a JSON message, and logs it to CloudWatch Logs. Next, configure CloudWatch to send those logs to New Relic.
In most cases, the .NET Agent will automatically instrument your AWS Lambda function and switch into a "serverless mode" that will disable sending data directly to New Relic as well some other features. You must either use the New Relic Lambda Extension or the newrelic-log-ingestion
lambda method to send data to New Relic.
To instrument your .NET Lambda:
- Add the NewRelic.Agent nuget package to your AWS Lambda project. For more information, see our installation guide.
- To enable distributed tracing and configure environmental variables, refer environment variables documentation.
- Publish the project to your AWS Lambda account.
- Configure either the New Relic Lambda Extension or the
newrelic-log-ingestion
lambda. - Invoke the Lambda at least once to check for errors and ensure the data is visible in the New Relic UI.