本文整理汇总了Python中opentracing.tracer方法的典型用法代码示例。如果您正苦于以下问题:Python opentracing.tracer方法的具体用法?Python opentracing.tracer怎么用?Python opentracing.tracer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类opentracing
的用法示例。
在下文中一共展示了opentracing.tracer方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: before_sending_request
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
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
示例2: start_child_span
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
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
)
示例3: test_decorator_with_start_hook
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
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()
开发者ID:uber-common,项目名称:opentracing-python-instrumentation,代码行数:21,代码来源:test_traced_function_decorator.py
示例4: test_nested_functions
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
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
开发者ID:uber-common,项目名称:opentracing-python-instrumentation,代码行数:20,代码来源:test_traced_function_decorator.py
示例5: test_nested_functions_with_exception
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
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
开发者ID:uber-common,项目名称:opentracing-python-instrumentation,代码行数:24,代码来源:test_traced_function_decorator.py
示例6: test_no_parent_span
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
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)
开发者ID:uber-common,项目名称:opentracing-python-instrumentation,代码行数:24,代码来源:test_traced_function_decorator.py
示例7: test_http_fetch
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
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
示例8: initialize_tracer
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
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
示例9: initialize_global_tracer
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
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
示例10: span_in_context
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
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)
示例11: start_basic_span
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
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')
示例12: span_to_tracing_field
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
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()
示例13: serve
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
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()
示例14: test_opentracing_tracer
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
def test_opentracing_tracer():
assert opentracing.tracer is opentracing.global_tracer()
assert isinstance(opentracing.global_tracer(), opentracing.Tracer)
示例15: test_set_global_tracer
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import tracer [as 别名]
def test_set_global_tracer():
tracer = mock.Mock()
opentracing.set_global_tracer(tracer)
assert opentracing.global_tracer() is tracer
assert opentracing.is_global_tracer_registered()
# Register another value.
tracer = mock.Mock()
opentracing.set_global_tracer(tracer)
assert opentracing.global_tracer() is tracer
assert opentracing.is_global_tracer_registered()