Skip to content

Commit

Permalink
Override __new__ to instrument multiple engines
Browse files Browse the repository at this point in the history
  • Loading branch information
hemma committed Jun 13, 2022
1 parent 51ba801 commit 4234bf3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ class SQLAlchemyInstrumentor(BaseInstrumentor):
See `BaseInstrumentor`
"""

def __new__(cls, *args, **kwargs):
return object.__new__(cls, *args, **kwargs)

def instrumentation_dependencies(self) -> Collection[str]:
return _instruments

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,28 @@ def test_trace_integration(self):
self.assertEqual(spans[0].name, "SELECT :memory:")
self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT)

def test_instrument_two_engines(self):
engine_1 = create_engine("sqlite:///:memory:")
engine_2 = create_engine("sqlite:///:memory:")

SQLAlchemyInstrumentor().instrument(
engine=engine_1,
tracer_provider=self.tracer_provider,
)
cnx_1 = engine_1.connect()
cnx_1.execute("SELECT 1 + 1;").fetchall()

SQLAlchemyInstrumentor().instrument(
engine=engine_2,
tracer_provider=self.tracer_provider,
)
cnx_2 = engine_2.connect()
cnx_2.execute("SELECT 1 + 1;").fetchall()

spans = self.memory_exporter.get_finished_spans()

self.assertEqual(len(spans), 2)

@pytest.mark.skipif(
not sqlalchemy.__version__.startswith("1.4"),
reason="only run async tests for 1.4",
Expand Down

0 comments on commit 4234bf3

Please sign in to comment.