首页 > 系统相关 >Nginx loki监控日志的学习

Nginx loki监控日志的学习

时间:2023-11-22 23:13:07浏览次数:35  
标签:http upstream request Nginx loki time protocol 日志

Nginx loki监控日志的学习


背景

学习自: 
https://mp.weixin.qq.com/s/Qt1r7vzWvCcJpNDilWHuxQ
增加了一些自己的理解

第一部分nginx日志的完善

在logformat的后面增加一个:
log_format json_analytics escape=json '{'
                            '"msec": "$msec", ' # request unixtime in seconds with a milliseconds resolution
                            '"connection": "$connection", ' # connection serial number
                            '"connection_requests": "$connection_requests", ' # number of requests made in connection
                    '"pid": "$pid", ' # process pid
                    '"request_id": "$request_id", ' # the unique request id
                    '"request_length": "$request_length", ' # request length (including headers and body)
                    '"remote_addr": "$remote_addr", ' # client IP
                    '"remote_user": "$remote_user", ' # client HTTP username
                    '"remote_port": "$remote_port", ' # client port
                    '"time_local": "$time_local", '
                    '"time_iso8601": "$time_iso8601", ' # local time in the ISO 8601 standard format
                    '"request": "$request", ' # full path no arguments if the request
                    '"request_uri": "$request_uri", ' # full path and arguments if the request
                    '"args": "$args", ' # args
                    '"status": "$status", ' # response status code
                    '"body_bytes_sent": "$body_bytes_sent", ' # the number of body bytes exclude headers sent to a client
                    '"bytes_sent": "$bytes_sent", ' # the number of bytes sent to a client
                    '"http_referer": "$http_referer", ' # HTTP referer
                    '"http_user_agent": "$http_user_agent", ' # user agent
                    '"http_x_forwarded_for": "$http_x_forwarded_for", ' # http_x_forwarded_for
                    '"http_host": "$http_host", ' # the request Host: header
                    '"server_name": "$server_name", ' # the name of the vhost serving the request
                    '"request_time": "$request_time", ' # request processing time in seconds with msec resolution
                    '"upstream": "$upstream_addr", ' # upstream backend server for proxied requests
                    '"upstream_connect_time": "$upstream_connect_time", ' # upstream handshake time incl. TLS
                    '"upstream_header_time": "$upstream_header_time", ' # time spent receiving upstream headers
                    '"upstream_response_time": "$upstream_response_time", ' # time spend receiving upstream body
                    '"upstream_response_length": "$upstream_response_length", ' # upstream response length
                    '"upstream_cache_status": "$upstream_cache_status", ' # cache HIT/MISS where applicable
                    '"ssl_protocol": "$ssl_protocol", ' # TLS protocol
                    '"ssl_cipher": "$ssl_cipher", ' # TLS cipher
                    '"scheme": "$scheme", ' # http or https
                    '"request_method": "$request_method", ' # request method
                    '"server_protocol": "$server_protocol", ' # request protocol, like HTTP/1.1 or HTTP/2.0
                    '"pipe": "$pipe", ' # "p" if request was pipelined, "." otherwise
                    '"gzip_ratio": "$gzip_ratio", '
                    '"http_cf_ray": "$http_cf_ray"'
                    '}';
然后日志修改为:
access_log /data/nginx/gscloudsu.log json_analytics ;
注意文件日志等属性. 
重启ningx:
lsof -i:5000 |awk '{print $2}' |xargs kill -9
cd /data/nginx
./nginx -c 5000.conf

下载loki等组件

https://github.com/grafana/loki/releases/tag/v2.9.2

最新版本是 2.9.2 我这边下载了四个文件备用
promtail-linux-arm64.zip
promtail-linux-amd64.zip
loki-linux-arm64.zip
loki-linux-amd64.zip

将 x86的截止放到 /usr/bin 目录下面
scp loki-linux-amd64 /usr/bin/loki
scp promtail-linux-amd64 /usr/bin/promtail

解压缩开启服务等

下载loki的配置文件:
地址为: 
https://raw.githubusercontent.com/grafana/loki/main/examples/getting-started/loki-config.yaml
详细内容为: 
mkdir /etc/loki 
cat >/etc/loki/loki.yaml <<EOF
---
server:
  http_listen_port: 3100
#memberlist:
#  join_members:
#    - loki:7946
schema_config:
  configs:
    - from: 2021-08-01
      store: tsdb
      object_store: s3
      schema: v12
      index:
        prefix: index_
        period: 24h
common:
  path_prefix: /loki
  replication_factor: 1
  storage:
    s3:
      endpoint: minio:9000
      insecure: true
      bucketnames: loki-data
      access_key_id: loki
      secret_access_key: supersecret
      s3forcepathstyle: true
  ring:
    kvstore:
      store: memberlist
ruler:
  storage:
    s3:
      bucketnames: loki-ruler
