-
Notifications
You must be signed in to change notification settings - Fork 70
feat: emit $is_server property on captured events #643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cd1db41
c2be91e
2f1a29d
214b79f
2037d02
b9760e7
4e9fc63
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| pypi/posthog: minor | ||
| --- | ||
|
|
||
| Add a configurable `$is_server` event property (default `true`) so PostHog can identify server-side events. Set `is_server=False` when using posthog-python as a client/CLI so the device OS is attributed normally. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -282,6 +282,9 @@ def get_tags() -> Dict[str, Any]: | |
| poll_interval: Seconds between local feature flag definition refreshes. | ||
| disable_geoip: Whether to disable server-side GeoIP enrichment. Defaults to | ||
| True. | ||
| is_server: Whether events are emitted from a server-side runtime. Defaults to | ||
| True; set to False when using the SDK as a client/CLI so the device OS is | ||
| attributed to the person normally. | ||
| feature_flags_request_timeout_seconds: Timeout in seconds for feature flag | ||
| and remote config requests. | ||
| super_properties: Properties merged into every captured event. | ||
|
|
@@ -313,6 +316,7 @@ def get_tags() -> Dict[str, Any]: | |
| project_api_key = None # type: Optional[str] | ||
| poll_interval = 30 # type: int | ||
| disable_geoip = True # type: bool | ||
| is_server = True # type: bool | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bool would be hard to change in the future without breaking changes if needed. Wondering if we should be defining as String ( I'll hold back review on other SDKs since they follow the same patters so these points can be discussed here?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CLI is also a client side, its all about if its running in the users machine or not at the end of the day, does not matter if its a desktop app, a mobile app, a CLI, etc
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yea I think bool is ok, opening up to strings can make it complicated and we really just want to know if its server or not. |
||
| feature_flags_request_timeout_seconds = 3 # type: int | ||
| super_properties = None # type: Optional[Dict] | ||
| enable_exception_autocapture = False # type: bool | ||
|
|
@@ -1084,6 +1088,7 @@ def setup() -> Client: | |
| poll_interval=poll_interval, | ||
| disabled=disabled, | ||
| disable_geoip=disable_geoip, | ||
| is_server=is_server, | ||
| feature_flags_request_timeout_seconds=feature_flags_request_timeout_seconds, | ||
| super_properties=super_properties, | ||
| # TODO: Currently this monitoring begins only when the Client is initialised (which happens when you do something with the SDK) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -194,6 +194,7 @@ def __init__( | |
| personal_api_key=None, | ||
| disabled=False, | ||
| disable_geoip=True, | ||
| is_server=True, | ||
| historical_migration=False, | ||
| feature_flags_request_timeout_seconds=3, | ||
| super_properties=None, | ||
|
|
@@ -239,6 +240,9 @@ def __init__( | |
| disabled: If True, disable captures and API requests. Useful in tests. | ||
| disable_geoip: Whether to disable server-side GeoIP enrichment. | ||
| Defaults to True. | ||
| is_server: Whether events are emitted from a server-side runtime. | ||
| Defaults to True; set to False when using the SDK as a client/CLI | ||
| so the device OS is attributed to the person normally. | ||
| historical_migration: Mark events as historical migration imports. | ||
| feature_flags_request_timeout_seconds: Timeout in seconds for feature | ||
| flag and remote config requests. | ||
|
|
@@ -314,6 +318,7 @@ def __init__( | |
| self._flag_definition_cache_provider_async_runner_lock = threading.Lock() | ||
| self.disabled = disabled or not self.api_key | ||
| self.disable_geoip = disable_geoip | ||
| self.is_server = is_server | ||
| self.historical_migration = historical_migration | ||
| self.super_properties = super_properties | ||
| self.enable_exception_autocapture = enable_exception_autocapture | ||
|
|
@@ -1328,6 +1333,11 @@ def _enqueue(self, msg, disable_geoip): | |
| if self.super_properties: | ||
| msg["properties"] = {**msg["properties"], **self.super_properties} | ||
|
|
||
| # Set after the super_properties merge so this SDK's server classification | ||
| # can't be silently overridden by a user-provided super property. | ||
| if self.is_server: | ||
| msg["properties"]["$is_server"] = True | ||
|
Comment on lines
+1338
to
+1339
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious, why do we omit vs set to False?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. makes sense to me, we dont send on the client side so its the same semantics |
||
|
|
||
| msg["distinct_id"] = stringify_id(msg.get("distinct_id", None)) | ||
|
|
||
| msg = clean(msg) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We are missing a docstring entry above
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated, should be there for all others as well