|
31 | 31 | # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
32 | 32 |
|
33 | 33 | import os
|
| 34 | +from typing import cast |
34 | 35 |
|
35 | 36 | import pytest
|
36 | 37 |
|
37 |
| -from elasticapm.conf.constants import TRANSACTION |
38 |
| -from elasticapm.instrumentation.packages.psycopg2 import PGCursorProxy, extract_signature |
| 38 | +from elasticapm import get_client |
| 39 | +from elasticapm.conf.constants import SPAN, TRANSACTION |
| 40 | +from elasticapm.instrumentation.packages.psycopg2 import PGCursorProxy, extract_signature, get_destination_info |
39 | 41 | from elasticapm.utils import default_ports
|
| 42 | +from tests.fixtures import TempStoreClient |
40 | 43 |
|
41 | 44 | psycopg2 = pytest.importorskip("psycopg2")
|
42 | 45 |
|
@@ -162,10 +165,10 @@ def test_select_with_dollar_quotes_custom_token():
|
162 | 165 |
|
163 | 166 |
|
164 | 167 | def test_select_with_difficult_table_name():
|
165 |
| - sql_statement = u"""SELECT id FROM "myta\n-æøåble" WHERE id = 2323""" |
| 168 | + sql_statement = """SELECT id FROM "myta\n-æøåble" WHERE id = 2323""" |
166 | 169 | actual = extract_signature(sql_statement)
|
167 | 170 |
|
168 |
| - assert u"SELECT FROM myta\n-æøåble" == actual |
| 171 | + assert "SELECT FROM myta\n-æøåble" == actual |
169 | 172 |
|
170 | 173 |
|
171 | 174 | def test_select_subselect():
|
@@ -507,3 +510,30 @@ def test_psycopg2_execute_values(instrument, postgres_connection, elasticapm_cli
|
507 | 510 | spans = elasticapm_client.spans_for_transaction(transactions[0])
|
508 | 511 | assert spans[0]["name"] == "INSERT INTO test"
|
509 | 512 | assert len(spans[0]["context"]["db"]["statement"]) == 10000, spans[0]["context"]["db"]["statement"]
|
| 513 | + |
| 514 | + |
| 515 | +@pytest.mark.integrationtest |
| 516 | +def test_psycopg2_connection(instrument, elasticapm_transaction, postgres_connection): |
| 517 | + # elastciapm_client.events is only available on `TempStoreClient`, this keeps the type checkers happy |
| 518 | + elasticapm_client = cast(TempStoreClient, get_client()) |
| 519 | + elasticapm_client.end_transaction("test", "success") |
| 520 | + span = elasticapm_client.events[SPAN][0] |
| 521 | + host = os.environ.get("POSTGRES_HOST", "localhost") |
| 522 | + assert span["name"] == f"psycopg2.connect {host}:5432" |
| 523 | + assert span["action"] == "connect" |
| 524 | + |
| 525 | + |
| 526 | +@pytest.mark.parametrize( |
| 527 | + "host_in,port_in,expected_host_out,expected_port_out", |
| 528 | + ( |
| 529 | + (None, None, "localhost", 5432), |
| 530 | + (None, 5432, "localhost", 5432), |
| 531 | + ("localhost", "5432", "localhost", 5432), |
| 532 | + ("foo.bar", "5432", "foo.bar", 5432), |
| 533 | + ("localhost,foo.bar", "5432,1234", "localhost", 5432), # multiple hosts |
| 534 | + ), |
| 535 | +) |
| 536 | +def test_get_destination(host_in, port_in, expected_host_out, expected_port_out): |
| 537 | + host, port = get_destination_info(host_in, port_in) |
| 538 | + assert host == expected_host_out |
| 539 | + assert port == expected_port_out |
0 commit comments