feat(metrics): support metrics config via module-level settings#753
Conversation
Apps configured through module attributes (posthog.api_key = ..., then setup()) had no way to pass the metrics config dict, so their client.metrics series all shipped service.name='unknown_service'. Adds the module-level `metrics` attr, passes it through setup(), and refreshes the lazily-consumed config on repeat setup() calls until the metrics API is first used, so Django-style ready() hooks that run after an early setup() still apply. Generated-By: PostHog Code Task-Id: 470062dd-05c0-40bf-bfa3-bc89b194121e
Generated-By: PostHog Code Task-Id: 470062dd-05c0-40bf-bfa3-bc89b194121e
posthog-python Compliance ReportDate: 2026-07-20 22:51:23 UTC ✅ All Tests Passed!111/111 tests passed Capture_V1 Tests✅ 94/94 tests passed View Details
Feature_Flags Tests✅ 17/17 tests passed View Details
|
|
Reviews (1): Last reviewed commit: "chore: add changeset for module-level me..." | Re-trigger Greptile |
Generated-By: PostHog Code Task-Id: 470062dd-05c0-40bf-bfa3-bc89b194121e
sdk-dustin review — single passFully automated AI review; comments were not individually reviewed by a human before posting. Applied VerdictAPPROVE (agent pre-review; Client Libraries review still required) — no BLOCKING/IMPORTANT findings. One design question flagged below for a deliberate call before this ships in a minor release. Existing supported workaround / API necessity
Review comment drafts[P2/design] Criteria summary
Non-blocking observations
|
dustinbyrne
left a comment
There was a problem hiding this comment.
recommend adding a client constructor argument for this as well
otherwise, users who don't use the default global posthog cannot set this (unless it already exists, i didn't check). if you do add it, make sure it's positioned as the last argument in the constructor for backwards compatibility
Already there 👍🏽 |
💡 Motivation and Context
Apps configured through module attributes (
posthog.api_key = ...+ implicitsetup()) — including PostHog's own Django monorepo viaposthoganalytics— have no way to pass themetricsconfig dict. Every series they record throughclient.metricsshipsservice.name='unknown_service', which defeats the Metrics UI's service filtering.This adds the module-level
metricsattr (mirroring how every otherClientkwarg is exposed as a module setting) and passes it throughsetup(). Because the config is consumed lazily on first.metricsaccess, a repeatsetup()call also refreshes it until the metrics API is first used — Djangoready()hooks that run after something already forcedsetup()still apply.Groundwork for the monorepo's bump-ready
client.metricsusage from web/celery (companion PR in PostHog/posthog).💚 How did you test it?
posthog/test/test_metrics.py, red before the change (service.namecame backunknown_service): module attr flows into thesetup()client's OTLP resource attributes, and late assignment applies on repeatsetup()until first use.pytest --timeout=30: 1640 passed; the 9 failures are the pre-existing network-dependentposthog/test/ai/*integration tests (verified they fail identically on a clean checkout in the same sandbox).ruff format --check,ruff check,mypy | mypy-baseline filter(no issues),python -W error -c "import posthog"all clean.📝 Checklist
If releasing new changes
sampo addto generate a changeset file (authored directly in.sampo/changesets/module-level-metrics-config.md— same format)🤖 Agent context
Autonomy: Human-driven (agent-assisted)
Written by Claude (PostHog Code session) while making the PostHog monorepo bump-ready for
client.metrics. TDD: the two tests were written and observed failing before the implementation. The late-assignment refresh insetup()follows the existing "always set in case user changes it" post-construction block, and is limited to before first.metricsuse so an active aggregation window is never reconfigured mid-flight.Created with PostHog Code