# Getting and Setting Transaction ID

**URL:** <https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947>\
**Category:** APM\
**Tags:** python\
**Created:** [September 3, 2019, 8:55pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947 "2019-09-03T20:55:59Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alsheh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alsheh/32/36799_2.png) [@Alsheh](https://discuss.elastic.co/u/Alsheh)\
**Post date:** [September 3, 2019, 8:55pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947/1 "2019-09-03T20:55:59Z")

</div>

We're using the APM Python Flask agent to trace HTTP transactions and collect service metrics and Filebeat+Logstash for collecting service logs. We would like to link logs with the APM metrics:

1. Using the APM transaction ID seems like a good idea but how the transaction ID can be accessed using the APM Flask agent?
2. Moreover, we'd like to replace the APM transaction ID with the Nginx $request\_id since Nginx is higher in the stack. This way, Nginx logs, APM metrics, and service logs can all be linked using the Nignx $request\_id. How the APM transaction ID can be set using the APM Flask agent such that the set transaction ID gets propagated to the downstream services?
3. Any recommended ways to implement 1 and 2?
4. Any pitfalls that we need to be aware of when setting and getting a transaction ID?

---

<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:** [September 3, 2019, 9:16pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947/2 "2019-09-03T21:16:52Z")

</div>

Transaction ID in logs is something we definitely want to add/make easier -- in fact, we have [https://github.com/elastic/apm-agent-python/issues/520](https://github.com/elastic/apm-agent-python/issues/520) open already. I'm not certain whether there's a more manual way to do it in the current version -- @beniwohli ?

On (2), I'm not sure if there's a way for us to capture the Nginx request ID, I'll leave that to @beniwohli as well.

---

<div class="post-metadata">

**Author:** ![Alsheh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alsheh/32/36799_2.png) [@Alsheh](https://discuss.elastic.co/u/Alsheh)\
**Post date:** [September 3, 2019, 9:31pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947/3 "2019-09-03T21:31:21Z")

</div>

Thanks for the quick response, @basepi!

For 1 and 2, I was hoping to learn how to access and set the transaction ID in the code, something like this:

```auto
...
transaction = execution_context.get_transaction()
# get transation id
old_trans_id = transaction.get_transaction_id()
# set transaction id
new_trans_id = '1234567890'
transaction.set_transaction_id(new_trans_id)
...

```

Currently, we're passing the Nginx request ID in a custom header, and, supporting that out of the box would be nice, but, for the time being, we would be interested to manually set the Nginx request id as an APM transaction id using the transaction id setter.

---

<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:** [September 3, 2019, 9:47pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947/4 "2019-09-03T21:47:24Z")

</div>

Ah! I think that's actually pretty easy. I think when you are talking about "transaction ID", I think what you actually want is "transaction name". Turns out there's a nice API call for just this purpose: [https://www.elastic.co/guide/en/apm/agent/python/current/api.html#api-set-transaction-name](https://www.elastic.co/guide/en/apm/agent/python/current/api.html#api-set-transaction-name)

I think that will get you where you want to go.

---

<div class="post-metadata">

**Author:** ![Alsheh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alsheh/32/36799_2.png) [@Alsheh](https://discuss.elastic.co/u/Alsheh)\
**Post date:** [September 3, 2019, 10:29pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947/5 "2019-09-03T22:29:38Z")

</div>

Actually, I would like to keep the transaction name as is which usually corresponds to `<HTTP-method> <API-Endpoint>` for my micro-service; I have looked at the docs you provided for an API call that can do `elasticapm.get_transaction_id()` and `elasticapm.set_transaction_id()` but I couldn't find any. I'm wondering if there is any undocumented API call that can be used to set and get the transaction id?

---

<div class="post-metadata">

**Author:** ![beniwohli](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/beniwohli/32/21256_2.png) [@beniwohli](https://discuss.elastic.co/u/beniwohli)\
**Post date:** [September 4, 2019, 1:28pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947/6 "2019-09-04T13:28:17Z")

</div>

Hi @Alsheh

custom transaction IDs is not something we plan to support I'm afraid. The transaction and trace IDs follow the format defined in the [W3C Trace Context](https://www.w3.org/TR/trace-context/#traceparent-header) standard. Providing APIs to change the ID would risk breaking our conformity with that standard.

Instead, I suggest to tag the transaction with the nginx request ID, something like (assuming you configured nginx to add an "nginx-request-id" header to the request)

```
import elasticapm

elasticapm.tag(nginx_request_id=request.headers["nginx-request-id"])

```

This code could e.g. be hooked up to the [`request_started`](https://flask.palletsprojects.com/en/1.1.x/api/#flask.request_started) signal. Just make sure to only register the signal after the `ElasticAPM` call, this ensures that our signal handler (which creates the transaction object) is called before your handler (which then tags said transaction object).

---

<div class="post-metadata">

**Author:** ![Alsheh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alsheh/32/36799_2.png) [@Alsheh](https://discuss.elastic.co/u/Alsheh)\
**Post date:** [September 4, 2019, 6:12pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947/7 "2019-09-04T18:12:07Z")

</div>

@beniwohli this is useful, thanks! I see how setting a custom trace ID could result in unexpected results but would it be possible to get the generated APM trace ID in the code for logging purposes?

---

<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:** [September 4, 2019, 8:38pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947/8 "2019-09-04T20:38:59Z")

</div>

That's where that [issue link I noted above](https://github.com/elastic/apm-agent-python/issues/520) comes in. We're going to provide a logging filter that will add the transaction ID and other useful information into your log records, so you can format them into your logs. Keep an eye on that issue for more details.

---

<div class="post-metadata">

**Author:** ![Alsheh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alsheh/32/36799_2.png) [@Alsheh](https://discuss.elastic.co/u/Alsheh)\
**Post date:** [September 4, 2019, 9:08pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947/9 "2019-09-04T21:08:39Z")

</div>

@basepi thanks for the clarification! Until a PR is merged for the logging filter, isn't there a way to get the trace id from a transaction so I can manually add it to the log record?

---

<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:** [September 4, 2019, 10:30pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947/10 "2019-09-04T22:30:56Z")

</div>

Yep. It's a touch hairy, since it's not designed as a public API, but here's an example (untested, I'm in the process of implementing that issue we've been discussing):

```
from elasticapm.traces import execution_context

transaction = execution_context.get_transaction()
id = transaction.id
trace = transaction.trace_parent.trace_id if transaction.trace_parent else None
```

---

<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:** [September 17, 2019, 3:16pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947/11 "2019-09-17T15:16:53Z")

</div>

I just wanted to update that we just merged some public API helpers for these IDs, as well as some logging filters and a structlog processor that can make adding these IDs to your logs super easy. [https://github.com/elastic/apm-agent-python/pull/586](https://github.com/elastic/apm-agent-python/pull/586)

---

<div class="post-metadata">

**Author:** ![Alsheh](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alsheh/32/36799_2.png) [@Alsheh](https://discuss.elastic.co/u/Alsheh)\
**Post date:** [September 24, 2019, 8:44pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947/12 "2019-09-24T20:44:15Z")

</div>

Hi @basepi! thanks for the update! I found the update on the [documentation](https://www.elastic.co/guide/en/apm/agent/python/master/logging-integrations.html). It looks like it hasn't been released, yet, any ideas when to expect the release that will have these logging updates?

---

<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:** [September 24, 2019, 9:28pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947/13 "2019-09-24T21:28:48Z")

</div>

Yep! We'll be releasing alongside the Elastic Stack 7.4.0 release, which we are expecting soon.

---

<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:** [October 15, 2019, 5:31pm UTC](https://discuss.elastic.co/t/getting-and-setting-transaction-id/197947/14 "2019-10-15T17:31:10Z")

</div>

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