Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/api/FeatureFlags/Internal/NativeEvaluator.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function evaluate(
}
}

$details = $this->mapper->map($rawResult, $expectedType, $defaultValue);
$details = $this->mapper->map($rawResult, $expectedType, $defaultValue, SpanEnrichmentRegistry::gateEnabled());

if ($this->exposureRecorder !== null && is_object($rawResult)
&& !empty($rawResult->exposureCandidates) && !$details->isError()) {
Expand Down
11 changes: 8 additions & 3 deletions src/api/FeatureFlags/Internal/ResultMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class ResultMapper
* @param mixed $defaultValue
* @return EvaluationDetails
*/
public function map($rawResult, $expectedType, $defaultValue)
public function map($rawResult, $expectedType, $defaultValue, $spanEnrichmentEnabled = false)
{
if (!EvaluationType::isValid($expectedType)) {
throw new \InvalidArgumentException('Unknown feature flag value type: ' . (string) $expectedType);
Expand Down Expand Up @@ -93,7 +93,7 @@ public function map($rawResult, $expectedType, $defaultValue)
$this->read($rawResult, array('variant'), null),
null,
null,
$this->flagMetadata($rawResult),
$this->flagMetadata($rawResult, $spanEnrichmentEnabled),
Comment thread
hhan2024 marked this conversation as resolved.
Resolved
Comment thread
hhan2024 marked this conversation as resolved.
Resolved
$this->exposureData($rawResult),
$this->providerState($rawResult)
);
Expand Down Expand Up @@ -290,7 +290,7 @@ private function exposureData($rawResult)
return $exposureData;
}

private function flagMetadata($rawResult)
private function flagMetadata($rawResult, $spanEnrichmentEnabled)
Comment thread
hhan2024 marked this conversation as resolved.
Resolved
{
$metadata = $this->readArray($rawResult, array('flag_metadata', 'flagMetadata', 'metadata'));
foreach (SplitFeatures::toMetadata($this->read($rawResult, array('features'), null)) as $key => $value) {
Expand All @@ -305,6 +305,11 @@ private function flagMetadata($rawResult)
}
}

$serialId = $this->read($rawResult, array('serial_id', 'serialId'), null);
Comment thread
hhan2024 marked this conversation as resolved.
Resolved
if ($spanEnrichmentEnabled && $serialId !== null) {
$metadata[SplitFeatures::SERIAL_ID_KEY] = (int) $serialId;
}

return $metadata;
}
Comment thread
hhan2024 marked this conversation as resolved.
Resolved

Expand Down
31 changes: 31 additions & 0 deletions tests/api/Unit/FeatureFlags/ResultMapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,37 @@

final class ResultMapperTest extends TestCase
{
/**
* @dataProvider serialIdMetadataProvider
*/
public function testSerialIdFlagMetadata($serialFields, $enabled, $expectedMetadata)
Comment thread
hhan2024 marked this conversation as resolved.
Resolved
{
$rawResult = array_merge(array(
'value_json' => '"blue"',
'variant' => 'variant-a',
'reason' => ResultMapper::BRIDGE_REASON_SPLIT,
'error_code' => ResultMapper::BRIDGE_ERROR_NONE,
'flag_metadata' => array('owner' => 'ffe'),
), $serialFields);

foreach (array($rawResult, (object) $rawResult) as $result) {
$details = (new ResultMapper())->map($result, EvaluationType::STRING, 'red', $enabled);
$this->assertSame($expectedMetadata, $details->getFlagMetadata());
}
}

public function serialIdMetadataProvider()
{
return array(
'positive' => array(array('serial_id' => 4242), true, array('owner' => 'ffe', '__dd_split_serial_id' => 4242)),
'zero' => array(array('serial_id' => 0), true, array('owner' => 'ffe', '__dd_split_serial_id' => 0)),
'camel case' => array(array('serialId' => 7), true, array('owner' => 'ffe', '__dd_split_serial_id' => 7)),
'missing' => array(array(), true, array('owner' => 'ffe')),
'null' => array(array('serial_id' => null), true, array('owner' => 'ffe')),
'gate disabled' => array(array('serial_id' => 4242), false, array('owner' => 'ffe')),
);
}

public function testMapsSuccessfulBridgeResultToEvaluationDetails()
{
$details = (new ResultMapper())->map(array(
Expand Down
Loading