Python opentracing.tracer() Examples
The following are 30
code examples of opentracing.tracer().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
opentracing
, or try the search function
.
Example #1
Source File: config.py From jaeger-client-python with Apache License 2.0 | 6 votes |
def initialize_tracer(self, io_loop=None): """ Initialize Jaeger Tracer based on the passed `jaeger_client.Config`. Save it to `opentracing.tracer` global variable. Only the first call to this method has any effect. """ with Config._initialized_lock: if Config._initialized: logger.warn('Jaeger tracer already initialized, skipping') return Config._initialized = True tracer = self.new_tracer(io_loop) self._initialize_global_tracer(tracer=tracer) return tracer
Example #2
Source File: server.py From tchannel-python with MIT License | 6 votes |
def serve(): """main entry point""" logging.getLogger().setLevel(logging.DEBUG) logging.info('Python Tornado Crossdock Server Starting ...') tracer = Tracer( service_name='python', reporter=NullReporter(), sampler=ConstSampler(decision=True), scope_manager=TornadoScopeManager() ) opentracing.tracer = tracer tchannel = TChannel(name='python', hostport=':%d' % DEFAULT_SERVER_PORT, trace=True) register_tchannel_handlers(tchannel=tchannel) tchannel.listen() app = tornado.web.Application(debug=True) register_http_handlers(app) app.listen(DEFAULT_CLIENT_PORT) tornado.ioloop.IOLoop.current().start()
Example #3
Source File: utils.py From opentracing-python-instrumentation with MIT License | 6 votes |
def start_child_span(operation_name, tracer=None, parent=None, tags=None): """ Start a new span as a child of parent_span. If parent_span is None, start a new root span. :param operation_name: operation name :param tracer: Tracer or None (defaults to opentracing.tracer) :param parent: parent Span or None :param tags: optional tags :return: new span """ tracer = tracer or opentracing.tracer return tracer.start_span( operation_name=operation_name, child_of=parent.context if parent else None, tags=tags )
Example #4
Source File: tracing.py From tchannel-python with MIT License | 6 votes |
def span_to_tracing_field(span): """ Inject the span into Trace field, if Zipkin format is supported :param span: OpenTracing Span """ if span is None: return common.random_tracing() # noinspection PyBroadException try: carrier = {} span.tracer.inject(span, ZIPKIN_SPAN_FORMAT, carrier) tracing = Tracing(span_id=carrier['span_id'], trace_id=carrier['trace_id'], parent_id=carrier['parent_id'] or int(0), traceflags=carrier['traceflags']) return tracing except opentracing.UnsupportedFormatException: pass # tracer might not support Zipkin format except: log.exception('Failed to inject tracing span into headers') return common.random_tracing()
Example #5
Source File: tracing.py From tchannel-python with MIT License | 6 votes |
def start_basic_span(self, request): """ Start tracing span from the protocol's `tracing` fields. This will only work if the `tracer` supports Zipkin-style span context. :param request: inbound request :type request: tchannel.tornado.request.Request """ # noinspection PyBroadException try: # Currently Java does not populate Tracing field, so do not # mistaken it for a real trace ID. if request.tracing.trace_id: context = self.tracer.extract( format=ZIPKIN_SPAN_FORMAT, carrier=request.tracing) self.span = self.tracer.start_span( operation_name=request.endpoint, child_of=context, tags={tags.SPAN_KIND: tags.SPAN_KIND_RPC_SERVER}, ) except opentracing.UnsupportedFormatException: pass # tracer might not support Zipkin format except: log.exception('Cannot extract tracing span from Trace field')
Example #6
Source File: tracing.py From tchannel-python with MIT License | 6 votes |
def span_in_context(self, span): """ Store the `span` in the request context and return a `StackContext`. This method is meant to be used as a context manager: .. code-block:: python with tchannel.context_provider.span_in_context(span): f = handler_fn() res = yield f Note: StackContext does not allow yield when used a context manager. Instead, save the future and yield it outside of `with:` statement. :param span: an OpenTracing Span :return: ``StackContext``-based context manager """ if isinstance(opentracing.tracer.scope_manager, opentracing_instrumentation.request_context.TornadoScopeManager): # noqa return opentracing_instrumentation.span_in_stack_context(span) return opentracing_instrumentation.span_in_context(span)
Example #7
Source File: test_traced_function_decorator.py From opentracing-python-instrumentation with MIT License | 6 votes |
def test_no_parent_span(self): @gen.coroutine def run(): # verify a new trace is started for func1, func2 in (('regular', 'regular_require_active_trace'), ('coro', 'coro_require_active_trace')): with patch_object(opentracing.tracer, 'start_span') as start: r = yield self.call(func1, 123) assert r == 'oh yeah' start.assert_called_once_with( operation_name=func1, child_of=None, tags=None) # verify no new trace or child span is started with patch_object(opentracing.tracer, 'start_span') as start: r = yield self.call(func2, 123) assert r == 'oh yeah' start.assert_not_called() raise tornado.gen.Return(1) yield run_coroutine_with_span(span=None, coro=run)
Example #8
Source File: test_traced_function_decorator.py From opentracing-python-instrumentation with MIT License | 6 votes |
def test_decorator_with_start_hook(self): parent = opentracing.tracer.start_span('hello') @gen.coroutine def run(): # verify call_size_tag argument is extracted and added as tag for func in ('regular_with_hook', 'coro_with_hook', ): child = mock.Mock() with patch_object(opentracing.tracer, 'start_span') \ as start_child: start_child.return_value = child r = yield self.call( func, 'somewhere', call_site_tag='somewhere') assert r == 'oh yeah' start_child.assert_called_once_with( operation_name=func, child_of=parent.context, tags=None) child.set_tag.assert_called_once_with( 'call_site_tag', 'somewhere') raise tornado.gen.Return(1) yield run_coroutine_with_span(span=parent, coro=run)
Example #9
Source File: context_in_headers.py From lightstep-tracer-python with MIT License | 6 votes |
def before_sending_request(request): """Context manager creates Span and encodes the span's SpanContext into request. """ span = opentracing.tracer.start_span('Sending request') span.set_tag('server.http.url', request.get_full_url()) try: # Python 2 host = request.get_host() except: # Python 3 host = request.host if host: span.set_tag(opentracing.ext.tags.PEER_HOST_IPV4, host) carrier_dict = {} span.tracer.inject(span.context, opentracing.Format.HTTP_HEADERS, carrier_dict) for k, v in carrier_dict.items(): request.add_header(k, v) return span
Example #10
Source File: tracing.py From python-django with BSD 3-Clause "New" or "Revised" License | 6 votes |
def initialize_global_tracer(tracing): ''' Initialisation as per https://github.com/opentracing/opentracing-python/blob/9f9ef02d4ef7863fb26d3534a38ccdccf245494c/opentracing/__init__.py#L36 # noqa Here the global tracer object gets initialised once from Django settings. ''' if initialize_global_tracer.complete: return # DjangoTracing may be already relying on the global tracer, # hence check for a non-None value. tracer = tracing._tracer_implementation if tracer is not None: opentracing.tracer = tracer initialize_global_tracer.complete = True
Example #11
Source File: test_traced_function_decorator.py From opentracing-python-instrumentation with MIT License | 6 votes |
def test_decorator_with_start_hook(self): parent = opentracing.tracer.start_span('hello') with opentracing.tracer.scope_manager.activate(parent, True) as scope: # verify call_size_tag argument is extracted and added as tag child = mock.Mock() with patch_object(opentracing.tracer, 'start_span') as start_child: start_child.return_value = child r = self.client.regular_with_hook( 'somewhere', call_site_tag='somewhere') assert r == 'oh yeah' start_child.assert_called_once_with( operation_name='regular_with_hook', child_of=parent.context, tags=None) child.set_tag.assert_called_once_with( 'call_site_tag', 'somewhere') scope.close()
Example #12
Source File: test_tornado_http.py From opentracing-python-instrumentation with MIT License | 6 votes |
def test_http_fetch(base_url, http_client, tornado_http_patch, tracer): @tornado.gen.coroutine def make_downstream_call(): resp = yield http_client.fetch(base_url) raise tornado.gen.Return(resp) with patch('opentracing.tracer', tracer): assert opentracing.tracer == tracer # sanity check that patch worked span = tracer.start_span('test') trace_id = '{:x}'.format(span.context.trace_id) with span_in_stack_context(span): response = make_downstream_call() response = yield response # cannot yield when in StackContext context span.finish() assert response.code == 200 assert response.body.decode('utf-8') == trace_id
Example #13
Source File: test_traced_function_decorator.py From opentracing-python-instrumentation with MIT License | 6 votes |
def test_nested_functions(self): tracer = opentracing.tracer parent = opentracing.tracer.start_span('hello') with opentracing.tracer.scope_manager.activate(parent, True) as scope: self.client.regular_with_nested(123) spans = tracer.finished_spans() assert len(spans) == 3 root = spans[2] assert root.operation_name == 'regular_with_nested' assert spans[0].operation_name == 'regular' assert spans[0].parent_id == root.context.span_id assert spans[1].operation_name == 'some_name' assert spans[1].parent_id == root.context.span_id # Check parent context has been restored. assert tracer.scope_manager.active is scope
Example #14
Source File: test_traced_function_decorator.py From opentracing-python-instrumentation with MIT License | 6 votes |
def test_nested_functions_with_exception(self): tracer = opentracing.tracer parent = opentracing.tracer.start_span('hello') with opentracing.tracer.scope_manager.activate(parent, True) as scope: # First nested function (`regular`) raises Exception. with pytest.raises(AssertionError): self.client.regular_with_nested(999) spans = tracer.finished_spans() # Second nested function has not been invoked. assert len(spans) == 2 root = spans[1] assert root.operation_name == 'regular_with_nested' assert spans[0].operation_name == 'regular' assert spans[0].parent_id == root.context.span_id assert len(spans[0].tags) == 1 assert spans[0].tags['error'] == 'true' assert spans[0].logs[0].key_values['event'] == 'exception' # Check parent context has been restored. assert tracer.scope_manager.active is scope
Example #15
Source File: test_middleware.py From python-django with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_tracer_callable_str(self): settings.OPENTRACING_TRACER_CALLABLE = 'opentracing.mocktracer.MockTracer' settings.OPENTRACING_TRACER_PARAMETERS = { 'scope_manager': ThreadLocalScopeManager() } OpenTracingMiddleware() assert getattr(settings, 'OPENTRACING_TRACING', None) is not None assert isinstance(settings.OPENTRACING_TRACING.tracer, MockTracer)
Example #16
Source File: test_tornado_http.py From opentracing-python-instrumentation with MIT License | 5 votes |
def tracer(): t = BasicTracer( recorder=InMemoryRecorder(), scope_manager=TornadoScopeManager(), ) t.register_required_propagators() return t
Example #17
Source File: tchannel.py From tchannel-python with MIT License | 5 votes |
def tracer(self): if self._tracer: return self._tracer else: return opentracing.tracer
Example #18
Source File: test_crossdock.py From tchannel-python with MIT License | 5 votes |
def tracer(): tracer = Tracer( service_name='test-tracer', sampler=ConstSampler(True), reporter=InMemoryReporter(), scope_manager=TornadoScopeManager() ) opentracing.set_global_tracer(tracer) try: yield tracer finally: opentracing._reset_global_tracer() tracer.close()
Example #19
Source File: test_middleware.py From python-django with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_tracing_none(self): OpenTracingMiddleware() assert getattr(settings, 'OPENTRACING_TRACING', None) is not None assert settings.OPENTRACING_TRACING.tracer is opentracing.tracer assert settings.OPENTRACING_TRACING._get_tracer_impl() is None
Example #20
Source File: test_middleware.py From python-django with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_set_global_tracer_no_tracing(self): settings.OPENTRACING_SET_GLOBAL_TRACER = True with mock.patch('opentracing.tracer'): OpenTracingMiddleware() assert getattr(settings, 'OPENTRACING_TRACING', None) is not None assert settings.OPENTRACING_TRACING.tracer is opentracing.tracer assert settings.OPENTRACING_TRACING._get_tracer_impl() is None
Example #21
Source File: tracing.py From tchannel-python with MIT License | 5 votes |
def api_check(tracer): tracer = tracer or opentracing.tracer assert not hasattr(tracer, 'join'), \ 'This version of TChannel requires opentracing>=1.1'
Example #22
Source File: tracing.py From python-django with BSD 3-Clause "New" or "Revised" License | 5 votes |
def __init__(self, tracer=None, start_span_cb=None): if start_span_cb is not None and not callable(start_span_cb): raise ValueError('start_span_cb is not callable') self._tracer_implementation = tracer self._start_span_cb = start_span_cb self._current_scopes = {} self._trace_all = False
Example #23
Source File: test_tornado_http.py From opentracing-python-instrumentation with MIT License | 5 votes |
def get(self): request = TornadoRequestWrapper(self.request) with before_request(request, tracer=opentracing.tracer) as span: self.write('{:x}'.format(span.context.trace_id)) self.set_status(200)
Example #24
Source File: test_traced_function_decorator.py From opentracing-python-instrumentation with MIT License | 5 votes |
def test_no_arg_decorator(self): parent = opentracing.tracer.start_span('hello') @gen.coroutine def run(): # test both co-routine and regular function for func in ('regular', 'coro', ): child = mock.Mock() # verify start_child is called with actual function name with patch_object(opentracing.tracer, 'start_span', return_value=child) as start_child: r = yield self.call(func, 123) start_child.assert_called_once_with( operation_name=func, child_of=parent.context, tags=None) child.set_tag.assert_not_called() child.error.assert_not_called() child.finish.assert_called_once() assert r == 'oh yeah' # verify span.error() is called on exception child = mock.Mock() with patch_object(opentracing.tracer, 'start_span') \ as start_child: start_child.return_value = child with pytest.raises(AssertionError): yield self.call(func, 999) child.log.assert_called_once() child.finish.assert_called_once() raise tornado.gen.Return(1) yield run_coroutine_with_span(span=parent, coro=run)
Example #25
Source File: test_traced_function_decorator.py From opentracing-python-instrumentation with MIT License | 5 votes |
def test_no_parent_span(self): with patch_object(opentracing.tracer, 'start_span') as start: r = self.client.regular(123) assert r == 'oh yeah' start.assert_called_once_with( operation_name='regular', child_of=None, tags=None) # verify no new trace or child span is started with patch_object(opentracing.tracer, 'start_span') as start: r = self.client.regular_require_active_trace(123) assert r == 'oh yeah' start.assert_not_called()
Example #26
Source File: test_traced_function_decorator.py From opentracing-python-instrumentation with MIT License | 5 votes |
def test_no_arg_decorator(self): parent = opentracing.tracer.start_span('hello') with opentracing.tracer.scope_manager.activate(parent, True) as scope: child = mock.Mock() # verify start_child is called with actual function name with patch_object(opentracing.tracer, 'start_span', return_value=child) as start_child: r = self.client.regular(123) start_child.assert_called_once_with( operation_name='regular', child_of=parent.context, tags=None) child.set_tag.assert_not_called() child.error.assert_not_called() child.finish.assert_called_once() assert r == 'oh yeah' # verify span.error() is called on exception child = mock.Mock() with patch_object(opentracing.tracer, 'start_span') as start_child: start_child.return_value = child with pytest.raises(AssertionError): self.client.regular(999) child.log.assert_called_once() child.finish.assert_called_once() scope.close()
Example #27
Source File: test_traced_function_decorator.py From opentracing-python-instrumentation with MIT License | 5 votes |
def setUp(self): super(PrepareMixin, self).setUp() self.patcher = mock.patch( 'opentracing.tracer', MockTracer(self.scope_manager())) self.patcher.start() self.client = Client()
Example #28
Source File: test_local_span.py From opentracing-python-instrumentation with MIT License | 5 votes |
def test_db_span(): tracer = opentracing.tracer span = tracer.start_span(operation_name='parent') with span_in_context(span=span): with db_span(_COMMIT, 'MySQLdb') as child_span: assert span is child_span with db_span('select * from X', 'MySQLdb') as child_span: assert span is child_span
Example #29
Source File: test_local_span.py From opentracing-python-instrumentation with MIT License | 5 votes |
def test_func_span(): tracer = opentracing.tracer span = tracer.start_span(operation_name='parent') with span_in_context(span=span): with func_span('test') as child_span: assert span is child_span with func_span('test', tags={'x': 'y'}) as child_span: assert span is child_span
Example #30
Source File: test_globaltracer.py From opentracing-python with Apache License 2.0 | 5 votes |
def test_opentracing_tracer(): assert opentracing.tracer is opentracing.global_tracer() assert isinstance(opentracing.global_tracer(), opentracing.Tracer)