Skip to content

Commit

Permalink
update feedback #238 (comment)
Browse files Browse the repository at this point in the history
  • Loading branch information
nhatdo-nfq committed Mar 16, 2022
1 parent 2b1a31c commit 5f6f438
Show file tree
Hide file tree
Showing 23 changed files with 306 additions and 99 deletions.
6 changes: 4 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,5 +31,7 @@ yarn-error.log
# cypress test
/cypress/screenshots/
/cypress/videos/
/swoole/.env
/swoole/vendor/
/log-receiver/.env
/log-receiver/vendor
/log-receiver/config/*
!/log-receiver/config/syslog.yml
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"type": "project",
"license": "proprietary",
"require": {
"php": ">=7.2.5",
"php": ">=8.1",
"ext-ctype": "*",
"ext-iconv": "*",
"ext-json": "*",
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ services:
- "127.0.0.1:9506:9506/udp"
- "9507:9507"
volumes:
- ./swoole:/var/www/dev-php:cached
- ./log-receiver:/var/www/dev-php:cached
command: "/usr/bin/bash -c 'composer install && php server.php'"
links:
- db
3 changes: 3 additions & 0 deletions swoole/.env.dist → log-receiver/.env.dist
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#LOG LEVEL: debug, error //default: error
LOG_LEVEL=debug

SERVER_HOST=0.0.0.0
UDP_SERVER_PORT=9506
TCP_SERVER_PORT=9507
Expand Down
18 changes: 11 additions & 7 deletions swoole/README.md → log-receiver/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,42 @@
### 1.1 Server

```dotenv
#Log level: debug, error //default: error
LOG_LEVEL=error
#Server
SERVER_HOST=0.0.0.0
UDP_SERVER_PORT=9506
TCP_SERVER_PORT=9507
#Database
CLICKHOUSE_DB_HOST=
CLICKHOUSE_DB_PORT=
CLICKHOUSE_DB_USERNAME=
CLICKHOUSE_DB_PASSWORD=
CLICKHOUSE_DB_DBNAME=
CLICKHOUSE_DB_POOL_SIZE=50
CLICKHOUSE_DB_TIMEOUT=5 #in seconds
CLICKHOUSE_DB_CONNECTION_TIMEOUT=5 #in seconds
```

### 1.2 Syslog pattern

```yaml
// config/syslog.yml
# config/syslog.yml
pattern: '<%{POSINT:pri}>%{POSINT:version} %{TIMESTAMP_ISO8601:timestamp} %{HOSTNAME:hostname} %{USERNAME:table_name} %{USERNAME:proc_id} %{USERNAME:app_name} \- %{GREEDYDATA:message}'
```
### 1.3 Table
```yaml
// config/{table_name}.yml
// Ex: config/nginx_access.yml
# config/{table_name}.yaml
# Ex: config/nginx_access.yaml
type: grok
pattern: '%{TIMESTAMP_ISO8601:date} %{TIMESTAMP_ISO8601:timestamp} %{IP} %{GREEDYDATA:text}'
// or
# or
type: json
```
Expand All @@ -54,11 +58,11 @@ php server.php
### 1. Send udp message with json format
```shell
docker run --log-driver syslog --log-opt syslog-address=udp://{udp_server_address}:9506 --log-opt tag="{table_name}" --log-opt syslog-format=rfc5424 alpine echo '{"text":"testing json format","date":"2022-02-18 09:47:14","timestamp":"2022-02-18 09:47:00"}'
docker-compose exec php logger --udp --server {udp_server_address} --port {udp_server_ports} --tag udp_logs '{"text":"testing json format","date":"2022-02-18 09:47:14","timestamp":"2022-02-18 09:47:00"}'
```
### 2. Send udp message with grok format
```shell
docker run --log-driver syslog --log-opt syslog-address=udp://{udp_server_address}:9506 --log-opt tag="{table_name}" --log-opt syslog-format=rfc5424 alpine echo '2022-02-18 17:11:14 2022-02-18 17:11:14 10.0.0.219 testing grok message'
docker-compose exec php logger --udp --server {udp_server_address} --port {udp_server_ports} --tag udp_logs '2022-02-18 17:11:14 2022-02-18 17:11:14 10.0.0.219 testing grok message'
```
3 changes: 2 additions & 1 deletion swoole/composer.json → log-receiver/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"smi2/phpclickhouse": "^1.4",
"vlucas/phpdotenv": "^5.4",
"symfony/yaml": "^6.0",
"symfony/uid": "^6.0"
"symfony/uid": "^6.0",
"php": ">=8.0"
},
"autoload": {
"psr-4": {
Expand Down
38 changes: 20 additions & 18 deletions swoole/composer.lock → log-receiver/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1 +1 @@
pattern: '<%{POSINT:pri}>%{POSINT:version} %{TIMESTAMP_ISO8601:timestamp} %{HOSTNAME:hostname} %{USERNAME:table_name} %{USERNAME:proc_id} %{USERNAME:app_name} \- %{GREEDYDATA:message}'
pattern: '<%{POSINT:pri}>%{POSINT:version} %{TIMESTAMP_ISO8601:timestamp} %{HOSTNAME:hostname} %{USERNAME:table_name} %{USERNAME:proc_id} %{USERNAME:app_name} (\[%{DATA:structured_data}\]|\-) %{GREEDYDATA:message}'
53 changes: 48 additions & 5 deletions swoole/server.php → log-receiver/server.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

require_once dirname(__FILE__) . "/vendor/autoload.php";

use App\Enum\LogLevel;
use App\Logger\Logger;
use App\Message\Process;
use Swoole\Server;

Expand All @@ -18,38 +20,79 @@
throw new Exception('Please provide at least TCP or UDP port');
}

/**
* Config log level
*/
$logLevel = LogLevel::tryFrom($_ENV['LOG_LEVEL'] ?? 'error');

