Skip to content

Commit

Permalink
added opentelemetry sdk module cleanup to system test
Browse files Browse the repository at this point in the history
  • Loading branch information
gkevinzheng committed May 10, 2024
1 parent 196749d commit 3dab0c3
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions tests/system/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import numbers
import os
import pytest
import sys
import unittest
import uuid

Expand All @@ -40,9 +41,6 @@

from google.protobuf.struct_pb2 import Struct, Value, ListValue, NullValue

from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider

from test_utils.retry import RetryErrors
from test_utils.retry import RetryResult
from test_utils.system import unique_resource_id
Expand Down Expand Up @@ -119,6 +117,22 @@ def setUpModule():
Config.use_mtls == "always", reason="Skip the test case for mTLS testing"
)

def _cleanup_otel_sdk_modules(f):
"""
Decorator to delete all references to opentelemetry SDK modules after a
testcase is run. Test case should import opentelemetry SDK modules inside
the function. This is to test situations where the opentelemetry SDK
is not imported at all.
"""
def wrapped(*args, **kwargs):
f(*args, **kwargs)

# Deleting from sys.modules should be good enough in this use case
for module_name in list(sys.modules.keys()):
if module_name.startswith('opentelemetry.sdk'):
sys.modules.pop(module_name)

return wrapped

class TestLogging(unittest.TestCase):
JSON_PAYLOAD = {
Expand Down Expand Up @@ -665,7 +679,12 @@ def test_log_root_handler(self):
self.assertEqual(len(entries), 1)
self.assertEqual(entries[0].payload, expected_payload)

@_cleanup_otel_sdk_modules
def test_log_handler_otel_integration(self):
# Doing OTel imports here to not taint the other tests with OTel SDK imports
from opentelemetry import trace
from opentelemetry.sdk.trace import TracerProvider

LOG_MESSAGE = "This is a test of OpenTelemetry"
LOGGER_NAME = "otel-integration"
handler_name = self._logger_name(LOGGER_NAME)
Expand Down

0 comments on commit 3dab0c3

Please sign in to comment.