背景

我们公司项目组用的是elastic的一整套技术栈,es,kibana,filebeat和apm,目前已经可以采集网关+各个微服务的日志。

架构图

现在需要在原来的基础上把nginx这的日志也采集上来,方便做链路跟踪

问题与思路

原先traceId是在网关这层产生并且传递下去,因为nginx并不是java实现,没办法用agent方式处理,那么如何在nginx这层产生traceId 并且传递下去?

想到2种解决办法

nginx在1.11版本之后可以在http的header头设置request。

参考nginx 文档 http://nginx.org/en/docs/http/ngx_http_core_module.html#var_request_id

那么解决起来就简单了,在nginx开始的时候设置requestId,注意header里面的key必须是服务elastic的APM规范,让他可以识别出来是trace id。(这边只是给下思路,并没有实际去操作)

在响应的response设置traceid

因为nginx是在请求结束才打印日志,那么可以在gateway写一个filter,在response设置traceId,然后去nginx就可以获取的到traceid

思路二实战

设置nginx的日志格式

vi /{nginx_home}/conf/nginx.conf

log_format  main  '$remote_addr - $remote_user [$time_local] "$request" ''$status $request_time $request_length $bytes_sent $body_bytes_sent ''$http_host $sent_http_traceid ''"$upstream_addr" "$upstream_status" "$upstream_response_time" ''"$http_referer" "$http_user_agent" "$http_x_forwarded_for"';access_log  logs/access.log  main;

设置nginx输出的日志格式 主要关注 $sent_http_traceid 用来获取header里的traceid

修改完文件之后 记得重新加载 ./nginx -s reload

设置filebeat采集nginx日志

filebeat.inputs:
- type: log #多类日志需定义多个typeenabled: truepaths:- D:\nginx-1.23.2\logs\access.*exclude_files: ['\.gz$']encoding: utf-8fields:service_name: test1service_environment: testtype: nginx.access####### nginx error 日志文件 ########
- type: logenabled: truepaths:- D:\nginx-1.23.2\logs\error.*exclude_files: ['\.gz$']encoding: utf-8fields:service_name: test1service_environment: testtype: nginx.error

这边只展示了nginx的日志采集的input,主要是注意修改nginx的日志目录

在网关项目添加代码

添加maven依赖

 <dependency><groupId>co.elastic.apm</groupId><artifactId>apm-agent-api</artifactId><version>1.30.1</version>
</dependency>

增加filter,在请求头添加traceId

import cn.hutool.core.collection.ListUtil;
import co.elastic.apm.api.ElasticApm;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
import org.springframework.web.server.WebFilterChain;
import reactor.core.publisher.Mono;
/*** 设置trace id*/
@Component
@Order(3)
@Slf4j
public class ApmTraceIdFilter implements WebFilter {@Overridepublic Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {String apmTraceId = ElasticApm.currentTransaction().getTraceId();;exchange.getResponse().getHeaders().put("traceid", ListUtil.toList(apmTraceId));log.info("ApmTraceIdFilter设置trace.id:{}",apmTraceId);return chain.filter(exchange);}}

如果WebFilter获取不到trace.id,那么可以试试用spring cloud gateway的 filter
key必须是traceid,和前面nginx的sent_http_traceid有关联

以上都做完各自重启查看效果

最终效果

先查看nginx的日志

很好,已经获取到traceid

然后去kibana看整体的效果

搞定,下班