EOF

启动服务:
nohup /usr/bin/loki  -config.file=/etc/loki/loki.yaml -auth.enabled=false &

配置promtail

cat >/etc/loki/promtail.yaml <<EOF
server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://127.0.0.1:3100/loki/api/v1/push

scrape_configs:
- job_name: nginx
  static_configs:
  - targets:
      - localhost
    labels:
      job: nginx_logs
      __path__: /data/nginx/gscloudsu.log
EOF

# 启动服务
nohup /usr/bin/promtail  -config.file=/etc/loki/promtail.yaml & 

数据源以及grafana的dashboard

增加 loki的数据源
增加 12559 的 2022年版本的json导入文件
注意不能选择新版本的. 新版本换了数据源. 

todo:
1. loki 设置密码
2. nginx 的geoip的设置
3. 学习另外一个springboot的监控指标
https://grafana.com/grafana/dashboards/17175-spring-boot-observability/
https://github.com/blueswen/spring-boot-observability

效果

image

标签:http,upstream,request,Nginx,loki,time,protocol,日志
From: https://www.cnblogs.com/jinanxiaolaohu/p/17850561.html

相关文章

  • 对linux下日志文件error监控
    对日志文件中的error进行监控,当日志文件中出现error关键字时,就截取日志(grep-ierror不区分大小写进行搜索"error"关键字,但是会将包含error大小写字符的单词搜索出来),大家可以去看这编文章   1)第一类日志在每天的日志目录下生产的error日志,此日志文件每天都会自动生成,里面有......
  • 通用 log4j2 日志模板
    通用log4j2日志模板 <?xmlversion="1.0"encoding="UTF-8"?><!--配置项集属性status指明全局的最低日志级别。属性monitorInterval指明监控日志变化的时间间隔。这里指明了日志级别为debug,监控日志文件变化的周期是30毫秒格式:%m输出的......
  • 通用的 logback.xml 日志模版
    通用的logback.xml日志模版 <?xmlversion="1.0"encoding="UTF-8"?><configuration> <!--定义日志输出目录--> <substitutionPropertyname="log.base"value="/log/joyupx/trade/"/> <!--<jmxConfig......
  • nginx高可用
    高可用方案简单说就是集群,什么是集群呢,加单说就是一个不够来俩个,俩不够来三。所以高可用就是在之前传统方式的基础上,万一nginx挂了,所有服务都将不可用。所以可以使用以下结构,进行设计让两台需要集群的nginx进行绑定一个虚拟ip。准备:需要两台虚拟机、两台都安装nginx、安装ke......
  • redis中查看慢日志以及返回参数值的解释
    1、问题描述 业务反馈,出现很多连接redis的readtimedout的报错  2、问题分析及解决 由于redis是单线程处理的,所有redis接收到命令,都会进入到队列中,等待执行。 当客户端,由于等待时间过长,没有接收到server端返回的数据,就是出现超时的报错。 程序里,jedis客户端,默......
  • nginx出现很多进程,怎么杀掉进程。
    linux关闭nginx的方法:1、利用“ps-ef|grepnginx”命令查看nginx进程后,利用“kill-QUIT进程号”关闭nginx即可;2、利用“kill-TERM 999”快速杀死进程即可;3、利用“pkill-9nginx”强行停止进程即可。4、利用“kill-9 999”杀死进程5、查找进程ps-ef|grepnginx|g......
  • python日志Loguru库
    环境python3.7介绍什么是Loguru?Loguru是一个Python日志记录库,以其易用性和灵活性而闻名。与Python内置的logging模块相比,Loguru提供了更简洁的API和更多的功能,让日志记录变得平易近人GitHub地址:https://github.com/Delgan/loguru安装pipinstallloguru基本使用fromlog......
  • Activiti + spring +事务+回滚 申批流过程中抛异常导至日志表回滚
    在您的服务任务A、B和您的流程之间放置一个层,让我们将其命名为TransactionService。此服务应触发您的服务方法。此外,这些服务方法应该为它们的操作使用新的事务(您可以使用@Transactional注释)。在TransactionService中,不要做任何事情,只需触发它们并将它们封装在trycatch块中。......
  • nginx安装
    1.安装、cd/usr/nginxwgethttp://nginx.org/download/nginx-1.9.2.tar.gztar-xzvfnginx-1.9.2.tar.gzcd/usr/nginx/nginx-1.9.2./configure--prefix=/usr/local/nginx-1.9.2--with-http_ssl_module--with-http_stub_status_modulemake&&makeinstall2......
  • nginx遇到的坑
    1)自定义请求头获取不到请求头request_token中包含'_'字符,会nginx过滤掉。默认情况下,underscores_in_headers为off,表示当客户端请求头中带有下划线的字段默认将会被标识为无效字段。解决方案添加如下配置:http{underscores_in_headerson;}2)nginx没有透传host请求头,tomcat......