Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 11 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| FluentdUdpStorage | |
0.00% |
0 / 11 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 1 |
| log | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getSocket | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| sendData | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2017 (original work) Open Assessment Technologies SA; |
| 19 | * |
| 20 | * |
| 21 | */ |
| 22 | |
| 23 | namespace oat\taoEventLog\model\requestLog\fluentd; |
| 24 | |
| 25 | use oat\taoEventLog\model\requestLog\AbstractRequestLogStorage; |
| 26 | use oat\oatbox\user\User; |
| 27 | use Psr\Http\Message\RequestInterface; |
| 28 | |
| 29 | /** |
| 30 | * Class FluentdUdpStorage |
| 31 | * |
| 32 | * Configuration example (RequestLogStorage.conf.php): |
| 33 | * ```php |
| 34 | * use oat\taoEventLog\model\requestLog\fluentd\FluentdUdpStorage; |
| 35 | * use oat\taoEventLog\model\requestLog\RequestLogService; |
| 36 | * |
| 37 | * return new RequestLogService([ |
| 38 | * RequestLogService::OPTION_STORAGE => FluentdStorage::class, |
| 39 | * RequestLogService::OPTION_STORAGE_PARAMETERS => [ |
| 40 | * FluentdUdpStorage::OPTION_HOST => 'localhost', |
| 41 | * FluentdUdpStorage::OPTION_PORT => '8888', |
| 42 | * ] |
| 43 | * ]); |
| 44 | * |
| 45 | * ``` |
| 46 | * |
| 47 | * td agent config: |
| 48 | * ``` |
| 49 | * <source> |
| 50 | * @type udp #required |
| 51 | * tag tao.requestlog #required; tag of output (used to chose output). |
| 52 | * format json #required |
| 53 | * bind 0.0.0.0 #required; IP address to listen to |
| 54 | * port 8888 #required; Port to listen to |
| 55 | * </source> |
| 56 | * ``` |
| 57 | * |
| 58 | * @package oat\taoEventLog\model\storage |
| 59 | * @author Aleh Hutnikau <hutnikau@1pt.com> |
| 60 | */ |
| 61 | class FluentdUdpStorage extends AbstractRequestLogStorage |
| 62 | { |
| 63 | public const OPTION_HOST = 'host'; |
| 64 | public const OPTION_PORT = 'port'; |
| 65 | |
| 66 | private $resource; |
| 67 | private $host; |
| 68 | private $port; |
| 69 | |
| 70 | |
| 71 | /** |
| 72 | * @param RequestInterface $request |
| 73 | * @param User $user |
| 74 | * @return bool|void |
| 75 | */ |
| 76 | public function log(RequestInterface $request, User $user) |
| 77 | { |
| 78 | $this->sendData($this->prepareData($request, $user)); |
| 79 | } |
| 80 | |
| 81 | private function getSocket() |
| 82 | { |
| 83 | if ($this->resource === null) { |
| 84 | $this->resource = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); |
| 85 | $this->host = $this->getOption(self::OPTION_HOST); |
| 86 | $this->port = $this->getOption(self::OPTION_PORT); |
| 87 | socket_set_nonblock($this->resource); |
| 88 | } |
| 89 | return $this->resource; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * @param array $data |
| 94 | */ |
| 95 | private function sendData(array $data) |
| 96 | { |
| 97 | $message = json_encode($data); |
| 98 | try { |
| 99 | socket_sendto($this->getSocket(), $message, strlen($message), 0, $this->host, $this->port); |
| 100 | } catch (\Exception $e) { |
| 101 | \common_Logger::e('Error logging to Fluentd ' . $e->getMessage()); |
| 102 | } |
| 103 | } |
| 104 | } |