Example #1
0
 public function sendMetrics()
 {
     $metricsKeys = Di::getCache()->getKeys(MetricsCache::getCacheKeySearchLatencyPattern());
     $metricsCache = new MetricsCache();
     $cachedMetrics = array();
     foreach ($metricsKeys as $key) {
         $metricName = MetricsCache::getMetricNameFromKey($key);
         $metricBucket = MetricsCache::getBucketFromKey($key);
         if (!isset($cachedMetrics[$metricName]) || !is_array($cachedMetrics[$metricName])) {
             //initialization latency butckets
             $maxBucket = Latency::getBucketForLatencyMicros(Latency::MAX_LATENCY);
             $cachedMetrics[$metricName] = array_fill(0, $maxBucket + 1, 0);
         }
         $cachedMetrics[$metricName][$metricBucket] = $metricsCache->getLatencyAndReset($key);
     }
     $dataset = array();
     foreach ($cachedMetrics as $name => $latencies) {
         $dataset[] = array('name' => $name, 'latencies' => $latencies);
     }
     //Sending Metrics dataset.
     $response = $this->post($this->servicePath, $dataset);
     if ($response->isSuccessful()) {
         Di::getLogger()->info(count($dataset) . " Metrics sent successfuly");
     } else {
         Di::getLogger()->error("Metrics have not been sent successfully");
         Di::getLogger()->error("HTTP Code: " . $response->getStatusCode());
         Di::getLogger()->error("HTTP Body: " . $response->getBody());
     }
 }
Example #2
0
 private function evalTreatment($matchingKey, $bucketingKey, $featureName, array $attributes = null)
 {
     $split = null;
     $do_evaluation = false;
     $cachedFeature = $this->getCachedFeature($featureName);
     if ($cachedFeature !== null) {
         $split = $cachedFeature;
         $do_evaluation = true;
     } else {
         $splitCacheKey = SplitCache::getCacheKeyForSplit($featureName);
         $splitCachedItem = SplitApp::cache()->getItem($splitCacheKey);
         if ($splitCachedItem->isHit()) {
             SplitApp::logger()->info("{$featureName} is present on cache");
             $splitRepresentation = $splitCachedItem->get();
             $split = new Split(json_decode($splitRepresentation, true));
             $this->cacheFeature($featureName, $split);
             $do_evaluation = true;
         }
     }
     if ($do_evaluation) {
         //If the split was killed, log the impression and return default treatment.
         if ($split->killed()) {
             $defaultTreatment = $split->getDefaultTratment();
             $chn = $split->getChangeNumber();
             $this->logImpression($matchingKey, $featureName, $defaultTreatment, ImpressionLabel::KILLED, $chn);
             return $defaultTreatment;
         }
         $timeStart = Metrics::startMeasuringLatency();
         $treatment = Engine::getTreatment($matchingKey, $bucketingKey, $split, $attributes);
         $latency = Metrics::calculateLatency($timeStart);
         //If the given key doesn't match on any condition, default treatment is returned
         $impressionLabel = '';
         if ($treatment == null) {
             $treatment = $split->getDefaultTratment();
             $impressionLabel = ImpressionLabel::NO_CONDITION_MATCHED;
         }
         //Registering latency value
         MetricsCache::addLatencyOnBucket(Metrics::MNAME_SDK_GET_TREATMENT, Metrics::getBucketForLatencyMicros($latency));
         SplitApp::logger()->info("*Treatment for {$matchingKey} in {$split->getName()} is: {$treatment}");
         //Logging treatment impressions
         $this->logImpression($matchingKey, $featureName, $treatment, $impressionLabel, $split->getChangeNumber());
         //Returning treatment.
         return $treatment;
     }
     // Split not found impression
     $this->logImpression($matchingKey, $featureName, TreatmentEnum::CONTROL, ImpressionLabel::SPLIT_NOT_FOUND);
     SplitApp::logger()->warning("The SPLIT definition for '{$featureName}' has not been found'");
     return TreatmentEnum::CONTROL;
 }