How to start and end transaction in python aws lambda function , also how to add labels between transaction

If you are asking about a problem you are experiencing, please use the following template, as it will help us help you. If you have a different problem, please delete all of this text :slight_smile:

TIP 1: select at least one tag that further categorizes your topic. For example server for APM Server related questions, java for questions regarding the Elastic APM Java agent, or ui for questions about the APM App within Kibana.

TIP 2: Check out the troubleshooting guide first. Not only will it help you to resolve common problems faster but it also explains in more detail which information we need before we can properly help you.

Kibana version:

Elasticsearch version: 7.15.0

APM Server version: 7.15.0

APM Agent language and version: python

Browser version:

Original install method (e.g. download page, yum, deb, from source, etc.) and version:

Fresh install or upgraded from other version?

Is there anything special in your setup? For example, are you using the Logstash or Kafka outputs? Are you using a load balancer in front of the APM Servers? Have you changed index pattern, generated custom templates, changed agent configuration etc.

Description of the problem including expected versus actual behavior. Please include screenshots (if relevant):

Steps to reproduce:
1.
2.
3.

Errors in browser console (if relevant):

Provide logs and/or server output (if relevant):

1 Like

Hey @Pratiksha_Nagoshe -- check out the docs for our Lambda integration! Feel free to reach out if you have any questions. Monitoring AWS Lambda Python Functions | APM Python Agent Reference [6.x] | Elastic

@basepi thanks for sharing this but I want know how to add labels in python script

1 Like

elasticapm.label() should get you there.

Thank you @basepi

1 Like

Hi team
{
"log.level": "debug",
"@timestamp": "2023-02-03T13:52:08.884Z",
"log.origin": {
"file.name": "apmproxy/apmserver.go",
"file.line": 55
},
"message": "Invocation context cancelled, not processing any more agent data",
"ecs.version": "1.6.0"
}
{
"log.level": "debug",
"@timestamp": "2023-02-03T13:52:08.884Z",
"log.origin": {
"file.name": "apmproxy/apmserver.go",
"file.line": 78
},
"message": "Flush started - Checking for agent data",
"ecs.version": "1.6.0"
}

{
"log.level": "warn",
"@timestamp": "2023-02-03T13:52:08.884Z",
"log.origin": {
"file.name": "apmproxy/apmserver.go",
"file.line": 90
},
"message": "Metadata not available at flush, skipping sending lambda data to APM Server",
"ecs.version": "1.6.0"
}

{
"log.level": "info",
"@timestamp": "2023-02-03T13:52:08.884Z",
"log.origin": {
"file.name": "app/run.go",
"file.line": 129
},
"message": "Waiting for next event...",
"ecs.version": "1.6.0"
}

Not sending data to apm server
added environment variables in aws lambda :
ELASTIC_APM_LAMBDA_APM_SERVER : "ABC"
ELASTIC_APM_LOG_LEVEL : DEBUG
ELASTIC_APM_SERVER_TIMEOUT : 60s
ELASTIC_APM_SERVICE_NAME : "abc"

code added
import elasticapm
elasticapm.label("abc")

Thanks

1 Like

In order for elasticapm.label() to work, you need to be using our @capture_serverless() decorator on your handler, as shown in the lambda docs.

@basepi

for @capture_serverless(),
import : from elasticapm.elasticapm.contrib.serverless import capture_serverless
I tried but its showing me import error
Error: {"cannot import name 'get_client' from 'elasticapm' (/var/task/elasticapm/init.py)
Traceback (most recent call last):
[ERROR] Runtime.ImportModuleError: Unable to import module 'test_lambda': cannot import name 'get_client' from 'elasticapm' (/var/task/elasticapm/init.py) Traceback (most recent call last): "}

1 Like

It appears maybe you have an oddity in your paths? You did

from elasticapm.elasticapm.contrib.serverless import capture_serverless

Which has an extra .elasticapm in it. It should just be elasticapm.contrib.serverless if you've installed it correctly. I could see that causing the import error because your pythonpath has an extra nested elasticapm folder.

How did you install the python agent?

@basepi thanks
I corrected the import but now I am getting below error
[ERROR] TypeError: label() takes 0 positional arguments but 2 were given Traceback (most recent call last): File "/var/task/src/main/logger.py", line 43, in inner return fn(*args, **kwargs) File "/var/task/test_lambda.py", line 32, in lambda_handler elasticapm.label("Key","value")

1 Like

Try elasticapm.label(key="value") instead. docs

@basepi Thank you

It working but its sending event to kibana when next event pushed

Code :
apm_client.begin_transaction('success')
# Set a label on the transaction
request_id = str(uuid.uuid4())
elasticapm.label(request_id=request_id)
# End the transaction
apm_client.end_transaction('success',"OK")

result of 1st api hit like request_id = 'urtuew76834uyerw26';
I can see the above data on kibana when 2nd api hits like request_id = 'mbkuuw213jhada6';
so now after 2 hits , I can see only 1st api result request_id = 'urtuew76834uyerw26';

Also from 10 events , 2 events are missing on kibana
why is this so ?

@capture_serverless is not working for me

1 Like

Can you share your code? @capture_serverless does some extra goodness aside from starting and ending the transaction. For example, it forces a flush of the transport so that the transaction is sent to the extension before the lambda function is frozen.

@basepi please check below code

import elasticapm
from elasticapm import  Client
from elasticapm.contrib.serverless.aws import capture_serverless

@logger_warp
def lambda_handler(event: 'a json payload', context: 'un-used {} a-b proxy') -> 'a json':
    """ this is the lambda handle, request lands here and routed to src-code"""
   if from_alb:
        if not error_flag:
            return {"statusCode": 200, "headers": {"Content-Type": "application/json"}, "body": json.dumps(ret)}
        else:
            return {"statusCode": 500, "headers": {"Content-Type": "application/json"}, "body": json.dumps(ret)}
    else:
        if not error_flag:
            return ret
        else:
            return {'code': 500, 'data': ret, 'message': 'failed'}
#end 
@logger_warp
@capture_serverless()
def lambda_handler(event: 'a json payload', context: 'un-used {} a-b proxy') -> 'a json':
    """ this is the lambda handle, request lands here and routed to src-code"""

when I added @capture_serverless on lambda function , aws lamdba unable to find lambda handler with bellow error
[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'abc_lambda': invalid syntax > (base_events.py, line 296)

Traceback (most recent call last):
File "/var/task/asyncio/base_events.py" Line 296
future = tasks.async(future, loop=self)

asyncio pkg of python is creating issue when I used @capture_serverless on lambda handler
please help here
Thanks

1 Like

Hmmm. I wonder if the @logger_warp decorator is interacting with our decorator in such a way to make this error? Does the error go away if you remove the @logger_warp decorator?

Also what version of python are you using with your lambda function? It appears you have installed the asyncio package which shouldn't be needed for python 3.4+.

@basepi
Its not because of @logger_wrap , its because of asyncio package
If I remove asyncio package, then it's working absolutely fine
Lambda version is python 3.9
Don't we need asyncio package for python versions 3.4+?

Thanks

1 Like

@basepi Thank you
we got the answers of all queries

2 Likes

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.