Skip to content

Commit

Permalink
Use is_recording flag in aiopg, asyncpg, dbapi, psycopg2, pymemcache,…
Browse files Browse the repository at this point in the history
… pymongo, redis, sqlalchemy instrumentations (#1212)
  • Loading branch information
lzchen authored and codeboten committed Oct 22, 2020
1 parent 825adb8 commit 901980e
Showing 1 changed file with 23 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,29 @@ def test_instrumentor(self, mock_connect):
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)

@mock.patch("psycopg2.connect")
# pylint: disable=unused-argument
def test_not_recording(self, mock_connect):
mock_tracer = mock.Mock()
mock_span = mock.Mock()
mock_span.is_recording.return_value = False
mock_tracer.start_span.return_value = mock_span
mock_tracer.use_span.return_value.__enter__ = mock_span
mock_tracer.use_span.return_value.__exit__ = True
Psycopg2Instrumentor().instrument()
with mock.patch("opentelemetry.trace.get_tracer") as tracer:
tracer.return_value = mock_tracer
cnx = psycopg2.connect(database="test")
cursor = cnx.cursor()
query = "SELECT * FROM test"
cursor.execute(query)
self.assertFalse(mock_span.is_recording())
self.assertTrue(mock_span.is_recording.called)
self.assertFalse(mock_span.set_attribute.called)
self.assertFalse(mock_span.set_status.called)

Psycopg2Instrumentor().uninstrument()

@mock.patch("psycopg2.connect")
# pylint: disable=unused-argument
def test_custom_tracer_provider(self, mock_connect):
Expand Down

0 comments on commit 901980e

Please sign in to comment.