Skip to content

Commit f269669

Browse files
authored
feat: Conditionally add flags instead of filtering (#139)
1 parent 500cf40 commit f269669

File tree

5 files changed

+69
-43
lines changed

5 files changed

+69
-43
lines changed

src/LaunchDarkly/Impl/Events/EventFactory.php

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,20 @@ public function newEvalEvent(
4444
'kind' => 'feature',
4545
'creationDate' => Util::currentTimeUnixMillis(),
4646
'key' => $flag->getKey(),
47-
'samplingRatio' => $flag->getSamplingRatio(),
48-
'excludeFromSummaries' => $flag->getExcludeFromSummaries(),
4947
'context' => $context,
5048
'variation' => $detail->getVariationIndex(),
5149
'value' => $detail->getValue(),
5250
'default' => $default,
5351
'version' => $flag->getVersion()
5452
];
53+
5554
// the following properties are handled separately so we don't waste bandwidth on unused keys
55+
if ($flag->getExcludeFromSummaries()) {
56+
$e['excludeFromSummaries'] = true;
57+
}
58+
if ($flag->getSamplingRatio() !== 1) {
59+
$e['samplingRatio'] = $flag->getSamplingRatio();
60+
}
5661
if ($forceReasonTracking || $flag->isTrackEvents()) {
5762
$e['trackEvents'] = true;
5863
}
@@ -68,35 +73,6 @@ public function newEvalEvent(
6873
return $e;
6974
}
7075

71-
/**
72-
* @return mixed[]
73-
*/
74-
public function newDefaultEvent(FeatureFlag $flag, LDContext $context, EvaluationDetail $detail): array
75-
{
76-
$e = [
77-
'kind' => 'feature',
78-
'creationDate' => Util::currentTimeUnixMillis(),
79-
'key' => $flag->getKey(),
80-
'samplingRatio' => $flag->getSamplingRatio(),
81-
'excludeFromSummaries' => $flag->getExcludeFromSummaries(),
82-
'context' => $context,
83-
'value' => $detail->getValue(),
84-
'default' => $detail->getValue(),
85-
'version' => $flag->getVersion()
86-
];
87-
// the following properties are handled separately so we don't waste bandwidth on unused keys
88-
if ($flag->isTrackEvents()) {
89-
$e['trackEvents'] = true;
90-
}
91-
if ($flag->getDebugEventsUntilDate()) {
92-
$e['debugEventsUntilDate'] = $flag->getDebugEventsUntilDate();
93-
}
94-
if ($this->_withReasons) {
95-
$e['reason'] = $detail->getReason()->jsonSerialize();
96-
}
97-
return $e;
98-
}
99-
10076
/**
10177
* @return mixed[]
10278
*/

src/LaunchDarkly/Impl/Events/EventProcessor.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,11 @@ public function enqueue(array $event): bool
5151
return false;
5252
}
5353

