GitHub

otlp_tracer

API reference for otlp_tracer.

Exporting spans over OTLP/HTTP (Effect's OtlpTracer): a Tracer whose spans are posted to a traces endpoint when they end, with their ids, parent, attributes, events, links, timing, kind and status.

Classes

Functions

make

make(*, url, resource=None, headers=None, compression='none', export_interval=None, max_batch_size=None, context=None, shutdown_timeout=None) -> Effect[Tracer, Never, Flusher | OtlpSerialization | HttpClient | Scope]

A tracer exporting each sampled span to url when it ends, in batches of max_batch_size (1000) every export_interval (5 seconds), flushed for at most shutdown_timeout (3 seconds) when the scope closes. context runs every step of a fiber inside a span (an OpenTelemetry-style bridge).

ParameterTypeDescription
urlstr
resourceResourceOptions | None = None
headers_headers.Input | None = None
compressionCompression = 'none'
export_intervalDurationInput | None = None
max_batch_sizeint | None = None
contextSpanContextHook | None = None
shutdown_timeoutDurationInput | None = None

Returns Effect[Tracer, Never, Flusher | OtlpSerialization | HttpClient | Scope]

def make(
    *,
    url: str,
    resource: ResourceOptions | None = None,
    headers: _headers.Input | None = None,
    compression: Compression = "none",
    export_interval: DurationInput | None = None,
    max_batch_size: int | None = None,
    context: SpanContextHook | None = None,
    shutdown_timeout: DurationInput | None = None,
) -> Effect[Tracer, Never, Flusher | OtlpSerialization | HttpClient | Scope]:
    """A tracer exporting each sampled span to ``url`` when it ends, in
    batches of ``max_batch_size`` (1000) every ``export_interval`` (5
    seconds), flushed for at most ``shutdown_timeout`` (3 seconds) when the
    scope closes. ``context`` runs every step of a fiber inside a span
    (an OpenTelemetry-style bridge)."""

    @gen
    def build():
        otel_resource = yield from from_options(resource)
        serialization = yield from OtlpSerialization()
        scope = instrumentation_scope()

        def body(spans: list[Any]) -> tuple[Any, Effect[None]]:
            data: TraceData = {
                "resourceSpans": [
                    {"resource": otel_resource, "scopeSpans": [{"scope": scope, "spans": spans}]}
                ]
            }
            return serialization.traces(data), succeed(None)

        made = yield from exporter.make(
            label="OtlpTracer",
            url=url,
            headers=headers,
            compression=compression,
            export_interval="5 seconds" if export_interval is None else export_interval,
            max_batch_size=1000 if max_batch_size is None else max_batch_size,
            body=body,
            shutdown_timeout="3 seconds" if shutdown_timeout is None else shutdown_timeout,
        )

        def export(span: _OtlpTracerSpan) -> None:
            if span.sampled:
                made.push(_otlp_span(span))

        return Tracer(
            lambda request: _OtlpTracerSpan(request, export),
            None if context is None else _context_hook(context),
        )

    return build

layer

layer(*, url, resource=None, headers=None, compression='none', export_interval=None, max_batch_size=None, context=None, shutdown_timeout=None) -> Layer[Flusher, Never, OtlpSerialization | HttpClient]

Installs make's tracer, with the shared layer_flusher.

ParameterTypeDescription
urlstr
resourceResourceOptions | None = None
headers_headers.Input | None = None
compressionCompression = 'none'
export_intervalDurationInput | None = None
max_batch_sizeint | None = None
contextSpanContextHook | None = None
shutdown_timeoutDurationInput | None = None

Returns Layer[Flusher, Never, OtlpSerialization | HttpClient]

def layer(
    *,
    url: str,
    resource: ResourceOptions | None = None,
    headers: _headers.Input | None = None,
    compression: Compression = "none",
    export_interval: DurationInput | None = None,
    max_batch_size: int | None = None,
    context: SpanContextHook | None = None,
    shutdown_timeout: DurationInput | None = None,
) -> Layer[Flusher, Never, OtlpSerialization | HttpClient]:
    """Installs ``make``'s tracer, with the shared ``layer_flusher``."""
    tracer = make(
        url=url,
        resource=resource,
        headers=headers,
        compression=compression,
        export_interval=export_interval,
        max_batch_size=max_batch_size,
        context=context,
        shutdown_timeout=shutdown_timeout,
    )
    return _layer.effect(CurrentTracer, tracer).pipe(_layer.provide_merge(exporter.layer_flusher))

layer_from_config

layer_from_config(*, resource=None, headers=None, context=None) -> Layer[Flusher, Never, HttpClient | OtlpSerialization]

layer configured from the OpenTelemetry environment: nothing but the flusher unless OTEL_TRACES_EXPORTER lists otlp, an endpoint is set and OTEL_SDK_DISABLED is not true. Reads the compression, OTEL_EXPORTER_OTLP_(TRACES_)TIMEOUT / OTEL_BSP_EXPORT_TIMEOUT (shutdown), OTEL_BSP_SCHEDULE_DELAY and OTEL_BSP_MAX_EXPORT_BATCH_SIZE. Invalid configuration is a defect.

ParameterTypeDescription
resourceResourceOptions | None = None
headers_headers.Input | None = None
contextSpanContextHook | None = None

Returns Layer[Flusher, Never, HttpClient | OtlpSerialization]

def layer_from_config(
    *,
    resource: ResourceOptions | None = None,
    headers: _headers.Input | None = None,
    context: SpanContextHook | None = None,
) -> Layer[Flusher, Never, HttpClient | OtlpSerialization]:
    """``layer`` configured from the OpenTelemetry environment: nothing
    but the flusher unless ``OTEL_TRACES_EXPORTER`` lists ``otlp``, an
    endpoint is set and ``OTEL_SDK_DISABLED`` is not true. Reads the
    compression, ``OTEL_EXPORTER_OTLP_(TRACES_)TIMEOUT`` /
    ``OTEL_BSP_EXPORT_TIMEOUT`` (shutdown), ``OTEL_BSP_SCHEDULE_DELAY``
    and ``OTEL_BSP_MAX_EXPORT_BATCH_SIZE``. Invalid configuration is a
    defect."""

    @gen
    def build():
        disabled = yield from config.boolean("OTEL_SDK_DISABLED").with_default(False)
        endpoint = yield from env.endpoint("TRACES")
        exporters = yield from env.exporters("TRACES")
        if disabled or endpoint is None or "otlp" not in exporters:
            return exporter.layer_flusher
        compression = yield from env.compression("TRACES")
        base_timeout = yield from env.millis("OTEL_EXPORTER_OTLP_TIMEOUT")
        traces_timeout = yield from env.millis("OTEL_EXPORTER_OTLP_TRACES_TIMEOUT")
        export_timeout = yield from env.millis("OTEL_BSP_EXPORT_TIMEOUT")
        schedule_delay = yield from env.millis("OTEL_BSP_SCHEDULE_DELAY")
        max_batch_size = yield from config.integer("OTEL_BSP_MAX_EXPORT_BATCH_SIZE").with_default(
            None
        )
        signal_headers = headers if headers is not None else (yield from env.headers("TRACES"))
        return layer(
            url=str(endpoint),
            resource=resource,
            headers=signal_headers,
            compression=compression,
            export_interval=schedule_delay,
            max_batch_size=max_batch_size,
            context=context,
            shutdown_timeout=env.first_of(traces_timeout, base_timeout, export_timeout),
        )

    return _layer.unwrap(build.or_die())