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

Is this still the appropriate way to record custom events on already instrumented lambdas using the latest NewRelic lambda layer?

@newrelic.agent.lambda_handler()

The above produces a deprecation warning today and I cannot find any documentation on proper agent calling from an already instrumented lambda.

Following the suggested approach above produces the following output:

[NR_EXT] Startup check failed: Vendored agent found at '/var/task/newrelic', a layer already includes this agent at '/opt/python/lib/python3.8/site-packages/newrelic'. Recommend using the layer agent to avoid unexpected agent behavior.

Also, does this endpoint take awhile to reflect during a NRQL query? My lambda is only sending custom events like 50% of the time despite the fact that I have the follow environment variables set as suggested here: Relic Solution: Record Custom Event Troubleshooting

NEW_RELIC_STARTUP_TIMEOUT: 10
NEW_RELIC_SHUTDOWN_TIMEOUT: 10

From what I can tell, the documentation doesn’t really exist. The only thing I found that appears to be “current” is this thread (linked from the documentation).

That said, I think there’s an expectation in general that when using a Lambda layer that includes libraries, your deployment package does not need to include them. So I think your code is correct… kind of annoying, but I think the solution is to remove the New Relic library from your deployment. I’d consider it a “development” library so you can still run it locally on your machine.

For your last part about recording custom events 50% of the time… that is worrying. Curious if anyone else has made a thread about that? We noticed it too, but was only when running locally so have been hoping it does not do that in an AWS environment.

-Jason

Hey @torrance.graham,

What @JCapriotti said about removing New Relic from your deployment is correct. We want to ensure there is only one agent which generates the NR_LAMBDA_MONITORING payload. Our Extension looks for that payload and may not find it if it was generated by the agent included in your deployment. That could be the reason for it missing 50% of the custom events if half came from the agent in our layer and half from the agent in your deployment.

In my test Python 3.8 function, I found it to be unnecessary to include the @newrelic.agent.lambda_handler() line.

We already import the agent, handler, and initialize the agent in our wrapper in the layer.

If you remove the agent in your deployment, does the warning disappear and 100% of custom events get captured?