54-
$samplingRatio = $event['samplingRatio'] ?? 1;
55-
if (is_int($samplingRatio) && !Util::sample($samplingRatio)) {
56-
return false;
54+
if (isset($event['samplingRatio'])) {
55+
$samplingRatio = $event['samplingRatio'];
56+
if (is_int($samplingRatio) && !Util::sample($samplingRatio)) {
57+
return false;
58+
}
5759
}
5860

5961
$this->_queue[] = $event;

src/LaunchDarkly/Impl/Events/EventSerializer.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,7 @@ private function filterEvent(array $e): array
5050
{
5151
$ret = [];
5252
foreach ($e as $key => $value) {
53-
if ($key == 'samplingRatio' && $value === 1) {
54-
// 1 is the default case, so we don't have to include it in the final output.
55-
continue;
56-
} elseif ($key == 'excludeFromSummaries' && $value === false) {
57-
// false is the default case, so we don't have to include it in the final output.
58-
continue;
59-
} elseif ($key == 'context') {
53+
if ($key == 'context') {
6054
$ret[$key] = $this->serializeContext($value);
6155
} else {
6256
$ret[$key] = $value;

src/LaunchDarkly/Migrations/OpTracker.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,6 @@ public function build(): array|string
139139

140140
$event = [
141141
'kind' => 'migration_op',
142-
'samplingRatio' => 1,
143142
'creationDate' => Util::currentTimeUnixMillis(),
144143
'contextKeys' => $this->context->getKeys(),
145144
'operation' => $this->operation->value,
@@ -160,7 +159,10 @@ public function build(): array|string
160159

161160
if ($this->flag) {
162161
$event['evaluation']['version'] = $this->flag->getVersion();
163-
$event['samplingRatio'] = $this->flag->getSamplingRatio();
162+
163+
if ($this->flag->getSamplingRatio() !== 1) {
164+
$event['samplingRatio'] = $this->flag->getSamplingRatio();
165+
}
164166
}
165167

166168
if ($this->detail->getVariationIndex() !== null) {

tests/Impl/Events/EventFactoryTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use LaunchDarkly\Impl\Events\EventFactory;
99
use LaunchDarkly\Impl\Model\FeatureFlag;
1010
use LaunchDarkly\LDContext;
11+
use LaunchDarkly\Tests\ModelBuilders;
1112
use PHPUnit\Framework\TestCase;
1213

1314
class EventFactoryTest extends TestCase
@@ -76,6 +77,57 @@ public function testTrackEventTrue()
7677
$this->assertTrue($result['trackEvents']);
7778
}
7879

80+
public function testEvalEventHandlesSamplingRatio()
81+
{
82+
$builder = ModelBuilders::flagBuilder('flag')
83+
->variations('fall', 'off', 'on')
84+
->on(true)
85+
->offVariation(1)
86+
->fallthroughVariation(0);
87+
88+
$ef = new EventFactory(false);
89+
$context = LDContext::create('userkey');
90+
$detail = new EvaluationDetail('off', 1, EvaluationReason::fallthrough());
91+
92+
$flag = $builder->build();
93+
$result = $ef->newEvalEvent($flag, $context, new EvalResult($detail, false), null);
94+
$this->assertArrayNotHasKey('samplingRatio', $result);
95+
96+
$flag = $builder->samplingRatio(0)->build();
97+
$result = $ef->newEvalEvent($flag, $context, new EvalResult($detail, false), null);
98+
$this->assertEquals(0, $result['samplingRatio']);
99+
100+
$flag = $builder->samplingRatio(1)->build();
101+
$result = $ef->newEvalEvent($flag, $context, new EvalResult($detail, false), null);
102+
$this->assertArrayNotHasKey('samplingRatio', $result);
103+
104+
$flag = $builder->samplingRatio(2)->build();
105+
$result = $ef->newEvalEvent($flag, $context, new EvalResult($detail, false), null);
106+
$this->assertEquals(2, $result['samplingRatio']);
107+
}
108+
109+
public function testEvalEventHandlesExcludeFromSummaries()
110+
{
111+
$builder = ModelBuilders::flagBuilder('flag')
112+
->variations('fall', 'off', 'on')
113+
->on(true)
114+
->offVariation(1)
115+
->fallthroughVariation(0);
116+
117+
$ef = new EventFactory(false);
118+
$context = LDContext::create('userkey');
119+
120+
$detail = new EvaluationDetail('off', 1, EvaluationReason::fallthrough());
121+
122+
$flag = $builder->excludeFromSummaries(true)->build();
123+
$result = $ef->newEvalEvent($flag, $context, new EvalResult($detail, false), null);
124+
$this->assertTrue($result['excludeFromSummaries']);
125+
126+
$flag = $builder->excludeFromSummaries(false)->build();
127+
$result = $ef->newEvalEvent($flag, $context, new EvalResult($detail, false), null);
128+
$this->assertArrayNotHasKey('excludeFromSummaries', $result);
129+
}
130+
79131
public function testTrackEventTrueWhenTrackEventsFalseButExperimentFallthroughReason()
80132
{
81133
$ef = new EventFactory(false);

0 commit comments

Comments
 (0)