|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace LaunchDarkly\Migrations; |
| 6 | + |
| 7 | +use Exception; |
| 8 | +use LaunchDarkly\EvaluationDetail; |
| 9 | +use LaunchDarkly\Impl; |
| 10 | +use LaunchDarkly\Impl\Util; |
| 11 | +use LaunchDarkly\LDContext; |
| 12 | +use LaunchDarkly\LDUser; |
| 13 | +use Psr\Log\LoggerInterface; |
| 14 | + |
| 15 | +/** |
| 16 | + * An OpTracker is responsible for managing the collection of measurements that |
| 17 | + * which a user might wish to record throughout a migration-assisted operation. |
| 18 | + * |
| 19 | + * Example measurements include latency, errors, and consistency. |
| 20 | + * |
| 21 | + * The OpTracker is not expected to be instantiated directly. Consumers should |
| 22 | + * instead call {@see \LaunchDarkly\LDClient:migration_variation()} and use the |
| 23 | + * returned tracker instance. |
| 24 | + */ |
| 25 | +class OpTracker |
| 26 | +{ |
| 27 | + private LDContext $context; |
| 28 | + private ?Operation $operation = null; |
| 29 | + private array $invoked = []; |
| 30 | + private ?bool $consistent = null; |
| 31 | + private int $consistentRatio = 1; |
| 32 | + private array $errors = []; |
| 33 | + private array $latencies = []; |
| 34 | + |
| 35 | + public function __construct( |
| 36 | + private LoggerInterface $logger, |
| 37 | + private string $key, |
| 38 | + private ?Impl\Model\FeatureFlag $flag, |
| 39 | + LDContext|LDUser $contextOrUser, |
| 40 | + private EvaluationDetail $detail, |
| 41 | + private Stage $default_stage |
| 42 | + ) { |
| 43 | + $this->context = $contextOrUser instanceof LDUser ? LDContext::fromUser($contextOrUser) : $contextOrUser; |
| 44 | + $this->consistentRatio = 1; # TODO(sc-219378): This needs to get set from the flag once we support those fields |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + |
| 49 | + /** |
| 50 | + * Sets the migration related {@see Operation} associated with these tracking measurements. |
| 51 | + */ |
| 52 | + public function operation(Operation $operation): OpTracker |
| 53 | + { |
| 54 | + $this->operation = $operation; |
| 55 | + return $this; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Allows recording which {@see Origin}s were called during a migration. |
| 60 | + */ |
| 61 | + public function invoked(Origin $origin): OpTracker |
| 62 | + { |
| 63 | + $this->invoked[$origin->value] = true; |
| 64 | + return $this; |
| 65 | + } |
| 66 | + |
| 67 | + |
| 68 | + /** |
| 69 | + * Allows recording the results of a consistency check. |
| 70 | + * |
| 71 | + * This method accepts a callable which should take no parameters and return |
| 72 | + * a single boolean to represent the consistency check results for a read |
| 73 | + * operation. |
| 74 | + * |
| 75 | + * A callable is provided in case sampling rules do not require consistency |
| 76 | + * checking to run. In this case, we can avoid the overhead of a function by |
| 77 | + * not using the callable. |
| 78 | + * |
| 79 | + * @param callable $isConsistent Callable that accepts 0 parameters and must return a boolean |
| 80 | + */ |
| 81 | + public function consistent(callable $isConsistent): OpTracker |
| 82 | + { |
| 83 | + # TODO(sc-219378): Add sampling support |
| 84 | + try { |
| 85 | + $this->consistent = boolval($isConsistent()); |
| 86 | + } catch (Exception $e) { |
| 87 | + $msg = $e->getMessage(); |
| 88 | + $this->logger->error("exception raised during consistency check $msg; failed to record measurement"); |
| 89 | + } |
| 90 | + |
| 91 | + return $this; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Allows recording whether an error occurred during the operation. |
| 96 | + */ |
| 97 | + public function error(Origin $origin): OpTracker |
| 98 | + { |
| 99 | + $this->errors[$origin->value] = true; |
| 100 | + return $this; |
| 101 | + } |
| 102 | + |
| 103 | + |
| 104 | + /** |
| 105 | + * Allows tracking the recorded latency for an individual operation. |
| 106 | + */ |
| 107 | + public function latency(Origin $origin, float $elapsedMs): OpTracker |
| 108 | + { |
| 109 | + $this->latencies[$origin->value] = $elapsedMs; |
| 110 | + return $this; |
| 111 | + } |
| 112 | + |
| 113 | + |
| 114 | + /** |
| 115 | + * Returns an array representing a migration operation event. This event |
| 116 | + * data can be provided to {@see \LaunchDarkly\LDClient::trackMigrationOp()} |
| 117 | + * to relay this metric information upstream to LaunchDarkly services. |
| 118 | + * |
| 119 | + * @return array<string, mixed>|string |
| 120 | + */ |
| 121 | + public function build(): array|string |
| 122 | + { |
| 123 | + if (!$this->operation) { |
| 124 | + return "operation not provided"; |
| 125 | + } elseif (strlen($this->key) === 0) { |
| 126 | + return "migration operation cannot contain an empty key"; |
| 127 | + } elseif (count($this->invoked) === 0) { |
| 128 | + return "no origins were invoked"; |
| 129 | + } elseif (!$this->context->isValid()) { |
| 130 | + return "provided context was invalid"; |
| 131 | + } |
| 132 | + |
| 133 | + $error = $this->checkInvokedConsistency(); |
| 134 | + if ($error !== null) { |
| 135 | + return $error; |
| 136 | + } |
| 137 | + |
| 138 | + $event = [ |
| 139 | + 'kind' => 'migration_op', |
| 140 | + 'creationDate' => Util::currentTimeUnixMillis(), |
| 141 | + 'contextKeys' => $this->context->getKeys(), |
| 142 | + 'operation' => $this->operation->value, |
| 143 | + 'evaluation' => [ |
| 144 | + 'key' => $this->key, |
| 145 | + 'value' => $this->detail->getValue(), |
| 146 | + 'default' => $this->default_stage->value, |
| 147 | + 'reason' => $this->detail->getReason(), |
| 148 | + ], |
| 149 | + |
| 150 | + 'measurements' => [ |
| 151 | + [ |
| 152 | + 'key' => 'invoked', |
| 153 | + 'values' => $this->invoked, |
| 154 | + ] |
| 155 | + ], |
| 156 | + ]; |
| 157 | + |
| 158 | + if ($this->flag) { |
| 159 | + $event['evaluation']['version'] = $this->flag->getVersion(); |
| 160 | + } |
| 161 | + |
| 162 | + if ($this->detail->getVariationIndex() !== null) { |
| 163 | + $event['evaluation']['variation'] = $this->detail->getVariationIndex(); |
| 164 | + } |
| 165 | + |
| 166 | + if ($this->consistent !== null) { |
| 167 | + $measurement = [ |
| 168 | + 'key' => 'consistent', |
| 169 | + 'value' => $this->consistent, |
| 170 | + ]; |
| 171 | + |
| 172 | + if ($this->consistentRatio !== 1) { |
| 173 | + $measurement['samplingRatio'] = $this->consistentRatio; |
| 174 | + } |
| 175 | + |
| 176 | + $event['measurements'][] = $measurement; |
| 177 | + } |
| 178 | + |
| 179 | + if (count($this->errors)) { |
| 180 | + $event['measurements'][] = [ |
| 181 | + 'key' => 'error', |
| 182 | + 'values' => $this->errors, |
| 183 | + ]; |
| 184 | + } |
| 185 | + |
| 186 | + if (count($this->latencies)) { |
| 187 | + $event['measurements'][] = [ |
| 188 | + 'key' => 'latency_ms', |
| 189 | + 'values' => $this->latencies, |
| 190 | + ]; |
| 191 | + } |
| 192 | + |
| 193 | + return $event; |
| 194 | + } |
| 195 | + |
| 196 | + private function checkInvokedConsistency(): ?string |
| 197 | + { |
| 198 | + foreach (Origin::cases() as $origin) { |
| 199 | + $originValue = $origin->value; |
| 200 | + if (isset($this->invoked[$originValue])) { |
| 201 | + continue; |
| 202 | + } |
| 203 | + |
| 204 | + if (isset($this->latencies[$originValue])) { |
| 205 | + return "provided latency for origin {$originValue} without recording invocation"; |
| 206 | + } |
| 207 | + |
| 208 | + if (isset($this->errors[$originValue])) { |
| 209 | + return "provided error for origin {$originValue} without recording invocation"; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + if ($this->consistent !== null && count($this->invoked) !== 2) { |
| 214 | + return "provided consistency without recording both invocations"; |
| 215 | + } |
| 216 | + |
| 217 | + return null; |
| 218 | + } |
| 219 | +} |
0 commit comments