I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

Lesi sihloko sizoxoxa ngephrojekthi nginx-log-umqoqi, ezofunda izingodo ze-nginx, izithumele kuqoqo le-Clickhouse. Ngokuvamile i-ElasticSearch isetshenziselwa amalogi. I-Clickhouse idinga izinsiza ezimbalwa (isikhala sediski, i-RAM, i-CPU). I-Clickhouse ibhala idatha ngokushesha. I-Clickhouse icindezela idatha, okwenza idatha ekudiski ihlangane nakakhulu. Izinzuzo zeClickhouse zingabonakala kumaslayidi angu-2 embikweni I-VK ifaka kanjani idatha ku-ClickHouse emashumini ezinkulungwane zamaseva.

I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

Ukuze ubuke izibalo ngamalogi, masidale ideshibhodi ye-Grafana.

Ubani onendaba, wamukelekile ngaphansi kwekati.

Faka i-nginx, i-grafana ngendlela evamile.

Faka iqoqo le-clickhouse nge-ansible-playbook evela UDenis Proskurin.

Ukudala i-database namatafula ku-Clickhouse

Kulokho ifayela Imibuzo ye-SQL yokudala imininingwane namathebula e-nginx-log-collector ku-Clickhouse ichaziwe.

Senza isicelo ngasinye ngokuvula iseva ngayinye yeqoqo le-Clickhouse.

Inothi elibalulekile. Kulo mugqa, i-logs_cluster kufanele kufakwe igama le-cluster yakho kusuka kufayela le-clickhouse_remote_servers.xml phakathi kwe-"remote_servers" kanye "ne-shard".

ENGINE = Distributed('logs_cluster', 'nginx', 'access_log_shard', rand())

Ukufaka nokumisa i-nginx-log-collector-rpm

I-Nginx-log-collector ayinayo i-rpm. Lapha https://github.com/patsevanton/nginx-log-collector-rpm yenzela i-rpm. rpm izokwakhiwa kusetshenziswa Fedora Copr

Faka iphakheji ye-rpm nginx-log-collector-rpm

yum -y install yum-plugin-copr
yum copr enable antonpatsev/nginx-log-collector-rpm
yum -y install nginx-log-collector
systemctl start nginx-log-collector

Hlela ukuhlela /etc/nginx-log-collector/config.yaml:

  .......
  upload:
    table: nginx.access_log
    dsn: http://ip-адрес-кластера-clickhouse:8123/

- tag: "nginx_error:"
  format: error  # access | error
  buffer_size: 1048576
  upload:
    table: nginx.error_log
    dsn: http://ip-адрес-кластера-clickhouse:8123/

Isetha i-nginx

Ukulungiselelwa kwe-nginx okujwayelekile:

user  nginx;
worker_processes  auto;

