Skip to content

fix(py): v1 /api/notify turns on Dev UI log export - #7070

Closed
garvitkaushik-123 wants to merge 1 commit into
genkit-ai:mainfrom
garvitkaushik-123:fix-py-v1-notify-log-export
Closed

garvitkaushik-123 wants to merge 1 commit into
genkit-ai:mainfrom
garvitkaushik-123:fix-py-v1-notify-log-export

Conversation

@garvitkaushik-123

Copy link
Copy Markdown
Contributor

Fixes #6128

Problem

#6128 asks for Dev UI log export to be turned on at both collector-URL entry points, not only at provider init. On current main, two of the three paths already do this:

  • genkit start -- python spawn (URL in GENKIT_TELEMETRY_SERVER): _maybe_enable_log_export() in _logger.py.
  • v2 reflection handshake: ReflectionServerV2.apply_handshake_telemetry() calls enable_log_export(url=url) next to connect_developer_ui_collector(url=url).

The v1 /api/notify handler in _reflection.py only called connect_developer_ui_collector. The v2 docstring says the handshake is the "same wiring as HTTP /api/notify", but it wasn't. With v1 reflection and no GENKIT_TELEMETRY_SERVER in the environment, traces reached the Dev UI and debug logs did not.

Fix

/api/notify now also calls enable_log_export(url=url) when it receives a telemetryServerUrl, matching the v2 handshake. enable_log_export already ignores an empty URL, honours GENKIT_OTEL_ENABLE_LOGS=false, and retargets an existing exporter instead of starting a second one. reset_instrumentation() still calls reset_log_export(), so tests can reconnect.

Testing

  • New tests in py/packages/genkit/tests/genkit/core/telemetry/log_exporter_test.py:
    • /api/notify with a URL enables log export when GENKIT_TELEMETRY_SERVER is unset. This test fails without the change.
    • /api/notify with no URL, or an empty one, leaves log export off.
  • Full py/packages suite: 6929 passed, 0 failed (IPv6 loopback test deselected; my sandbox has no IPv6).
  • ruff format, ruff check, ty check and scripts/check_consistency.py pass.

Checklist:

The v2 handshake calls enable_log_export() next to
connect_developer_ui_collector(), and its docstring says it is the same
wiring as HTTP /api/notify. The v1 /api/notify handler only connected the
trace collector, so with the default `genkit start` (v1 reflection) and no
GENKIT_TELEMETRY_SERVER in the environment, debug logs never reached the
Dev UI. /api/notify now enables log export with the URL it receives.

Fixes genkit-ai#6128

@gemini-code-assist gemini-code-assist 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.

Code Review

This pull request enables log exporting when the v1 /api/notify endpoint is called with a valid telemetry server URL, aligning its behavior with the v2 handshake. It also adds corresponding unit tests to verify this behavior. The feedback suggests using pytest's built-in monkeypatch fixture instead of directly modifying os.environ with pop in the new tests to prevent potential side effects.

Comment on lines +355 to +357
async def test_notify_enables_log_export_when_env_url_missing() -> None:
"""The v1 /api/notify URL turns on log export too, same as the v2 handshake."""
os.environ.pop('GENKIT_TELEMETRY_SERVER', None)

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.

medium

Modifying os.environ directly using pop can leak side effects to other tests if the surrounding fixture (_dev_env) is ever refactored or if tests are run in a different context. Using pytest's built-in monkeypatch fixture is the idiomatic and safe way to temporarily modify environment variables during a test.

Suggested change
async def test_notify_enables_log_export_when_env_url_missing() -> None:
"""The v1 /api/notify URL turns on log export too, same as the v2 handshake."""
os.environ.pop('GENKIT_TELEMETRY_SERVER', None)
async def test_notify_enables_log_export_when_env_url_missing(monkeypatch) -> None:
"""The v1 /api/notify URL turns on log export too, same as the v2 handshake."""
monkeypatch.delenv('GENKIT_TELEMETRY_SERVER', raising=False)

Comment on lines +372 to +373
async def test_notify_without_url_leaves_log_export_off(body: dict[str, str]) -> None:
os.environ.pop('GENKIT_TELEMETRY_SERVER', None)

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.

medium

Modifying os.environ directly using pop can leak side effects to other tests if the surrounding fixture (_dev_env) is ever refactored or if tests are run in a different context. Using pytest's built-in monkeypatch fixture is the idiomatic and safe way to temporarily modify environment variables during a test.

Suggested change
async def test_notify_without_url_leaves_log_export_off(body: dict[str, str]) -> None:
os.environ.pop('GENKIT_TELEMETRY_SERVER', None)
async def test_notify_without_url_leaves_log_export_off(body: dict[str, str], monkeypatch) -> None:
monkeypatch.delenv('GENKIT_TELEMETRY_SERVER', raising=False)

@MichaelDoyle

Copy link
Copy Markdown
Contributor

Closing as already resolved: Python SDK development has moved to https://github.com/genkit-ai/genkit-python, where /api/notify Dev UI log export was enabled in genkit-ai/genkit-python#53.

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

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(py): hang Dev UI log export on the two collector URL call sites

2 participants