Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ variables:
| `service_version` | `RIUS_SERVICE_VERSION` | none | Sets `service.version` on the resource. Unset leaves it off, so `OTEL_RESOURCE_ATTRIBUTES` can supply it. |
| `disabled` | `RIUS_DISABLED` | `false` | Kill switch. When true, spans are created but never exported. |
| `sample_rate` | `RIUS_SAMPLE_RATE` | `1.0` | Head sampling ratio `0.0`–`1.0` (whole-trace; children follow root). |
| `capture_content` | `RIUS_CAPTURE_CONTENT` | `true` | When false, prompt/response content is stripped at export (metadata still sent). |
| `capture_content` | `RIUS_CAPTURE_CONTENT` | `true` | When false, prompt/response content is stripped at export (metadata still sent). When `RIUS_CAPTURE_CONTENT` is unset, `OTEL_INSTRUMENTATION_GENAI_CAPTURE_CONTENT` is read (see below). |
| `bridge_foreign_provider` | `RIUS_BRIDGE_FOREIGN_PROVIDER` | `false` | Also export the spans of another OpenTelemetry SDK that owns the global provider (see below). |
| `agent_name` | `RIUS_AGENT_NAME` | `service_name` | The agent this process is. Also the `gen_ai.main_agent.id` when `main_agent_id` is unset, so renaming it changes that id. |
| `main_agent_id` | `RIUS_MAIN_AGENT_ID` | a `gen_ai.main_agent.id` in `OTEL_RESOURCE_ATTRIBUTES`, else the agent name | A stable id for the agent (a registry id or an ARN), stamped as `gen_ai.main_agent.id`. Never a per-process value. |
Expand Down Expand Up @@ -149,6 +149,18 @@ rius.init(mask=lambda value, *, key: hash_pii(value) if "input" in key else valu
rius.init(capture_content=False) # drop content entirely, keep metadata
```

Content capture also follows the OpenTelemetry GenAI setting
`OTEL_INSTRUMENTATION_GENAI_CAPTURE_CONTENT` when `RIUS_CAPTURE_CONTENT` is
unset. `NO_CONTENT` turns capture off, and so does `EVENT_ONLY`, because Rius
records content on spans. `SPAN_ONLY` and `SPAN_AND_EVENT` keep it on. The value
is case-insensitive. An unrecognised one keeps capture on and logs a warning
naming the valid values (the OpenTelemetry conventions fall back to
`NO_CONTENT` instead). An empty value of either variable counts as unset (an
empty `RIUS_CAPTURE_CONTENT` also logs a warning).
The `capture_content` argument wins over both variables, and
`RIUS_CAPTURE_CONTENT` wins over the OpenTelemetry one. With neither set,
capture is on, unlike the OpenTelemetry default of `NO_CONTENT`.

### Sessions, users and workspaces

Three context managers scope every span started inside them, including the
Expand Down
5 changes: 4 additions & 1 deletion src/rius/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,10 @@ def init(
headers: Extra headers for the OTLP exporter.
disabled: If True, no exporter is attached (spans are dropped).
sample_rate: Head sampling ratio 0.0-1.0 (whole-trace). Default 1.0.
capture_content: If False, strip prompt/response content at export. Default True.
capture_content: If False, strip prompt/response content at export. Default True,
or ``RIUS_CAPTURE_CONTENT``, or when that is unset
``OTEL_INSTRUMENTATION_GENAI_CAPTURE_CONTENT`` (``NO_CONTENT`` and
``EVENT_ONLY`` turn capture off).
mask: Redact content attribute values at export (applies to all spans).
instruments: Auto-instrumentation selection. ``None`` (default) enables
every bundled instrumentor whose package is installed; a list
Expand Down
54 changes: 52 additions & 2 deletions src/rius/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
ENV_DISABLED = "RIUS_DISABLED"
ENV_SAMPLE_RATE = "RIUS_SAMPLE_RATE"
ENV_CAPTURE_CONTENT = "RIUS_CAPTURE_CONTENT"
# The GenAI conventions' content-capture setting, read when
# RIUS_CAPTURE_CONTENT is unset.
ENV_OTEL_CAPTURE_CONTENT = "OTEL_INSTRUMENTATION_GENAI_CAPTURE_CONTENT"
ENV_HEARTBEAT = "RIUS_HEARTBEAT"
ENV_HEARTBEAT_INTERVAL = "RIUS_HEARTBEAT_INTERVAL"
ENV_AGENT_NAME = "RIUS_AGENT_NAME"
Expand Down Expand Up @@ -84,6 +87,50 @@ def _env_bool(name: str, *, default: bool) -> bool:
return default


#: The conventions' capture modes, by whether Rius captures content under
#: them. Rius records content on spans only, so EVENT_ONLY (content on events,
#: none on spans) captures none.
_OTEL_CAPTURE_MODES = {
"NO_CONTENT": False,
"EVENT_ONLY": False,
"SPAN_ONLY": True,
"SPAN_AND_EVENT": True,
}


def _capture_content_from_env() -> bool:
"""``RIUS_CAPTURE_CONTENT`` when set, else the conventions' setting, else on.

An empty (or blank) value of either variable counts as unset, as
OpenTelemetry reads its own variables: templated deployments render an
omitted value as an empty one. A blank ``RIUS_CAPTURE_CONTENT`` still logs
a warning, as the other blank ``RIUS_*`` flags do. The conventions'
``OTEL_INSTRUMENTATION_GENAI_CAPTURE_CONTENT`` is read case-insensitively.
Its own default is ``NO_CONTENT``, and the conventions ask for that on an
unrecognised value too. Rius keeps capture on in both cases, on purpose:
capture is on by default, and an unrecognised value logs a warning naming
the valid ones.
"""
rius = os.getenv(ENV_CAPTURE_CONTENT)
if rius is not None:
if rius.strip():
return _env_bool(ENV_CAPTURE_CONTENT, default=True)
logger.warning("%s is empty; treated as unset.", ENV_CAPTURE_CONTENT)
raw = os.getenv(ENV_OTEL_CAPTURE_CONTENT)
if raw is None or not raw.strip():
return True
mode = _OTEL_CAPTURE_MODES.get(raw.strip().upper())
if mode is not None:
return mode
logger.warning(
"%s=%r is not recognised; content capture stays on. "
"Valid values: NO_CONTENT, SPAN_ONLY, EVENT_ONLY, SPAN_AND_EVENT.",
ENV_OTEL_CAPTURE_CONTENT,
raw,
)
return True


def _env_float(name: str, *, default: float) -> float:
raw = os.getenv(name)
if raw is None:
Expand Down Expand Up @@ -297,7 +344,10 @@ def resolve_config(
sample_rate: Head-sampling ratio for root traces
(``RIUS_SAMPLE_RATE``).
capture_content: When ``False``, content attributes are stripped at
export (``RIUS_CAPTURE_CONTENT``).
export (``RIUS_CAPTURE_CONTENT``; when that is unset, the
conventions' ``OTEL_INSTRUMENTATION_GENAI_CAPTURE_CONTENT``:
``NO_CONTENT`` and ``EVENT_ONLY`` turn capture off, ``SPAN_ONLY``
and ``SPAN_AND_EVENT`` keep it on). On by default.
heartbeat: Enable the agent-lifetime heartbeat thread
(``RIUS_HEARTBEAT``). On by default; set ``False`` or
``RIUS_HEARTBEAT=false`` to opt out.
Expand Down Expand Up @@ -372,7 +422,7 @@ def resolve_config(
else _finite("sample_rate", sample_rate, 1.0)
)
resolved_capture_content = (
_env_bool(ENV_CAPTURE_CONTENT, default=True) if capture_content is None else capture_content
_capture_content_from_env() if capture_content is None else capture_content
)

resolved_heartbeat = _env_bool(ENV_HEARTBEAT, default=True) if heartbeat is None else heartbeat
Expand Down
98 changes: 97 additions & 1 deletion tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@
"PARTIAL_SPANS_DELAY",
]
ENV_VARS = [f"RIUS_{suffix}" for suffix in _SUFFIXES]
OTEL_CAPTURE_CONTENT = "OTEL_INSTRUMENTATION_GENAI_CAPTURE_CONTENT"


@pytest.fixture(autouse=True)
def _clear_env(monkeypatch: pytest.MonkeyPatch) -> None:
for var in ENV_VARS:
for var in [*ENV_VARS, OTEL_CAPTURE_CONTENT]:
monkeypatch.delenv(var, raising=False)


Expand Down Expand Up @@ -223,6 +224,101 @@ def test_the_agent_definition_version_is_not_the_service_version(
assert resolve_config(main_agent_version="7").service_version is None


# --- the conventions' OTEL_INSTRUMENTATION_GENAI_CAPTURE_CONTENT ---


@pytest.mark.parametrize(
("raw", "expected"),
[
("NO_CONTENT", False),
# Rius records content on spans only, so a request for events alone
# asks for no content on the spans Rius keeps.
("EVENT_ONLY", False),
("SPAN_ONLY", True),
("SPAN_AND_EVENT", True),
# Case-insensitive, as the conventions define it.
("no_content", False),
(" span_only ", True),
],
)
def test_capture_content_follows_the_otel_setting_when_rius_is_unset(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, raw: str, expected: bool
) -> None:
monkeypatch.setenv(OTEL_CAPTURE_CONTENT, raw)
with caplog.at_level(logging.WARNING, logger="rius.config"):
assert resolve_config().capture_content is expected
assert not caplog.records


@pytest.mark.parametrize("raw", ["maybe", "true", "false", "SPAN", "NO-CONTENT"])
def test_an_unrecognised_otel_capture_setting_keeps_capture_on_and_warns(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, raw: str
) -> None:
# Deliberately not the conventions' NO_CONTENT: capture is on by default in
# Rius, and the warning names the value and what to set instead.
monkeypatch.setenv(OTEL_CAPTURE_CONTENT, raw)
with caplog.at_level(logging.WARNING, logger="rius.config"):
assert resolve_config().capture_content is True
(record,) = caplog.records
message = record.getMessage()
assert f"{OTEL_CAPTURE_CONTENT}={raw!r} is not recognised" in message
assert "content capture stays on" in message
for valid in ("NO_CONTENT", "SPAN_ONLY", "EVENT_ONLY", "SPAN_AND_EVENT"):
assert valid in message


@pytest.mark.parametrize("raw", ["", " "])
def test_an_empty_otel_capture_setting_is_unset(
monkeypatch: pytest.MonkeyPatch, caplog: pytest.LogCaptureFixture, raw: str
) -> None:
# OpenTelemetry reads an empty variable as unset; templated deployments
# render an omitted value as one.
monkeypatch.setenv(OTEL_CAPTURE_CONTENT, raw)
with caplog.at_level(logging.WARNING, logger="rius.config"):
assert resolve_config().capture_content is True
assert not caplog.records


@pytest.mark.parametrize("raw", ["", " "])
@pytest.mark.parametrize(
("otel", "expected"), [("NO_CONTENT", False), ("SPAN_ONLY", True), (None, True)]
)
def test_an_empty_rius_capture_content_is_unset_so_the_otel_setting_applies(
monkeypatch: pytest.MonkeyPatch,
caplog: pytest.LogCaptureFixture,
raw: str,
otel: str | None,
expected: bool,
) -> None:
# Still a warning, as for the other blank RIUS_* flags, but it does not
# shadow the OpenTelemetry setting.
monkeypatch.setenv("RIUS_CAPTURE_CONTENT", raw)
if otel is not None:
monkeypatch.setenv(OTEL_CAPTURE_CONTENT, otel)
with caplog.at_level(logging.WARNING, logger="rius.config"):
assert resolve_config().capture_content is expected
(record,) = caplog.records
assert "RIUS_CAPTURE_CONTENT is empty; treated as unset" in record.getMessage()


@pytest.mark.parametrize(("rius", "expected"), [("true", True), ("false", False)])
def test_rius_capture_content_wins_over_the_otel_setting(
monkeypatch: pytest.MonkeyPatch, rius: str, expected: bool
) -> None:
monkeypatch.setenv("RIUS_CAPTURE_CONTENT", rius)
monkeypatch.setenv(OTEL_CAPTURE_CONTENT, "SPAN_ONLY" if not expected else "NO_CONTENT")
assert resolve_config().capture_content is expected


@pytest.mark.parametrize("argument", [True, False])
def test_the_capture_content_argument_wins_over_both_variables(
monkeypatch: pytest.MonkeyPatch, argument: bool
) -> None:
monkeypatch.setenv("RIUS_CAPTURE_CONTENT", str(not argument).lower())
monkeypatch.setenv(OTEL_CAPTURE_CONTENT, "SPAN_ONLY" if not argument else "NO_CONTENT")
assert resolve_config(capture_content=argument).capture_content is argument


# --- an unrecognised boolean keeps the default, with a warning ---


Expand Down
Loading