<?php namespace Gatekeeper; use Gatekeeper\Metrics\Metrics; $Endpoint = $_EVENT['request']->getEndpoint(); $userIdentifier = $_EVENT['request']->getUserIdentifier(); // build identifier string for current user $_EVENT['metrics']['endpointRequests'] = Metrics::appendCounter("endpoints/{$Endpoint->ID}/requests"); $_EVENT['metrics']['endpointUserRequests'] = Metrics::appendCounter("endpoints/{$Endpoint->ID}/users/{$userIdentifier}/requests");
<?php namespace Gatekeeper; use Gatekeeper\Metrics\Metrics; $Endpoint = $_EVENT['request']->getEndpoint(); // append metrics $_EVENT['metrics']['endpointResponsesExecuted'] = Metrics::appendCounter("endpoints/{$Endpoint->ID}/responsesExecuted"); $_EVENT['metrics']['endpointBytesExecuted'] = Metrics::appendCounter("endpoints/{$Endpoint->ID}/bytesExecuted", $_EVENT['Transaction']->ResponseBytes); $_EVENT['metrics']['endpointResponseTime'] = Metrics::appendAverage("endpoints/{$Endpoint->ID}/responseTime", $_EVENT['Transaction']->ResponseTime, $_EVENT['metrics']['endpointResponsesExecuted']); // drip bandwidth bucket if ($Endpoint->GlobalBandwidthPeriod && $Endpoint->GlobalBandwidthCount) { HitBuckets::drip("endpoints/{$Endpoint->ID}/bandwidth", function () use($Endpoint) { return ['seconds' => $Endpoint->GlobalBandwidthPeriod, 'count' => $Endpoint->GlobalBandwidthCount]; }, $_EVENT['Transaction']->ResponseBytes); }
<?php namespace Gatekeeper; use Gatekeeper\Alerts\TransactionFailed; use Gatekeeper\Alerts\ResponseTimeLimitExceeded; use Gatekeeper\Metrics\Metrics; $Endpoint = $_EVENT['request']->getEndpoint(); // send email alert if response code is 500+ and alerts are enabled if ($_EVENT['responseCode'] >= 500 and $Endpoint->AlertOnError) { $_EVENT['metrics']['endpointResponsesFailed'] = Metrics::appendCounter("endpoints/{$Endpoint->ID}/responsesFailed"); TransactionFailed::open($Endpoint, ['transactionId' => $_EVENT['Transaction']->ID, 'request' => ['uri' => $_EVENT['request']->getUrl()], 'response' => ['status' => $_EVENT['responseCode'], 'headers' => $_EVENT['responseHeaders'], 'body' => $_EVENT['responseBody'], 'bytes' => $_EVENT['Transaction']->ResponseBytes, 'time' => $_EVENT['Transaction']->ResponseTime]]); } elseif ($_EVENT['responseCode'] == 0 && $_EVENT['curlError'] == CURLE_OPERATION_TIMEOUTED) { $_EVENT['metrics']['endpointResponsesTimedOut'] = Metrics::appendCounter("endpoints/{$Endpoint->ID}/responsesTimedOut"); ResponseTimeLimitExceeded::open($Endpoint, ['transactionId' => $_EVENT['Transaction']->ID, 'request' => ['uri' => $_EVENT['request']->getUrl()], 'response' => ['time' => round($_EVENT['curlInfo']['total_time'] * 1000)]]); }
public function getAverageMetric($averageName, $counterName) { if (!array_key_exists($averageName, $this->_metricsCache['averages'])) { $this->_metricsCache['averages'][$averageName] = Metrics::estimateAverage("endpoints/{$this->ID}/{$averageName}", "endpoints/{$this->ID}/{$counterName}"); } return $this->_metricsCache['averages'][$averageName]; }
die('Host does not have a cron_key configured'); } if (empty($_GET['cron_key'])) { header('HTTP/1.0 401 Unauthorized'); die('cron_key required'); } if ($_GET['cron_key'] != $cronKey) { header('HTTP/1.0 401 Unauthorized'); die('cron_key is incorrect'); } if ($_SERVER['REQUEST_METHOD'] != 'POST') { header('HTTP/1.0 405 Method Not Allowed'); die('POST method required'); } $cacheKeyPrefix = Cache::getKeyPrefix(); $currentSampleIndex = Metrics::getCurrentSampleIndex(); $flushed = 0; foreach (Cache::getIterator('|^metrics/|') as $cacheEntry) { if (!preg_match('|^' . preg_quote($cacheKeyPrefix) . '(metrics/(.*)/(\\d+))$|', $cacheEntry['key'], $matches)) { continue; } $cacheKey = $matches[1]; $metricKey = $matches[2]; $sampleIndex = (int) $matches[3]; // skip active and previus samples if ($sampleIndex >= $currentSampleIndex - 1) { continue; } // delete sample from cache Cache::delete($cacheKey); // save metric to DB
<?php namespace Gatekeeper; use Cache; use Gatekeeper\Metrics\Metrics; $Endpoint = $_EVENT['request']->getEndpoint(); $url = $_EVENT['request']->getUrl(); // attempt to load response from cache if enabled for this endpoint if ($_SERVER['REQUEST_METHOD'] == 'GET' && $Endpoint->CachingEnabled) { $cacheKey = "response:{$Endpoint->ID}:{$url}"; if ($cachedResponse = Cache::fetch($cacheKey)) { if ($cachedResponse['expires'] < $_EVENT['request']->getStartTime()) { Cache::delete($cacheKey); $cachedResponse = false; } else { foreach ($cachedResponse['headers'] as $header) { header($header); } print $cachedResponse['body']; $_EVENT['metrics']['endpointResponsesCached'] = Metrics::appendCounter("endpoints/{$Endpoint->ID}/responsesCached"); $_EVENT['metrics']['endpointBytesCached'] = Metrics::appendCounter("endpoints/{$Endpoint->ID}/bytesCached", strlen($cachedResponse['body'])); \Site::finishRequest(); } } }