Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a test for mysql instrumentation using NoOpTracerProvider #1423

Merged
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#1413](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1413))
- `opentelemetry-instrumentation-pyramid` Add support for regular expression matching and sanitization of HTTP headers.
([#1414](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1414))
- Add a test for mysql using NoOpTracerProvider
srikanthccv marked this conversation as resolved.
Show resolved Hide resolved
([#1423](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1423))
- `opentelemetry-instrumentation-grpc` Add support for grpc.aio Clients and Servers
([#1245](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1245))
- Add metric exporter for Prometheus Remote Write
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import mysql.connector

import opentelemetry.instrumentation.mysql
from opentelemetry import trace as trace_api
from opentelemetry.instrumentation.mysql import MySQLInstrumentor
from opentelemetry.sdk import resources
from opentelemetry.test.test_base import TestBase
Expand All @@ -31,6 +32,15 @@ def cursor(self):
return MockConnection()


def connect_and_execute_query():
cnx = mysql.connector.connect(database="test")
cursor = cnx.cursor()
query = "SELECT * FROM test"
cursor.execute(query)

return cnx, query


class TestMysqlIntegration(TestBase):
def tearDown(self):
super().tearDown()
Expand All @@ -42,10 +52,7 @@ def tearDown(self):
def test_instrumentor(self):
MySQLInstrumentor().instrument()

cnx = mysql.connector.connect(database="test")
cursor = cnx.cursor()
query = "SELECT * FROM test"
cursor.execute(query)
connect_and_execute_query()

spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
Expand All @@ -59,10 +66,7 @@ def test_instrumentor(self):
# check that no spans are generated after uninstrumen
MySQLInstrumentor().uninstrument()

cnx = mysql.connector.connect(database="test")
cursor = cnx.cursor()
query = "SELECT * FROM test"
cursor.execute(query)
connect_and_execute_query()

spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
Expand All @@ -74,10 +78,7 @@ def test_custom_tracer_provider(self):
tracer_provider, exporter = result

MySQLInstrumentor().instrument(tracer_provider=tracer_provider)
cnx = mysql.connector.connect(database="test")
cursor = cnx.cursor()
query = "SELECT * FROM test"
cursor.execute(query)
connect_and_execute_query()

span_list = exporter.get_finished_spans()
self.assertEqual(len(span_list), 1)
Expand All @@ -88,10 +89,7 @@ def test_custom_tracer_provider(self):
@patch("mysql.connector.connect", new=mock_connect)
# pylint: disable=unused-argument
def test_instrument_connection(self):
cnx = mysql.connector.connect(database="test")
query = "SELECT * FROM test"
cursor = cnx.cursor()
cursor.execute(query)
cnx, query = connect_and_execute_query()

spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 0)
Expand All @@ -103,14 +101,20 @@ def test_instrument_connection(self):
spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)

@patch("mysql.connector.connect", new=mock_connect)
def test_instrument_connection_no_op_tracer_provider(self):
tracer_provider = trace_api.NoOpTracerProvider()
MySQLInstrumentor().instrument(tracer_provider=tracer_provider)
connect_and_execute_query()

spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 0)

@patch("mysql.connector.connect", new=mock_connect)
# pylint: disable=unused-argument
def test_uninstrument_connection(self):
MySQLInstrumentor().instrument()
cnx = mysql.connector.connect(database="test")
query = "SELECT * FROM test"
cursor = cnx.cursor()
cursor.execute(query)
cnx, query = connect_and_execute_query()

spans_list = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans_list), 1)
Expand Down