|
| 1 | +# Copyright 2020, OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import time |
| 17 | +import unittest |
| 18 | + |
| 19 | +import psycopg2 |
| 20 | + |
| 21 | +from opentelemetry import trace as trace_api |
| 22 | +from opentelemetry.ext.psycopg2 import trace_integration |
| 23 | +from opentelemetry.sdk.trace import Tracer, TracerProvider |
| 24 | +from opentelemetry.sdk.trace.export import SimpleExportSpanProcessor |
| 25 | +from opentelemetry.sdk.trace.export.in_memory_span_exporter import ( |
| 26 | + InMemorySpanExporter, |
| 27 | +) |
| 28 | + |
| 29 | +POSTGRES_HOST = os.getenv("POSTGRESQL_HOST ", "localhost") |
| 30 | +POSTGRES_PORT = int(os.getenv("POSTGRESQL_PORT ", "5432")) |
| 31 | +POSTGRES_DB_NAME = os.getenv("POSTGRESQL_DB_NAME ", "opentelemetry-tests") |
| 32 | +POSTGRES_PASSWORD = os.getenv("POSTGRESQL_HOST ", "testpassword") |
| 33 | +POSTGRES_USER = os.getenv("POSTGRESQL_HOST ", "testuser") |
| 34 | + |
| 35 | + |
| 36 | +class TestFunctionalPsycopg(unittest.TestCase): |
| 37 | + @classmethod |
| 38 | + def setUpClass(cls): |
| 39 | + cls._connection = None |
| 40 | + cls._cursor = None |
| 41 | + cls._tracer_provider = TracerProvider() |
| 42 | + cls._tracer = Tracer(cls._tracer_provider, None) |
| 43 | + cls._span_exporter = InMemorySpanExporter() |
| 44 | + cls._span_processor = SimpleExportSpanProcessor(cls._span_exporter) |
| 45 | + cls._tracer_provider.add_span_processor(cls._span_processor) |
| 46 | + trace_integration(cls._tracer) |
| 47 | + cls._connection = psycopg2.connect( |
| 48 | + dbname=POSTGRES_DB_NAME, |
| 49 | + user=POSTGRES_USER, |
| 50 | + password=POSTGRES_PASSWORD, |
| 51 | + host=POSTGRES_HOST, |
| 52 | + port=POSTGRES_PORT, |
| 53 | + ) |
| 54 | + cls._connection.set_session(autocommit=True) |
| 55 | + cls._cursor = cls._connection.cursor() |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def tearDownClass(cls): |
| 59 | + if cls._cursor: |
| 60 | + cls._cursor.close() |
| 61 | + if cls._connection: |
| 62 | + cls._connection.close() |
| 63 | + |
| 64 | + def setUp(self): |
| 65 | + self._span_exporter.clear() |
| 66 | + |
| 67 | + def validate_spans(self): |
| 68 | + spans = self._span_exporter.get_finished_spans() |
| 69 | + self.assertEqual(len(spans), 2) |
| 70 | + for span in spans: |
| 71 | + if span.name == "rootSpan": |
| 72 | + root_span = span |
| 73 | + else: |
| 74 | + child_span = span |
| 75 | + self.assertIsInstance(span.start_time, int) |
| 76 | + self.assertIsInstance(span.end_time, int) |
| 77 | + self.assertIsNotNone(root_span) |
| 78 | + self.assertIsNotNone(child_span) |
| 79 | + self.assertEqual(root_span.name, "rootSpan") |
| 80 | + self.assertEqual(child_span.name, "postgresql.opentelemetry-tests") |
| 81 | + self.assertIsNotNone(child_span.parent) |
| 82 | + self.assertEqual(child_span.parent.name, root_span.name) |
| 83 | + self.assertIs(child_span.kind, trace_api.SpanKind.CLIENT) |
| 84 | + self.assertEqual( |
| 85 | + child_span.attributes["db.instance"], POSTGRES_DB_NAME |
| 86 | + ) |
| 87 | + self.assertEqual(child_span.attributes["net.peer.name"], POSTGRES_HOST) |
| 88 | + self.assertEqual(child_span.attributes["net.peer.port"], POSTGRES_PORT) |
| 89 | + |
| 90 | + def test_execute(self): |
| 91 | + """Should create a child span for execute method |
| 92 | + """ |
| 93 | + with self._tracer.start_as_current_span("rootSpan"): |
| 94 | + self._cursor.execute( |
| 95 | + "CREATE TABLE IF NOT EXISTS test (id integer)" |
| 96 | + ) |
| 97 | + self.validate_spans() |
| 98 | + |
| 99 | + def test_executemany(self): |
| 100 | + """Should create a child span for executemany |
| 101 | + """ |
| 102 | + with self._tracer.start_as_current_span("rootSpan"): |
| 103 | + data = ("1", "2", "3") |
| 104 | + stmt = "INSERT INTO test (id) VALUES (%s)" |
| 105 | + self._cursor.executemany(stmt, data) |
| 106 | + self.validate_spans() |
| 107 | + |
| 108 | + def test_callproc(self): |
| 109 | + """Should create a child span for callproc |
| 110 | + """ |
| 111 | + with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( |
| 112 | + Exception |
| 113 | + ): |
| 114 | + self._cursor.callproc("test", ()) |
| 115 | + self.validate_spans() |
0 commit comments