|
| 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 time |
| 17 | +import unittest |
| 18 | + |
| 19 | +import mysql.connector |
| 20 | + |
| 21 | +from opentelemetry import trace as trace_api |
| 22 | +from opentelemetry.ext.mysql 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 | +MYSQL_USER = os.getenv("MYSQL_USER ", "testuser") |
| 30 | +MYSQL_PASSWORD = os.getenv("MYSQL_PASSWORD ", "testpassword") |
| 31 | +MYSQL_HOST = os.getenv("MYSQL_HOST ", "localhost") |
| 32 | +MYSQL_PORT = int(os.getenv("MYSQL_PORT ", "3306")) |
| 33 | +MYSQL_DB_NAME = os.getenv("MYSQL_DB_NAME ", "opentelemetry-tests") |
| 34 | + |
| 35 | + |
| 36 | +class TestFunctionalMysql(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 = mysql.connector.connect( |
| 48 | + user=MYSQL_USER, |
| 49 | + password=MYSQL_PASSWORD, |
| 50 | + host=MYSQL_HOST, |
| 51 | + port=MYSQL_PORT, |
| 52 | + database=MYSQL_DB_NAME, |
| 53 | + ) |
| 54 | + cls._cursor = cls._connection.cursor() |
| 55 | + |
| 56 | + @classmethod |
| 57 | + def tearDownClass(cls): |
| 58 | + if cls._connection: |
| 59 | + cls._connection.close() |
| 60 | + |
| 61 | + def setUp(self): |
| 62 | + self._span_exporter.clear() |
| 63 | + |
| 64 | + def validate_spans(self): |
| 65 | + spans = self._span_exporter.get_finished_spans() |
| 66 | + self.assertEqual(len(spans), 2) |
| 67 | + for span in spans: |
| 68 | + if span.name == "rootSpan": |
| 69 | + root_span = span |
| 70 | + else: |
| 71 | + db_span = span |
| 72 | + self.assertIsInstance(span.start_time, int) |
| 73 | + self.assertIsInstance(span.end_time, int) |
| 74 | + self.assertIsNotNone(root_span) |
| 75 | + self.assertIsNotNone(db_span) |
| 76 | + self.assertEqual(root_span.name, "rootSpan") |
| 77 | + self.assertEqual(db_span.name, "mysql.opentelemetry-tests") |
| 78 | + self.assertIsNotNone(db_span.parent) |
| 79 | + self.assertEqual(db_span.parent.name, root_span.name) |
| 80 | + self.assertIs(db_span.kind, trace_api.SpanKind.CLIENT) |
| 81 | + self.assertEqual(db_span.attributes["db.instance"], MYSQL_DB_NAME) |
| 82 | + self.assertEqual(db_span.attributes["net.peer.name"], MYSQL_HOST) |
| 83 | + self.assertEqual(db_span.attributes["net.peer.port"], MYSQL_PORT) |
| 84 | + |
| 85 | + def test_execute(self): |
| 86 | + """Should create a child span for execute |
| 87 | + """ |
| 88 | + with self._tracer.start_as_current_span("rootSpan"): |
| 89 | + self._cursor.execute("CREATE TABLE IF NOT EXISTS test (id INT)") |
| 90 | + self.validate_spans() |
| 91 | + |
| 92 | + def test_executemany(self): |
| 93 | + """Should create a child span for executemany |
| 94 | + """ |
| 95 | + with self._tracer.start_as_current_span("rootSpan"): |
| 96 | + data = ["1", "2", "3"] |
| 97 | + stmt = "INSERT INTO test (id) VALUES (%s)" |
| 98 | + self._cursor.executemany(stmt, data) |
| 99 | + self.validate_spans() |
| 100 | + |
| 101 | + def test_callproc(self): |
| 102 | + """Should create a child span for callproc |
| 103 | + """ |
| 104 | + with self._tracer.start_as_current_span("rootSpan"), self.assertRaises( |
| 105 | + Exception |
| 106 | + ): |
| 107 | + self._cursor.callproc("test", ()) |
| 108 | + self.validate_spans() |
0 commit comments