HTTP Log

本文原文链接:https://docs.konghq.com/hub/kong-inc/http-log/

将请求和响应日志发送到HTTP服务器。

注意:此插件的功能与0.12.0之前的Kong版本捆绑在一起,与此处记录的不同。 有关详细信息,请参阅CHANGELOG

术语

  • plugin: 在请求被代理到上游API之前或之后,在Kong内部执行操作的插件。
  • Service: 表示外部 upstream API或微服务的Kong实体。
  • Route: 表示将下游请求映射到上游服务的方法的Kong实体。
  • Consumer: 代表使用API的开发人员或机器的Kong实体。当使用Kong时,Consumer 仅与Kong通信,其代理对所述上游API的每次调用。
  • Credential: 与Consumer关联的唯一字符串,也称为API密钥。
  • upstream service: 这是指位于Kong后面的您自己的 API/service,转发客户端请求。

配置

此插件与具有以下协议的请求兼容:

  • http
  • https
  • grpc
  • grpcs

此插件与无DB模式兼容。

在 Service 上启用插件

使用数据库:

通过发出以下请求在Service上配置此插件:

  1. $ curl -X POST http://kong:8001/services/{service}/plugins \
  2. --data "name=http-log" \
  3. --data "config.http_endpoint=http://mockbin.org/bin/:id" \
  4. --data "config.method=POST" \
  5. --data "config.timeout=1000" \
  6. --data "config.keepalive=1000"

不使用数据库:

通过添加此部分在服务上配置此插件执行声明性配置文件:

  1. plugins:
  2. - name: http-log
  3. service: {service}
  4. config:
  5. http_endpoint: http://mockbin.org/bin/:id
  6. method: POST
  7. timeout: 1000
  8. keepalive: 1000

在这两种情况下,{service}是此插件配置将定位的Service的idname

在 Consumer 上启用插件

使用数据库:

您可以使用http://localhost:8001/plugins在特定的Consumers上启用此插件:

  1. $ curl -X POST http://kong:8001/consumers/{consumer}/plugins \
  2. --data "name=http-log" \
  3. \
  4. --data "config.http_endpoint=http://mockbin.org/bin/:id" \
  5. --data "config.method=POST" \
  6. --data "config.timeout=1000" \
  7. --data "config.keepalive=1000"

不使用数据库:

通过添加此部分在Consumer上配置此插件执行声明性配置文件:

  1. plugins:
  2. - name: http-log
  3. consumer: {consumer}
  4. config:
  5. http_endpoint: http://mockbin.org/bin/:id
  6. method: POST
  7. timeout: 1000
  8. keepalive: 1000

在这两种情况下,{consumer}都是此插件配置将定位的Consumeridusername
您可以组合consumer_idservice_id 。 在同一个请求中,进一步缩小插件的范围。

全局插件

  • 使用数据库: 可以使用http://kong:8001/plugins/配置所有插件。
  • 不使用数据库: 可以通过plugins: 配置所有插件:声明性配置文件中的条目。

与任何 Service ,Route 或 Consumer (或API,如果您使用旧版本的Kong)无关的插件被视为“全局”,并将在每个请求上运行。有关更多信息,请阅读插件参考插件优先级部分。

参数

以下是可在此插件配置中使用的所有参数的列表:

参数 默认值 描述
name 要使用的插件的名称,在本例中为http-log
service_id 此插件将定位的 Service 的ID。
route_id 此插件将定位的 Route 的ID。
enabled true 是否将应用此插件。
consumer_id 此插件将定位的Consumer的id
config.http_endpoin 将数据将发送到的HTTP端点(包括要使用的协议)。
config.method
optional
POST 用于将数据发送到http服务器的可选方法,其他受支持的值为PUTPATCH
config.timeout
optional
10000 向上游服务器发送数据时的可选超时(以毫秒为单位)
config.keepalive
optional
60000 一个可选值(以毫秒为单位),用于定义空闲连接在关闭之前的生存时间

