Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 38 |
|
0.00% |
0 / 7 |
CRAP | |
0.00% |
0 / 1 |
| AwsCloudWatchLogRdsLoadMetric | |
0.00% |
0 / 38 |
|
0.00% |
0 / 7 |
306 | |
0.00% |
0 / 1 |
| collect | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| getLogGroupName | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getLogStreamName | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getLogEventsLimit | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getMetric | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
| getAwsClient | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| parseMetricValue | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace oat\taoDelivery\model\Metrics; |
| 4 | |
| 5 | use Aws\Result; |
| 6 | use oat\awsTools\AwsClient; |
| 7 | use oat\tao\model\metrics\implementations\abstractMetrics; |
| 8 | |
| 9 | class AwsCloudWatchLogRdsLoadMetric extends abstractMetrics implements InfrastructureLoadMetricInterface |
| 10 | { |
| 11 | /** @var string */ |
| 12 | public const OPTION_LOG_GROUP_NAME = 'logGroupName'; |
| 13 | |
| 14 | /** @var string */ |
| 15 | public const OPTION_LOG_STREAM_NAME = 'logStreamName'; |
| 16 | |
| 17 | /** @var string */ |
| 18 | public const OPTION_LOG_EVENTS_LIMIT = 'logEventsLimit'; |
| 19 | |
| 20 | /** @var int */ |
| 21 | public const DEFAULT_LOG_EVENTS_LIMIT = 1; |
| 22 | |
| 23 | /** @var int */ |
| 24 | public const DEFAULT_METRIC_VALUE = 0; |
| 25 | |
| 26 | /** |
| 27 | * @var AwsClient |
| 28 | */ |
| 29 | private $awsClient; |
| 30 | |
| 31 | /** |
| 32 | * @param bool $force |
| 33 | * @return float |
| 34 | * @throws \common_Exception |
| 35 | * @throws \oat\oatbox\service\exception\InvalidServiceManagerException |
| 36 | */ |
| 37 | public function collect($force = false) |
| 38 | { |
| 39 | if ($force || !$metricValue = $this->getPersistence()->get(self::class)) { |
| 40 | try { |
| 41 | $metricValue = $this->getMetric(); |
| 42 | } catch (\RuntimeException $e) { |
| 43 | $metricValue = self::DEFAULT_METRIC_VALUE; |
| 44 | $this->logAlert($e->getMessage()); |
| 45 | } |
| 46 | $this->getPersistence()->set(self::class, $metricValue, $this->getOption(self::OPTION_TTL)); |
| 47 | } |
| 48 | |
| 49 | return $metricValue; |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * @return mixed |
| 54 | * @throws MetricConfigurationException |
| 55 | */ |
| 56 | private function getLogGroupName() |
| 57 | { |
| 58 | if (!$this->hasOption(self::OPTION_LOG_GROUP_NAME)) { |
| 59 | throw new MetricConfigurationException('AWS CloudWatch Logs group not configured.'); |
| 60 | } |
| 61 | |
| 62 | return $this->getOption(self::OPTION_LOG_GROUP_NAME); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @return mixed |
| 67 | * @throws MetricConfigurationException |
| 68 | */ |
| 69 | private function getLogStreamName() |
| 70 | { |
| 71 | if (!$this->hasOption(self::OPTION_LOG_STREAM_NAME)) { |
| 72 | throw new MetricConfigurationException('AWS CloudWatch Logs stream not configured.'); |
| 73 | } |
| 74 | |
| 75 | return $this->getOption(self::OPTION_LOG_STREAM_NAME); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * @return int |
| 80 | */ |
| 81 | private function getLogEventsLimit() |
| 82 | { |
| 83 | if (!$this->hasOption(self::OPTION_LOG_EVENTS_LIMIT)) { |
| 84 | return self::DEFAULT_LOG_EVENTS_LIMIT; |
| 85 | } |
| 86 | |
| 87 | return $this->getOption(self::OPTION_LOG_EVENTS_LIMIT); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | |
| 92 | /** |
| 93 | * @return mixed |
| 94 | * @throws MetricConfigurationException |
| 95 | */ |
| 96 | private function getMetric() |
| 97 | { |
| 98 | $cloudWatchClient = $this->getAwsClient()->getCloudWatchLogsClient(); |
| 99 | $result = $cloudWatchClient->getLogEvents([ |
| 100 | 'limit' => $this->getLogEventsLimit(), |
| 101 | 'logGroupName' => $this->getLogGroupName(), // REQUIRED |
| 102 | 'logStreamName' => $this->getLogStreamName(), // REQUIRED |
| 103 | 'startFromHead' => false, |
| 104 | ]); |
| 105 | $this->logInfo('RDS_METRICS:' . json_encode($result->toArray())); |
| 106 | return $this->parseMetricValue($result); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Requires lib-generis-aws at least 0.10.0 |
| 111 | * @return AwsClient |
| 112 | */ |
| 113 | private function getAwsClient() |
| 114 | { |
| 115 | if (!$this->awsClient) { |
| 116 | $this->awsClient = $this->getServiceLocator()->get('generis/awsClient'); |
| 117 | } |
| 118 | return $this->awsClient; |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * @param Result $result |
| 123 | * @return mixed |
| 124 | */ |
| 125 | private function parseMetricValue(Result $result) |
| 126 | { |
| 127 | $metricValue = self::DEFAULT_METRIC_VALUE; |
| 128 | if (!$result->hasKey('events')) { |
| 129 | $this->logAlert("CloudWatch Logs doesn't have log events records for RDS Metrics."); |
| 130 | return $metricValue; |
| 131 | } |
| 132 | $logEvents = $result->get('events'); |
| 133 | |
| 134 | foreach ($logEvents as $logEvent) { |
| 135 | $logMessage = json_decode($logEvent['message'], true); |
| 136 | $currentValue = (float) $logMessage['cpuUtilization']['total']; |
| 137 | $metricValue = ($metricValue < $currentValue) ? $currentValue : $metricValue; |
| 138 | } |
| 139 | |
| 140 | return $metricValue; |
| 141 | } |
| 142 | } |