Skip to content

fix(telemetry): strip nested PII keys inside mapping and sequence attributes - #8772

Open
yaosongding wants to merge 1 commit into
livekit:mainfrom
yaosongding:fix/pii-filter-nested-attributes
Open

yaosongding wants to merge 1 commit into
livekit:mainfrom
yaosongding:fix/pii-filter-nested-attributes

Conversation

@yaosongding

Copy link
Copy Markdown

Summary

  • _serialize_session_options() aliases stt_context_options["keyterms"] to lk.pii.keyterms inside the nested session.options dict so client-side PII filtering can strip customer keyterms when redaction is enabled, and _upload_session_report() emits the "session report" OTel log record with attributes={"session.options": _serialize_session_options(report.options), ...}.
  • Previously, pii._contains_pii() and pii.filter_attributes() only inspected top-level attribute keys: _PIIFilteringLogProcessor.on_emit() skipped records whose top-level keys did not match is_pii_attribute(), and filter_attributes() shallow-copied nested Mapping and Sequence values verbatim, leaving lk.pii.keyterms in exported OTLP log payloads even when redaction was enabled.
  • Recursively detect and strip PII-marked keys (is_pii_attribute, ATTR_EXCEPTION_TRACE, ATTR_EXCEPTION_MESSAGE) across nested Mapping and non-string/bytes Sequence attribute values while preserving tuple vs list container types used by OpenTelemetry BoundedAttributes.

Test plan

  • uv run pytest tests/test_telemetry_redaction.py tests/test_session_options_report.py tests/test_recording.py --unit (covers nested Mapping/Sequence filtering and end-to-end _upload_session_report -> LoggerProvider -> _PIIFilteringLogProcessor -> OTLP protobuf encode_logs redaction)
  • uv run ruff check and uv run ruff format --check on touched files
  • uv run mypy -p livekit.agents

@yaosongding
yaosongding marked this pull request as ready for review November 25, 2026 00:48
@yaosongding
yaosongding requested a review from a team as a code owner November 25, 2026 00:48

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔍 Devin Review: 1 flag

Not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

@1AbdullahButt 1AbdullahButt left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fix is right. _serialize_session_options renames keyterms to lk.pii.keyterms inside session.options. On main, _contains_pii only looks at top-level keys, so the log processor skips the record and the terms are exported even with redaction on. I reproduced that on the base commit. Both new tests fail there and pass here, and test_telemetry_redaction.py, test_session_options_report.py and test_recording.py pass (245). I also checked non-str keys (kept), tuple vs list (type preserved), bytes (untouched), and dicts inside lists inside tuples (filtered).

One optional note on cost: _PIIFilteringSpanProcessor.on_end now walks every sequence attribute item by item. With 50 attributes of 100 ints each, _contains_pii went from about 32 µs to about 3 ms per call. Span attributes can only be primitives or flat sequences of primitives, so for spans that walk can never find anything. Recursing in _value_contains_pii (pii.py:95-96) only into items that are a Mapping or a non-str Sequence brought it down to about 0.7 ms with the same results. Skipping the value walk for spans entirely would remove the cost.

@yaosongding

Copy link
Copy Markdown
Author

The fix is right. _serialize_session_options renames keyterms to lk.pii.keyterms inside session.options. On main, _contains_pii only looks at top-level keys, so the log processor skips the record and the terms are exported even with redaction on. I reproduced that on the base commit. Both new tests fail there and pass here, and test_telemetry_redaction.py, test_session_options_report.py and test_recording.py pass (245). I also checked non-str keys (kept), tuple vs list (type preserved), bytes (untouched), and dicts inside lists inside tuples (filtered).

One optional note on cost: _PIIFilteringSpanProcessor.on_end now walks every sequence attribute item by item. With 50 attributes of 100 ints each, _contains_pii went from about 32 µs to about 3 ms per call. Span attributes can only be primitives or flat sequences of primitives, so for spans that walk can never find anything. Recursing in _value_contains_pii (pii.py:95-96) only into items that are a Mapping or a non-str Sequence brought it down to about 0.7 ms with the same results. Skipping the value walk for spans entirely would remove the cost.

This is Gold, thanks for your test and great instruction, I'll update this PR!

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants