Skip to content

Commit ac800f0

Browse files
authored
Snapshot reporting: Better version extraction (instana#94)
* Better version extraction Don't include builtin modules * Better exception message; linter fixes
1 parent 2ec90f8 commit ac800f0

File tree

4 files changed

+26
-26
lines changed

4 files changed

+26
-26
lines changed

instana/instrumentation/django/middleware.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ def process_request(self, request):
3939
self.scope.span.set_tag("http.params", env['QUERY_STRING'])
4040
if 'HTTP_HOST' in env:
4141
self.scope.span.set_tag("http.host", env['HTTP_HOST'])
42-
except Exception as e:
43-
logger.debug("Instana middleware @ process_response: ", e)
42+
except Exception:
43+
logger.debug("Django middleware @ process_response", exc_info=True)
4444

4545
def process_response(self, request, response):
4646
try:

instana/meter.py

+15-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import time
99
from types import ModuleType
1010

11-
import instana
11+
from pkg_resources import get_distribution, DistributionNotFound
1212

1313
from .agent_const import AGENT_DATA_URL
1414
from .log import logger as log
@@ -188,29 +188,31 @@ def jsonable(self, value):
188188

189189
def collect_modules(self):
190190
try:
191-
r = {}
191+
res = {}
192192
m = sys.modules
193193
for k in m:
194194
# Don't report submodules (e.g. django.x, django.y, django.z)
195-
if ('.' in k):
195+
# Skip modules that begin with underscore
196+
if ('.' in k) or k[0] == '_':
196197
continue
197198
if m[k]:
198199
try:
199200
d = m[k].__dict__
200201
if "version" in d and d["version"]:
201-
r[k] = self.jsonable(d["version"])
202+
res[k] = self.jsonable(d["version"])
202203
elif "__version__" in d and d["__version__"]:
203-
r[k] = self.jsonable(d["__version__"])
204+
res[k] = self.jsonable(d["__version__"])
204205
else:
205-
r[k] = "builtin"
206-
except Exception as e:
207-
r[k] = "unknown"
208-
log.debug("collect_modules: could not process module ", k, str(e))
209-
210-
except Exception as e:
211-
log.debug(e.message)
206+
res[k] = get_distribution(k).version
207+
except DistributionNotFound:
208+
pass
209+
except Exception:
210+
log.debug("collect_modules: could not process module: %s" % k)
211+
212+
except Exception:
213+
log.debug("collect_modules", exc_info=True)
212214
else:
213-
return r
215+
return res
214216

215217
def collect_metrics(self):
216218
u = resource.getrusage(resource.RUSAGE_SELF)

instana/tracer.py

+8-9
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,14 @@ def __init__(self, options=o.Options()):
2626
self._propagators[ot.Format.HTTP_HEADERS] = HTTPPropagator()
2727
self._propagators[ot.Format.TEXT_MAP] = TextPropagator()
2828

29-
3029
def start_active_span(self,
31-
operation_name,
32-
child_of=None,
33-
references=None,
34-
tags=None,
35-
start_time=None,
36-
ignore_active_span=False,
37-
finish_on_close=True):
30+
operation_name,
31+
child_of=None,
32+
references=None,
33+
tags=None,
34+
start_time=None,
35+
ignore_active_span=False,
36+
finish_on_close=True):
3837

3938
# create a new Span
4039
span = self.start_span(
@@ -48,7 +47,6 @@ def start_active_span(self,
4847

4948
return self.scope_manager.activate(span, finish_on_close)
5049

51-
5250
def start_span(self,
5351
operation_name=None,
5452
child_of=None,
@@ -122,6 +120,7 @@ def init(options):
122120
"""
123121
return internal_tracer
124122

123+
125124
def get_tracer():
126125
return internal_tracer
127126

tests/test_api_client.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,14 @@
66

77
raise unittest.SkipTest("Manual tests due to API key requirement")
88

9+
910
class TestAPIClient(object):
1011
def setUp(self):
1112
""" Clear all spans before a test run """
1213
self.client = APIClient()
1314

1415
def tearDown(self):
1516
""" Do nothing for now """
16-
# after each test, tracer context should be None (not tracing)
17-
# assert_equals(None, tracer.current_context())
1817
return None
1918

2019
def test_tokens(self):

0 commit comments

Comments
 (0)