本文整理汇总了Python中opentracing.Span方法的典型用法代码示例。如果您正苦于以下问题:Python opentracing.Span方法的具体用法?Python opentracing.Span怎么用?Python opentracing.Span使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类opentracing
的用法示例。
在下文中一共展示了opentracing.Span方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: start_active_span
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def start_active_span(self,
operation_name,
child_of=None,
references=None,
tags=None,
start_time=None,
ignore_active_span=False,
finish_on_close=True):
# create a new Span
span = self.start_span(
operation_name=operation_name,
child_of=child_of,
references=references,
tags=tags,
start_time=start_time,
ignore_active_span=ignore_active_span,
)
return self.scope_manager.activate(span, finish_on_close)
示例2: get_package_information
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def get_package_information(self, parent_span: opentracing.Span) -> list:
if self.is_stdlib:
return [{
"package": {"name": "cpython"},
"dependencies": []
}]
else:
return [
{
"package": {"name": p},
# multiple packages in the project share the same
# dependencies
"dependencies": self.get_dependencies(parent_span)
} for p in self.project_packages
]
# finds a project module using the newer, more dynamic import rules detailed in PEP 420
# (see https://www.python.org/dev/peps/pep-0420/)
示例3: test_log_kv
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def test_log_kv(self):
"""Test the `log_kv()` method on `Span` objects."""
with self.shim.start_span("TestSpan") as span:
span.log_kv({"foo": "bar"})
self.assertEqual(span.unwrap().events[0].attributes["foo"], "bar")
# Verify timestamp was generated automatically.
self.assertIsNotNone(span.unwrap().events[0].timestamp)
# Test explicit timestamp.
now = time.time()
span.log_kv({"foo": "bar"}, now)
result = util.time_seconds_from_ns(
span.unwrap().events[1].timestamp
)
self.assertEqual(span.unwrap().events[1].attributes["foo"], "bar")
# Tolerate inaccuracies of less than a microsecond. See Note:
# https://open-telemetry.github.io/opentelemetry-python/opentelemetry.ext.opentracing_shim.html
# TODO: This seems to work consistently, but we should find out the
# biggest possible loss of precision.
self.assertAlmostEqual(result, now, places=6)
示例4: get_current_span
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def get_current_span():
"""
Access current request context and extract current Span from it.
:return:
Return current span associated with the current request context.
If no request context is present in thread local, or the context
has no span, return None.
"""
# Check against the old, ScopeManager-less implementation,
# for backwards compatibility.
context = RequestContextManager.current_context()
if context is not None:
return context.span
active = opentracing.tracer.scope_manager.active
return active.span if active else None
示例5: finish
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def finish(self, finish_time=None):
"""Indicate that the work represented by this span has been completed
or terminated, and is ready to be sent to the Reporter.
If any tags / logs need to be added to the span, it should be done
before calling finish(), otherwise they may be ignored.
:param finish_time: an explicit Span finish timestamp as a unix
timestamp per time.time()
"""
if not self.is_sampled():
return
with self.update_lock:
if self.finished:
logger.warning('Span has already been finished; will not be reported again.')
return
self.finished = True
self.end_time = finish_time or time.time()
self.tracer.report_span(self)
示例6: incoming_trace
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def incoming_trace(operation: str, request: Request,
tracer: Tracer) -> Generator[Span, None, None]:
span_context = tracer.extract(
format=Format.HTTP_HEADERS,carrier=dict(request.headers))
params = {}
if span_context:
params["child_of"] = span_context
with tracer.start_span(operation, **params) as span:
span.set_tag('http.url', request.url)
remote_ip = request.client.host
if remote_ip:
span.set_tag(tags.PEER_HOST_IPV4, remote_ip)
remote_port = request.client.port
if remote_port:
span.set_tag(tags.PEER_PORT, remote_port)
yield span
示例7: incoming_trace
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def incoming_trace(operation: str, request: Request,
tracer: Tracer) -> Generator[Span, None, None]:
span_context = tracer.extract(
format=Format.HTTP_HEADERS,carrier=dict(request.headers))
print(request.headers, flush=True)
print(span_context, flush=True)
params = {}
if span_context:
params["child_of"] = span_context
with tracer.start_span(operation, **params) as span:
span.set_tag('http.url', request.url)
remote_ip = request.client.host
if remote_ip:
span.set_tag(tags.PEER_HOST_IPV4, remote_ip)
remote_port = request.client.port
if remote_port:
span.set_tag(tags.PEER_PORT, remote_port)
yield span
示例8: outgoing_trace
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def outgoing_trace(operation: str, request: Request, tracer: Tracer,
parent: Span) \
-> Generator[Tuple[Span, Dict[str, Any]], None, None]:
with tracer.start_span(operation, child_of=parent) as span:
span.set_tag('http.url', request.url)
remote_ip = request.client.host
if remote_ip:
span.set_tag(tags.PEER_HOST_IPV4, remote_ip)
remote_port = request.client.port
if remote_port:
span.set_tag(tags.PEER_PORT, remote_port)
http_header_carrier = {}
tracer.inject(
span_context=span,
format=Format.HTTP_HEADERS,
carrier=http_header_carrier
)
yield (span, http_header_carrier)
示例9: outgoing_trace
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def outgoing_trace(operation: str, request: Request, tracer: Tracer,
parent: Span) \
-> Generator[Tuple[Span, Dict[str, Any]], None, None]:
with tracer.start_span(operation, child_of=parent) as span:
span.set_tag('http.url', request.url)
remote_ip = request.client.host
if remote_ip:
span.set_tag(tags.PEER_HOST_IPV4, remote_ip)
remote_port = request.client.port
if remote_port:
span.set_tag(tags.PEER_PORT, remote_port)
http_header_carrier = {}
tracer.inject(
span_context=span,
format=Format.HTTP_HEADERS,
carrier=http_header_carrier
)
yield span, http_header_carrier
示例10: open_module_file
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def open_module_file(self, the_module: Module,
parent_span: opentracing.Span):
if the_module.path not in self.source_paths:
return None
elif the_module.is_external:
return DummyFile(self.local_fs.open(the_module.path, parent_span))
else:
return DummyFile(self.fs.open(the_module.path, parent_span))
示例11: get_dependencies
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def get_dependencies(self, parent_span: opentracing.Span) -> list:
top_level_stdlib = {p.split(".")[0] for p in self.stdlib}
top_level_imports = get_imports(
self.fs, self.PROJECT_ROOT, parent_span)
stdlib_imports = top_level_imports & top_level_stdlib
external_imports = top_level_imports - top_level_stdlib - self.project_packages
dependencies = [{"attributes": {"name": n}} for n in external_imports]
if stdlib_imports:
dependencies.append({
"attributes": {
"name": "cpython",
"repoURL": "git://github.com/python/cpython"
}
})
return dependencies
示例12: unwrap
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def unwrap(self):
"""Returns the wrapped :class:`opentelemetry.trace.Span` object.
Returns:
The :class:`opentelemetry.trace.Span` object wrapped by this
:class:`SpanShim`.
"""
return self._otel_span
示例13: from_context_manager
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def from_context_manager(cls, manager, span_cm):
"""Constructs a :class:`ScopeShim` from an OpenTelemetry
`opentelemetry.trace.Span` context
manager.
The method extracts a `opentelemetry.trace.Span` object from the
context manager by calling the context manager's ``__enter__()``
method. This causes the span to start in the OpenTelemetry tracer.
Example usage::
span = otel_tracer.start_span("TestSpan")
span_cm = otel_tracer.use_span(span)
scope_shim = ScopeShim.from_context_manager(
scope_manager_shim,
span_cm=span_cm,
)
Args:
manager: The :class:`ScopeManagerShim` that created this
:class:`ScopeShim`.
span_cm: A context manager as returned by
:meth:`opentelemetry.trace.Tracer.use_span`.
"""
otel_span = span_cm.__enter__()
span_context = SpanContextShim(otel_span.get_context())
span = SpanShim(manager.tracer, span_context, otel_span)
return cls(manager, span, span_cm)
示例14: test_start_active_span
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def test_start_active_span(self):
"""Test span creation and activation using `start_active_span()`."""
with self.shim.start_active_span("TestSpan") as scope:
# Verify correct type of Scope and Span objects.
self.assertIsInstance(scope, opentracing.Scope)
self.assertIsInstance(scope.span, opentracing.Span)
# Verify span is started.
self.assertIsNotNone(scope.span.unwrap().start_time)
# Verify span is active.
self.assertEqual(
self.shim.active_span.context.unwrap(),
scope.span.context.unwrap(),
)
# TODO: We can't check for equality of self.shim.active_span and
# scope.span because the same OpenTelemetry span is returned inside
# different SpanShim objects. A possible solution is described
# here:
# https://github.com/open-telemetry/opentelemetry-python/issues/161#issuecomment-534136274
# Verify span has ended.
self.assertIsNotNone(scope.span.unwrap().end_time)
# Verify no span is active.
self.assertIsNone(self.shim.active_span)
示例15: test_start_span
# 需要导入模块: import opentracing [as 别名]
# 或者: from opentracing import Span [as 别名]
def test_start_span(self):
"""Test span creation using `start_span()`."""
with self.shim.start_span("TestSpan") as span:
# Verify correct type of Span object.
self.assertIsInstance(span, opentracing.Span)
# Verify span is started.
self.assertIsNotNone(span.unwrap().start_time)
# Verify `start_span()` does NOT make the span active.
self.assertIsNone(self.shim.active_span)
# Verify span has ended.
self.assertIsNotNone(span.unwrap().end_time)