File: //var/opt/nydus/ops/mysql/opentelemetry/sdk/trace/__pycache__/sampling.cpython-39.pyc
a
�,�hpA � @ s� d Z ddlZddlZddlZddlmZ ddlmZ ddlm Z m
Z
ddlmZ ddl
mZmZ ddlmZmZmZ dd lmZ dd
lmZ ee�ZG dd� dej�ZG d
d� d�ZG dd� dej�ZG dd� de�Zeej �Z!eej"�Z#G dd� de�Z$G dd� de�Z%e%e!�Z&e%e#�Z'G dd� de%�Z(G dd� de�Z)G dd� de�Z*G dd� de%�Z+G dd � d e%�Z,e#e!e'e&e$e(d!�Z-ed"�d#d$�Z.e d% d"�d&d'�Z/dS )(a�
For general information about sampling, see `the specification <https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/trace/sdk.md#sampling>`_.
OpenTelemetry provides two types of samplers:
- `StaticSampler`
- `TraceIdRatioBased`
A `StaticSampler` always returns the same sampling result regardless of the conditions. Both possible StaticSamplers are already created:
- Always sample spans: ALWAYS_ON
- Never sample spans: ALWAYS_OFF
A `TraceIdRatioBased` sampler makes a random sampling result based on the sampling probability given.
If the span being sampled has a parent, `ParentBased` will respect the parent delegate sampler. Otherwise, it returns the sampling result from the given root sampler.
Currently, sampling results are always made during the creation of the span. However, this might not always be the case in the future (see `OTEP #115 <https://github.com/open-telemetry/oteps/pull/115>`_).
Custom samplers can be created by subclassing `Sampler` and implementing `Sampler.should_sample` as well as `Sampler.get_description`.
Samplers are able to modify the `mysql.opentelemetry.trace.span.TraceState` of the parent of the span being created. For custom samplers, it is suggested to implement `Sampler.should_sample` to utilize the
parent span context's `mysql.opentelemetry.trace.span.TraceState` and pass into the `SamplingResult` instead of the explicit trace_state field passed into the parameter of `Sampler.should_sample`.
To use a sampler, pass it into the tracer provider constructor. For example:
.. code:: python
from opentelemetry import trace
from mysql.opentelemetry.sdk.trace import TracerProvider
from mysql.opentelemetry.sdk.trace.export import (
ConsoleSpanExporter,
SimpleSpanProcessor,
)
from mysql.opentelemetry.sdk.trace.sampling import TraceIdRatioBased
# sample 1 in every 1000 traces
sampler = TraceIdRatioBased(1/1000)
# set the sampler onto the global tracer provider
trace.set_tracer_provider(TracerProvider(sampler=sampler))
# set up an exporter for sampled spans
trace.get_tracer_provider().add_span_processor(
SimpleSpanProcessor(ConsoleSpanExporter())
)
# created spans will now be sampled by the TraceIdRatioBased sampler
with trace.get_tracer(__name__).start_as_current_span("Test Span"):
...
The tracer sampler can also be configured via environment variables ``OTEL_TRACES_SAMPLER`` and ``OTEL_TRACES_SAMPLER_ARG`` (only if applicable).
The list of built-in values for ``OTEL_TRACES_SAMPLER`` are:
* always_on - Sampler that always samples spans, regardless of the parent span's sampling decision.
* always_off - Sampler that never samples spans, regardless of the parent span's sampling decision.
* traceidratio - Sampler that samples probabalistically based on rate.
* parentbased_always_on - (default) Sampler that respects its parent span's sampling decision, but otherwise always samples.
* parentbased_always_off - Sampler that respects its parent span's sampling decision, but otherwise never samples.
* parentbased_traceidratio - Sampler that respects its parent span's sampling decision, but otherwise samples probabalistically based on rate.
Sampling probability can be set with ``OTEL_TRACES_SAMPLER_ARG`` if the sampler is traceidratio or parentbased_traceidratio. Rate must be in the range [0.0,1.0]. When not provided rate will be set to
1.0 (maximum rate possible).
Prev example but with environment variables. Please make sure to set the env ``OTEL_TRACES_SAMPLER=traceidratio`` and ``OTEL_TRACES_SAMPLER_ARG=0.001``.
.. code:: python
from opentelemetry import trace
from mysql.opentelemetry.sdk.trace import TracerProvider
from mysql.opentelemetry.sdk.trace.export import (
ConsoleSpanExporter,
SimpleSpanProcessor,
)
trace.set_tracer_provider(TracerProvider())
# set up an exporter for sampled spans
trace.get_tracer_provider().add_span_processor(
SimpleSpanProcessor(ConsoleSpanExporter())
)
# created spans will now be sampled by the TraceIdRatioBased sampler with rate 1/1000.
with trace.get_tracer(__name__).start_as_current_span("Test Span"):
...
When utilizing a configurator, you can configure a custom sampler. In order to create a configurable custom sampler, create an entry point for the custom sampler
factory method or function under the entry point group, ``opentelemetry_traces_sampler``. The custom sampler factory method must be of type ``Callable[[str], Sampler]``, taking a single string argument and
returning a Sampler object. The single input will come from the string value of the ``OTEL_TRACES_SAMPLER_ARG`` environment variable. If ``OTEL_TRACES_SAMPLER_ARG`` is not configured, the input will
be an empty string. For example:
.. code:: python
setup(
...
entry_points={
...
"opentelemetry_traces_sampler": [
"custom_sampler_name = path.to.sampler.factory.method:CustomSamplerFactory.get_sampler"
]
}
)
# ...
class CustomRatioSampler(Sampler):
def __init__(rate):
# ...
# ...
class CustomSamplerFactory:
@staticmethod
get_sampler(sampler_argument):
try:
rate = float(sampler_argument)
return CustomSampler(rate)
except ValueError: # In case argument is empty string.
return CustomSampler(0.5)
In order to configure you application with a custom sampler's entry point, set the ``OTEL_TRACES_SAMPLER`` environment variable to the key name of the entry point. For example, to configured the
above sampler, set ``OTEL_TRACES_SAMPLER=custom_sampler_name`` and ``OTEL_TRACES_SAMPLER_ARG=0.5``.
� N)� getLogger)�MappingProxyType)�Optional�Sequence)�Context)�OTEL_TRACES_SAMPLER�OTEL_TRACES_SAMPLER_ARG)�Link�SpanKind�get_current_span)�
TraceState)�
Attributesc @ s( e Zd ZdZdZdZdd� Zdd� ZdS ) �Decisionr � � c C s | t jt jfv S �N)r �RECORD_ONLY�RECORD_AND_SAMPLE��self� r �Y/opt/nydus/tmp/pip-target-wkfpz8uv/lib64/python/mysql/opentelemetry/sdk/trace/sampling.py�is_recording� s zDecision.is_recordingc C s
| t ju S r )r r r r r r �
is_sampled� s zDecision.is_sampledN)�__name__�
__module__�__qualname__�DROPr r r r r r r r r � s
r c @ s4 e Zd ZdZed�dd�Zdedddd�d d
�ZdS )�SamplingResulta� A sampling result as applied to a newly-created Span.
Args:
decision: A sampling decision based off of whether the span is recorded
and the sampled flag in trace flags in the span context.
attributes: Attributes to add to the `mysql.opentelemetry.trace.Span`.
trace_state: The tracestate used for the `mysql.opentelemetry.trace.Span`.
Could possibly have been modified by the sampler.
��returnc C s( t | �j� dt| j�� dt| j�� d�S )N�(z
, attributes=�))�typer �str�decision�
attributesr r r r �__repr__� s zSamplingResult.__repr__Nr
r )r% r&