Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .sampo/changesets/metrics-autocapture.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
pypi/posthog: minor
---

Add `metrics={"autocapture": True}`: HTTP, database and runtime metrics from the official OpenTelemetry instrumentations, with no instrumentation code. Install `posthog[metrics]` and run `opentelemetry-bootstrap -a install` to add the instrumentors for your libraries.
46 changes: 46 additions & 0 deletions posthog/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1108,6 +1108,13 @@ def __init__(
)
self._lanes = [self._analytics_lane, self._ai_lane]

self._metrics_autocapture = None
autocapture = metrics.get("autocapture") if isinstance(metrics, dict) else None
if autocapture and not self.disabled:
self._metrics_autocapture = self._start_metrics_autocapture(
metrics or {}, autocapture if isinstance(autocapture, dict) else {}
)

if hasattr(os, "register_at_fork"):
weak_self = weakref.ref(self)
os.register_at_fork(
Expand All @@ -1116,6 +1123,37 @@ def __init__(

self._warn_if_duplicate_async_client()

def _start_metrics_autocapture(self, metrics_config: dict, areas: dict):
# Imported here, so a client without autocapture never loads OpenTelemetry.
from .metrics_autocapture import start_metrics_autocapture

resource_attributes = metrics_config.get("resource_attributes")
resource_attributes = (
dict(resource_attributes) if isinstance(resource_attributes, dict) else {}
)
for key, attribute in (
("service_version", "service.version"),
("environment", "deployment.environment"),
):
if metrics_config.get(key) and attribute not in resource_attributes:
resource_attributes[attribute] = metrics_config[key]
weak_self = weakref.ref(self)
return start_metrics_autocapture(
host=self.host,
api_key=self.api_key,
areas={
"http": areas.get("http", True),
"db": areas.get("db", True),
"runtime": areas.get("runtime", True),
},
service_name=resource_attributes.get("service.name")
or metrics_config.get("service_name"),
resource_attributes=resource_attributes,
is_enabled=lambda: (client := weak_self()) is not None
and not client.disabled,
export_interval_seconds=metrics_config.get("flush_interval"),
)

def _set_library_identity(self, library_id: str, library_version: str) -> None:
"""Override the SDK identity stamped on events and outbound requests."""
self._library_id = library_id
Expand Down Expand Up @@ -2923,6 +2961,14 @@ def _shutdown_once(self, errors: list[Exception]) -> None:
self._run_lifecycle_cleanup(
"Failed to reset metrics on shutdown", self._metrics.reset, errors
)
if self._metrics_autocapture is not None:
# Exports the last window, then removes the instrumentation.
self._run_lifecycle_cleanup(
"Failed to shut down metrics autocapture",
self._metrics_autocapture.shutdown,
errors,
)
self._metrics_autocapture = None
with self._traces_lock:
traces = self._traces
if traces is not None:
Expand Down
Loading
Loading