Skip to content

Commit d3b6b1e

Browse files
authored
Fix: Singleton Import Protection (#230)
* Add try block to protect against import failes from sub packages * Linter fixes
1 parent 979cb8b commit d3b6b1e

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

instana/singletons.py

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import opentracing
44

55
from .agent import StandardAgent, AWSLambdaAgent
6+
from .log import logger
67
from .tracer import InstanaTracer
78
from .recorder import StandardRecorder, AWSLambdaRecorder
89

@@ -18,15 +19,23 @@
1819
span_recorder = StandardRecorder()
1920

2021

21-
# Retrieve the globally configured agent
2222
def get_agent():
23+
"""
24+
Retrieve the globally configured agent
25+
@return: The Instana Agent singleton
26+
"""
2327
global agent
2428
return agent
2529

2630

27-
# Set the global agent for the Instana package. This is used for the
28-
# test suite only currently.
2931
def set_agent(new_agent):
32+
"""
33+
Set the global agent for the Instana package. This is used for the
34+
test suite only currently.
35+
36+
@param new_agent: agent to replace current singleton
37+
@return: None
38+
"""
3039
global agent
3140
agent = new_agent
3241

@@ -36,8 +45,11 @@ def set_agent(new_agent):
3645
tracer = InstanaTracer(recorder=span_recorder)
3746

3847
if sys.version_info >= (3, 4):
39-
from opentracing.scope_managers.asyncio import AsyncioScopeManager
40-
async_tracer = InstanaTracer(scope_manager=AsyncioScopeManager(), recorder=span_recorder)
48+
try:
49+
from opentracing.scope_managers.asyncio import AsyncioScopeManager
50+
async_tracer = InstanaTracer(scope_manager=AsyncioScopeManager(), recorder=span_recorder)
51+
except Exception:
52+
logger.debug("Error setting up async_tracer:", exc_info=True)
4153

4254

4355
# Mock the tornado tracer until tornado is detected and instrumented first
@@ -54,14 +66,21 @@ def setup_tornado_tracer():
5466
opentracing.tracer = tracer
5567

5668

57-
# Retrieve the globally configured tracer
5869
def get_tracer():
70+
"""
71+
Retrieve the globally configured tracer
72+
@return: Tracer
73+
"""
5974
global tracer
6075
return tracer
6176

6277

63-
# Set the global tracer for the Instana package. This is used for the
64-
# test suite only currently.
6578
def set_tracer(new_tracer):
79+
"""
80+
Set the global tracer for the Instana package. This is used for the
81+
test suite only currently.
82+
@param new_tracer: The new tracer to replace the singleton
83+
@return: None
84+
"""
6685
global tracer
6786
tracer = new_tracer

0 commit comments

Comments
 (0)