Skip to content

Instrument application with PostHog - #20

Open
posthog[bot] wants to merge 1 commit into
mainfrom
posthog/wizard-01a20282ada8
Open

posthog[bot] wants to merge 1 commit into
mainfrom
posthog/wizard-01a20282ada8

Conversation

@posthog

@posthog posthog Bot commented Nov 22, 2026

Copy link
Copy Markdown

PostHog setup report

PostHog is installed and initialized for the FastAPI risk engine, with five successful-outcome events, automatic uncaught-exception capture, isolated OTLP log export, and a starter dashboard.

Installed and initialized

  • Declared posthog>=7.69,<8 in requirements.txt; the reviewed virtual environment resolved PostHog 7.91.4.
  • Declared and installed opentelemetry-api, opentelemetry-sdk, and opentelemetry-exporter-otlp; the reviewed environment resolved them at 1.45.2.
  • core/config.py loads POSTHOG_PROJECT_TOKEN and POSTHOG_HOST; both are documented in .env.example and were confirmed present in the local .env.
  • layer6/api.py creates one Posthog client, conditionally installs PosthogASGIMiddleware, enables exception autocapture, and shuts analytics down with a five-second budget.
  • Review verification: dependency installation succeeded twice and python -m compileall core/config.py layer6/api.py passed. No project typechecker, linter, formatter, or configured equivalent was available; no test suite or runtime delivery check was run.

Events instrumented

All captures are guarded after successful engine outcomes and use aggregate technical metadata only (counts, duration, cache state, and engine availability). No event arrival was observed during this run.

Event What it measures File
asset_risk_scored A single-asset climate-risk score returned, including cache status and pipeline outcome metadata. layer6/api.py
portfolio_risk_scored A portfolio-risk calculation completed with aggregate asset and timing metadata. layer6/api.py
risk_stress_test_completed A scenario stress test returned for an asset. layer6/api.py
risk_histogram_generated A loss-distribution histogram was successfully generated. layer6/api.py
risk_simulation_completed A CAT-event simulation completed and returned a result. layer6/api.py

Identification

User identification was deliberately not wired. This server-only engine receives only a shared service credential from a separate Go gateway—no trusted end-user, session, or caller-resource identifier reaches this application. Using ENGINE_SECRET or the Authorization header as a distinct ID would collapse users onto a service identity and expose credential-derived data.

Follow-up issue: stable identity is unavailable

The Go gateway must propagate a trusted stable user ID (or its established PostHog tracing identity) to this request boundary. Until then, the product events and errors remain personless, so per-user adoption, retention, and attribution cannot be meaningfully analyzed. Once that contract exists, bind it in a request-boundary dependency or middleware using posthog_client.new_context() and identify_context(stable_user_id).

Error tracking

Global uncaught FastAPI exception capture is configured through PosthogASGIMiddleware together with enable_exception_autocapture=True in layer6/api.py. No catch-all exception handler was added, avoiding duplicate exception instrumentation. Runtime exception delivery was not exercised.

AI Observability

AI Observability was not applicable in this repository. No production LLM provider/client invocation exists here: the legacy /analyze and /chat routes return HTTP 410, and the recorded handoff states that live model calls belong to the separate Go gateway. Consequently, no AI-generation event or privacy-mode setting was configured in this service, and no AI delivery was verified. Instrument the actual Go gateway call path in its own project if AI Observability is needed.

Starter analytics dashboard

Created Analytics basics (wizard) with five 30-day daily trend views for the engine’s instrumented outcome events: asset and portfolio risk scoring, stress-test completion, histogram generation, and simulation completion. The dashboard and every insight are tagged wizard; charts will populate as production requests emit these events.

PostHog Logs

Configured an isolated OpenTelemetry log exporter for the FastAPI risk engine. It exports only the purpose-written meteorium.posthog_logs lines added in this run, not the service’s existing logs. Structured records cover engine start/stop plus successful asset and portfolio scoring outcomes, with technical aggregate metadata only (for example duration, cache state, and counts); they exclude request bodies, secrets, asset IDs, locations, and user data.

Install opentelemetry-api, opentelemetry-sdk, and opentelemetry-exporter-otlp during review, then trigger an authenticated successful POST /risk/asset and find asset_risk_scoring_completed in PostHog Logs. Delivery remains unverified until that observation. To export existing application logs later, deliberately attach the configured LoggingHandler to the selected existing logger or root logger; that broader export was intentionally not enabled here.

Final review state

The OpenTelemetry dependencies named above were subsequently added to requirements.txt and installed during review, so no further package installation is required. Runtime event, exception, and log delivery remain unconfirmed because the application was not exercised against PostHog. No build conflict was reported; the review made one safe-delivery fix in layer6/api.py, replacing a bounded force_flush() plus unbounded shutdown with shutdown(timeout_millis=5000).

MCP analytics

MCP analytics was not configured because no MCP server was detected within this project scope.

Before you merge

  • Run the full production build and resolve any generated-code lint or type errors; the wizard only compiled core/config.py and layer6/api.py.
  • Run the project test suite; no tests were run during this integration, and instrumented call sites may need fixture or mock updates.
  • Confirm POSTHOG_PROJECT_TOKEN and POSTHOG_HOST from .env.example are set in every deployment environment—not only local .env—and inspect the configuration additions in core/config.py and initialization in layer6/api.py.
  • Trigger each successful risk-engine outcome in layer6/api.py and confirm all five listed events arrive in PostHog; compilation does not demonstrate ingestion.
  • Trigger an uncaught route exception through the middleware configured in layer6/api.py and confirm it appears in PostHog error tracking.
  • Trigger an authenticated successful POST /risk/asset and confirm the asset_risk_scoring_completed entry from layer6/api.py appears in PostHog Logs with its expected service and severity.
  • Before relying on person-level analysis, arrange for the Go gateway to pass a trusted stable identity and implement request-boundary identification in layer6/api.py; do not use ENGINE_SECRET or Authorization as the distinct ID.

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.

0 participants