opentelemetry.sdk.trace package¶
Submodules¶
-
class
opentelemetry.sdk.trace.SpanProcessor[source]¶ Bases:
objectInterface which allows hooks for SDK’s
Spanstart and end method invocations.Span processors can be registered directly using
TracerProvider.add_span_processor()and they are invoked in the same order as they were registered.-
on_start(span, parent_context=None)[source]¶ Called when a
opentelemetry.trace.Spanis started.This method is called synchronously on the thread that starts the span, therefore it should not block or throw an exception.
- Parameters
span (
Span) – Theopentelemetry.trace.Spanthat just started.parent_context (
Optional[Context]) – The parent context of the span that just started.
- Return type
None
-
on_end(span)[source]¶ Called when a
opentelemetry.trace.Spanis ended.This method is called synchronously on the thread that ends the span, therefore it should not block or throw an exception.
- Parameters
span (
Span) – Theopentelemetry.trace.Spanthat just ended.- Return type
None
-
shutdown()[source]¶ Called when a
opentelemetry.sdk.trace.Traceris shutdown.- Return type
None
-
-
class
opentelemetry.sdk.trace.SynchronousMultiSpanProcessor[source]¶ Bases:
opentelemetry.sdk.trace.SpanProcessorImplementation of class:
SpanProcessorthat forwards all received events to a list of span processors sequentially.The underlying span processors are called in sequential order as they were added.
-
add_span_processor(span_processor)[source]¶ Adds a SpanProcessor to the list handled by this instance.
- Return type
None
-
on_start(span, parent_context=None)[source]¶ Called when a
opentelemetry.trace.Spanis started.This method is called synchronously on the thread that starts the span, therefore it should not block or throw an exception.
- Parameters
span (
Span) – Theopentelemetry.trace.Spanthat just started.parent_context (
Optional[Context]) – The parent context of the span that just started.
- Return type
None
-
on_end(span)[source]¶ Called when a
opentelemetry.trace.Spanis ended.This method is called synchronously on the thread that ends the span, therefore it should not block or throw an exception.
- Parameters
span (
Span) – Theopentelemetry.trace.Spanthat just ended.- Return type
None
-
force_flush(timeout_millis=30000)[source]¶ Sequentially calls force_flush on all underlying
SpanProcessor- Parameters
timeout_millis (
int) – The maximum amount of time over all span processors to wait for spans to be exported. In case the first n span processors exceeded the timeout followup span processors will be skipped.- Return type
- Returns
True if all span processors flushed their spans within the given timeout, False otherwise.
-
-
class
opentelemetry.sdk.trace.ConcurrentMultiSpanProcessor(num_threads=2)[source]¶ Bases:
opentelemetry.sdk.trace.SpanProcessorImplementation of
SpanProcessorthat forwards all received events to a list of span processors in parallel.Calls to the underlying span processors are forwarded in parallel by submitting them to a thread pool executor and waiting until each span processor finished its work.
- Parameters
num_threads (
int) – The number of threads managed by the thread pool executor and thus defining how many span processors can work in parallel.
-
add_span_processor(span_processor)[source]¶ Adds a SpanProcessor to the list handled by this instance.
- Return type
None
-
on_start(span, parent_context=None)[source]¶ Called when a
opentelemetry.trace.Spanis started.This method is called synchronously on the thread that starts the span, therefore it should not block or throw an exception.
- Parameters
span (
Span) – Theopentelemetry.trace.Spanthat just started.parent_context (
Optional[Context]) – The parent context of the span that just started.
- Return type
None
-
on_end(span)[source]¶ Called when a
opentelemetry.trace.Spanis ended.This method is called synchronously on the thread that ends the span, therefore it should not block or throw an exception.
- Parameters
span (
Span) – Theopentelemetry.trace.Spanthat just ended.- Return type
None
-
class
opentelemetry.sdk.trace.Event(name, attributes=None, timestamp=None)[source]¶ Bases:
opentelemetry.sdk.trace.EventBaseA text annotation with a set of attributes.
- Parameters
name (
str) – Name of the event.attributes (
Optional[Mapping[str,Union[str,bool,int,float,Sequence[Optional[str]],Sequence[Optional[bool]],Sequence[Optional[int]],Sequence[Optional[float]]]]]) – Attributes of the event.timestamp (
Optional[int]) – Timestamp of the event. IfNoneit will filled automatically.
-
class
opentelemetry.sdk.trace.Span(name, context, parent=None, sampler=None, trace_config=None, resource=<opentelemetry.sdk.resources.Resource object>, attributes=None, events=None, links=(), kind=<SpanKind.INTERNAL: 0>, span_processor=<opentelemetry.sdk.trace.SpanProcessor object>, instrumentation_info=None, set_status_on_exception=True)[source]¶ Bases:
opentelemetry.trace.span.SpanUsers should create
Spanobjects via theTracerinstead of this constructor.- Parameters
name (
str) – The name of the operation this span representscontext (
SpanContext) – The immutable span contextparent (
Optional[SpanContext]) – This span’s parent’sopentelemetry.trace.SpanContext, or None if this is a root spansampler (
Optional[Sampler]) – The sampler used to create this spantrace_config (
None) – TODOresource (
Resource) – Entity producing telemetryattributes (
Optional[Mapping[str,Union[str,bool,int,float,Sequence[Optional[str]],Sequence[Optional[bool]],Sequence[Optional[int]],Sequence[Optional[float]]]]]) – The span’s attributes to be exportedevents (
Optional[Sequence[Event]]) – Timestamped events to be exportedlinks (
Sequence[Link]) – Links to other spans to be exportedspan_processor (
SpanProcessor) –SpanProcessorto invoke when starting and ending thisSpan.
-
property
start_time¶
-
property
end_time¶
-
get_span_context()[source]¶ Gets the span’s SpanContext.
Get an immutable, serializable identifier for this span that can be used to create new child spans.
- Returns
A
opentelemetry.trace.SpanContextwith a copy of this span’s immutable state.
-
set_attribute(key, value)[source]¶ Sets an Attribute.
Sets a single Attribute with the key and value passed as arguments.
- Return type
None
-
add_event(name, attributes=None, timestamp=None)[source]¶ Adds an
Event.Adds a single
Eventwith the name and, optionally, a timestamp and attributes passed as arguments. Implementations should generate a timestamp if thetimestampargument is omitted.- Return type
None
-
end(end_time=None)[source]¶ Sets the current time as the span’s end time.
The span’s end time is the wall time at which the operation finished.
Only the first call to
endshould modify the span, and implementations are free to ignore or raise on further calls.- Return type
None
-
class
opentelemetry.sdk.trace.Tracer(sampler, resource, span_processor, ids_generator, instrumentation_info)[source]¶ Bases:
opentelemetry.trace.TracerSee
opentelemetry.trace.Tracer.-
start_as_current_span(name, context=None, kind=<SpanKind.INTERNAL: 0>, attributes=None, links=(), record_exception=True)[source]¶ Context manager for creating a new span and set it as the current span in this tracer’s context.
Exiting the context manager will call the span’s end method, as well as return the current span to it’s previous value by returning to the previous context.
Example:
with tracer.start_as_current_span("one") as parent: parent.add_event("parent's event") with trace.start_as_current_span("two") as child: child.add_event("child's event") trace.get_current_span() # returns child trace.get_current_span() # returns parent trace.get_current_span() # returns previously active span
This is a convenience method for creating spans attached to the tracer’s context. Applications that need more control over the span lifetime should use
start_span()instead. For example:with tracer.start_as_current_span(name) as span: do_work()
is equivalent to:
span = tracer.start_span(name) with tracer.use_span(span, end_on_exit=True): do_work()
- Parameters
name (
str) – The name of the span to be created.context (
Optional[Context]) – An optional Context containing the span’s parent. Defaults to the global context.kind (
SpanKind) – The span’s kind (relationship to parent). Note that is meaningful even if there is no parent.attributes (
Optional[Mapping[str,Union[str,bool,int,float,Sequence[Optional[str]],Sequence[Optional[bool]],Sequence[Optional[int]],Sequence[Optional[float]]]]]) – The span’s attributes.record_exception (
bool) – Whether to record any exceptions raised within the context as error event on the span.
- Yields
The newly-created span.
- Return type
-
start_span(name, context=None, kind=<SpanKind.INTERNAL: 0>, attributes=None, links=(), start_time=None, set_status_on_exception=True)[source]¶ Starts a span.
Create a new span. Start the span without setting it as the current span in the context. To start the span and use the context in a single method, see
start_as_current_span().By default the current span in the context will be used as parent, but an explicit context can also be specified, by passing in a
Contextcontaining a currentSpan. If there is no current span in the globalContextor in the specified context, the created span will be a root span.The span can be used as a context manager. On exiting the context manager, the span’s end() method will be called.
Example:
# trace.get_current_span() will be used as the implicit parent. # If none is found, the created span will be a root instance. with tracer.start_span("one") as child: child.add_event("child's event")
- Parameters
name (
str) – The name of the span to be created.context (
Optional[Context]) – An optional Context containing the span’s parent. Defaults to the global context.kind (
SpanKind) – The span’s kind (relationship to parent). Note that is meaningful even if there is no parent.attributes (
Optional[Mapping[str,Union[str,bool,int,float,Sequence[Optional[str]],Sequence[Optional[bool]],Sequence[Optional[int]],Sequence[Optional[float]]]]]) – The span’s attributes.set_status_on_exception (
bool) – Only relevant if the returned span is used in a with/context manager. Defines wether the span status will be automatically set to ERROR when an uncaught exception is raised in the span with block. The span status won’t be set by this mechanism if it was previously set manually.
- Return type
- Returns
The newly-created span.
-
use_span(span, end_on_exit=False, record_exception=True)[source]¶ Context manager for setting the passed span as the current span in the context, as well as resetting the context back upon exiting the context manager.
Set the given span as the current span in this tracer’s context.
On exiting the context manager set the span that was previously active as the current span (this is usually but not necessarily the parent of the given span). If
end_on_exitisTrue, then the span is also ended when exiting the context manager.
-
-
class
opentelemetry.sdk.trace.TracerProvider(sampler=<opentelemetry.sdk.trace.sampling.ParentBased object>, resource=<opentelemetry.sdk.resources.Resource object>, shutdown_on_exit=True, active_span_processor=None, ids_generator=None)[source]¶ Bases:
opentelemetry.trace.TracerProvider-
get_tracer(instrumenting_module_name, instrumenting_library_version='')[source]¶ Returns a
Tracerfor use by the given instrumentation library.For any two calls it is undefined whether the same or different
Tracerinstances are returned, even for different library names.This function may return different
Tracertypes (e.g. a no-op tracer vs. a functional tracer).- Parameters
instrumenting_module_name (
str) –The name of the instrumenting module (usually just
__name__).This should not be the name of the module that is instrumented but the name of the module doing the instrumentation. E.g., instead of
"requests", use"opentelemetry.instrumentation.requests".instrumenting_library_version (
str) – Optional. The version string of the instrumenting library. Usually this should be the same aspkg_resources.get_distribution(instrumenting_library_name).version.
- Return type
-
add_span_processor(span_processor)[source]¶ Registers a new
SpanProcessorfor thisTracerProvider.The span processors are invoked in the same order they are registered.
- Return type
None
-
force_flush(timeout_millis=30000)[source]¶ Requests the active span processor to process all spans that have not yet been processed.
By default force flush is called sequentially on all added span processors. This means that span processors further back in the list have less time to flush their spans. To have span processors flush their spans in parallel it is possible to initialize the tracer provider with an instance of
ConcurrentMultiSpanProcessorat the cost of using multiple threads.
-