apisix~kafka-logger插件

打印 上一主题 下一主题

主题 967|帖子 967|积分 2901

作用

将http哀求与响应的内容发到kafka的topic,以json的形式发送存储
设置相关


  • log_format为自定义设置字段,添加后,默认的哀求响应消息将被覆盖
  1. {
  2.   "_meta": {
  3.     "disable": false
  4.   },
  5.   "batch_max_size": 1,
  6.   "brokers": [
  7.     {
  8.       "host": "192.168.10.132",
  9.       "port": 9091
  10.     },
  11.     {
  12.       "host": "192.168.10.133",
  13.       "port": 9092
  14.     },
  15.     {
  16.       "host": "192.168.10.134",
  17.       "port": 9097
  18.     }
  19.   ],
  20.   "disable": false,
  21.   "include_req_body": false,
  22.   "include_resp_body": false,
  23.   "kafka_topic": "apisix-logger-test",
  24.   "log_format": {
  25.     "api": "$request_uri",
  26.     "userId": "$http_sub",
  27.     "userName": "$http_preferred_username",
  28.     "now":"$time_iso8601",
  29.   },
  30.   "producer_type": "async",
  31.   "required_acks": 1
  32. }
复制代码

  • $开头表现是系统变量
  • $http_开头的,表现是从哀求头里获取
  • 如果获取字段的内容为空,则不会出如今消息体里
  • kafka里存储的消息为以下内容
  1. {
  2.   "now": "2024-09-10T02:01:22+00:00",
  3.   "route_id": "528226539821597458",
  4.   "api": "/pkulaw-chat-gpt-api-4/6.0.0.0/gpt/writer/ping",
  5. }
复制代码
上面设置,是直接在路由或者全局插件设置中,可以通过apisix-dashboard进行设置,使用log_format对记录的信息进行了设置,我们不建议自定义log_format,由于默认的就是最全的信息
不使用log_format,默认的消息体内容如下
  1. [
  2.   {
  3.     "client_ip": "192.168.60.136",
  4.     "upstream": "10.42.4.236:8080",
  5.     "apisix_latency": 7.0000324249268,
  6.     "start_time": 1716198064095,
  7.     "latency": 16.000032424927,
  8.     "request": {
  9.       "uri": "/kpi/hello",
  10.       "method": "GET",
  11.       "size": 1757,
  12.       "url": "http://test-apisix.pkulaw.com:9080/kpi/hello",
  13.       "headers": {
  14.         "accept": "text/html,application/xhtml+xml,application/xml;q\u003d0.9,image/avif,image/webp,*/*;q\u003d0.8",
  15.         "connection": "close",
  16.         "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:125.0) Gecko/20100101 Firefox/125.0",
  17.         "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",
  18.         "sec-fetch-dest": "document",
  19.         "upgrade-insecure-requests": "1",
  20.         "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",
  21.         "sec-fetch-mode": "navigate",
  22.         "accept-encoding": "gzip, deflate, br",
  23.         "sec-fetch-site": "none",
  24.         "sec-fetch-user": "?1",
  25.         "host": "test-apisix.pkulaw.com",
  26.         "x-forwarded-for": "111.198.143.194"
  27.       },
  28.       "querystring": {}
  29.     },
  30.     "response": {
  31.       "status": 200,
  32.       "headers": {
  33.         "server": "APISIX/3.8.0",
  34.         "content-type": "application/json",
  35.         "x-ratelimit-limit": "5",
  36.         "transfer-encoding": "chunked",
  37.         "connection": "close",
  38.         "date": "Mon, 20 May 2024 09:41:04 GMT",
  39.         "x-ratelimit-reset": "60",
  40.         "x-ratelimit-remaining": "4"
  41.       },
  42.       "size": 2064
  43.     },
  44.     "route_id": "513923429800346372",
  45.     "upstream_latency": 9,
  46.     "service_id": "",
  47.     "server": {
  48.       "version": "3.8.0",
  49.       "hostname": "apisix-78bcfb45c6-26746"
  50.     }
  51.   }
  52. ]
复制代码
在log_format的同步添加扩展字段


  • 这需要修改kafka-logger的源码了
  • 获取user-agent中的操作系统,欣赏器等信息,你可以添加下面的方法
  1. local function parse_user_agent(user_agent)
  2.     local os, browser, version
  3.     -- 检测操作系统
  4.     if user_agent:find("Windows") then
  5.         os = "Windows"
  6.     elseif user_agent:find("Macintosh") then
  7.         os = "Mac OS"
  8.     elseif user_agent:find("Linux") then
  9.         os = "Linux"
  10.     elseif user_agent:find("Android") then
  11.         os = "Android"
  12.     elseif user_agent:find("iPhone") then
  13.         os = "iOS"
  14.     end
  15.     -- 检测浏览器
  16.     if user_agent:find("Chrome") then
  17.         browser = "Chrome"
  18.         version = user_agent:match("Chrome%/(%d+%.%d+)")
  19.     elseif user_agent:find("Firefox") then
  20.         browser = "Firefox"
  21.         version = user_agent:match("Firefox%/(%d+%.%d+)")
  22.     elseif user_agent:find("Safari") then
  23.         browser = "Safari"
  24.         version = user_agent:match("Version%/(%d+%.%d+)")
  25.     elseif user_agent:find("MSIE") then
  26.         browser = "Internet Explorer"
  27.         version = user_agent:match("MSIE (%d+%.%d+)")
  28.     elseif user_agent:find("Trident") then
  29.         browser = "Internet Explorer"
  30.         version = user_agent:match("rv:(%d+%.%d+)")
  31.     end
  32.     return {
  33.         os = os or "Unknown",
  34.         browser = browser or "Unknown",
  35.         version = version or "Unknown"
  36.     }
  37. end
复制代码
在kafka-logger的_M.log(conf, ctx)方法中,添加欣赏器扩展字段
  1. function _M.log(conf, ctx)
  2.     local entry
  3.     if conf.meta_format == "origin" then
  4.         entry = log_util.get_req_original(ctx, conf)
  5.     else
  6.         entry = log_util.get_log_entry(plugin_name, conf, ctx)
  7.     end
  8.     -- 添加扩展字段开始
  9.     local user_agent = ngx.var.http_user_agent
  10.     local info = parse_user_agent(user_agent)
  11.     entry.os = info.os
  12.     entry.browser=info.browser
  13.     entry.version=info.version
  14.     -- 添加扩展字段结束
  15.     if batch_processor_manager:add_entry(conf, entry) then
  16.         return
  17.     end
  18. ...
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

笑看天下无敌手

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表