filebeat采集nginx日志相关推荐

  1. 通过filebeat、logstash、rsyslog采集nginx日志的几种方式

    由于nginx功能强大,性能突出,越来越多的web应用采用nginx作为http和反向代理的web服务器.而nginx的访问日志不管是做用户行为分析还是安全分析都是非常重要的数据源之一.如何有效便捷的 ...

  2. 部署filebeat收集nginx日志

    搭建filebeat自动发现日志 在上一篇博客我们部署了logstash去读取日志,但是logstash需要消耗的资源较大.在每台客户端安装logstash不现实. Filebeat是一个轻量级的日志 ...

  3. 2021年大数据ELK(十九):使用FileBeat采集Kafka日志到Elasticsearch

    全网最详细的大数据ELK文章系列,强烈建议收藏加关注! 新文章都已经列出历史文章目录,帮助大家回顾前面的知识重点. 目录 使用FileBeat采集Kafka日志到Elasticsearch 一.需求分 ...

  4. 采集Nginx日志的几种方式

    点击上方蓝色"程序猿DD",选择"设为星标" 回复"资源"获取独家整理的学习资料! 来源 | https://dwz.cn/ofiCxRK0 ...

  5. 闭眼入!采集 Nginx 日志的几种方式!

    作者:xiejava 来源:cnblogs.com/xiejava/p/12452434.html 由于nginx功能强大,性能突出,越来越多的web应用采用nginx作为http和反向代理的web服 ...

  6. Filebeat配置module采集nginx日志

    环境 CentOS 7.3 Filebeat 7.6.0 (Filebeat安装和基本使用参考这里) Elasticsearch 7.6.0 Nginx Module Filebeat集成了大量的mo ...

  7. 微服务架构日志集中化 安装 EFK (Fluentd ElasticSearch Kibana) 采集nginx日志

    本文描述如何通过FEK组合集中化nginx的访问日志.本人更喜欢按顺序来命名,所以使用FEK而不是EFK. 首先在nginx服务器上执行以下操作. 安装ruby http://blog.csdn.ne ...

  8. elasticsearch查询filebeat采集的日志

    依赖 不要问为什么不用7或者8,因为不会 <dependency><groupId>org.elasticsearch.client</groupId><ar ...

  9. ELK 收集 Nginx 日志

    01 安装 Nginx 和 ab 工具 1.1 安装 nginx sudo apt-get install nginx -y # 安装Nginx sudo apt-get install apache ...

最新文章

  1. java中PL层_安装pljava - RuralHunter的个人空间 - OSCHINA - 中文开源技术交流社区
  2. cubemx stm32 配置两个串口_STM32CubeMX的串口配置,以及驱动代码
  3. php header()的用法
  4. 可悲的外企Infrastructure - 些须感触(杂)
  5. Linux 搭建SVN server
  6. 让Python删除window下文件
  7. Vue(二十三)vuex + axios + 缓存 运用 (以登陆功能为例)
  8. android studio 中怎么写aspectj代码,Android AOP三剑客之AspectJ
  9. 4TB的移动硬盘,显示只有1.63TB
  10. 可行解、最优解、基解、基可行解、基最优解
  11. OpenGL---PBO
  12. xrdp在ylmf下的问题
  13. python 教程英语版_Python基础教程第3版 英文原版pdf
  14. oracle中角色的用户和权限管理,用户角色与权限控制
  15. linux常用各种服务配置
  16. 日本工程师耗时11年打造17吨重机械甲虫
  17. c语言中0xa0f对应的十进制,c语言起始
  18. 相机卡丢失照片如何恢复
  19. AutoML论文笔记(九)CARS Continuous Evolution for Efficient Neural Architecture Search:连续进化神经网络搜索
  20. 云店云端服务器收银机怎么注册,T+云加密产品,已经加了站点。服务器上如何重新注册-...

热门文章

  1. 500左右能买到降噪蓝牙耳机吗?平价四款高性价比降噪蓝牙耳机推荐
  2. 基于数字孪生的数字化车间升级方案
  3. 如何解决微信二维码收款总被限封?
  4. python天涯帖子_python多线程抓取天涯帖子内容示例
  5. HTML网页表单学习(全方面详解)
  6. Profibus 接线
  7. php100万并发怎么优化,百万并发处理-100个并发需要多少带宽
  8. Error parsing Mapper XML. The XML location is ‘com/xxxxx/com/system/mapper/XXXXMapper.xml‘.
  9. 一、验证票据(component_verify_ticket)
  10. 开发汤姆猫app(没有语音只有点击)