Skip to content

Commit e6a9d97

Browse files
alrexmauriciovasquezbernalocelotlc24t
authored
Porting sqlalchemy instrumentation from contrib repo (open-telemetry#591)
Porting the existing sqlalchemy instrumentation from https://github.com/open-telemetry/opentelemetry-python-contrib/tree/master/reference/ddtrace/contrib/sqlalchemy Co-authored-by: Mauricio Vásquez <mauricio@kinvolk.io> Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com> Co-authored-by: Chris Kleinknecht <libc@google.com>
1 parent bdd9925 commit e6a9d97

File tree

19 files changed

+1158
-0
lines changed

19 files changed

+1158
-0
lines changed

docs/ext/sqlalchemy/sqlalchemy.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
OpenTelemetry SQLAlchemy Instrumentation
2+
========================================
3+
4+
.. automodule:: opentelemetry.ext.sqlalchemy
5+
:members:
6+
:undoc-members:
7+
:show-inheritance:
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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.
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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 contextlib
16+
17+
from sqlalchemy import Column, Integer, String, create_engine
18+
from sqlalchemy.ext.declarative import declarative_base
19+
from sqlalchemy.orm import sessionmaker
20+
21+
from opentelemetry import trace
22+
from opentelemetry.ext.sqlalchemy import SQLAlchemyInstrumentor
23+
from opentelemetry.ext.sqlalchemy.engine import _DB, _ROWS, _STMT
24+
from opentelemetry.test.test_base import TestBase
25+
26+
Base = declarative_base()
27+
28+
29+
def _create_engine(engine_args):
30+
# create a SQLAlchemy engine
31+
config = dict(engine_args)
32+
url = config.pop("url")
33+
return create_engine(url, **config)
34+
35+
36+
class Player(Base):
37+
"""Player entity used to test SQLAlchemy ORM"""
38+
39+
__tablename__ = "players"
40+
41+
id = Column(Integer, primary_key=True)
42+
name = Column(String(20))
43+
44+
45+
class SQLAlchemyTestMixin(TestBase):
46+
__test__ = False
47+
48+
"""SQLAlchemy test mixin that includes a complete set of tests
49+
that must be executed for different engine. When a new test (or
50+
a regression test) should be added to SQLAlchemy test suite, a new
51+
entry must be appended here so that it will be executed for all
52+
available and supported engines. If the test is specific to only
53+
one engine, that test must be added to the specific `TestCase`
54+
implementation.
55+
56+
To support a new engine, create a new `TestCase` that inherits from
57+
`SQLAlchemyTestMixin` and `TestCase`. Then you must define the following
58+
static class variables:
59+
* VENDOR: the database vendor name
60+
* SQL_DB: the `db.type` tag that we expect (it's the name of the database available in the `.env` file)
61+
* SERVICE: the service that we expect by default
62+
* ENGINE_ARGS: all arguments required to create the engine
63+
64+
To check specific tags in each test, you must implement the
65+
`check_meta(self, span)` method.
66+
"""
67+
68+
VENDOR = None
69+
SQL_DB = None
70+
SERVICE = None
71+
ENGINE_ARGS = None
72+
73+
@contextlib.contextmanager
74+
def connection(self):
75+
# context manager that provides a connection
76+
# to the underlying database
77+
try:
78+
conn = self.engine.connect()
79+
yield conn
80+
finally:
81+
conn.close()
82+
83+
def check_meta(self, span):
84+
"""function that can be implemented according to the
85+
specific engine implementation
86+
"""
87+
88+
def setUp(self):
89+
super().setUp()
90+
# create an engine with the given arguments
91+
self.engine = _create_engine(self.ENGINE_ARGS)
92+
93+
# create the database / entities and prepare a session for the test
94+
Base.metadata.drop_all(bind=self.engine)
95+
Base.metadata.create_all(self.engine, checkfirst=False)
96+
self.session = sessionmaker(bind=self.engine)()
97+
# trace the engine
98+
SQLAlchemyInstrumentor().instrument(
99+
engine=self.engine, tracer_provider=self.tracer_provider
100+
)
101+
self.memory_exporter.clear()
102+
103+
def tearDown(self):
104+
# pylint: disable=invalid-name
105+
# clear the database and dispose the engine
106+
self.session.close()
107+
Base.metadata.drop_all(bind=self.engine)
108+
self.engine.dispose()
109+
SQLAlchemyInstrumentor().uninstrument()
110+
super().tearDown()
111+
112+
def _check_span(self, span):
113+
self.assertEqual(span.name, "{}.query".format(self.VENDOR))
114+
self.assertEqual(span.attributes.get("service"), self.SERVICE)
115+
self.assertEqual(span.attributes.get(_DB), self.SQL_DB)
116+
self.assertIs(
117+
span.status.canonical_code, trace.status.StatusCanonicalCode.OK
118+
)
119+
self.assertGreater((span.end_time - span.start_time), 0)
120+
121+
def test_orm_insert(self):
122+
# ensures that the ORM session is traced
123+
wayne = Player(id=1, name="wayne")
124+
self.session.add(wayne)
125+
self.session.commit()
126+
127+
spans = self.memory_exporter.get_finished_spans()
128+
self.assertEqual(len(spans), 1)
129+
span = spans[0]
130+
self._check_span(span)
131+
self.assertIn("INSERT INTO players", span.attributes.get(_STMT))
132+
self.assertEqual(span.attributes.get(_ROWS), 1)
133+
self.check_meta(span)
134+
135+
def test_session_query(self):
136+
# ensures that the Session queries are traced
137+
out = list(self.session.query(Player).filter_by(name="wayne"))
138+
self.assertEqual(len(out), 0)
139+
140+
spans = self.memory_exporter.get_finished_spans()
141+
self.assertEqual(len(spans), 1)
142+
span = spans[0]
143+
self._check_span(span)
144+
self.assertIn(
145+
"SELECT players.id AS players_id, players.name AS players_name \nFROM players \nWHERE players.name",
146+
span.attributes.get(_STMT),
147+
)
148+
self.check_meta(span)
149+
150+
def test_engine_connect_execute(self):
151+
# ensures that engine.connect() is properly traced
152+
with self.connection() as conn:
153+
rows = conn.execute("SELECT * FROM players").fetchall()
154+
self.assertEqual(len(rows), 0)
155+
156+
spans = self.memory_exporter.get_finished_spans()
157+
self.assertEqual(len(spans), 1)
158+
span = spans[0]
159+
self._check_span(span)
160+
self.assertEqual(span.attributes.get(_STMT), "SELECT * FROM players")
161+
self.check_meta(span)
162+
163+
def test_parent(self):
164+
"""Ensure that sqlalchemy works with opentelemetry."""
165+
tracer = self.tracer_provider.get_tracer("sqlalch_svc")
166+
167+
with tracer.start_as_current_span("sqlalch_op"):
168+
with self.connection() as conn:
169+
rows = conn.execute("SELECT * FROM players").fetchall()
170+
self.assertEqual(len(rows), 0)
171+
172+
spans = self.memory_exporter.get_finished_spans()
173+
self.assertEqual(len(spans), 2)
174+
child_span, parent_span = spans
175+
176+
# confirm the parenting
177+
self.assertIsNone(parent_span.parent)
178+
self.assertIs(child_span.parent, parent_span.get_context())
179+
180+
self.assertEqual(parent_span.name, "sqlalch_op")
181+
self.assertEqual(parent_span.instrumentation_info.name, "sqlalch_svc")
182+
183+
self.assertEqual(child_span.name, "{}.query".format(self.VENDOR))
184+
self.assertEqual(child_span.attributes.get("service"), self.SERVICE)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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 unittest
17+
18+
import sqlalchemy
19+
20+
from opentelemetry import trace
21+
from opentelemetry.ext.sqlalchemy import SQLAlchemyInstrumentor
22+
from opentelemetry.test.test_base import TestBase
23+
24+
POSTGRES_CONFIG = {
25+
"host": "127.0.0.1",
26+
"port": int(os.getenv("TEST_POSTGRES_PORT", "5432")),
27+
"user": os.getenv("TEST_POSTGRES_USER", "testuser"),
28+
"password": os.getenv("TEST_POSTGRES_PASSWORD", "testpassword"),
29+
"dbname": os.getenv("TEST_POSTGRES_DB", "opentelemetry-tests"),
30+
}
31+
32+
33+
class SQLAlchemyInstrumentTestCase(TestBase):
34+
"""TestCase that checks if the engine is properly traced
35+
when the `instrument()` method is used.
36+
"""
37+
38+
def setUp(self):
39+
# create a traced engine with the given arguments
40+
SQLAlchemyInstrumentor().instrument()
41+
dsn = (
42+
"postgresql://%(user)s:%(password)s@%(host)s:%(port)s/%(dbname)s"
43+
% POSTGRES_CONFIG
44+
)
45+
self.engine = sqlalchemy.create_engine(dsn)
46+
47+
# prepare a connection
48+
self.conn = self.engine.connect()
49+
super().setUp()
50+
51+
def tearDown(self):
52+
# clear the database and dispose the engine
53+
self.conn.close()
54+
self.engine.dispose()
55+
SQLAlchemyInstrumentor().uninstrument()
56+
57+
def test_engine_traced(self):
58+
# ensures that the engine is traced
59+
rows = self.conn.execute("SELECT 1").fetchall()
60+
self.assertEqual(len(rows), 1)
61+
62+
traces = self.memory_exporter.get_finished_spans()
63+
# trace composition
64+
self.assertEqual(len(traces), 1)
65+
span = traces[0]
66+
# check subset of span fields
67+
self.assertEqual(span.name, "postgres.query")
68+
self.assertEqual(span.attributes.get("service"), "postgres")
69+
self.assertIs(
70+
span.status.canonical_code, trace.status.StatusCanonicalCode.OK
71+
)
72+
self.assertGreater((span.end_time - span.start_time), 0)
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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 unittest
17+
18+
import pytest
19+
from sqlalchemy.exc import ProgrammingError
20+
21+
from opentelemetry import trace
22+
from opentelemetry.ext.sqlalchemy.engine import _DB, _HOST, _PORT, _ROWS, _STMT
23+
24+
from .mixins import SQLAlchemyTestMixin
25+
26+
MYSQL_CONFIG = {
27+
"host": "127.0.0.1",
28+
"port": int(os.getenv("TEST_MYSQL_PORT", "3306")),
29+
"user": os.getenv("TEST_MYSQL_USER", "testuser"),
30+
"password": os.getenv("TEST_MYSQL_PASSWORD", "testpassword"),
31+
"database": os.getenv("TEST_MYSQL_DATABASE", "opentelemetry-tests"),
32+
}
33+
34+
35+
class MysqlConnectorTestCase(SQLAlchemyTestMixin):
36+
"""TestCase for mysql-connector engine"""
37+
38+
__test__ = True
39+
40+
VENDOR = "mysql"
41+
SQL_DB = "opentelemetry-tests"
42+
SERVICE = "mysql"
43+
ENGINE_ARGS = {
44+
"url": "mysql+mysqlconnector://%(user)s:%(password)s@%(host)s:%(port)s/%(database)s"
45+
% MYSQL_CONFIG
46+
}
47+
48+
def check_meta(self, span):
49+
# check database connection tags
50+
self.assertEqual(span.attributes.get(_HOST), MYSQL_CONFIG["host"])
51+
self.assertEqual(span.attributes.get(_PORT), MYSQL_CONFIG["port"])
52+
53+
def test_engine_execute_errors(self):
54+
# ensures that SQL errors are reported
55+
with pytest.raises(ProgrammingError):
56+
with self.connection() as conn:
57+
conn.execute("SELECT * FROM a_wrong_table").fetchall()
58+
59+
spans = self.memory_exporter.get_finished_spans()
60+
self.assertEqual(len(spans), 1)
61+
span = spans[0]
62+
# span fields
63+
self.assertEqual(span.name, "{}.query".format(self.VENDOR))
64+
self.assertEqual(span.attributes.get("service"), self.SERVICE)
65+
self.assertEqual(
66+
span.attributes.get(_STMT), "SELECT * FROM a_wrong_table"
67+
)
68+
self.assertEqual(span.attributes.get(_DB), self.SQL_DB)
69+
self.assertIsNone(span.attributes.get(_ROWS))
70+
self.check_meta(span)
71+
self.assertTrue(span.end_time - span.start_time > 0)
72+
# check the error
73+
self.assertIs(
74+
span.status.canonical_code,
75+
trace.status.StatusCanonicalCode.UNKNOWN,
76+
)
77+
self.assertIn("a_wrong_table", span.status.description)

0 commit comments

Comments
 (0)