apisix~kafka-logger插件

news/2024/11/17 15:33:16/文章来源:https://www.cnblogs.com/lori/p/18405910

作用

将http请求与响应的内容发到kafka的topic,以json的形式发送存储

配置相关

  • log_format为自定义配置字段,添加后,默认的请求响应消息将被覆盖
{"_meta": {"disable": false},"batch_max_size": 1,"brokers": [{"host": "192.168.10.132","port": 9091},{"host": "192.168.10.133","port": 9092},{"host": "192.168.10.134","port": 9097}],"disable": false,"include_req_body": false,"include_resp_body": false,"kafka_topic": "apisix-logger-test","log_format": {"api": "$request_uri","userId": "$http_sub","userName": "$http_preferred_username","now":"$time_iso8601",},"producer_type": "async","required_acks": 1
}
  • $开头表示是系统变量
  • $http_开头的,表示是从请求头里获取
  • 如果获取字段的内容为空,则不会出现在消息体里
  • kafka里存储的消息为以下内容
{"now": "2024-09-10T02:01:22+00:00","route_id": "528226539821597458","api": "/pkulaw-chat-gpt-api-4/6.0.0.0/gpt/writer/ping",
}

上面配置,是直接在路由或者全局插件配置中,可以通过apisix-dashboard进行配置,使用log_format对记录的信息进行了设置,我们不建议自定义log_format,因为默认的就是最全的信息

不使用log_format,默认的消息体内容如下

[{"client_ip": "192.168.60.136","upstream": "10.42.4.236:8080","apisix_latency": 7.0000324249268,"start_time": 1716198064095,"latency": 16.000032424927,"request": {"uri": "/kpi/hello","method": "GET","size": 1757,"url": "http://test-apisix.pkulaw.com:9080/kpi/hello","headers": {"accept": "text/html,application/xhtml+xml,application/xml;q\u003d0.9,image/avif,image/webp,*/*;q\u003d0.8","connection": "close","user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0","cookie": "Hm_lvt_8266968662c086f34b2a3e2ae9014bf8\u003d1715752532; Hm_up_8266968662c086f34b2a3e2ae9014bf8\u003d%7B%22ysx_yhqx_20220602%22%3A%7B%22value%22%3A%220%22%2C%22scope%22%3A1%7D%2C%22ysx_hy_20220527%22%3A%7B%22value%22%3A%2206%22%2C%22scope%22%3A1%7D%2C%22uid_%22%3A%7B%22value%22%3A%22fd9959db-ab28-eb11-b390-00155d3c0709%22%2C%22scope%22%3A1%7D%2C%22ysx_yhjs_20220602%22%3A%7B%22value%22%3A%221%22%2C%22scope%22%3A1%7D%7D; cookieUUID\u003dcookieUUID_1715593171791; Hm_lpvt_8266968662c086f34b2a3e2ae9014bf8\u003d1715852645; CookieId\u003daa4be28db45fcd6c3edd84d304533838; SUB\u003d79781f2d-8b4f-483e-ac31-dd55ada12e21; preferred_username\u003dphone2021090709182413393; session_state\u003dde594c09-5681-47c2-ad76-6a7979ff38cd; sensorsdata2015jssdkcross\u003d%7B%22distinct_id%22%3A%22CPcsuEJK%22%2C%22first_id%22%3A%2218f938551ccfe-0b472eec538364-e505625-2073600-18f938551cde56%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC_%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%2C%22%24latest_referrer%22%3A%22%22%7D%2C%22%24device_id%22%3A%2218f938551ccfe-0b472eec538364-e505625-2073600-18f938551cde56%22%7D; sajssdk_2015_cross_new_user\u003d1","sec-fetch-dest": "document","upgrade-insecure-requests": "1","accept-language": "zh-CN,en;q\u003d0.8,en-US;q\u003d0.7,zh;q\u003d0.5,zh-TW;q\u003d0.3,zh-HK;q\u003d0.2","sec-fetch-mode": "navigate","accept-encoding": "gzip, deflate, br","sec-fetch-site": "none","sec-fetch-user": "?1","host": "test-apisix.pkulaw.com","x-forwarded-for": "111.198.143.194"},"querystring": {}},"response": {"status": 200,"headers": {"server": "APISIX/3.8.0","content-type": "application/json","x-ratelimit-limit": "5","transfer-encoding": "chunked","connection": "close","date": "Mon, 20 May 2024 09:41:04 GMT","x-ratelimit-reset": "60","x-ratelimit-remaining": "4"},"size": 2064},"route_id": "513923429800346372","upstream_latency": 9,"service_id": "","server": {"version": "3.8.0","hostname": "apisix-78bcfb45c6-26746"}}
]

在log_format的同步添加扩展字段

  • 这需要修改kafka-logger的源码了
  • 获取user-agent中的操作系统,浏览器等信息,你可以添加下面的方法
local function parse_user_agent(user_agent)local os, browser, version-- 检测操作系统if user_agent:find("Windows") thenos = "Windows"elseif user_agent:find("Macintosh") thenos = "Mac OS"elseif user_agent:find("Linux") thenos = "Linux"elseif user_agent:find("Android") thenos = "Android"elseif user_agent:find("iPhone") thenos = "iOS"end-- 检测浏览器if user_agent:find("Chrome") thenbrowser = "Chrome"version = user_agent:match("Chrome%/(%d+%.%d+)")elseif user_agent:find("Firefox") thenbrowser = "Firefox"version = user_agent:match("Firefox%/(%d+%.%d+)")elseif user_agent:find("Safari") thenbrowser = "Safari"version = user_agent:match("Version%/(%d+%.%d+)")elseif user_agent:find("MSIE") thenbrowser = "Internet Explorer"version = user_agent:match("MSIE (%d+%.%d+)")elseif user_agent:find("Trident") thenbrowser = "Internet Explorer"version = user_agent:match("rv:(%d+%.%d+)")endreturn {os = os or "Unknown",browser = browser or "Unknown",version = version or "Unknown"}
end

