Share
Embed
PHPとFluentdで実現するリアルタイムログ分析
PHPerKaigi2024の登壇資料です。
https://fortee.jp/phperkaigi-2024/proposal/c27dee07-1f76-481f-89fa-3d7e7604bdfd
picopico
March 09, 2024
Transcript
98 slides
-
2 会社 ピクシブ株式会社 経歴 2023年4⽉ 新卒⼊社 職種 pixiv事業本部ウェブチーム 兼 インフラ部SREチーム
当⽥ 昇 @picopico_dev -
7 Databases Syslog Application logs Access logs Error logs Elastic
Search Big Query MySQL Amazon S3 -
(例)Nginxのアクセスログ 192.168.1.1 - - [08/Mar/2023:12:00:00 +0000] "GET /index.html HTTP/1.1" 200
612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" -
(例)Nginxのアクセスログ 192.168.1.1 - - [08/Mar/2023:12:00:00 +0000] "GET /index.html HTTP/1.1" 200
612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" →構造化されていない、独自のシンプルな形式が多い -
20 想定シナリオ: あるウェブアプリケーションが毎日平均して 100万回のアクセスを受けるとします。各アクセスは、ユー ザー認証、ページビュー、 APIリクエストなど複数のログエントリを生成すると仮定します。 ログ量の推計 1アクセスあたりのログエントリ数 : 5件(認証、ページビュー、
APIリクエストx3) 1日あたりの総ログエントリ数 : 100万アクセス × 5 = 500万件 1ログエントリの平均サイズ : 500バイト(日時、セッション ID、アクション詳細などを含む) これらの値を元に1日あたりのログデータの総量を計算すると、 -
21 想定シナリオ: あるウェブアプリケーションが毎日平均して 100万回のアクセスを受けるとします。各アクセスは、ユー ザー認証、ページビュー、 APIリクエストなど複数のログエントリを生成すると仮定します。 ログ量の推計 1アクセスあたりのログエントリ数 : 5件(認証、ページビュー、
APIリクエストx3) 1日あたりの総ログエントリ数 : 100万アクセス × 5 = 500万件 1ログエントリの平均サイズ : 500バイト(日時、セッション ID、アクション詳細などを含む) これらの値を元に1日あたりのログデータの総量を計算すると、 1日あたりのログデータ量: 500万件 × 500バイト = 2,500,000,000バイト(約2.33GB) -
[08-Mar-2023 12:00:00 UTC] PHP Warning: Division by zero in /path/to/your/script.php
on line 10 [08-Mar-2023 12:00:02 UTC] PHP Fatal error: Uncaught Error: Call to undefined function testFunction() in /path/to/your/script.php:15 PHPのエラーログ -
[08-Mar-2023 12:00:00 UTC] PHP Warning: Division by zero in /path/to/your/script.php
on line 10 [08-Mar-2023 12:00:02 UTC] PHP Fatal error: Uncaught Error: Call to undefined function testFunction() in /path/to/your/script.php:15 PHPのエラーログ タイムスタンプ -
[08-Mar-2023 12:00:00 UTC] PHP Warning: Division by zero in /path/to/your/script.php
on line 10 [08-Mar-2023 12:00:02 UTC] PHP Fatal error: Uncaught Error: Call to undefined function testFunction() in /path/to/your/script.php:15 PHPのエラーログ エラーの種類 -
[08-Mar-2023 12:00:00 UTC] PHP Warning: Division by zero in /path/to/your/script.php
on line 10 [08-Mar-2023 12:00:02 UTC] PHP Fatal error: Uncaught Error: Call to undefined function testFunction() in /path/to/your/script.php:15 PHPのエラーログ エラーの詳細、発生場所 -
Nginxのアクセスログ 192.168.1.1 - - [08/Mar/2023:12:00:00 +0000] "GET /index.html HTTP/1.1" 200
612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" -
Nginxのアクセスログ 192.168.1.1 - - [08/Mar/2023:12:00:00 +0000] "GET /index.html HTTP/1.1" 200
612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" IPアドレス -
Nginxのアクセスログ 192.168.1.1 - - [08/Mar/2023:12:00:00 +0000] "GET /index.html HTTP/1.1" 200
612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" リモートユーザー名・ユーザーID -
Nginxのアクセスログ 192.168.1.1 - - [08/Mar/2023:12:00:00 +0000] "GET /index.html HTTP/1.1" 200
612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" タイムスタンプ -
Nginxのアクセスログ 192.168.1.1 - - [08/Mar/2023:12:00:00 +0000] "GET /index.html HTTP/1.1" 200
612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" リクエスト -
Nginxのアクセスログ 192.168.1.1 - - [08/Mar/2023:12:00:00 +0000] "GET /index.html HTTP/1.1" 200
612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" HTTPステータスコード・バイト数 -
Nginxのアクセスログ 192.168.1.1 - - [08/Mar/2023:12:00:00 +0000] "GET /index.html HTTP/1.1" 200
612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" リファラ -
Nginxのアクセスログ 192.168.1.1 - - [08/Mar/2023:12:00:00 +0000] "GET /index.html HTTP/1.1" 200
612 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36" ユーザーエージェント -
MySQLのログ 2023-03-08T12:00:00.000000Z 0 [Note] InnoDB: Buffer pool(s) load completed at
230308 8:00:00 2023-03-08T12:00:00.000000Z 0 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode. 2023-03-08T12:00:00.000000Z 0 [ERROR] Cannot open table test/users from the internal data dictionary of InnoDB though the .frm file for the table exists. -
MySQLのログ 2023-03-08T12:00:00.000000Z 0 [Note] InnoDB: Buffer pool(s) load completed at
230308 8:00:00 2023-03-08T12:00:00.000000Z 0 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode. 2023-03-08T12:00:00.000000Z 0 [ERROR] Cannot open table test/users from the internal data dictionary of InnoDB though the .frm file for the table exists. タイムスタンプ -
MySQLのログ 2023-03-08T12:00:00.000000Z 0 [Note] InnoDB: Buffer pool(s) load completed at
230308 8:00:00 2023-03-08T12:00:00.000000Z 0 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode. 2023-03-08T12:00:00.000000Z 0 [ERROR] Cannot open table test/users from the internal data dictionary of InnoDB though the .frm file for the table exists. プロセスID -
MySQLのログ 2023-03-08T12:00:00.000000Z 0 [Note] InnoDB: Buffer pool(s) load completed at
230308 8:00:00 2023-03-08T12:00:00.000000Z 0 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode. 2023-03-08T12:00:00.000000Z 0 [ERROR] Cannot open table test/users from the internal data dictionary of InnoDB though the .frm file for the table exists. ログレベル -
MySQLのログ 2023-03-08T12:00:00.000000Z 0 [Note] InnoDB: Buffer pool(s) load completed at
230308 8:00:00 2023-03-08T12:00:00.000000Z 0 [Warning] 'user' entry 'root@localhost' ignored in --skip-name-resolve mode. 2023-03-08T12:00:00.000000Z 0 [ERROR] Cannot open table test/users from the internal data dictionary of InnoDB though the .frm file for the table exists. ログ内容 -
システムログ(syslog) Mar 8 12:00:00 myhost systemd[1]: Started Session 1234 of
user root. Mar 8 12:05:00 myhost sshd[23456]: Accepted publickey for user1 from 192.168.1.100 port 22 ssh2 Mar 8 12:10:00 myhost CRON[23457]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) Mar 8 12:15:00 myhost kernel: [123456.789012] Firewall: *TCP_IN Blocked* IN=eth0 OUT= MAC=01:23:45:67:89:ab:cd:ef:gh:ij:kl:mn SRC=10.1.2.3 DST=192.168.1.100 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=12345 DF PROTO=TCP SPT=12345 DPT=80 WINDOW=65535 RES=0x00 SYN URGP=0 -
システムログ(syslog) Mar 8 12:00:00 myhost systemd[1]: Started Session 1234 of
user root. Mar 8 12:05:00 myhost sshd[23456]: Accepted publickey for user1 from 192.168.1.100 port 22 ssh2 Mar 8 12:10:00 myhost CRON[23457]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) Mar 8 12:15:00 myhost kernel: [123456.789012] Firewall: *TCP_IN Blocked* IN=eth0 OUT= MAC=01:23:45:67:89:ab:cd:ef:gh:ij:kl:mn SRC=10.1.2.3 DST=192.168.1.100 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=12345 DF PROTO=TCP SPT=12345 DPT=80 WINDOW=65535 RES=0x00 SYN URGP=0 タイムスタンプ -
システムログ(syslog) Mar 8 12:00:00 myhost systemd[1]: Started Session 1234 of
user root. Mar 8 12:05:00 myhost sshd[23456]: Accepted publickey for user1 from 192.168.1.100 port 22 ssh2 Mar 8 12:10:00 myhost CRON[23457]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) Mar 8 12:15:00 myhost kernel: [123456.789012] Firewall: *TCP_IN Blocked* IN=eth0 OUT= MAC=01:23:45:67:89:ab:cd:ef:gh:ij:kl:mn SRC=10.1.2.3 DST=192.168.1.100 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=12345 DF PROTO=TCP SPT=12345 DPT=80 WINDOW=65535 RES=0x00 SYN URGP=0 ホスト名 -
システムログ(syslog) Mar 8 12:00:00 myhost systemd[1]: Started Session 1234 of
user root. Mar 8 12:05:00 myhost sshd[23456]: Accepted publickey for user1 from 192.168.1.100 port 22 ssh2 Mar 8 12:10:00 myhost CRON[23457]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) Mar 8 12:15:00 myhost kernel: [123456.789012] Firewall: *TCP_IN Blocked* IN=eth0 OUT= MAC=01:23:45:67:89:ab:cd:ef:gh:ij:kl:mn SRC=10.1.2.3 DST=192.168.1.100 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=12345 DF PROTO=TCP SPT=12345 DPT=80 WINDOW=65535 RES=0x00 SYN URGP=0 プロセス名とプロセスID -
システムログ(syslog) Mar 8 12:00:00 myhost systemd[1]: Started Session 1234 of
user root. Mar 8 12:05:00 myhost sshd[23456]: Accepted publickey for user1 from 192.168.1.100 port 22 ssh2 Mar 8 12:10:00 myhost CRON[23457]: (root) CMD (command -v debian-sa1 > /dev/null && debian-sa1 1 1) Mar 8 12:15:00 myhost kernel: [123456.789012] Firewall: *TCP_IN Blocked* IN=eth0 OUT= MAC=01:23:45:67:89:ab:cd:ef:gh:ij:kl:mn SRC=10.1.2.3 DST=192.168.1.100 LEN=60 TOS=0x00 PREC=0x00 TTL=64 ID=12345 DF PROTO=TCP SPT=12345 DPT=80 WINDOW=65535 RES=0x00 SYN URGP=0 ログ内容 -
バッチ処理の例 total 40 -rw-r--r-- 1 root root 1024 Mar 5
00:00 app.log-20230304 -rw-r--r-- 1 root root 20480 Mar 6 00:00 app.log-20230305 -rw-r--r-- 1 root root 10240 Mar 7 00:00 app.log-20230306 -rw-r--r-- 1 root root 5120 Mar 8 00:00 app.log-20230307 -rw-r--r-- 1 root root 123 Mar 8 12:00 app.log -
バッチ処理の例 total 40 -rw-r--r-- 1 root root 1024 Mar 5
00:00 app.log-20230304 -rw-r--r-- 1 root root 20480 Mar 6 00:00 app.log-20230305 -rw-r--r-- 1 root root 10240 Mar 7 00:00 app.log-20230306 -rw-r--r-- 1 root root 5120 Mar 8 00:00 app.log-20230307 -rw-r--r-- 1 root root 123 Mar 8 12:00 app.log 1日ごとに処理する -
バッチ処理の例 total 40 -rw-r--r-- 1 root root 1024 Mar 5
00:00 app.log-20230304 -rw-r--r-- 1 root root 20480 Mar 6 00:00 app.log-20230305 -rw-r--r-- 1 root root 10240 Mar 7 00:00 app.log-20230306 -rw-r--r-- 1 root root 5120 Mar 8 00:00 app.log-20230307 -rw-r--r-- 1 root root 123 Mar 8 12:00 app.log 1日ごとに処理する -
バッチ処理の例 total 40 -rw-r--r-- 1 root root 1024 Mar 5
00:00 app.log-20230304 -rw-r--r-- 1 root root 20480 Mar 6 00:00 app.log-20230305 -rw-r--r-- 1 root root 10240 Mar 7 00:00 app.log-20230306 -rw-r--r-- 1 root root 5120 Mar 8 00:00 app.log-20230307 -rw-r--r-- 1 root root 123 Mar 8 12:00 app.log 1日ごとに処理する -
バッチ処理の例 total 40 -rw-r--r-- 1 root root 1024 Mar 5
00:00 app.log-20230304 -rw-r--r-- 1 root root 20480 Mar 6 00:00 app.log-20230305 -rw-r--r-- 1 root root 10240 Mar 7 00:00 app.log-20230306 -rw-r--r-- 1 root root 5120 Mar 8 00:00 app.log-20230307 -rw-r--r-- 1 root root 123 Mar 8 12:00 app.log 1日ごとに処理する -
ストリーム処理の例 (app.log) [2023-03-08 12:00:01 UTC] PHP Warning: include(/path/to/file/mi [2023-03-08 12:01:02
UTC] PHP Notice: Undefined variable: usern [2023-03-08 12:02:03 UTC] PHP Fatal error: Uncaught Error: Call Stack trace: #0 {main} thrown in /path/to/your/script.php on line 70 -
ストリーム処理の例 (app.log) [2023-03-08 12:00:01 UTC] PHP Warning: include(/path/to/file/mi [2023-03-08 12:01:02
UTC] PHP Notice: Undefined variable: usern -
ストリーム処理の例 (app.log) [2023-03-08 12:00:01 UTC] PHP Warning: include(/path/to/file/mi [2023-03-08 12:01:02
UTC] PHP Notice: Undefined variable: usern [2023-03-08 12:02:03 UTC] PHP Fatal error: Uncaught Error: Call Stack trace: #0 {main} thrown in /path/to/your/script.php on line 70 -
Fluentdの特徴 60 • Unified Logging with JSON • Pluggable Architecture
• Minimum Resources Required • Built-in Reliability -
Fluentdの特徴 61 • Unified Logging with JSON • Pluggable Architecture
• Minimum Resources Required • Built-in Reliability -
62 イベント [08-Mar-2023 12:00:00 UTC] PHP Warning: Division by zero
in /path/to/your/script.php on line 10 [“app.log”, 2023-03-08T12:00:00Z, {"level": "WARNING","message": "Division by zero", …}] Input -
63 イベント [08-Mar-2023 12:00:00 UTC] PHP Warning: Division by zero
in /path/to/your/script.php on line 10 [“app.log”, 2023-03-08T12:00:00Z, {"level": "WARNING","message": "Division by zero", …}] Inputプラグインがログからイベ ントを生成する Input -
Fluentdの特徴 64 • Unified Logging with JSON • Pluggable Architecture
• Minimum Resources Required • Built-in Reliability -
Fluentdの特徴 67 • Unified Logging with JSON • Pluggable Architecture
• Minimum Resources Required • Built-in Reliability -
Fluentdの特徴 69 • Unified Logging with JSON • Pluggable Architecture
• Minimum Resources Required • Built-in Reliability -
fluent.conf <source> @type forward </source> <filter app.**> @type grep <regexp>
key level pattern /^ERROR$/ </regexp> </filter> <match app.**> @type file path /var/log/fluentd/error_logs </match> -
fluent.conf <source> @type forward </source> <filter app.**> @type grep <regexp>
key level pattern /^ERROR$/ </regexp> </filter> <match app.**> @type file path /var/log/fluentd/error_logs </match> -
fluent.conf <source> @type forward </source> <filter app.**> @type grep <regexp>
key level pattern /^ERROR$/ </regexp> </filter> <match app.**> @type file path /var/log/fluentd/error_logs </match> -
fluent.conf <source> @type forward </source> <filter app.**> @type grep <regexp>
key level pattern /^ERROR$/ </regexp> </filter> <match app.**> @type file path /var/log/fluentd/error_logs </match> -
ディレクティブ 81 • <source>: 入力 • <match>: 出力 • <parse>:
ログの構造化 • <filter>: ログの選択・加工 • <buffer>: バッファリング • … -
Fluentdの設定(AP側) <source> @type tail path /var/log/app.log pos_file /var/log/app.log.pos tag php.error
<parse> @type regexp expression /^(?<time>[^ ]* [^ ]*) (?<type>[^:]*): (?<message time_format %d-%b-%Y %H:%M:%S types time:time,type:string,message:string,file:string,line: </parse> </source> -
Fluentdの設定(Aggregator側) <match php.error> @type mysql_bulk host mysql database db username
root password root column_names time,type,message,file,line table php_errors <buffer> flush_interval 10s </buffer> </match>