-
Notifications
You must be signed in to change notification settings - Fork 39
feat: report $release_id from POSTHOG_RELEASE_ID on exceptions #301
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "posthog-go": minor | ||
| --- | ||
|
|
||
| Report a `$release_id` on `$exception` events when `POSTHOG_RELEASE_ID` is set in the environment. This is the native, deploy-time counterpart to injecting `$release_id` into a web bundle: a build tool creates the release with `posthog-cli release resolve`, launches the app with the printed id in `POSTHOG_RELEASE_ID`, and the SDK stamps it on each exception so the server resolves that exception's release by a direct id lookup — no release name or version has to match anything the app reports. Only exception events carry it (that is where a release is resolved), the variable is read once, and an unset or blank value changes nothing. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package posthog | ||
|
|
||
| import ( | ||
| "os" | ||
| "strings" | ||
| "sync" | ||
| ) | ||
|
|
||
| // releaseIDEnvVar is the environment variable the SDK reads the release id from. | ||
| // | ||
| // This is the native, deploy-time counterpart to injecting $release_id into a web bundle. A | ||
| // compiled binary has no bundle, so a build tool creates the release with `posthog-cli release | ||
| // resolve`, launches the app with the printed id in this variable, and the SDK reports it as | ||
| // $release_id on every exception — so the server resolves the exception's release by a direct id | ||
| // lookup, with no release name or version having to match anything the app reports. | ||
| const releaseIDEnvVar = "POSTHOG_RELEASE_ID" | ||
|
|
||
| var ( | ||
| releaseIDOnce sync.Once | ||
| releaseIDValue *string | ||
| ) | ||
|
|
||
| // releaseIDFromEnv returns the release id from POSTHOG_RELEASE_ID, read once. It returns nil when | ||
| // the variable is unset or blank, so no $release_id is sent. | ||
| func releaseIDFromEnv() *string { | ||
| releaseIDOnce.Do(func() { | ||
| releaseIDValue = normalizeReleaseID(os.Getenv(releaseIDEnvVar)) | ||
| }) | ||
| return releaseIDValue | ||
| } | ||
|
|
||
| // normalizeReleaseID trims the raw value and treats a blank string as unset, so POSTHOG_RELEASE_ID= | ||
| // (or whitespace) does not send an empty $release_id. | ||
| func normalizeReleaseID(raw string) *string { | ||
| trimmed := strings.TrimSpace(raw) | ||
| if trimmed == "" { | ||
| return nil | ||
| } | ||
| return &trimmed | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| package posthog | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestNormalizeReleaseID(t *testing.T) { | ||
| if got := normalizeReleaseID(""); got != nil { | ||
| t.Errorf("unset: want nil, got %q", *got) | ||
| } | ||
| if got := normalizeReleaseID(" "); got != nil { | ||
| t.Errorf("blank: want nil, got %q", *got) | ||
| } | ||
| const id = "01a04245-8c54-0000-7530-28eed93002b0" | ||
| if got := normalizeReleaseID(" " + id + " "); got == nil || *got != id { | ||
| t.Errorf("value: want %q trimmed, got %v", id, got) | ||
| } | ||
| } | ||
|
|
||
| // forceReleaseID pins the cached POSTHOG_RELEASE_ID value for a test and restores it afterwards, | ||
| // bypassing the process-global env read (which is behind a sync.Once). | ||
| func forceReleaseID(t *testing.T, value *string) { | ||
| t.Helper() | ||
| releaseIDOnce.Do(func() {}) // spend the Once so releaseIDFromEnv returns releaseIDValue | ||
| old := releaseIDValue | ||
| releaseIDValue = value | ||
| t.Cleanup(func() { releaseIDValue = old }) | ||
| } | ||
|
|
||
| func TestReleaseIDIsAddedOnlyToExceptionEvents(t *testing.T) { | ||
| const id = "01a04245-8c54-0000-7530-28eed93002b0" | ||
| forceReleaseID(t, Ptr(id)) | ||
|
|
||
| exc := Exception{ | ||
| DistinctId: "user-1", | ||
| ExceptionList: []ExceptionItem{{Type: "Error", Value: "boom"}}, | ||
| } | ||
|
|
||
| // v0 / callback path. | ||
| api, ok := exc.APIfy().(ExceptionInApi) | ||
| if !ok { | ||
| t.Fatalf("APIfy did not return ExceptionInApi") | ||
| } | ||
| if api.Properties.ReleaseId == nil || *api.Properties.ReleaseId != id { | ||
| t.Errorf("APIfy: want $release_id %q, got %v", id, api.Properties.ReleaseId) | ||
| } | ||
|
|
||
| // v1 path. | ||
| if got, _ := exc.apifyEvent().properties["$release_id"].(string); got != id { | ||
| t.Errorf("apifyEvent: want $release_id %q, got %q", id, got) | ||
| } | ||
|
|
||
| // A non-exception event must not carry it — the release is only resolved on exceptions. | ||
| capEv := Capture{Event: "custom_event", DistinctId: "user-1"}.apifyEvent() | ||
| if _, present := capEv.properties["$release_id"]; present { | ||
| t.Errorf("capture event must not carry $release_id") | ||
| } | ||
| } | ||
|
|
||
| func TestReleaseIDAbsentWhenUnset(t *testing.T) { | ||
| forceReleaseID(t, nil) | ||
|
|
||
| exc := Exception{ | ||
| DistinctId: "user-1", | ||
| ExceptionList: []ExceptionItem{{Type: "Error", Value: "boom"}}, | ||
| } | ||
| if api := exc.APIfy().(ExceptionInApi); api.Properties.ReleaseId != nil { | ||
| t.Errorf("unset: want nil $release_id, got %q", *api.Properties.ReleaseId) | ||
| } | ||
| if _, present := exc.apifyEvent().properties["$release_id"]; present { | ||
| t.Errorf("unset: exception must not carry $release_id") | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
similar to my comment on the rust PR, should this come from
ldflagsinstead? otherwise this is a runtime requirement instead of a build time requirement