Repository navigation
Instrument application with PostHog - #21
Open
posthog[bot] wants to merge 1 commit into
Open
posthog[bot] wants to merge 1 commit into
posthog[bot] wants to merge 1 commit into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PostHog setup report — FastAPI risk engine
PostHog is installed and initialized for the FastAPI risk engine, with request-scoped identity context, five completed-operation events, automatic exception capture, dedicated OTLP log export, and a starter dashboard.
Installed and initialized
posthogSDK and the OpenTelemetry log-export dependencies inrequirements.txt.layer6/api.pycreates one guarded, process-widePosthogclient fromPOSTHOG_PROJECT_TOKENandPOSTHOG_HOST, registersPosthogASGIMiddleware(client=posthog_client), and flushes/shuts down safely during application lifespan termination and process exit..env.exampledocuments the requiredPOSTHOG_PROJECT_TOKENandPOSTHOG_HOSTvariables. The real project environment has both keys configured..venv/bin/python3 -m compileall layer6/api.py. Dependency installation also completed successfully. No project type checker, linter, formatter, or corresponding script was available, and the runtime disallowedpip check.Events instrumented
These events are implemented at successful completion paths only. The run verified their code and dashboard queries; it did not observe any event arrive in PostHog.
asset_risk_assessment_completedlayer6/api.pyportfolio_risk_assessment_completedlayer6/api.pyrisk_stress_test_completedlayer6/api.pyloss_histogram_generatedlayer6/api.pyrisk_simulation_completedlayer6/api.pyThe guarded capture helper preserves successful engine responses if analytics delivery fails. Event properties are non-PII operational metadata, including scenario, asset type, cache status, result counts, duration, and target year; they do not include asset IDs, coordinates, country codes, asset values, or user-entered content.
Identification and attribution
Identification is wired through FastAPI's PostHog ASGI middleware rather than per-route
identifycalls. For every request, the middleware readsX-PostHog-Distinct-IDandX-PostHog-Session-ID, creates request context, and applies that identity/session to custom captures and automatically captured exceptions.Follow-up issue: gateway identity propagation
The separately maintained Go gateway owns end-user identity and was outside this project's scope, so its forwarding behavior was not verified. It must forward a stable internal user ID (never an email) in
X-PostHog-Distinct-ID, plusX-PostHog-Session-ID, on each proxied request. Without that propagation, events and exceptions can be unattributed or fragmented across identities, reducing the reliability of adoption and funnel analysis.Error tracking
Error tracking is configured through the shared client with
enable_exception_autocapture=Trueand the single globalPosthogASGIMiddlewareerror boundary inlayer6/api.py. Uncaught route exceptions should inherit request context without duplicate manual exception capture. The run verified the implementation but did not run the service or observe an exception arrive in PostHog.AI Observability
AI Observability was not added because this repository has no production LLM call site. The legacy
/analyzeand/chatengine handlers return HTTP 410; the actual LLM routes are owned by an external Go API gateway that was not available in this project. No provider SDK, manual$ai_generationcapture, AI environment configuration, or dependency was introduced.When the gateway source is available, instrument its real provider calls there and make an explicit privacy-mode decision. Delivery is unconfirmed because no generation was run or observed.
Log capture
Configured guarded Python OTLP log export in
layer6/api.py. The exporter uses the existingPOSTHOG_PROJECT_TOKENandPOSTHOG_HOSTconfiguration and is attached only to the dedicatedmeteorium.posthog.logslogger, so only purpose-written lines added in this run leave the service. It emitsrisk_engine_readyat successful engine initialization andasset_risk_assessment_completedafter successful/risk/assetresponses, with safe operational metadata only. The provider is shut down during FastAPI lifespan shutdown; startup does not wait for delivery.Install the required OpenTelemetry packages before verification, then start the service and make a successful authenticated
POST /risk/asset; search PostHog Logs forasset_risk_assessment_completed. Delivery is unverified until observed. To export existing application logs later, explicitly attach the sameLoggingHandlerto the root logger rather than the dedicated logger.Starter analytics dashboard
Created and verified Analytics basics (wizard), with five 30-day trends for completed asset and portfolio risk assessments, stress tests, histogram generation, and simulations. The asset and portfolio tiles were corrected to query the current integration event names. The dashboard is ready to populate when the service begins sending production traffic.
All five dashboard tiles executed successfully with no warnings, but currently return zero events. This is expected until the newly instrumented service receives traffic; it is not evidence that any event has been delivered.
Build conflicts
No unresolved build conflict was reported. The only unavailable verification was
pip check, which the runtime disallowed. No project type checker, linter, formatter, or associated script is declared, so those checks could not be run.Next steps
POST /risk/assetto verify the dedicated log export.Before you merge
layer6/api.pyonly; inspect the PostHog initialization, middleware, capture, and OTLP logger blocks inlayer6/api.py(line numbers were not recorded by the run).layer6/api.py(line numbers were not recorded by the run).POSTHOG_PROJECT_TOKENandPOSTHOG_HOSTfrom.env.examplein each deployment environment and any bootstrap configuration; inspect the two variable entries in.env.example(line numbers were not recorded by the run).X-PostHog-Distinct-IDandX-PostHog-Session-IDto the middleware registration and capture paths inlayer6/api.py(line numbers were not recorded by the run). Use a stable internal user ID, not an email..posthog-wizard-cache/.posthog-events.jsonappears in PostHog; inspect the corresponding capture call inlayer6/api.py(line numbers were not recorded by the run).POST /risk/asset; confirmrisk_engine_readyorasset_risk_assessment_completedappears in PostHog Logs. Inspect the dedicatedmeteorium.posthog.logslogger and/risk/assetcompletion log inlayer6/api.py(line numbers were not recorded by the run).