GitHub

resource

API reference for resource.

The OpenTelemetry resource: what process or service emits the spans, logs and metrics (Effect's Resource).

layer(service_name=...) builds one from service metadata; layer_from_env() reads OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES through Config; layer_empty has no attributes.

AttributeTypeDescription
layer_emptyFinal[Layer[OtelResource]] = _layer.succeed(OtelResource, Resource.get_empty())

A resource with no attributes.

Classes

Functions

config_to_attributes

config_to_attributes(*, service_name, service_version=None, attributes=None) -> dict[str, AttributeValue]

The SDK defaults, then attributes, then the service name and version (an empty version counts as none), later ones winning.

ParameterTypeDescription
service_namestr
service_versionstr | None = None
attributesMapping[str, AttributeValue] | None = None

Returns dict[str, AttributeValue]

def config_to_attributes(
    *,
    service_name: str,
    service_version: str | None = None,
    attributes: Mapping[str, AttributeValue] | None = None,
) -> dict[str, AttributeValue]:
    """The SDK defaults, then ``attributes``, then the service name and
    version (an empty version counts as none), later ones winning."""
    merged: dict[str, AttributeValue] = {
        **_sdk_attributes(),
        **(attributes or {}),
        "service.name": service_name,
    }
    if service_version:
        merged["service.version"] = service_version
    return merged

layer

layer(*, service_name, service_version=None, attributes=None) -> Layer[OtelResource]

A resource made from config_to_attributes.

ParameterTypeDescription
service_namestr
service_versionstr | None = None
attributesMapping[str, AttributeValue] | None = None

Returns Layer[OtelResource]

def layer(
    *,
    service_name: str,
    service_version: str | None = None,
    attributes: Mapping[str, AttributeValue] | None = None,
) -> Layer[OtelResource]:
    """A resource made from ``config_to_attributes``."""
    return _layer.succeed(
        OtelResource,
        Resource(
            config_to_attributes(
                service_name=service_name,
                service_version=service_version,
                attributes=attributes,
            )
        ),
    )

layer_from_env

layer_from_env(additional_attributes=None) -> Layer[OtelResource]

A resource from the SDK defaults, then OTEL_RESOURCE_ATTRIBUTES and OTEL_SERVICE_NAME (read through the CurrentConfigProvider when the layer builds; a config error is a defect), then additional_attributes, later ones winning.

ParameterTypeDescription
additional_attributesMapping[str, AttributeValue] | None = None

Returns Layer[OtelResource]

def layer_from_env(
    additional_attributes: Mapping[str, AttributeValue] | None = None,
) -> Layer[OtelResource]:
    """A resource from the SDK defaults, then ``OTEL_RESOURCE_ATTRIBUTES``
    and ``OTEL_SERVICE_NAME`` (read through the ``CurrentConfigProvider``
    when the layer builds; a config error is a defect), then
    ``additional_attributes``, later ones winning."""

    def make(service_name: Option[str], text: str) -> Resource:
        attributes = _env_attributes(text)
        if isinstance(service_name, Some):
            attributes["service.name"] = service_name.value
        return Resource({**_sdk_attributes(), **attributes, **(additional_attributes or {})})

    read = config.all(
        config.option(config.string("OTEL_SERVICE_NAME")),
        config.string("OTEL_RESOURCE_ATTRIBUTES").with_default(""),
    )
    return _layer.effect(OtelResource, read.map(lambda pair: make(*pair)).or_die())