Skip to content

symfonic.observability.otel.attributes

attributes

Centralized OTEL attribute key constants.

GenAI semconv keys: https://opentelemetry.io/docs/specs/semconv/gen-ai/ Symfonic-specific keys: symfonic.<domain>.<sub>

This module is intentionally free of opentelemetry imports so other internal tooling can reference the key strings without pulling OTEL.

truncate_for_attribute

truncate_for_attribute(value: str) -> str

Truncate value to ATTRIBUTE_VALUE_MAX_BYTES UTF-8 bytes.

Returns the value verbatim if it already fits; otherwise truncates at a UTF-8 boundary and appends TRUNCATION_MARKER so consumers can tell at a glance that the field is incomplete.

Source code in src/symfonic/observability/otel/attributes.py
def truncate_for_attribute(value: str) -> str:
    """Truncate ``value`` to ``ATTRIBUTE_VALUE_MAX_BYTES`` UTF-8 bytes.

    Returns the value verbatim if it already fits; otherwise truncates at
    a UTF-8 boundary and appends ``TRUNCATION_MARKER`` so consumers can
    tell at a glance that the field is incomplete.
    """
    if not value:
        return value
    encoded = value.encode("utf-8")
    if len(encoded) <= ATTRIBUTE_VALUE_MAX_BYTES:
        return value
    cut = ATTRIBUTE_VALUE_MAX_BYTES - len(TRUNCATION_MARKER.encode("utf-8"))
    # Decode with ``ignore`` so we never split mid-codepoint.
    head = encoded[:cut].decode("utf-8", errors="ignore")
    return f"{head}{TRUNCATION_MARKER}"