在kafka-logger的_M.log(conf, ctx)方法中,添加浏览器扩展字段

function _M.log(conf, ctx)local entryif conf.meta_format == "origin" thenentry = log_util.get_req_original(ctx, conf)elseentry = log_util.get_log_entry(plugin_name, conf, ctx)end-- 添加扩展字段开始local user_agent = ngx.var.http_user_agentlocal info = parse_user_agent(user_agent)entry.os = info.osentry.browser=info.browserentry.version=info.version-- 添加扩展字段结束if batch_processor_manager:add_entry(conf, entry) thenreturnend
...

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/794835.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

安装mayavi命令,使用cxfreeze打包python脚本

pip install numpy出现A new release of pip is available: 23.2.1 -> 24.2 输入: python -m pip install --upgrade pip setuptools wheelpip install PyQt5pip install vtk pip install mayavi pip install scipypip install configobj 查看当前项目的依赖包:pip freeze…

工控机的重要技术:架构与功能

随着工业自动化的快速发展,工控机作为现代工业系统的重要组成部分,其核心技术的研究和应用愈发引起了各界的广泛关注。工控机,作为一种专门用于工业控制和监测的计算机系统,其架构与功能的有效设计和实现直接关系到工业生产的效率与安全。本文将从工控机的基本架构、核心功…

Kubernetes怎么进行NFS动态存储迁移

环境查看 系统环境# cat /etc/redhat-release CentOS Linux release 7.9.2009 (Core) # uname -a Linux CentOS7K8SMaster01063 3.10.0-1160.108.1.el7.x86_64 #1 SMP Thu Jan 25 16:17:31 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux软件环境 # kubectl version Client Version…

Flutter provider和 getx 区别

前沿 Provider 和 GetX 是 Flutter 社区中常用的两种状态管理解决方案。 它们各自有不同的特点和使用场景,下面是它们的区别和示例,以便更好地理解和选择适合的状态管理方法。示例:Provider 和 GetX 的使用 1. 使用 Provider 的示例 假设我们有一个简单的计数器应用,点击按…

NVIDIA驱动,CUDA,CONDA环境安装

NVIDIA驱动安装检查驱动信息last rebootuname -rcat /var/log/apt/history.lognvidia-smill /usr/src/lsmod |grep nvidialspci |grep -i nvidiadmesg -i |grep nvidiadmesg |grep -i nvidiadmesg |grep -i nvrm dpkg --get-selections | grep linux-image查看加载内核版本zg…

一觉醒来,Qt、C#界面开发的天塌了?

再算上VC、PyQt,之前说“人生苦短,快用python”,现在要说人生苦短,快跳出泥潭!上位机、客户端界面传统开发太重,也不够美。传统开发中,还算不错的 毕竟大部分场景,界面并不是核心。大量时间精力投入在交互、美观、还有各种界面类“面向对象的封装”上,实在不划算。有指…

Linguistics-English-时态16种:4种时间4种状态 + 16种时态详解 + 8种时态的被动语态

时态16种: 4种时间 4种状态时态标志词 + 时态详解 + 8种语态的被动语态:

雨量监测系统需要哪些设备,带你360度无死角了解雨量监测系统

作为星创易联技术支持林工,我很高兴为大家介绍我们公司的雨量监测系统。在过去的几年里,我亲自参与了多个雨量监测项目的设计、部署和维护工作,积累了丰富的实践经验。今天,我将详细介绍我们的雨量监测系统,包括其组成部分、工作原理、应用场景以及部署注意事项。有兴趣的…

基础漏洞

目录 目录目录一、暴力破解漏洞描述无防护措施锁定机制验证码绕过二、xss漏洞(跨站脚本攻击)分类反射型(非持续型)攻击步骤存储型(持续型)攻击步骤DOM型危害三、CSRF漏洞(跨站请求伪造)四、SQL注入漏洞描述注入分类SQL注入分类及判断判断是否存在 Sql 注入漏洞判断 Sql…

LLog:Spring轻量级请求日志监控组件,集成管理面板,支持多条件查询检索

开源地址 https://gitee.com/lboot/LLog 简介 LLog是基于AOP构建的请求日志记录和查询工具库,通过引入该工具库,完成配置,实现对接口请求日志的记录、查询检索等功能。请求状态、时间、来源、耗时,请求参数,响应结果,作用接口记录支持与鉴权服务结合,记录请求来源为用户…

如何实现深拷贝?structuredClone

经典的面试题:如何实现深拷贝。 常规的回答主要是通过JSON或者遍历对象递归。主要是考核对对象操作方经典的面试题:如何实现深拷贝。 常规的回答主要是通过JSON或者遍历对象递归。主要是考核对对象操作方法的熟悉程度。今天来介绍另一个方案structuredClone()。什么是 struct…

信奥一本通题陈老师解题:1209:分数求和

​ 【题目描述】【输入】第一行是一个整数nn,表示分数个数,1≤n≤101≤n≤10; 接下来nn行,每行一个分数,用"p/qp/q"的形式表示,不含空格,p,qp,q均不超过1010。【输出】输出只有一行,即最终结果的最简形式。若为分数,用"p/qp/q"的形式表示。【输…