-
Notifications
You must be signed in to change notification settings - Fork 34
feat: send $release_id from POSTHOG_RELEASE_ID on every event #247
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
Merged
+201
−1
Merged
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-php": minor | ||
| --- | ||
|
|
||
| Read the release id from the `POSTHOG_RELEASE_ID` environment variable and send it as `$release_id` on every event, including minimal `$feature_flag_called` events. On `$exception` events, error tracking uses it to link the exception to its release by a direct id lookup. Create the release and get its id with `posthog-cli release resolve`. An explicit `$release_id` in the event properties or in the request context wins over the environment variable. Under PHP-FPM, pass the variable through to workers (`clear_env = no` or an `env[POSTHOG_RELEASE_ID]` pool entry). |
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,174 @@ | ||
| <?php | ||
|
|
||
| namespace PostHog\Test; | ||
|
|
||
| use PHPUnit\Framework\Attributes\DataProvider; | ||
| use PHPUnit\Framework\TestCase; | ||
| use PostHog\Client; | ||
| use PostHog\PostHog; | ||
| use PostHog\Test\Assets\MockedResponses; | ||
| use ReflectionProperty; | ||
| use RuntimeException; | ||
|
|
||
| class ReleaseIdTest extends TestCase | ||
| { | ||
| private const FAKE_API_KEY = "random_key"; | ||
| private const ENV_RELEASE_ID = "POSTHOG_RELEASE_ID"; | ||
|
|
||
| private string|false $previousReleaseId; | ||
| private MockedHttpClient $httpClient; | ||
|
|
||
| public function setUp(): void | ||
| { | ||
| date_default_timezone_set("UTC"); | ||
| $this->previousReleaseId = getenv(self::ENV_RELEASE_ID); | ||
| } | ||
|
|
||
| public function tearDown(): void | ||
| { | ||
| $this->setReleaseIdEnv($this->previousReleaseId === false ? null : $this->previousReleaseId); | ||
| // The groupIdentify case installs its client in the facade, and a later test that uses the | ||
| // facade without calling init would otherwise send through it. | ||
| (new ReflectionProperty(PostHog::class, 'client'))->setValue(null, null); | ||
| } | ||
|
|
||
| public static function eventCases(): array | ||
| { | ||
| return [ | ||
| 'capture' => ['Module PHP Event', static function (Client $client): void { | ||
| $client->capture(["distinctId" => "john", "event" => "Module PHP Event"]); | ||
| }], | ||
| 'captureException' => ['$exception', static function (Client $client): void { | ||
| $client->captureException(new RuntimeException("boom"), "john"); | ||
| }], | ||
| 'identify' => ['$identify', static function (Client $client): void { | ||
| $client->identify(["distinctId" => "john", "properties" => ["email" => "john@example.com"]]); | ||
| }], | ||
| 'alias' => ['$create_alias', static function (Client $client): void { | ||
| $client->alias(["distinctId" => "john", "alias" => "anonymous-id"]); | ||
| }], | ||
| 'groupIdentify' => ['$groupidentify', static function (Client $client): void { | ||
| PostHog::init(null, null, $client); | ||
| PostHog::groupIdentify(["groupType" => "company", "groupKey" => "id:5"]); | ||
| }], | ||
| 'full $feature_flag_called' => ['$feature_flag_called', static function (Client $client): void { | ||
| $client->evaluateFlags('john')->isEnabled('simple-test'); | ||
| }], | ||
| 'minimal $feature_flag_called' => ['$feature_flag_called', static function (Client $client): void { | ||
| $client->evaluateFlags('john')->isEnabled('simple-test'); | ||
| }, true], | ||
| ]; | ||
| } | ||
|
|
||
| #[DataProvider('eventCases')] | ||
| public function testReleaseIdIsSentOnEveryEvent( | ||
| string $expectedEvent, | ||
| callable $send, | ||
| bool $minimalFlagCalledEvents = false | ||
| ): void { | ||
| $this->setReleaseIdEnv("rel-123"); | ||
| $flagsResponse = MockedResponses::FLAGS_V2_RESPONSE; | ||
| if ($minimalFlagCalledEvents) { | ||
| $flagsResponse['minimalFlagCalledEvents'] = true; | ||
| $flagsResponse['flags']['simple-test']['metadata']['has_experiment'] = false; | ||
| } | ||
| $client = $this->createClient($flagsResponse); | ||
|
|
||
| $send($client); | ||
| $client->flush(); | ||
|
|
||
| $event = $this->onlyBatchEvent(); | ||
| $this->assertSame($expectedEvent, $event['event']); | ||
| $this->assertSame("rel-123", $event['properties']['$release_id']); | ||
| if ($minimalFlagCalledEvents) { | ||
| // Proves the allowlist ran, so the release id survived minimization rather than skipping it. | ||
| $this->assertArrayNotHasKey('$lib_consumer', $event['properties']); | ||
| } | ||
| } | ||
|
|
||
| public static function envValueCases(): array | ||
| { | ||
| return [ | ||
| 'set' => ["rel-123", "rel-123"], | ||
| 'surrounding whitespace is trimmed' => [" rel-123 \n", "rel-123"], | ||
| 'empty counts as unset' => ["", null], | ||
| 'whitespace only counts as unset' => [" \t ", null], | ||
| 'unset' => [null, null], | ||
| ]; | ||
| } | ||
|
|
||
| #[DataProvider('envValueCases')] | ||
| public function testReleaseIdFollowsEnvValue(?string $envValue, ?string $expected): void | ||
| { | ||
| $this->setReleaseIdEnv($envValue); | ||
| $client = $this->createClient(); | ||
|
|
||
| $client->capture(["distinctId" => "john", "event" => "Module PHP Event"]); | ||
| $client->flush(); | ||
|
|
||
| $properties = $this->onlyBatchEvent()['properties']; | ||
| if ($expected === null) { | ||
| $this->assertArrayNotHasKey('$release_id', $properties); | ||
| } else { | ||
| $this->assertSame($expected, $properties['$release_id']); | ||
| } | ||
| } | ||
|
|
||
| public static function explicitReleaseIdCases(): array | ||
| { | ||
| return [ | ||
| 'event property' => [static function (Client $client): void { | ||
| $client->capture([ | ||
| "distinctId" => "john", | ||
| "event" => "Module PHP Event", | ||
| "properties" => ['$release_id' => "explicit"], | ||
| ]); | ||
| }], | ||
| 'request context property' => [static function (Client $client): void { | ||
| $client->withContext( | ||
| ["properties" => ['$release_id' => "explicit"]], | ||
| static function () use ($client): void { | ||
| $client->capture(["distinctId" => "john", "event" => "Module PHP Event"]); | ||
| } | ||
| ); | ||
| }], | ||
| ]; | ||
| } | ||
|
|
||
| #[DataProvider('explicitReleaseIdCases')] | ||
| public function testExplicitReleaseIdWinsOverEnv(callable $send): void | ||
| { | ||
| $this->setReleaseIdEnv("rel-123"); | ||
| $client = $this->createClient(); | ||
|
|
||
| $send($client); | ||
| $client->flush(); | ||
|
|
||
| $this->assertSame("explicit", $this->onlyBatchEvent()['properties']['$release_id']); | ||
| } | ||
|
|
||
| private function createClient(array $flagsResponse = MockedResponses::FLAGS_V2_RESPONSE): Client | ||
| { | ||
| $this->httpClient = new MockedHttpClient("app.posthog.com", flagsEndpointResponse: $flagsResponse); | ||
|
|
||
| return new Client(self::FAKE_API_KEY, [], $this->httpClient, null, false); | ||
| } | ||
|
|
||
| private function onlyBatchEvent(): array | ||
| { | ||
| $events = []; | ||
| foreach ($this->httpClient->calls as $call) { | ||
| if (($call["path"] ?? null) === "/batch/") { | ||
| $events = array_merge($events, json_decode($call["payload"], true)["batch"]); | ||
| } | ||
| } | ||
| $this->assertCount(1, $events); | ||
|
|
||
| return $events[0]; | ||
| } | ||
|
|
||
| private function setReleaseIdEnv(?string $value): void | ||
| { | ||
| putenv($value === null ? self::ENV_RELEASE_ID : self::ENV_RELEASE_ID . "=" . $value); | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.