Skip to content

Commit 4f87f4c

Browse files
authored
feat: Include instance ID header across API requests (#221)
1 parent eba4a05 commit 4f87f4c

File tree

6 files changed

+23
-1
lines changed

6 files changed

+23
-1
lines changed

Diff for: composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"php": ">=8.1",
1818
"monolog/monolog": "^2.0|^3.0",
1919
"psr/cache": "^3.0",
20-
"psr/log": "^1.0|^2.0|^3.0"
20+
"psr/log": "^1.0|^2.0|^3.0",
21+
"ramsey/uuid": "^4.7"
2122
},
2223
"require-dev": {
2324
"friendsofphp/php-cs-fixer": "^3.15.0",

Diff for: src/LaunchDarkly/Impl/Util.php

+4
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ public static function defaultHeaders(string $sdkKey, array $options): array
120120
}
121121
}
122122

123+
if (!empty($options['instance_id'])) {
124+
$headers['X-LaunchDarkly-Instance-Id'] = $options['instance_id'];
125+
}
126+
123127
return $headers;
124128
}
125129

Diff for: src/LaunchDarkly/LDClient.php

+12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Monolog\Handler\ErrorLogHandler;
2626
use Monolog\Logger;
2727
use Psr\Log\LoggerInterface;
28+
use Ramsey\Uuid\Uuid;
2829

2930
/**
3031
* A client for the LaunchDarkly API.
@@ -92,6 +93,17 @@ class LDClient
9293
*/
9394
public function __construct(string $sdkKey, array $options = [])
9495
{
96+
$apcuLoaded = extension_loaded('apcu');
97+
98+
if ($apcuLoaded && \apcu_enabled()) {
99+
$options['instance_id'] = \apcu_entry('ld::instanceid', function ($key) {
100+
$uuid = Uuid::uuid4()->toString();
101+
return $uuid;
102+
});
103+
} elseif (php_sapi_name() === 'cli') {
104+
$options['instance_id'] = Uuid::uuid4()->toString();
105+
}
106+
95107
$this->_sdkKey = $sdkKey;
96108
if (!isset($options['base_uri'])) {
97109
$this->_baseUri = self::DEFAULT_BASE_URI;

Diff for: test-service/TestService.php

+1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ public function getStatus(): array
7777
'migrations',
7878
'event-sampling',
7979
'inline-context-all',
80+
'instance-id',
8081
'anonymous-redaction',
8182
'client-prereq-events',
8283
'big-segments'

Diff for: tests/Impl/Integrations/EventPublisherTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public function getEventPublisher(): array
3434
'connect_timeout' => 3,
3535
'application_info' => $appInfo,
3636
'logger' => $logger,
37+
'instance_id' => 'my-instance-id',
3738
];
3839

3940
$curlPublisher = new Integrations\CurlEventPublisher('sdk-key', $config);
@@ -90,5 +91,6 @@ public function testSendsCorrectBodyAndHeaders($publisher)
9091
$this->assertEquals(EventPublisher::CURRENT_SCHEMA_VERSION, $headers['X-LaunchDarkly-Event-Schema']);
9192
$this->assertArrayHasKey('X-LaunchDarkly-Unsummarized', $headers);
9293
$this->assertEquals('application-id/my-id application-version/my-version', $headers['X-LaunchDarkly-Tags']);
94+
$this->assertEquals('my-instance-id', $headers['X-LaunchDarkly-Instance-Id']);
9395
}
9496
}

Diff for: tests/Impl/Integrations/GuzzleFeatureRequesterTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public function testSendsCorrectHeaders(): void
3232
'timeout' => 3,
3333
'connect_timeout' => 3,
3434
'application_info' => $appInfo,
35+
'instance_id' => 'my-instance-id',
3536
];
3637

3738
$requester = new GuzzleFeatureRequester('http://localhost:8080', 'sdk-key', $config);
@@ -71,6 +72,7 @@ public function testSendsCorrectHeaders(): void
7172
$this->assertEquals('sdk-key', $headers['Authorization']);
7273
$this->assertEquals('PHPClient/' . LDClient::VERSION, $headers['User-Agent']);
7374
$this->assertEquals('application-id/my-id application-version/my-version', $headers['X-LaunchDarkly-Tags']);
75+
$this->assertEquals('my-instance-id', $headers['X-LaunchDarkly-Instance-Id']);
7476
}
7577

7678
public function wrapperProvider(): array

0 commit comments

Comments
 (0)