Skip to content

Commit ed9ca71

Browse files
committed
add tests for async. fix black issues
1 parent fcc476a commit ed9ca71

File tree

8 files changed

+49
-12
lines changed

8 files changed

+49
-12
lines changed

Diff for: instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/engine.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,10 @@ def _handle_error(self, context):
113113
try:
114114
if span.is_recording():
115115
span.set_status(
116-
Status(StatusCode.ERROR, str(context.original_exception),)
116+
Status(
117+
StatusCode.ERROR,
118+
str(context.original_exception),
119+
)
117120
)
118121
finally:
119122
span.end()

Diff for: instrumentation/opentelemetry-instrumentation-sqlalchemy/src/opentelemetry/instrumentation/sqlalchemy/package.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,4 @@
1313
# limitations under the License.
1414

1515

16-
_instruments = ("sqlalchemy",)
16+
_instruments = ("sqlalchemy >= 1.3",)

Diff for: instrumentation/opentelemetry-instrumentation-sqlalchemy/tests/test_sqlalchemy.py

+30-3
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
from typing import Coroutine
1415
from unittest import mock
1516

1617
from sqlalchemy import create_engine
17-
18+
import sqlalchemy
1819
from opentelemetry import trace
1920
from opentelemetry.instrumentation.sqlalchemy import SQLAlchemyInstrumentor
2021
from opentelemetry.test.test_base import TestBase
22+
import asyncio
23+
24+
25+
def _call_async(coro: Coroutine):
26+
return asyncio.get_event_loop().run_until_complete(coro)
2127

2228

2329
class TestSqlalchemyInstrumentation(TestBase):
@@ -28,7 +34,8 @@ def tearDown(self):
2834
def test_trace_integration(self):
2935
engine = create_engine("sqlite:///:memory:")
3036
SQLAlchemyInstrumentor().instrument(
31-
engine=engine, tracer_provider=self.tracer_provider,
37+
engine=engine,
38+
tracer_provider=self.tracer_provider,
3239
)
3340
cnx = engine.connect()
3441
cnx.execute("SELECT 1 + 1;").fetchall()
@@ -38,6 +45,25 @@ def test_trace_integration(self):
3845
self.assertEqual(spans[0].name, "SELECT :memory:")
3946
self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT)
4047

48+
def test_async_trace_integration(self):
49+
if sqlalchemy.__version__.startswith("1.3"):
50+
return
51+
from sqlalchemy.ext.asyncio import (
52+
create_async_engine,
53+
) # pylint: disable-all
54+
55+
engine = create_async_engine("sqlite+aiosqlite:///:memory:")
56+
SQLAlchemyInstrumentor().instrument(
57+
engine=engine.sync_engine, tracer_provider=self.tracer_provider
58+
)
59+
cnx = _call_async(engine.connect())
60+
_call_async(cnx.execute(sqlalchemy.text("SELECT 1 + 1;"))).fetchall()
61+
_call_async(cnx.close())
62+
spans = self.memory_exporter.get_finished_spans()
63+
self.assertEqual(len(spans), 1)
64+
self.assertEqual(spans[0].name, "SELECT :memory:")
65+
self.assertEqual(spans[0].kind, trace.SpanKind.CLIENT)
66+
4167
def test_not_recording(self):
4268
mock_tracer = mock.Mock()
4369
mock_span = mock.Mock()
@@ -47,7 +73,8 @@ def test_not_recording(self):
4773
tracer.return_value = mock_tracer
4874
engine = create_engine("sqlite:///:memory:")
4975
SQLAlchemyInstrumentor().instrument(
50-
engine=engine, tracer_provider=self.tracer_provider,
76+
engine=engine,
77+
tracer_provider=self.tracer_provider,
5178
)
5279
cnx = engine.connect()
5380
cnx.execute("SELECT 1 + 1;").fetchall()

Diff for: tests/opentelemetry-docker-tests/tests/sqlalchemy_tests/test_mssql.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ def test_engine_execute_errors(self):
8585
self.assertTrue(span.end_time - span.start_time > 0)
8686
# check the error
8787
self.assertIs(
88-
span.status.status_code, trace.StatusCode.ERROR,
88+
span.status.status_code,
89+
trace.StatusCode.ERROR,
8990
)
9091
self.assertIn("a_wrong_table", span.status.description)
9192

Diff for: tests/opentelemetry-docker-tests/tests/sqlalchemy_tests/test_mysql.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ def test_engine_execute_errors(self):
8484
self.assertTrue(span.end_time - span.start_time > 0)
8585
# check the error
8686
self.assertIs(
87-
span.status.status_code, trace.StatusCode.ERROR,
87+
span.status.status_code,
88+
trace.StatusCode.ERROR,
8889
)
8990
self.assertIn("a_wrong_table", span.status.description)

Diff for: tests/opentelemetry-docker-tests/tests/sqlalchemy_tests/test_postgres.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ def test_engine_execute_errors(self):
7878
self.assertTrue(span.end_time - span.start_time > 0)
7979
# check the error
8080
self.assertIs(
81-
span.status.status_code, trace.StatusCode.ERROR,
81+
span.status.status_code,
82+
trace.StatusCode.ERROR,
8283
)
8384
self.assertIn("a_wrong_table", span.status.description)
8485

Diff for: tests/opentelemetry-docker-tests/tests/sqlalchemy_tests/test_sqlite.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ def test_engine_execute_errors(self):
5454
self.assertTrue((span.end_time - span.start_time) > 0)
5555
# check the error
5656
self.assertIs(
57-
span.status.status_code, trace.StatusCode.ERROR,
57+
span.status.status_code,
58+
trace.StatusCode.ERROR,
5859
)
5960
self.assertEqual(
6061
span.status.description, "no such table: a_wrong_table"

Diff for: tox.ini

+6-3
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ envlist =
126126
py3{6,7,8,9}-test-instrumentation-grpc
127127

128128
; opentelemetry-instrumentation-sqlalchemy
129-
py3{6,7,8,9}-test-instrumentation-sqlalchemy
130-
pypy3-test-instrumentation-sqlalchemy
129+
py3{6,7,8,9}-test-instrumentation-sqlalchemy{13,14}
130+
pypy3-test-instrumentation-sqlalchemy{13,14}
131131

132132
; opentelemetry-instrumentation-redis
133133
py3{6,7,8,9}-test-instrumentation-redis
@@ -177,6 +177,9 @@ deps =
177177
elasticsearch6: elasticsearch>=6.0,<7.0
178178
elasticsearch7: elasticsearch-dsl>=7.0,<8.0
179179
elasticsearch7: elasticsearch>=7.0,<8.0
180+
sqlalchemy13: sqlalchemy~=1.3,<1.4
181+
sqlalchemy14: aiosqlite
182+
sqlalchemy14: sqlalchemy~=1.4
180183

181184
; FIXME: add coverage testing
182185
; FIXME: add mypy testing
@@ -295,7 +298,7 @@ commands_pre =
295298

296299
sklearn: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-sklearn[test]
297300

298-
sqlalchemy: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-sqlalchemy[test]
301+
sqlalchemy{13,14}: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-sqlalchemy[test]
299302

300303
elasticsearch{2,5,6,7}: pip install {toxinidir}/instrumentation/opentelemetry-instrumentation-elasticsearch[test]
301304

0 commit comments

Comments
 (0)