注意:如果config.http_endpoint包含用户名和密码(例如http://bob:password@example.com/logs),则Kong将在日志请求中自动包括basic-auth Authorization 请求头。

日志格式

每个请求都将分别记录在JSON对象中,格式如下:

  1. {
  2. "request": {
  3. "method": "GET",
  4. "uri": "/get",
  5. "url": "http://httpbin.org:8000/get",
  6. "size": "75",
  7. "querystring": {},
  8. "headers": {
  9. "accept": "*/*",
  10. "host": "httpbin.org",
  11. "user-agent": "curl/7.37.1"
  12. },
  13. "tls": {
  14. "version": "TLSv1.2",
  15. "cipher": "ECDHE-RSA-AES256-GCM-SHA384",
  16. "supported_client_ciphers": "ECDHE-RSA-AES256-GCM-SHA384",
  17. "client_verify": "NONE"
  18. }
  19. },
  20. "upstream_uri": "/",
  21. "response": {
  22. "status": 200,
  23. "size": "434",
  24. "headers": {
  25. "Content-Length": "197",
  26. "via": "kong/0.3.0",
  27. "Connection": "close",
  28. "access-control-allow-credentials": "true",
  29. "Content-Type": "application/json",
  30. "server": "nginx",
  31. "access-control-allow-origin": "*"
  32. }
  33. },
  34. "tries": [
  35. {
  36. "state": "next",
  37. "code": 502,
  38. "ip": "127.0.0.1",
  39. "port": 8000
  40. },
  41. {
  42. "ip": "127.0.0.1",
  43. "port": 8000
  44. }
  45. ],
  46. "authenticated_entity": {
  47. "consumer_id": "80f74eef-31b8-45d5-c525-ae532297ea8e",
  48. "id": "eaa330c0-4cff-47f5-c79e-b2e4f355207e"
  49. },
  50. "route": {
  51. "created_at": 1521555129,
  52. "hosts": null,
  53. "id": "75818c5f-202d-4b82-a553-6a46e7c9a19e",
  54. "methods": null,
  55. "paths": [
  56. "/example-path"
  57. ],
  58. "preserve_host": false,
  59. "protocols": [
  60. "http",
  61. "https"
  62. ],
  63. "regex_priority": 0,
  64. "service": {
  65. "id": "0590139e-7481-466c-bcdf-929adcaaf804"
  66. },
  67. "strip_path": true,
  68. "updated_at": 1521555129
  69. },
  70. "service": {
  71. "connect_timeout": 60000,
  72. "created_at": 1521554518,
  73. "host": "example.com",
  74. "id": "0590139e-7481-466c-bcdf-929adcaaf804",
  75. "name": "myservice",
  76. "path": "/",
  77. "port": 80,
  78. "protocol": "http",
  79. "read_timeout": 60000,
  80. "retries": 5,
  81. "updated_at": 1521554518,
  82. "write_timeout": 60000
  83. },
  84. "workspaces": [
  85. {
  86. "id":"b7cac81a-05dc-41f5-b6dc-b87e29b6c3a3",
  87. "name": "default"
  88. }
  89. ],
  90. "consumer": {
  91. "username": "demo",
  92. "created_at": 1491847011000,
  93. "id": "35b03bfc-7a5b-4a23-a594-aa350c585fa8"
  94. },
  95. "latencies": {
  96. "proxy": 1430,
  97. "kong": 9,
  98. "request": 1921
  99. },
  100. "client_ip": "127.0.0.1",
  101. "started_at": 1433209822425
  102. }

关于上述JSON对象的一些注意事项:

  • request 包含有关客户端发送的请求的属性
  • response 包含有关发送给客户端的响应的属性
  • tries 包含负载均衡器对此请求进行的(重试)(成功和失败)列表
  • route 包含有关请求的特定 Route 的Kong属性
  • service 包含与所请求 Route 相关的 Service 的Kong属性
  • authenticated_entity 包含有关已认证凭据的Kong属性(如果已启用身份验证插件
  • workspaces包含与所请求 Route 关联的工作区的Kong属性。仅限于Kong Enterprise版本> = 0.34。
  • consumer 包含经过身份验证的 Consumer(如果已启用身份验证插件)
  • latencies 包含一些有关延迟的数据
    • proxy 是最终服务处理请求所花费的时间
    • kong 是运行所有插件所需的内部Kong延迟
    • request 是从客户端读取第一个字节到将最后一个字节发送到客户端之间经过的时间。对于检测请求慢的客户端非常有用。
  • client_ip 包含原始客户端IP地址
  • started_at 包含开始处理请求的时间的UTC时间戳。

Kong Process Errors

此日志记录插件将仅记录HTTP请求和响应数据。 如果要查找Kong进程错误文件(即nginx错误文件),则可以在以下路径中找到它:$KONG_PREFIX/logs/error.log,其中$ KONG_PREFIX表示配置的前缀