statsig.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. from functools import wraps
  2. from typing import Any, TYPE_CHECKING
  3. from sentry_sdk.feature_flags import add_feature_flag
  4. from sentry_sdk.integrations import Integration, DidNotEnable, _check_minimum_version
  5. from sentry_sdk.utils import parse_version
  6. try:
  7. from statsig import statsig as statsig_module
  8. from statsig.version import __version__ as STATSIG_VERSION
  9. except ImportError:
  10. raise DidNotEnable("statsig is not installed")
  11. if TYPE_CHECKING:
  12. from statsig.statsig_user import StatsigUser
  13. class StatsigIntegration(Integration):
  14. identifier = "statsig"
  15. @staticmethod
  16. def setup_once():
  17. # type: () -> None
  18. version = parse_version(STATSIG_VERSION)
  19. _check_minimum_version(StatsigIntegration, version, "statsig")
  20. # Wrap and patch evaluation method(s) in the statsig module
  21. old_check_gate = statsig_module.check_gate
  22. @wraps(old_check_gate)
  23. def sentry_check_gate(user, gate, *args, **kwargs):
  24. # type: (StatsigUser, str, *Any, **Any) -> Any
  25. enabled = old_check_gate(user, gate, *args, **kwargs)
  26. add_feature_flag(gate, enabled)
  27. return enabled
  28. statsig_module.check_gate = sentry_check_gate