# Python ZOPE Application Framework Integration to Elastic APM

**URL:** <https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277>\
**Category:** APM\
**Tags:** python\
**Created:** [September 27, 2022, 2:44pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277 "2022-09-27T14:44:11Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [October 6, 2022, 1:50pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/21 "2022-10-06T13:50:47Z")

</div>

hi @basepi ,

Implemented as per your suggestion, please review below and let me know any changes required ---

```auto
#@elasticapm.capture_span()
    def do_execute(self, cursor, statement, parameters, context=None):
        #client.begin_transaction('SQL_MXodbc_query')
        with elasticapm.capture_span("sql_statement", span_type='db', span_subtype='sql', span_action='query', extra={
                "db": {"type": "sql", "statement": statement, "instance": getattr(self, "_self_database", None)},
            },skip_frames=1,
            leaf=True):
           cursor.execute(statement, parameters, direct=self._get_direct(context))
        #client.end_transaction("MXODBC_"+statement, "Success")

```

OUTPUT looks like this ----

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/e/2/e25dea6c456ba6d2d32f07f4261522dac1b6d47e.png)

![image](https://us1.discourse-cdn.com/elastic/original/3X/6/6/66451fb9a4f1ce467e33752f0b5e0291fd979c20.png)

---

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [October 6, 2022, 1:54pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/22 "2022-10-06T13:54:29Z")

</div>

hi @basepi and @lwintergerst ,

What is the procedure to go for LOG INTEGRATION (Application LOGS) and tie them to the request.

I have application logs (FLASK) and want to trace the API logs with the request?

Thanks,  
Dixit

---

<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:** [October 6, 2022, 8:59pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/23 "2022-10-06T20:59:10Z")

</div>

Glad you got the sql statement working.

> [@Dixit](#):
>
> What is the procedure to go for LOG INTEGRATION (Application LOGS) and tie them to the request.
> 
> I have application logs (FLASK) and want to trace the API logs with the request?

You can find documentation about log correlation here: [Log correlation | APM Python Agent Reference [6.x] | Elastic](https://www.elastic.co/guide/en/apm/agent/python/current/log-correlation.html)

The tl;dr is that you want to use `ecs-logging` package to format your logs correctly, and then use Elastic Agent or Filebeat to ingest those logs. Then they should show up in the APM logs view.

---

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [October 7, 2022, 6:21am UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/24 "2022-10-07T06:21:44Z")

</div>

hi @basepi ,

Any sample code would help for LOG integration of Python Custom Integration and Flask based Log integration.

Referred, somewhat it's confusing about how - [Log correlation | APM Python Agent Reference [6.x] | Elastic](https://www.elastic.co/guide/en/apm/agent/python/current/log-correlation.html)

Thanks,  
Dixit

---

<div class="post-metadata">

**Author:** ![lwintergerst](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/lwintergerst/32/18164_2.png) [@lwintergerst](https://discuss.elastic.co/u/lwintergerst)\
**Post date:** [October 10, 2022, 1:00pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/25 "2022-10-10T13:00:17Z")

</div>

Hi, you can take a look at this example that I shared during the webinar

> <https://github.com/LucaWintergerst/observability-example/blob/main/services/06-app-ecs-logging.py>

I recommend doing it like this, and then as Colton said use Elastic Agent to collect the files

---

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [October 10, 2022, 2:19pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/26 "2022-10-10T14:19:05Z")

</div>

hi @lwintergerst and @basepi ,

\*\*Please let me know - log integration for CUSTOM IMPLEMENTATION for the above code/application what I have asked for HELP. \*\*

**I have log files in CUSTOM Implementation, and these log files I have to read and correlate.**

Can you please help me with a SAMPLE or EXAMPLE like above would help me to grasp .

Flask based am comfortable with now.

Appreciate your help once again here , Thank you @lwintergerst@basepi

Thanks,  
Dixit

---

<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:** [October 10, 2022, 4:07pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/27 "2022-10-10T16:07:44Z")

</div>

@Dixit have you tried applying the examples that we've given to your app? We're happy to help with issues that arise, but unfortunately we don't have the time to integrate it for you.

The way I recommend doing it is [implementing `structlog` in your app](https://www.structlog.org/en/stable/) (use their linked documentation).

Then it should be as easy as adding two processors, [the one from `elasticapm`](https://www.elastic.co/guide/en/apm/agent/python/current/log-correlation.html#structlog), and [the one from `ecs-logging`](https://www.elastic.co/guide/en/ecs-logging/python/current/installation.html#structlog).

---

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [October 10, 2022, 4:46pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/28 "2022-10-10T16:46:53Z")

</div>

Hi @basepi ,

Thanks for your suggestion. I am definitely not expecting for an integration help as it would take your valuable time, I don't want to waste your time.

Need help on sample integration for LOGS - Custom Integrations done in your past so that I can map a reference and try to work on the same lines.

Just a sample copy paste would help me to achieve Elastic APM POC goal and propose for my management for tool adoption .

Hope you can get my point !!

Again thanks for helping me all round !! @basepi @lwintergerst

Thanks,  
Dixit

---

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [October 28, 2022, 2:07pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/29 "2022-10-28T14:07:41Z")

</div>

hi @lwintergerst and @basepi ,

What is the PRODUCTION OVERHEAD for ELASTIC APM ? Do we have any benchmarks like 5 % or 10% etc.. May be sampling/Instrumentation rate would decide

Thanks,  
Dixit

---

<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:** [October 28, 2022, 5:55pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/30 "2022-10-28T17:55:33Z")

</div>

It's really difficult to measure, but I would say 5% is a pretty reasonable estimate. We work really hard to keep it very lightweight.

---

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [October 29, 2022, 12:07am UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/31 "2022-10-29T00:07:23Z")

</div>

Any examples of measurement would help @basepi

---

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [November 2, 2022, 4:30pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/32 "2022-11-02T16:30:01Z")

</div>

hi @basepi and @lwintergerst ,

- Is there any way we can truncate the URL in the transactions list so that LONG URLS which has repetitive domain names are removed .

- In my custom code instrumentation , the request comes through Apache Web Server and ZOPE captures it , the same request details are sniffed by Python Agent and produces the result/transaction details to APM Server.

**For Ex : - My request looks like this -**

- `/VirtualHostBase/https/perf65ci.abc.org:443/cms/ZeUI/VirtualHostRoot/cms/ZeUI/views/Sentinel/SREBase `

- which is lengthier and have redundant path because of APACHE passing to ZOPE .

Currently the client.end\_transaction is like this -

- `client.end_transaction(request.method + " " + request.get('PATH_INFO'), response.status)`

_ **How can I truncate/remove the part which is from APACHE and Domain , only actual path am interested in.** _

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/c/4c3a92569eedcfd477662ec9fe8c9d40c69ea938.png)

Request your help here please @basepi@lwintergerst

Thank you,

Thanks,  
Dixit

---

<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:** [November 2, 2022, 4:54pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/33 "2022-11-02T16:54:51Z")

</div>

When we're integrating with a framework, typically we like to name the transaction based on the route, not the URL, so that we don't run into cardinality issues like you're seeing. You can probably use a `.split()` or even a regex to trim your transactions to the relevant information.

---

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [November 2, 2022, 6:28pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/34 "2022-11-02T18:28:36Z")

</div>

Any example would help @basepi @lwintergerst

Tried with this and worked out - `client.end_transaction(request.method + " " + (request["ACTUAL_URL"]).split("/",3)[3], response.status)`

thanks for giving me pointers !! @basepi@lwintergerst

---

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [November 3, 2022, 3:29pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/35 "2022-11-03T15:29:17Z")

</div>

Hi @basepi & @lwintergerst ,

How to track the requests which are Served from **Kubernetes K8's ,** is there any **agent code we can incorporate where in we get to know how K8's** perform & get the metrics as well

Thanks,  
Dixit

---

<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:** [November 3, 2022, 5:43pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/36 "2022-11-03T17:43:12Z")

</div>

We gather a little bit of kubernetes information by default, it should be in the metadata for the transactions. Basically we collect the cgroup metadata for k8s and enrich with environment variables: [apm-agent-python/base.py at 98db6462cdb868862f2d96b715fa8b1c01a27274 · elastic/apm-agent-python · GitHub](https://github.com/elastic/apm-agent-python/blob/98db6462cdb868862f2d96b715fa8b1c01a27274/elasticapm/base.py#L378-L398)

---

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [November 7, 2022, 5:42pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/37 "2022-11-07T17:42:47Z")

</div>

hi @basepi and @lwintergerst ,

I went through ecs\_logging & struct\_log, somewhat am finding hard to understand.

Need your help please.

In custom instarumentation using "Client" as done above to build traces, how we can attach the exception to the span itself, just like how we can see in flask based elastic APM instrumentation ?

How to list down errors (mostly in logs section , the errors , exception exactly related to the particular transaction)

The code is :

```auto
client.begin_transaction('request')
            response = publish(request, module_name, after_list, debug=debug)
            client.end_transaction(request.method + " " + request.get('PATH_INFO'), response.status)

```

Request life cycle looks like -

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/4/a/4a94571bfde6be7ee0b28dd4945bf6a2b5963052.png)

Appreciate your help here please @basepi @lwintergerst

---

<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:** [November 8, 2022, 8:09am UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/38 "2022-11-08T08:09:28Z")

</div>

Capturing errors is actually very easy! Just use [`elasticapm.capture_exception()`](https://www.elastic.co/guide/en/apm/agent/python/current/api.html#client-api-capture-exception) inside of your `try:except:` block.

---

<div class="post-metadata">

**Author:** ![Dixit](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dixit/32/111369_2.png) [@Dixit](https://discuss.elastic.co/u/Dixit)\
**Post date:** [November 8, 2022, 4:21pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/39 "2022-11-08T16:21:39Z")

</div>

Hi @basepi ,

Try except should be surrounding client.begin and end calls?

---

<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:** [November 9, 2022, 2:57pm UTC](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277/40 "2022-11-09T14:57:21Z")

</div>

No, typically you would surround the contents. Something like this:

```python
client.begin_transaction('request')
try:
    response = publish(request, module_name, after_list, debug=debug)
except Exception:
    elasticapm.capture_exception(handled=False)
    raise
finally:
    client.end_transaction(request.method + " " + request.get('PATH_INFO'), response.status)

```

[Previous page](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277.md?page=1)

[Next page](https://discuss.elastic.co/t/python-zope-application-framework-integration-to-elastic-apm/315277.md?page=3)
