google-cloud-logging

描述

google-cloud-logging 插件可用于将请求日志发送到 Google Cloud Logging Service 进行分析和存储。

属性

名称必选项默认值描述
auth_configauth_configauth_file 必须配置一个。
auth_config.client_email谷歌服务帐号的 email 参数。
auth_config.private_key谷歌服务帐号的私钥参数。
auth_config.project_id谷歌服务帐号的项目 ID。
auth_config.token_urihttps://oauth2.googleapis.com/token请求谷歌服务帐户的令牌的 URI。
auth_config.entries_urihttps://logging.googleapis.com/v2/entries:write谷歌日志服务写入日志条目的 API。
auth_config.scopes谷歌服务账号的访问范围,可参考 OAuth 2.0 Scopes for Google APIs。可选项:”https://www.googleapis.com/auth/logging.read”、”https://www.googleapis.com/auth/logging.write”、”https://www.googleapis.com/auth/logging.admin”、”https://www.googleapis.com/auth/cloud-platform”。
auth_fileauth_configauth_file 必须配置一个。
ssl_verifytrue当设置为 true 时,启用 SSL 验证。
resource{“type”: “global”}谷歌监控资源,请参考 MonitoredResource
log_idapisix.apache.org%2Flogs谷歌日志 ID,请参考 LogEntry
log_format以 JSON 格式的键值对来声明日志格式。对于值部分,仅支持字符串。如果是以 $ 开头,则表明是要获取 APISIX 变量NGINX 内置变量

注意:schema 中还定义了 encrypt_fields = {"auth_config.private_key"},这意味着该字段将会被加密存储在 etcd 中。具体参考 加密存储字段

该插件支持使用批处理器来聚合并批量处理条目(日志和数据)。这样可以避免该插件频繁地提交数据。默认情况下每 5 秒钟或队列中的数据达到 1000 条时,批处理器会自动提交数据,如需了解更多信息或自定义配置,请参考 Batch Processor

默认日志格式示例

  1. {
  2. "insertId": "0013a6afc9c281ce2e7f413c01892bdc",
  3. "labels": {
  4. "source": "apache-apisix-google-cloud-logging"
  5. },
  6. "logName": "projects/apisix/logs/apisix.apache.org%2Flogs",
  7. "httpRequest": {
  8. "requestMethod": "GET",
  9. "requestUrl": "http://localhost:1984/hello",
  10. "requestSize": 59,
  11. "responseSize": 118,
  12. "status": 200,
  13. "remoteIp": "127.0.0.1",
  14. "serverIp": "127.0.0.1:1980",
  15. "latency": "0.103s"
  16. },
  17. "resource": {
  18. "type": "global"
  19. },
  20. "jsonPayload": {
  21. "service_id": "",
  22. "route_id": "1"
  23. },
  24. "timestamp": "2024-01-06T03:34:45.065Z"
  25. }

插件元数据

名称类型必选项默认值有效值描述
log_formatobject以 JSON 格式的键值对来声明日志格式。对于值部分,仅支持字符串。如果是以 $ 开头。则表明获取 APISIX 变量NGINX 内置变量
google-cloud-logging - 图1注意

该设置全局生效。如果指定了 log_format,则所有绑定 google-cloud-logging 的路由或服务都将使用该日志格式。

以下示例展示了如何通过 Admin API 配置插件元数据:

  1. curl http://127.0.0.1:9180/apisix/admin/plugin_metadata/google-cloud-logging \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "log_format": {
  5. "host": "$host",
  6. "@timestamp": "$time_iso8601",
  7. "client_ip": "$remote_addr"
  8. }
  9. }'

配置完成后,你将在日志系统中看到如下类似日志:

  1. {"partialSuccess":false,"entries":[{"jsonPayload":{"client_ip":"127.0.0.1","host":"localhost","@timestamp":"2023-01-09T14:47:25+08:00","route_id":"1"},"resource":{"type":"global"},"insertId":"942e81f60b9157f0d46bc9f5a8f0cc40","logName":"projects/apisix/logs/apisix.apache.org%2Flogs","timestamp":"2023-01-09T14:47:25+08:00","labels":{"source":"apache-apisix-google-cloud-logging"}}]}

启用插件

以下示例展示了如何在指定路由上启用该插件:

完整配置

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "plugins": {
  5. "google-cloud-logging": {
  6. "auth_config":{
  7. "project_id":"apisix",
  8. "client_email":"your service account email@apisix.iam.gserviceaccount.com",
  9. "private_key":"-----BEGIN RSA PRIVATE KEY-----your private key-----END RSA PRIVATE KEY-----",
  10. "token_uri":"https://oauth2.googleapis.com/token",
  11. "scopes":[
  12. "https://www.googleapis.com/auth/logging.admin"
  13. ],
  14. "entries_uri":"https://logging.googleapis.com/v2/entries:write"
  15. },
  16. "resource":{
  17. "type":"global"
  18. },
  19. "log_id":"apisix.apache.org%2Flogs",
  20. "inactive_timeout":10,
  21. "max_retry_count":0,
  22. "max_retry_count":0,
  23. "buffer_duration":60,
  24. "retry_delay":1,
  25. "batch_max_size":1
  26. }
  27. },
  28. "upstream": {
  29. "type": "roundrobin",
  30. "nodes": {
  31. "127.0.0.1:1980": 1
  32. }
  33. },
  34. "uri": "/hello"
  35. }'

最小化配置

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "plugins": {
  5. "google-cloud-logging": {
  6. "auth_config":{
  7. "project_id":"apisix",
  8. "client_email":"your service account email@apisix.iam.gserviceaccount.com",
  9. "private_key":"-----BEGIN RSA PRIVATE KEY-----your private key-----END RSA PRIVATE KEY-----"
  10. }
  11. }
  12. },
  13. "upstream": {
  14. "type": "roundrobin",
  15. "nodes": {
  16. "127.0.0.1:1980": 1
  17. }
  18. },
  19. "uri": "/hello"
  20. }'

测试插件

你可以通过以下命令向 APISIX 发出请求:

  1. curl -i http://127.0.0.1:9080/hello
  1. HTTP/1.1 200 OK
  2. ...
  3. hello, world

访问成功后,你可以登录 Google Cloud Logging Service 查看相关日志。

删除插件

当你需要删除该插件时,可以通过如下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "uri": "/hello",
  5. "plugins": {},
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:1980": 1
  10. }
  11. }
  12. }'