Skip to content

Commit

Permalink
Sync with sdk setup from setUpClass to setUp (#1193)
Browse files Browse the repository at this point in the history
  • Loading branch information
srikanthccv authored Jul 18, 2022
1 parent 2ce69a6 commit d75194a
Show file tree
Hide file tree
Showing 21 changed files with 151 additions and 170 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- 'release/*'
pull_request:
env:
CORE_REPO_SHA: 05d251c5d6baefe2f084e7361cb144c4fa10da96
CORE_REPO_SHA: c09f2076a1878c6d58b78d3895485ef5559f30f7

jobs:
build:
Expand Down Expand Up @@ -42,7 +42,7 @@ jobs:
path: |
.tox
~/.cache/pip
key: v6-build-tox-cache-${{ env.RUN_MATRIX_COMBINATION }}-${{ hashFiles('tox.ini', 'gen-requirements.txt', 'dev-requirements.txt') }}
key: v7-build-tox-cache-${{ env.RUN_MATRIX_COMBINATION }}-${{ hashFiles('tox.ini', 'gen-requirements.txt', 'dev-requirements.txt') }}
- name: run tox
run: tox -f ${{ matrix.python-version }}-${{ matrix.package }} -- --benchmark-json=${{ env.RUN_MATRIX_COMBINATION }}-benchmark.json
# - name: Find and merge ${{ matrix.package }} benchmarks
Expand Down Expand Up @@ -118,7 +118,7 @@ jobs:
path: |
.tox
~/.cache/pip
key: v6-misc-tox-cache-${{ matrix.tox-environment }}-${{ hashFiles('tox.ini', 'dev-requirements.txt', 'gen-requirements.txt', 'docs-requirements.txt') }}
key: v7-misc-tox-cache-${{ matrix.tox-environment }}-${{ hashFiles('tox.ini', 'dev-requirements.txt', 'gen-requirements.txt', 'docs-requirements.txt') }}
- name: run tox
run: tox -e ${{ matrix.tox-environment }}
- name: Ensure generated code is up to date
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ def get_dsn_parameters(self): # pylint: disable=no-self-use

class TestPostgresqlIntegration(TestBase):
def setUp(self):
super().setUp()
self.cursor_mock = mock.patch(
"opentelemetry.instrumentation.psycopg2.pg_cursor", MockCursor
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,6 @@ def _instrument(self, **kwargs):
request_hook = kwargs.get("request_hook", dummy_callback)
response_hook = kwargs.get("response_hook", dummy_callback)
failed_hook = kwargs.get("failed_hook", dummy_callback)

# Create and register a CommandTracer only the first time
if self._commandtracer_instance is None:
tracer = get_tracer(__name__, __version__, tracer_provider)
Expand All @@ -235,7 +234,6 @@ def _instrument(self, **kwargs):
failed_hook=failed_hook,
)
monitoring.register(self._commandtracer_instance)

# If already created, just enable it
self._commandtracer_instance.is_enabled = True

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,16 @@


class TestRedis(TestBase):
def setUp(self):
super().setUp()
RedisInstrumentor().instrument(tracer_provider=self.tracer_provider)

def tearDown(self):
super().tearDown()
RedisInstrumentor().uninstrument()

def test_span_properties(self):
redis_client = redis.Redis()
RedisInstrumentor().instrument(tracer_provider=self.tracer_provider)

with mock.patch.object(redis_client, "connection"):
redis_client.get("key")
Expand All @@ -36,7 +43,6 @@ def test_span_properties(self):

def test_not_recording(self):
redis_client = redis.Redis()
RedisInstrumentor().instrument(tracer_provider=self.tracer_provider)

mock_tracer = mock.Mock()
mock_span = mock.Mock()
Expand All @@ -53,7 +59,6 @@ def test_not_recording(self):

def test_instrument_uninstrument(self):
redis_client = redis.Redis()
RedisInstrumentor().instrument(tracer_provider=self.tracer_provider)

with mock.patch.object(redis_client, "connection"):
redis_client.get("key")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ def setUp(self):

broker.declare_actor(actor_div)

def tearDown(self):
RemouladeInstrumentor().uninstrument()
super().tearDown()

def test_message(self):
actor_div.send(2, 3)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,26 @@


class TestSQLite3(TestBase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls._connection = None
cls._cursor = None
cls._connection2 = None
cls._cursor2 = None
cls._tracer = cls.tracer_provider.get_tracer(__name__)
SQLite3Instrumentor().instrument(tracer_provider=cls.tracer_provider)
cls._connection = sqlite3.connect(":memory:")
cls._cursor = cls._connection.cursor()
cls._connection2 = dbapi2.connect(":memory:")
cls._cursor2 = cls._connection2.cursor()
def setUp(self):
super().setUp()
SQLite3Instrumentor().instrument(tracer_provider=self.tracer_provider)
self._tracer = self.tracer_provider.get_tracer(__name__)
self._connection = sqlite3.connect(":memory:")
self._cursor = self._connection.cursor()
self._connection2 = dbapi2.connect(":memory:")
self._cursor2 = self._connection2.cursor()

@classmethod
def tearDownClass(cls):
if cls._cursor:
cls._cursor.close()
if cls._connection:
cls._connection.close()
if cls._cursor2:
cls._cursor2.close()
if cls._connection2:
cls._connection2.close()
def tearDown(self):
super().tearDown()
if self._cursor:
self._cursor.close()
if self._connection:
self._connection.close()
if self._cursor2:
self._cursor2.close()
if self._connection2:
self._connection2.close()
SQLite3Instrumentor().uninstrument()

def validate_spans(self, span_name):
spans = self.memory_exporter.get_finished_spans()
Expand All @@ -65,6 +61,12 @@ def validate_spans(self, span_name):
self.assertIs(child_span.parent, root_span.get_span_context())
self.assertIs(child_span.kind, trace_api.SpanKind.CLIENT)

def _create_tables(self):
stmt = "CREATE TABLE IF NOT EXISTS test (id integer)"
self._cursor.execute(stmt)
self._cursor2.execute(stmt)
self.memory_exporter.clear()

def test_execute(self):
"""Should create a child span for execute method"""
stmt = "CREATE TABLE IF NOT EXISTS test (id integer)"
Expand All @@ -78,6 +80,9 @@ def test_execute(self):

def test_executemany(self):
"""Should create a child span for executemany"""
self._create_tables()

# real spans for executemany
stmt = "INSERT INTO test (id) VALUES (?)"
data = [("1",), ("2",), ("3",)]
with self._tracer.start_as_current_span("rootSpan"):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,7 @@ def test_mark_span_internal_in_presence_of_span_from_other_framework(self):
class TestAdditionOfCustomRequestResponseHeaders(WsgiTestBase):
def setUp(self):
super().setUp()
tracer_provider, _ = TestBase.create_tracer_provider()
self.tracer = tracer_provider.get_tracer(__name__)
self.tracer = self.tracer_provider.get_tracer(__name__)

def iterate_response(self, response):
while True:
Expand Down
3 changes: 0 additions & 3 deletions opentelemetry-instrumentation/tests/test_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@


class TestDependencyConflicts(TestBase):
def setUp(self):
pass

def test_get_dependency_conflicts_empty(self):
self.assertIsNone(get_dependency_conflicts([]))

Expand Down
4 changes: 3 additions & 1 deletion opentelemetry-instrumentation/tests/test_propagators.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

# pylint: disable=protected-access

import unittest

from opentelemetry import trace
from opentelemetry.instrumentation import propagators
from opentelemetry.instrumentation.propagators import (
Expand All @@ -39,7 +41,7 @@ def test_get_set(self):
propagators._RESPONSE_PROPAGATOR = original


class TestDictHeaderSetter(TestBase):
class TestDictHeaderSetter(unittest.TestCase):
def test_simple(self):
setter = DictHeaderSetter()
carrier = {}
Expand Down
4 changes: 2 additions & 2 deletions opentelemetry-instrumentation/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import unittest
from http import HTTPStatus

from opentelemetry.instrumentation.utils import (
_python_path_without_directory,
http_status_to_status_code,
)
from opentelemetry.test.test_base import TestBase
from opentelemetry.trace import StatusCode


class TestUtils(TestBase):
class TestUtils(unittest.TestCase):
# See https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/semantic_conventions/http.md#status
def test_http_status_to_status_code(self):
for status_code, expected in (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@ def async_call(coro):


class TestFunctionalAsyncPG(TestBase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls._connection = None
cls._cursor = None
cls._tracer = cls.tracer_provider.get_tracer(__name__)
AsyncPGInstrumentor().instrument(tracer_provider=cls.tracer_provider)
cls._connection = async_call(
def setUp(self):
super().setUp()
self._tracer = self.tracer_provider.get_tracer(__name__)
AsyncPGInstrumentor().instrument(tracer_provider=self.tracer_provider)
self._connection = async_call(
asyncpg.connect(
database=POSTGRES_DB_NAME,
user=POSTGRES_USER,
Expand All @@ -38,9 +35,9 @@ def setUpClass(cls):
)
)

@classmethod
def tearDownClass(cls):
def tearDown(self):
AsyncPGInstrumentor().uninstrument()
super().tearDown()

def check_span(self, span):
self.assertEqual(
Expand Down Expand Up @@ -148,16 +145,13 @@ def test_instrumented_method_doesnt_capture_parameters(self, *_, **__):


class TestFunctionalAsyncPG_CaptureParameters(TestBase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls._connection = None
cls._cursor = None
cls._tracer = cls.tracer_provider.get_tracer(__name__)
def setUp(self):
super().setUp()
self._tracer = self.tracer_provider.get_tracer(__name__)
AsyncPGInstrumentor(capture_parameters=True).instrument(
tracer_provider=cls.tracer_provider
tracer_provider=self.tracer_provider
)
cls._connection = async_call(
self._connection = async_call(
asyncpg.connect(
database=POSTGRES_DB_NAME,
user=POSTGRES_USER,
Expand All @@ -167,9 +161,9 @@ def setUpClass(cls):
)
)

@classmethod
def tearDownClass(cls):
def tearDown(self):
AsyncPGInstrumentor().uninstrument()
super().tearDown()

def check_span(self, span):
self.assertEqual(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,10 @@


class TestFunctionalMysql(TestBase):
@classmethod
def setUpClass(cls):
super().setUpClass()
cls._connection = None
cls._cursor = None
cls._tracer = cls.tracer_provider.get_tracer(__name__)
MySQLInstrumentor().instrument()

@classmethod
def tearDownClass(cls):
if cls._connection:
cls._connection.close()
MySQLInstrumentor().uninstrument()

def setUp(self):
super().setUp()
self._tracer = self.tracer_provider.get_tracer(__name__)
MySQLInstrumentor().instrument()
self._connection = mysql.connector.connect(
user=MYSQL_USER,
password=MYSQL_PASSWORD,
Expand All @@ -54,6 +42,12 @@ def setUp(self):
)
self._cursor = self._connection.cursor()

def tearDown(self):
self._cursor.close()
self._connection.close()
MySQLInstrumentor().uninstrument()
super().tearDown()

def validate_spans(self, span_name):
spans = self.memory_exporter.get_finished_spans()
self.assertEqual(len(spans), 2)
Expand Down
Loading

0 comments on commit d75194a

Please sign in to comment.