|
| 1 | +# Copyright The 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 unittest |
| 17 | + |
| 18 | +import pytest |
| 19 | +from sqlalchemy.exc import ProgrammingError |
| 20 | + |
| 21 | +from opentelemetry import trace |
| 22 | +from opentelemetry.semconv.trace import SpanAttributes |
| 23 | + |
| 24 | +from .mixins import Player, SQLAlchemyTestMixin |
| 25 | + |
| 26 | +MSSQL_CONFIG = { |
| 27 | + "host": "127.0.0.1", |
| 28 | + "port": int(os.getenv("TEST_MSSQL_PORT", "1433")), |
| 29 | + "user": os.getenv("TEST_MSSQL_USER", "sa"), |
| 30 | + "password": os.getenv("TEST_MSSQL_PASSWORD", "yourStrong(!)Password"), |
| 31 | + "database": os.getenv("TEST_MSSQL_DATABASE", "opentelemetry-tests"), |
| 32 | + "driver": os.getenv("TEST_MSSQL_DRIVER", "ODBC+Driver+17+for+SQL+Server"), |
| 33 | +} |
| 34 | + |
| 35 | + |
| 36 | +class MssqlConnectorTestCase(SQLAlchemyTestMixin): |
| 37 | + """TestCase for pyodbc engine""" |
| 38 | + |
| 39 | + __test__ = True |
| 40 | + |
| 41 | + VENDOR = "mssql" |
| 42 | + SQL_DB = "opentelemetry-tests" |
| 43 | + ENGINE_ARGS = { |
| 44 | + "url": "mssql+pyodbc://%(user)s:%(password)s@%(host)s:%(port)s/%(database)s?driver=%(driver)s" |
| 45 | + % MSSQL_CONFIG |
| 46 | + } |
| 47 | + |
| 48 | + def check_meta(self, span): |
| 49 | + # check database connection tags |
| 50 | + self.assertEqual( |
| 51 | + span.attributes.get(SpanAttributes.NET_PEER_NAME), |
| 52 | + MSSQL_CONFIG["host"], |
| 53 | + ) |
| 54 | + self.assertEqual( |
| 55 | + span.attributes.get(SpanAttributes.NET_PEER_PORT), |
| 56 | + MSSQL_CONFIG["port"], |
| 57 | + ) |
| 58 | + self.assertEqual( |
| 59 | + span.attributes.get(SpanAttributes.DB_NAME), |
| 60 | + MSSQL_CONFIG["database"], |
| 61 | + ) |
| 62 | + self.assertEqual( |
| 63 | + span.attributes.get(SpanAttributes.DB_USER), MSSQL_CONFIG["user"] |
| 64 | + ) |
| 65 | + |
| 66 | + def test_engine_execute_errors(self): |
| 67 | + # ensures that SQL errors are reported |
| 68 | + with pytest.raises(ProgrammingError): |
| 69 | + with self.connection() as conn: |
| 70 | + conn.execute("SELECT * FROM a_wrong_table").fetchall() |
| 71 | + |
| 72 | + spans = self.memory_exporter.get_finished_spans() |
| 73 | + self.assertEqual(len(spans), 1) |
| 74 | + span = spans[0] |
| 75 | + # span fields |
| 76 | + self.assertEqual(span.name, "SELECT opentelemetry-tests") |
| 77 | + self.assertEqual( |
| 78 | + span.attributes.get(SpanAttributes.DB_STATEMENT), |
| 79 | + "SELECT * FROM a_wrong_table", |
| 80 | + ) |
| 81 | + self.assertEqual( |
| 82 | + span.attributes.get(SpanAttributes.DB_NAME), self.SQL_DB |
| 83 | + ) |
| 84 | + self.check_meta(span) |
| 85 | + self.assertTrue(span.end_time - span.start_time > 0) |
| 86 | + # check the error |
| 87 | + self.assertIs( |
| 88 | + span.status.status_code, trace.StatusCode.ERROR, |
| 89 | + ) |
| 90 | + self.assertIn("a_wrong_table", span.status.description) |
| 91 | + |
| 92 | + def test_orm_insert(self): |
| 93 | + # ensures that the ORM session is traced |
| 94 | + wayne = Player(id=1, name="wayne") |
| 95 | + self.session.add(wayne) |
| 96 | + self.session.commit() |
| 97 | + |
| 98 | + spans = self.memory_exporter.get_finished_spans() |
| 99 | + # identity insert on before the insert, insert, and identity insert off after the insert |
| 100 | + self.assertEqual(len(spans), 3) |
| 101 | + span = spans[1] |
| 102 | + self._check_span(span, "INSERT") |
| 103 | + self.assertIn( |
| 104 | + "INSERT INTO players", |
| 105 | + span.attributes.get(SpanAttributes.DB_STATEMENT), |
| 106 | + ) |
| 107 | + self.check_meta(span) |
0 commit comments