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

**URL:** <https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320>\
**Category:** APM\
**Tags:** python, aws-lambda\
**Created:** [January 31, 2023, 11:21am UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320 "2023-01-31T11:21:24Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pratiksha\_Nagoshe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratiksha_nagoshe/32/99920_2.png) [@Pratiksha\_Nagoshe](https://discuss.elastic.co/u/Pratiksha_Nagoshe)\
**Post date:** [January 31, 2023, 11:21am UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/1 "2023-01-31T11:21:24Z")

</div>

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 🙂

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](https://www.elastic.co/guide/en/apm/get-started/current/troubleshooting.html) 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)**:

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [January 31, 2023, 6:01pm UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/2 "2023-01-31T18:01:02Z")

</div>

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](https://www.elastic.co/guide/en/apm/agent/python/current/lambda-support.html)

---

<div class="post-metadata">

**Author:** ![Pratiksha\_Nagoshe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratiksha_nagoshe/32/99920_2.png) [@Pratiksha\_Nagoshe](https://discuss.elastic.co/u/Pratiksha_Nagoshe)\
**Post date:** [February 1, 2023, 4:56am UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/3 "2023-02-01T04:56:08Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [February 1, 2023, 5:52pm UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/4 "2023-02-01T17:52:37Z")

</div>

[`elasticapm.label()`](https://www.elastic.co/guide/en/apm/agent/python/current/api.html#api-label) should get you there.

---

<div class="post-metadata">

**Author:** ![Pratiksha\_Nagoshe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratiksha_nagoshe/32/99920_2.png) [@Pratiksha\_Nagoshe](https://discuss.elastic.co/u/Pratiksha_Nagoshe)\
**Post date:** [February 2, 2023, 5:40am UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/5 "2023-02-02T05:40:00Z")

</div>

Thank you @basepi

---

<div class="post-metadata">

**Author:** ![Pratiksha\_Nagoshe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratiksha_nagoshe/32/99920_2.png) [@Pratiksha\_Nagoshe](https://discuss.elastic.co/u/Pratiksha_Nagoshe)\
**Post date:** [February 3, 2023, 1:58pm UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/6 "2023-02-03T13:58:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [February 6, 2023, 6:19pm UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/7 "2023-02-06T18:19:34Z")

</div>

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](https://www.elastic.co/guide/en/apm/agent/python/current/lambda-support.html).

---

<div class="post-metadata">

**Author:** ![Pratiksha\_Nagoshe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratiksha_nagoshe/32/99920_2.png) [@Pratiksha\_Nagoshe](https://discuss.elastic.co/u/Pratiksha_Nagoshe)\
**Post date:** [February 7, 2023, 4:42am UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/8 "2023-02-07T04:42:22Z")

</div>

@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): "}

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [February 7, 2023, 5:41pm UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/9 "2023-02-07T17:41:19Z")

</div>

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

```auto
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?

---

<div class="post-metadata">

**Author:** ![Pratiksha\_Nagoshe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratiksha_nagoshe/32/99920_2.png) [@Pratiksha\_Nagoshe](https://discuss.elastic.co/u/Pratiksha_Nagoshe)\
**Post date:** [February 8, 2023, 5:22pm UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/10 "2023-02-08T17:22:12Z")

</div>

@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")

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [February 8, 2023, 8:33pm UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/11 "2023-02-08T20:33:53Z")

</div>

Try `elasticapm.label(key="value")` instead. [docs](https://www.elastic.co/guide/en/apm/agent/python/current/api.html#api-label)

---

<div class="post-metadata">

**Author:** ![Pratiksha\_Nagoshe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratiksha_nagoshe/32/99920_2.png) [@Pratiksha\_Nagoshe](https://discuss.elastic.co/u/Pratiksha_Nagoshe)\
**Post date:** [February 11, 2023, 8:01am UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/12 "2023-02-11T08:01:32Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [February 13, 2023, 6:34pm UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/13 "2023-02-13T18:34:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Pratiksha\_Nagoshe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratiksha_nagoshe/32/99920_2.png) [@Pratiksha\_Nagoshe](https://discuss.elastic.co/u/Pratiksha_Nagoshe)\
**Post date:** [February 14, 2023, 4:17am UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/14 "2023-02-14T04:17:17Z")

</div>

@basepi please check below code

```auto
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 

```

```auto
@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

---

<div class="post-metadata">

**Author:** ![basepi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/basepi/32/53056_2.png) [@basepi](https://discuss.elastic.co/u/basepi)\
**Post date:** [February 14, 2023, 6:05pm UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/15 "2023-02-14T18:05:55Z")

</div>

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](https://pypi.org/project/asyncio/) which shouldn't be needed for python 3.4+.

---

<div class="post-metadata">

**Author:** ![Pratiksha\_Nagoshe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratiksha_nagoshe/32/99920_2.png) [@Pratiksha\_Nagoshe](https://discuss.elastic.co/u/Pratiksha_Nagoshe)\
**Post date:** [February 15, 2023, 4:10am UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/16 "2023-02-15T04:10:11Z")

</div>

@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

---

<div class="post-metadata">

**Author:** ![Pratiksha\_Nagoshe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pratiksha_nagoshe/32/99920_2.png) [@Pratiksha\_Nagoshe](https://discuss.elastic.co/u/Pratiksha_Nagoshe)\
**Post date:** [February 21, 2023, 4:21am UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/17 "2023-02-21T04:21:16Z")

</div>

@basepi Thank you  
we got the answers of all queries

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [March 14, 2023, 12:22am UTC](https://discuss.elastic.co/t/how-to-start-and-end-transaction-in-python-aws-lambda-function-also-how-to-add-labels-between-transaction/324320/18 "2023-03-14T00:22:14Z")

</div>

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