Skip to content

feat(context): add reset_context/logging_context and category-aware host.json warnings#84

Merged
yeongseon merged 1 commit intomainfrom
feat/context-lifecycle-and-category-host-config-78
Apr 26, 2026
Merged

feat(context): add reset_context/logging_context and category-aware host.json warnings#84
yeongseon merged 1 commit intomainfrom
feat/context-lifecycle-and-category-host-config-78

Conversation

@yeongseon
Copy link
Copy Markdown
Owner

Closes #78

Summary

P1 follow-ups from the external review, split into two related concerns: invocation-context lifecycle and category-aware host.json conflict detection.

Context lifecycle

  • Promote the previously private _reset_context_vars() helper to a public reset_context() API. Callers (custom middleware, test teardown, defensive wrappers) can now explicitly clear invocation context on reused workers.
  • Add logging_context(context) — a context manager that calls inject_context() on enter and always calls reset_context() on exit, even when the wrapped block raises. This eliminates the most common footgun: stale invocation_id / cold_start leaking into the next invocation on a warm worker.
  • with_context decorator now uses the new public reset_context() instead of its private duplicate, so there is a single source of truth for the reset semantics.
  • README Quick Start now leads with with logging_context(context): and keeps inject_context / reset_context documented as the lower-level primitives for custom integrations.

host.json category-aware warnings

warn_host_json_level_conflict previously inspected only the default key under logging.logLevel. Real Azure Functions apps configure per-category levels (Function, Function.<name>, Host.Results, Host.Aggregator, …) and those silent suppressions were going undetected.

  • Iterate every entry under logging.logLevel, resolve each level, and emit one warning per category that is more restrictive than the configured Python level.
  • Warning text now distinguishes default from named categories (logLevel for category 'Function' is set to 'Warning' …).
  • Non-dict logLevel blocks are tolerated (no warning, no crash).

Tests

  • reset_context_clears_all, reset_context_idempotent
  • logging_context_sets_then_resets, logging_context_resets_when_body_raises
  • warns_per_category, no_warning_when_all_permissive, no_warning_when_log_level_block_is_not_a_dict
  • does_not_install_duplicate_context_filters (idempotent ContextFilter install)
  • only_website_instance_id_is_false (Azure-env detection edge case)
  • test_public_api_exports and test_integration.test_public_api_exports updated for the two new exports.

Validation

  • make lint — clean
  • make typecheck — clean (24 source files)
  • make test — 142 passed, 3 skipped (e2e), coverage 97.05%
  • make build — sdist + wheel produced

Compatibility

  • No breaking change. inject_context and with_context keep their existing semantics.
  • Public surface gains two symbols: reset_context, logging_context. Both are typed and exported via __all__.
  • No new runtime dependencies. Python 3.10+ preserved.

…ost.json warnings

Closes #78

- Promote _reset_context_vars to public reset_context() so callers can
  defensively clear invocation context on reused workers.
- Add logging_context() context manager that injects on enter and always
  resets on exit (even when the wrapped block raises), preventing stale
  context from leaking across invocations.
- Update with_context decorator to use the new public reset_context().
- Make host.json conflict detection category-aware: iterate every entry
  under logging.logLevel (default, Function, Function.<name>, Host.*) and
  emit one warning per category that is more restrictive than the
  configured level. Previously only the 'default' key was inspected.
- Document logging_context as the recommended Quick Start pattern in
  README; keep inject_context/reset_context as the lower-level API for
  custom middleware.
- Tests: cover reset_context idempotency, logging_context happy path and
  exception path, per-category host.json warnings, ContextFilter
  install idempotency under repeated setup_logging calls, and the
  WEBSITE_INSTANCE_ID-only Azure environment branch.
@yeongseon yeongseon merged commit d823d16 into main Apr 26, 2026
12 checks passed
@yeongseon yeongseon deleted the feat/context-lifecycle-and-category-host-config-78 branch April 26, 2026 10:14
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.

P1: context lifecycle + host.json category-aware conflict detection

1 participant