# What's hidden in the psycopg2 connect span?

**URL:** <https://discuss.elastic.co/t/whats-hidden-in-the-psycopg2-connect-span/255700>\
**Category:** APM\
**Tags:** python\
**Created:** [November 17, 2020, 12:58pm UTC](https://discuss.elastic.co/t/whats-hidden-in-the-psycopg2-connect-span/255700 "2020-11-17T12:58:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rodrigo\_Jimenez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rodrigo_jimenez/32/34722_2.png) [@Rodrigo\_Jimenez](https://discuss.elastic.co/u/Rodrigo_Jimenez)\
**Post date:** [November 17, 2020, 12:58pm UTC](https://discuss.elastic.co/t/whats-hidden-in-the-psycopg2-connect-span/255700/1 "2020-11-17T12:58:31Z")

</div>

Hi!, this is probably a weird one but I need to know. Recently our API needed to connect to an external cloud (api's at AWS, psql db is at azure).  
We found that the **connect** span took more than a second to complete. That could be explained by inter cloud latency, but.. more than a second?! And what's more confusing is the fact that queries made to the same db instance run relatively fast (200ms avg)

So my question is, what's involved in that span that could explain this latency?

BTW: It's not DNS , I checked.

Thanks!

---

<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:** [November 17, 2020, 1:17pm UTC](https://discuss.elastic.co/t/whats-hidden-in-the-psycopg2-connect-span/255700/2 "2020-11-17T13:17:41Z")

</div>

Hey @Rodrigo_Jimenez

The `connect` span represents the time it takes to call the [`psycopg2.connect()`](https://www.psycopg.org/docs/module.html#psycopg2.connect) function. I can't really say what exactly could be slow there, as I'm not familiar enough with the internals of psycopg2. However, I do know that connecting to a PostgreSQL database can be relatively heavy, especially between cloud providers. Did you already look into connection pooling (e.g. [pgbouncer](https://www.pgbouncer.org/)) or persistent connections? That could limit the impact of latency while establishing the connection.

Cheers  
Beni

---

<div class="post-metadata">

**Author:** ![Rodrigo\_Jimenez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rodrigo_jimenez/32/34722_2.png) [@Rodrigo\_Jimenez](https://discuss.elastic.co/u/Rodrigo_Jimenez)\
**Post date:** [November 17, 2020, 6:49pm UTC](https://discuss.elastic.co/t/whats-hidden-in-the-psycopg2-connect-span/255700/3 "2020-11-17T18:49:32Z")

</div>

Thanks.  
I was actually trying **not** to use pg\_bouncer or similiar for now until I know wha's up.  
May be this is a delay I cant prevent and the only solution is connection pooling.  
I can't even sniff this traffic since it's all TLS.  
Thanks again!

---

<div class="post-metadata">

**Author:** ![Rodrigo\_Jimenez](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rodrigo_jimenez/32/34722_2.png) [@Rodrigo\_Jimenez](https://discuss.elastic.co/u/Rodrigo_Jimenez)\
**Post date:** [November 24, 2020, 6:56pm UTC](https://discuss.elastic.co/t/whats-hidden-in-the-psycopg2-connect-span/255700/4 "2020-11-24T18:56:54Z")

</div>

I'm not sure what's causing these big delays but thanks for answering anyways.  
I'll mark the solution.

---

<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:** [December 15, 2020, 2:57pm UTC](https://discuss.elastic.co/t/whats-hidden-in-the-psycopg2-connect-span/255700/5 "2020-12-15T14:57:05Z")

</div>

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