if ($logLevel === null) {
$logLevel = LogLevel::from('error');
}

$_ENV['LOG_LEVEL'] = $logLevel->value();

/**
* Create new server instance
*/
$server = new Server($host, null, SWOOLE_BASE);

/**
* Server configuration
*/
$server->set([
'log_level' => $_ENV['LOG_LEVEL']
]);

/**
* TCP request handler
*/
$server->on(
'receive',
function (Server $server, int $fd, int $reactorId, string $data) {
Logger::debug("[{$fd}] Received message {$data}");
try {
Process::process($data);
} catch (\Exception $e) {
echo $e->getMessage() . "\r\n" . $e->getTraceAsString();
Logger::error($e->getMessage() . "\r\n" . $e->getTraceAsString());
}

$server->send($fd, "OK");
}
);

/**
* Listening TCP request
*/
if ($tcpPort) {
$server->listen($host, $tcpPort, SWOOLE_SOCK_TCP);

Logger::info("TCP server listening on: \nHost {$host}\nPort {$tcpPort}");
}

/**
* UDP request handler
*/
if ($udpPort) {
$server->listen($host, $udpPort, SWOOLE_SOCK_UDP);
$server->on(
'packet',
function (Server $server, string $message, array $clientInfo) {
Logger::debug("Received message {$message} from " . $clientInfo['address']);

try {
Process::process($message);
} catch (\Exception $e) {
echo $e->getMessage() . "\r\n" . $e->getTraceAsString();
Logger::error($e->getMessage() . "\r\n" . $e->getTraceAsString());
}

$server->sendto($clientInfo['address'], $clientInfo['port'], "OK");
}
);
}

if ($tcpPort) {
$server->listen($host, $tcpPort, SWOOLE_SOCK_TCP);
Logger::info("UDP server listening on: \nHost {$host}\nPort {$udpPort}");
}

/**
* Starting server
*/
$server->start();
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions log-receiver/src/Config/Config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace App\Config;

use App\Exceptions\ConfigFileInvalid;
use App\Exceptions\ConfigFileNotFound;
use App\Logger\Logger;
use Symfony\Component\Yaml\Yaml;

class Config
{
public static function read($filename, $key, $default = '')
{
$path = $_ENV['ROOT_DIR'] . "/config/{$filename}.y{ml,aml}";
$files = glob($path, GLOB_BRACE);

if (empty($files)) {
throw new ConfigFileNotFound("Config file not found. Please create a config file \"{$filename}.yaml\" in \/config directory");
}

$configFile = array_shift($files);
$config = Yaml::parseFile($configFile);

if (empty($config)) {
throw new ConfigFileInvalid("The file {$configFile} could not be read or the YAML is not valid");
}

if (!array_key_exists($key, $config)) {
Logger::debug("Key \"{$key}\" does not exist in config file, return default value \"{$default}\"");

return $default;
}

return $config[$key];
}
}
17 changes: 17 additions & 0 deletions log-receiver/src/Enum/LogLevel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\Enum;

enum LogLevel: string
{
case DEBUG = 'debug';
case ERROR = 'error';

public function value(): int
{
return match ($this) {
LogLevel::DEBUG => SWOOLE_LOG_DEBUG,
LogLevel::ERROR => SWOOLE_LOG_ERROR,
};
}
}
8 changes: 8 additions & 0 deletions log-receiver/src/Exceptions/ConfigFileInvalid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace App\Exceptions;

class ConfigFileInvalid extends \Exception
{
protected $message = 'The file could not be read or the YAML is not valid';
}
Loading

0 comments on commit 5f6f438

Please sign in to comment.