#error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       /etc/nginx/mime.types;
    default_type  application/octet-stream;

    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    log_format avito_json escape=json
                     '{'
                     '"event_datetime": "$time_iso8601", '
                     '"server_name": "$server_name", '
                     '"remote_addr": "$remote_addr", '
                     '"remote_user": "$remote_user", '
                     '"http_x_real_ip": "$http_x_real_ip", '
                     '"status": "$status", '
                     '"scheme": "$scheme", '
                     '"request_method": "$request_method", '
                     '"request_uri": "$request_uri", '
                     '"server_protocol": "$server_protocol", '
                     '"body_bytes_sent": $body_bytes_sent, '
                     '"http_referer": "$http_referer", '
                     '"http_user_agent": "$http_user_agent", '
                     '"request_bytes": "$request_length", '
                     '"request_time": "$request_time", '
                     '"upstream_addr": "$upstream_addr", '
                     '"upstream_response_time": "$upstream_response_time", '
                     '"hostname": "$hostname", '
                     '"host": "$host"'
                     '}';

    access_log     syslog_server=unix:/var/run/nginx_log.sock,nohostname,tag=nginx avito_json; #ClickHouse
    error_log      syslog_server=unix:/var/run/nginx_log.sock,nohostname,tag=nginx_error; #ClickHouse

    #access_log  /var/log/nginx/access.log  main;

    proxy_ignore_client_abort on;
    sendfile        on;
    keepalive_timeout  65;
    include /etc/nginx/conf.d/*.conf;
}

I-Virtual host eyodwa:

vhost1.conf:

upstream backend {
    server ip-адрес-сервера-с-stub_http_server:8080;
    server ip-адрес-сервера-с-stub_http_server:8080;
    server ip-адрес-сервера-с-stub_http_server:8080;
    server ip-адрес-сервера-с-stub_http_server:8080;
    server ip-адрес-сервера-с-stub_http_server:8080;
}

server {
    listen   80;
    server_name vhost1;
    location / {
        proxy_pass http://backend;
    }
}

Engeza ababungazi ababonakalayo kufayela /etc/hosts:

ip-адрес-сервера-с-nginx vhost1

I-emulator yeseva ye-HTTP

Njenge-emulator yeseva ye-HTTP sizoyisebenzisa nodejs-stub-server kusukela UMaxim Ignatenko

I-nodejs-stub-server ayinayo i-rpm. Lapha https://github.com/patsevanton/nodejs-stub-server yenzela i-rpm. rpm izokwakhiwa kusetshenziswa Fedora Copr

Faka iphakheji ye-nodejs-stub-server ekukhuphukeni komfula nginx rpm

yum -y install yum-plugin-copr
yum copr enable antonpatsev/nodejs-stub-server
yum -y install stub_http_server
systemctl start stub_http_server

Ukuhlolwa Kwengcindezi

Ukuhlola kwenziwa kusetshenziswa ibhentshimakhi ye-Apache.

Yifake:

yum install -y httpd-tools

Siqala ukuhlola sisebenzisa ibhentshimakhi ye-Apache kusuka kumaseva ayi-5 ahlukene:

while true; do ab -H "User-Agent: 1server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done
while true; do ab -H "User-Agent: 2server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done
while true; do ab -H "User-Agent: 3server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done
while true; do ab -H "User-Agent: 4server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done
while true; do ab -H "User-Agent: 5server" -c 10 -n 10 -t 10 http://vhost1/; sleep 1; done

Isetha uGrafana

Ngeke uthole ideshibhodi kusizindalwazi esisemthethweni saseGrafana.

Ngakho-ke, sizokwenza ngesandla.

Ungathola ideshibhodi yami elondoloziwe lapha.

Udinga futhi ukudala i-variable yetafula nokuqukethwe nginx.access_log.
I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

Izicelo Eziphelele ze-Singlestat:

SELECT
 1 as t,
 count(*) as c
 FROM $table
 WHERE $timeFilter GROUP BY t

I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

Izicelo Ezihlulekile ze-Singlestat:

SELECT
 1 as t,
 count(*) as c
 FROM $table
 WHERE $timeFilter AND status NOT IN (200, 201, 401) GROUP BY t

I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

I-Singlestat Failing Percent:

SELECT
 1 as t, (sum(status = 500 or status = 499)/sum(status = 200 or status = 201 or status = 401))*100 FROM $table
 WHERE $timeFilter GROUP BY t

I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

Isikhathi sokuphendula esimaphakathi se-Singlestat:

SELECT
 1, avg(request_time) FROM $table
 WHERE $timeFilter GROUP BY 1

I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

Isikhathi Sempendulo Enkulu ye-Singlestat:

SELECT
 1 as t, max(request_time) as c
 FROM $table
 WHERE $timeFilter GROUP BY t

I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

Isimo sokubala:

$columns(status, count(*) as c) from $table

I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

Ukuze ukhiphe idatha njengophaya, udinga ukufaka i-plugin bese ulayisha kabusha i-grafana.

grafana-cli plugins install grafana-piechart-panel
service grafana-server restart

Isimo se-Pie TOP 5:

SELECT
    1, /* fake timestamp value */
    status,
    sum(status) AS Reqs
FROM $table
WHERE $timeFilter
GROUP BY status
ORDER BY Reqs desc
LIMIT 5

I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

Ngokuqhubekayo ngizonikeza izicelo ngaphandle kwezithombe-skrini:

Bala i-http_ejenti_yabasebenzisi:

$columns(http_user_agent, count(*) c) FROM $table

I-GoodRate/BadRate:

$rate(countIf(status = 200) AS good, countIf(status != 200) AS bad) FROM $table

isikhathi sokuphendula:

$rate(avg(request_time) as request_time) FROM $table

Isikhathi sokuphendula esikhuphukayo (isikhathi sokuphendula sokukhuphuka koku-1):

$rate(avg(arrayElement(upstream_response_time,1)) as upstream_response_time) FROM $table

Isimo Sokubala Kwethebula sawo wonke ama-vhost:

$columns(status, count(*) as c) from $table

Ukubuka okuvamile kwedeshibhodi

I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

Ukuqhathanisa isilinganiso () kanye ne-quantile()

isilinganiso()
I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse
iquantile()
I-Nginx-log-collector utility evela ku-Avito yokuthumela izingodo ze-nginx ku-Clickhouse

Isiphetho:

Ngethemba ukuthi umphakathi uzobandakanyeka ekuthuthukiseni/ukuhlola nokusebenzisa i-nginx-log-collector.
Futhi uma othile esebenzisa i-nginx-log-collector, uzokutshela ukuthi ulondoloze kangakanani idiski, i-RAM, i-CPU.

Iziteshi zocingo:

Ama-Milisecond:

Ubani onendaba nama-millisecond, bhala noma uvote, sicela, kulokhu Ukukhishwa.

Source: www.habr.com

Engeza amazwana