mirror of
https://github.com/codeflash-ai/codeflash.git
synced 2026-05-04 18:25:17 +00:00
perf: defer posthog, sentry_sdk, and requests imports in telemetry
Move heavy third-party imports (posthog, sentry_sdk, and their integrations) from module level into the functions that use them. These imports cost ~350ms combined but are only needed when telemetry is actually initialized, not on every CLI invocation. - posthog_cf.py: defer `from posthog import Posthog` into initialize_posthog(), defer cfapi/console/lsp imports into ph() - sentry.py: defer `import sentry_sdk` and integrations into init_sentry()
This commit is contained in:
parent
9e6923916c
commit
2cd23f9191
2 changed files with 37 additions and 31 deletions
|
|
@ -1,16 +1,11 @@
|
|||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
from typing import TYPE_CHECKING, Any
|
||||
|
||||
from posthog import Posthog
|
||||
if TYPE_CHECKING:
|
||||
from posthog import Posthog
|
||||
|
||||
from codeflash.api.cfapi import get_user_id
|
||||
from codeflash.cli_cmds.console import logger
|
||||
from codeflash.lsp.helpers import is_subagent_mode
|
||||
from codeflash.version import __version__
|
||||
|
||||
_posthog = None
|
||||
_posthog: Posthog | None = None
|
||||
|
||||
|
||||
def initialize_posthog(*, enabled: bool = True) -> None:
|
||||
|
|
@ -21,6 +16,10 @@ def initialize_posthog(*, enabled: bool = True) -> None:
|
|||
if not enabled:
|
||||
return
|
||||
|
||||
import logging
|
||||
|
||||
from posthog import Posthog
|
||||
|
||||
global _posthog
|
||||
_posthog = Posthog(project_api_key="phc_aUO790jHd7z1SXwsYCz8dRApxueplZlZWeDSpKc5hol", host="https://us.posthog.com")
|
||||
_posthog.log.setLevel(logging.CRITICAL) # Suppress PostHog logging
|
||||
|
|
@ -36,6 +35,10 @@ def ph(event: str, properties: dict[str, Any] | None = None) -> None:
|
|||
if _posthog is None:
|
||||
return
|
||||
|
||||
from codeflash.api.cfapi import get_user_id
|
||||
from codeflash.lsp.helpers import is_subagent_mode
|
||||
from codeflash.version import __version__
|
||||
|
||||
properties = properties or {}
|
||||
properties.update({"cli_version": __version__, "subagent": is_subagent_mode()})
|
||||
|
||||
|
|
@ -44,4 +47,6 @@ def ph(event: str, properties: dict[str, Any] | None = None) -> None:
|
|||
if user_id:
|
||||
_posthog.capture(distinct_id=user_id, event=event, properties=properties)
|
||||
else:
|
||||
from codeflash.cli_cmds.console import logger
|
||||
|
||||
logger.debug("Failed to log event to PostHog: User ID could not be retrieved.")
|
||||
|
|
|
|||
|
|
@ -1,24 +1,25 @@
|
|||
import logging
|
||||
|
||||
import sentry_sdk
|
||||
from sentry_sdk.integrations.logging import LoggingIntegration
|
||||
from sentry_sdk.integrations.stdlib import StdlibIntegration
|
||||
|
||||
|
||||
def init_sentry(*, enabled: bool = False, exclude_errors: bool = False) -> None:
|
||||
if enabled:
|
||||
sentry_logging = LoggingIntegration(
|
||||
level=logging.INFO, # Capture info and above as breadcrumbs
|
||||
event_level=logging.CRITICAL # Send only fatal errors as events if exclude_errors is True
|
||||
if exclude_errors
|
||||
else logging.ERROR, # Otherwise, error logs will create sentry events
|
||||
)
|
||||
if not enabled:
|
||||
return
|
||||
|
||||
sentry_sdk.init(
|
||||
dsn="https://4b9a1902f9361b48c04376df6483bc96@o4506833230561280.ingest.sentry.io/4506833262477312",
|
||||
integrations=[sentry_logging],
|
||||
disabled_integrations=[StdlibIntegration],
|
||||
traces_sample_rate=0,
|
||||
profiles_sample_rate=0,
|
||||
ignore_errors=[KeyboardInterrupt],
|
||||
)
|
||||
import logging
|
||||
|
||||
import sentry_sdk
|
||||
from sentry_sdk.integrations.logging import LoggingIntegration
|
||||
from sentry_sdk.integrations.stdlib import StdlibIntegration
|
||||
|
||||
sentry_logging = LoggingIntegration(
|
||||
level=logging.INFO, # Capture info and above as breadcrumbs
|
||||
event_level=logging.CRITICAL # Send only fatal errors as events if exclude_errors is True
|
||||
if exclude_errors
|
||||
else logging.ERROR, # Otherwise, error logs will create sentry events
|
||||
)
|
||||
|
||||
sentry_sdk.init(
|
||||
dsn="https://4b9a1902f9361b48c04376df6483bc96@o4506833230561280.ingest.sentry.io/4506833262477312",
|
||||
integrations=[sentry_logging],
|
||||
disabled_integrations=[StdlibIntegration],
|
||||
traces_sample_rate=0,
|
||||
profiles_sample_rate=0,
|
||||
ignore_errors=[KeyboardInterrupt],
|
||||
)
|
||||
|
|
|
|||
Loading…
Reference in a new issue