Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
mnbbrown committed Jul 6, 2021
1 parent 06710d0 commit 67b6a42
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# from threading import local
from threading import local

from sqlalchemy.event import listen # pylint: disable=no-name-in-module

Expand Down Expand Up @@ -60,19 +60,19 @@ def __init__(self, tracer, engine):
self.engine = engine
self.vendor = _normalize_vendor(engine.name)
self.cursor_mapping = {}
# self.local = local()
self.local = local()

listen(engine, "before_cursor_execute", self._before_cur_exec)
listen(engine, "after_cursor_execute", self._after_cur_exec)
listen(engine, "handle_error", self._handle_error)

# @property
# def current_thread_span(self):
# return getattr(self.local, "current_span", None)
@property
def current_thread_span(self):
return getattr(self.local, "current_span", None)

# @current_thread_span.setter
# def current_thread_span(self, span):
# setattr(self.local, "current_span", span)
@current_thread_span.setter
def current_thread_span(self, span):
setattr(self.local, "current_span", span)

def _operation_name(self, db_name, statement):
parts = []
Expand Down Expand Up @@ -100,7 +100,7 @@ def _before_cur_exec(self, conn, cursor, statement, *args):
self._operation_name(db_name, statement),
kind=trace.SpanKind.CLIENT,
)
# self.current_thread_span =
self.current_thread_span = span
self.cursor_mapping[cursor] = span
with trace.use_span(span, end_on_exit=False):
if span.is_recording():
Expand All @@ -119,8 +119,8 @@ def _after_cur_exec(self, conn, cursor, statement, *args):
self._cleanup(cursor)

def _handle_error(self, context):
span = self.cursor_mapping[context.cursor]
# span = self.current_thread_span
# span = self.cursor_mapping[context.cursor]
span = self.current_thread_span
if span is None:
return

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import contextlib
import logging
import threading
import unittest

from sqlalchemy import Column, Integer, String, create_engine, insert
from sqlalchemy.ext.declarative import declarative_base
Expand Down Expand Up @@ -242,4 +243,5 @@ def insert_players(session):
close_all_sessions()

spans = self.memory_exporter.get_finished_spans()
breakpoint()
self.assertEqual(len(spans), 5)
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class SQLiteTestCase(SQLAlchemyTestMixin):
def test_engine_execute_errors(self):
# ensures that SQL errors are reported
stmt = "SELECT * FROM a_wrong_table"
with pytest.raises(OperationalError):
with pytest.raises(Exception):
with self.connection() as conn:
conn.execute(stmt).fetchall()

Expand Down

0 comments on commit 67b6a42

Please sign in to comment.