Skip to content

Commit 5dfcfbb

Browse files
thomaspoignantrenovate[bot]CristianCurteanubeeme1mr
authoredJan 29, 2025··
feat(go-feature-flag): Support exporter metadata (#120)
* chore(deps): update dependency datadog/dd-trace to ^0.99.0 (#56) Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Signed-off-by: Thomas Poignant <[email protected]> * feat(go-feature-flag): Support exporter metadata Signed-off-by: Thomas Poignant <[email protected]> * upgrade rollout/rox to 6.0.1 (#119) Signed-off-by: CristianCurteanu <[email protected]> Signed-off-by: Thomas Poignant <[email protected]> * fix php doc Signed-off-by: Thomas Poignant <[email protected]> * chore: remove hardcoded version (#122) Signed-off-by: Michael Beemer <[email protected]> Signed-off-by: Thomas Poignant <[email protected]> * fix: add explicit usage of json_decode Signed-off-by: Thomas Poignant <[email protected]> * fix: use in alphabetical order Signed-off-by: Thomas Poignant <[email protected]> * fix lint type in doc Signed-off-by: Thomas Poignant <[email protected]> --------- Signed-off-by: Thomas Poignant <[email protected]> Signed-off-by: CristianCurteanu <[email protected]> Signed-off-by: Michael Beemer <[email protected]> Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: CristianCurteanu <[email protected]> Co-authored-by: Michael Beemer <[email protected]>
1 parent c9ffd9f commit 5dfcfbb

File tree

3 files changed

+81
-2
lines changed

3 files changed

+81
-2
lines changed
 

Diff for: ‎providers/GoFeatureFlag/src/config/Config.php

+31-2
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,38 @@ class Config
1919
*/
2020
private ?ClientInterface $httpclient;
2121

22+
/**
23+
* @var array<string, mixed> exporterMetadata - is the metadata we send to the GO Feature Flag relay proxy when we report
24+
* the evaluation data usage.
25+
*
26+
* ‼️Important: If you are using a GO Feature Flag relay proxy before version v1.41.0, the information of this
27+
* field will not be added to your feature events.
28+
*/
29+
private array $exporterMetadata = [];
30+
2231
/**
2332
* @param string $endpoint - The endpoint to your GO Feature Flag Instance
2433
* @param string|null $apiKey - API Key to use to connect to GO Feature Flag
2534
* @param array<string, string>|null $customHeaders - Custom headers you want to send
35+
* @param array<string, mixed>|null $exporterMetadata - Metadata to send to the relay proxy during evaluation data collection
2636
* @param ClientInterface|null $httpclient - The HTTP Client to use (if you want to use a custom one)
2737
*/
28-
public function __construct(string $endpoint, ?string $apiKey = '', ?array $customHeaders = [], ?ClientInterface $httpclient = null)
29-
{
38+
public function __construct(
39+
string $endpoint,
40+
?string $apiKey = '',
41+
?array $customHeaders = [],
42+
?array $exporterMetadata = [],
43+
?ClientInterface $httpclient = null,
44+
) {
3045
$this->httpclient = $httpclient;
3146
$this->endpoint = $endpoint;
3247
$this->customHeaders = $customHeaders ?? [];
48+
49+
// set default exporter metadata fields
50+
$this->exporterMetadata = $exporterMetadata ?? [];
51+
$this->exporterMetadata['openfeature'] = true;
52+
$this->exporterMetadata['provider'] = 'php';
53+
3354
if ($apiKey !== null && $apiKey !== '') {
3455
$this->customHeaders['Authorization'] = 'Bearer ' . $apiKey;
3556
}
@@ -57,4 +78,12 @@ public function getHttpClient(): ?ClientInterface
5778
{
5879
return $this->httpclient;
5980
}
81+
82+
/**
83+
* @return array<string, mixed>
84+
*/
85+
public function getExporterMetadata(): array
86+
{
87+
return $this->exporterMetadata;
88+
}
6089
}

Diff for: ‎providers/GoFeatureFlag/src/controller/OfrepApi.php

+3
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ public function evaluate(string $flagKey, EvaluationContext $evaluationContext):
7575
['targetingKey' => $evaluationContext->getTargetingKey()],
7676
);
7777

78+
// Add exporter metadata to the context
79+
$fields['gofeatureflag'] = ['exporterMetadata' => $this->options->getExporterMetadata()];
80+
7881
$requestBody = json_encode(['context' => $fields]);
7982
if ($requestBody === false) {
8083
throw new ParseException('failed to encode request body');

Diff for: ‎providers/GoFeatureFlag/tests/unit/GoFeatureFlagProviderTest.php

+47
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use ReflectionException;
2222

2323
use function PHPUnit\Framework\assertEquals;
24+
use function json_decode;
2425
use function json_encode;
2526

2627
class GoFeatureFlagProviderTest extends TestCase
@@ -474,6 +475,52 @@ public function testReturnAnErrorAPIResponseIf500(): void
474475
assertEquals('boolean_flag', $got->getFlagKey());
475476
}
476477

478+
public function testShouldSendExporterMetadataInContext(): void
479+
{
480+
$mockClient = $this->createMock(ClientInterface::class);
481+
$mockResponse = new Response(200, [], json_encode([
482+
'key' => 'integer_key',
483+
'value' => 42,
484+
'reason' => 'TARGETING_MATCH',
485+
'variant' => 'default',
486+
]));
487+
488+
$requestBody = '';
489+
$mockClient
490+
->expects($this->once())
491+
->method('sendRequest')
492+
->willReturnCallback(function ($request) use ($mockResponse, &$requestBody) {
493+
$requestBody = $request->getBody()->getContents();
494+
495+
return $mockResponse;
496+
});
497+
498+
$config = new Config(
499+
'http://gofeatureflag.org',
500+
null,
501+
[],
502+
['key1' => 'value', 'key2' => 123, 'key3' => 123.45],
503+
);
504+
505+
$provider = new GoFeatureFlagProvider($config);
506+
$this->mockHttpClient($provider, $mockClient);
507+
508+
$api = OpenFeatureAPI::getInstance();
509+
$api->setProvider($provider);
510+
$client = $api->getClient();
511+
$client->getBooleanDetails('boolean_flag', false, $this->defaultEvaluationContext);
512+
513+
// get the request body of the request received by the mock client
514+
$want = ['key1' => 'value',
515+
'key2' => 123,
516+
'key3' => 123.45,
517+
'openfeature' => true,
518+
'provider' => 'php',
519+
];
520+
$got = json_decode($requestBody, true)['context']['gofeatureflag']['exporterMetadata'];
521+
assertEquals($want, $got);
522+
}
523+
477524
protected function setUp(): void
478525
{
479526
parent::setUp();

0 commit comments

Comments
 (0)
Please sign in to comment.