Add custom event to already instrumented lambda functions

I have a serverless application with instrumented lamda functions by serverless-newrelic-lambda-layers plugin. Is it possible to add some custom events? I know that adding custom event can be done by

newrelic.agent.record_custom_event('CustomEvent', {'foo': 'bar'})

The question is: should I initialize New Relic agent fist? Isn’t it already initialized by lambda layer? If it is already initialized by lambda layer, won’t it break this initialization? Should I install newrelic package into the virtualenv or I can use somehow newrelic package from the lambda layer?

OR

Custom event implies custom Lambda instrumentation with newrelic python agent and refuse of serverless-newrelic-lambda-layers?

Hi @aandrusyk,

Thanks for posting your question here in Explorers Hub! My name is Keegan and I’m with the Lambda Support Team in Portland, Oregon.

Since our Lambda Monitoring utilizes the APM agents in serverless mode to record invocations, any custom attributes or events that you want to add would be done from the agent api. No need to remove our lambda layer.

https://docs.newrelic.com/docs/agents/python-agent/python-agent-api/recordcustomevent-python-agent-api

We detail this specific case in the manual steps for Python instrumentation for Lambda Monitoring.

https://docs.newrelic.com/docs/serverless-function-monitoring/aws-lambda-monitoring/get-started/enable-serverless-monitoring-aws-lambda#python


@newrelic.agent.lambda_handler()

def handler(event, context):

    newrelic.agent.record_custom_event('CustomEvent', {'foo': 'bar'})

...

Looking forward to seeing how that works for you! If you get it to work, I’d love to hear about it.

1 Like

Hi @aandrusyk,

I wanted to follow up with some more details here. When adding custom events in a Python Lambda that has our Layer installed, you’ll still need to import/initialize the Agent for access to the API.

import json
import newrelic.agent

newrelic.agent.initialize()
@newrelic.agent.lambda_handler()

def handler(event, context):
  # TODO implement
  newrelic.agent.record_custom_event('CustomEvent', {'foo': 'bar'}) return {
    'statusCode': 200,
    'body': json.dumps('Hello from Lambda!')
    }
1 Like