Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| FluentdTcpStorage | |
0.00% |
0 / 20 |
|
0.00% |
0 / 2 |
72 | |
0.00% |
0 / 1 |
| getSocket | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
20 | |||
| log | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
| 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 | * A fluentd tcp forwarding implementation |
| 31 | * |
| 32 | * @package oat\taoEventLog\model\storage |
| 33 | * @author Aleh Hutnikau <hutnikau@1pt.com> |
| 34 | */ |
| 35 | class FluentdTcpStorage extends AbstractRequestLogStorage |
| 36 | { |
| 37 | public const OPTION_HOST = 'host'; |
| 38 | public const OPTION_PORT = 'port'; |
| 39 | public const OPTION_DELIMITER = 'delimiter'; |
| 40 | |
| 41 | /** |
| 42 | * @var resource |
| 43 | */ |
| 44 | private $resource; |
| 45 | |
| 46 | /** |
| 47 | * Connect to the server and return the open socket |
| 48 | * |
| 49 | * @return resource |
| 50 | * @throws RequestLogException |
| 51 | */ |
| 52 | public function getSocket() |
| 53 | { |
| 54 | if (is_null($this->resource)) { |
| 55 | $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
| 56 | if ($socket === false) { |
| 57 | throw new RequestLogException('Unable to open TCP socket: ' . socket_strerror(socket_last_error())); |
| 58 | } |
| 59 | |
| 60 | $success = socket_connect( |
| 61 | $socket, |
| 62 | $this->getOption(self::OPTION_HOST), |
| 63 | $this->getOption(self::OPTION_PORT) |
| 64 | ); |
| 65 | if ($success === false) { |
| 66 | throw new RequestLogException('Unable to connect to host: ' . socket_strerror(socket_last_error())); |
| 67 | } |
| 68 | $this->resource = $socket; |
| 69 | } |
| 70 | return $this->resource; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * (non-PHPdoc) |
| 75 | * @see \oat\taoEventLog\model\requestLog\RequestLogStorageWritable::log() |
| 76 | */ |
| 77 | public function log(RequestInterface $request, User $user) |
| 78 | { |
| 79 | $message = json_encode($this->prepareData($request, $user)); |
| 80 | $message .= $this->hasOption(self::OPTION_DELIMITER) ? $this->getOption(self::OPTION_DELIMITER) : ''; |
| 81 | while (!empty($message)) { |
| 82 | $send = socket_write($this->getSocket(), $message, strlen($message)); |
| 83 | if ($send != strlen($message)) { |
| 84 | throw new RequestLogException('Unable to send msg: ' . socket_strerror(socket_last_error())); |
| 85 | } |
| 86 | $message = substr($message, $send); |
| 87 | } |
| 88 | } |
| 89 | } |