Skip to content
This repository has been archived by the owner on Jan 29, 2025. It is now read-only.

Commit

Permalink
Improve testing
Browse files Browse the repository at this point in the history
  • Loading branch information
geobeau authored and Adrien Gentil committed Oct 18, 2018
1 parent e4a2351 commit 2e0320c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 30 deletions.
3 changes: 1 addition & 2 deletions biggraphite/drivers/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import threading


from biggraphite import accessor as bg_accessor


Expand Down Expand Up @@ -104,4 +103,4 @@ def bool_from_str(value):
elif type(value) is bool:
return value

return str(value)
return str(value)
4 changes: 2 additions & 2 deletions biggraphite/drivers/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def trace(func):
def tracer(self, *args, **kwargs):
if not hasattr(self, 'module_name'):
self.module_name = func.__module__.split('.')[-1]
tracer = execution_context.get_opencensus_tracer()
with tracer.span(name=self.module_name + '.' + func.__name__):
_tracer = execution_context.get_opencensus_tracer()
with _tracer.span(name="%s.%s" % (self.module_name, func.__name__)):
return func(self, *args, **kwargs)
return tracer
40 changes: 14 additions & 26 deletions tests/drivers/test_tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import unittest
import mock

from opencensus.trace import tracer
from opencensus.trace import execution_context

from biggraphite.drivers import tracing
Expand All @@ -26,39 +27,26 @@ class TestTracingTrace(unittest.TestCase):
def testDecoratorTrace(self):
"""Test that we wrap correctly the function and that a span
is created."""
mock_span = mock.Mock()
mock_span.span_id = '1234'
mock_tracer = MockTracer(mock_span)
test_tracer = tracer.Tracer()

mock_accessor_func = mock.Mock()
mock_accessor_func.__name__ = 'func_name'
mock_accessor_func.__module__ = 'module_path.module_name'
def test_traced_func(self):
_tracer = execution_context.get_opencensus_tracer()
_span = _tracer.current_span()
return _span.name

execution_context.set_opencensus_tracer(mock_tracer)
execution_context.set_opencensus_tracer(test_tracer)

wrapped = tracing.trace(mock_accessor_func)
wrapped = tracing.trace(test_traced_func)

mock_self = mock.Mock()
wrapped(mock_self)
mock_self.module_name = 'module_name'

expected_name = 'module_name.func_name'
span_name = wrapped(mock_self)

self.assertEqual(expected_name, mock_tracer.span.name)
expected_name = 'module_name.test_traced_func'

self.assertEqual(expected_name, span_name)

class MockTracer(object):
def __init__(self, span=None):
self.span = span

def current_span(self):
return self.span

def start_span(self):
span = mock.Mock()
span.attributes = {}
span.context_tracer = mock.Mock()
span.context_tracer.span_context = mock.Mock()
span.context_tracer.span_context.trace_id = '123'
span.context_tracer.span_context.span_id = '456'
self.span = span
return span
if __name__ == "__main__":
unittest.main()

0 comments on commit 2e0320c

Please sign in to comment.