google-cloud-logging

描述

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

属性

名称必选项默认值描述
auth_configauth_configauth_file 必须配置一个。
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

插件元数据

名称类型必选项默认值有效值描述
log_formatobject{“host”: “$host”, “@timestamp”: “$time_iso8601”, “client_ip”: “$remote_addr”}以 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. "private_key":"-----BEGIN RSA PRIVATE KEY-----your private key-----END RSA PRIVATE KEY-----",
  9. "token_uri":"https://oauth2.googleapis.com/token",
  10. "scopes":[
  11. "https://www.googleapis.com/auth/logging.admin"
  12. ],
  13. "entries_uri":"https://logging.googleapis.com/v2/entries:write"
  14. },
  15. "resource":{
  16. "type":"global"
  17. },
  18. "log_id":"apisix.apache.org%2Flogs",
  19. "inactive_timeout":10,
  20. "max_retry_count":0,
  21. "max_retry_count":0,
  22. "buffer_duration":60,
  23. "retry_delay":1,
  24. "batch_max_size":1
  25. }
  26. },
  27. "upstream": {
  28. "type": "roundrobin",
  29. "nodes": {
  30. "127.0.0.1:1980": 1
  31. }
  32. },
  33. "uri": "/hello"
  34. }'

最小化配置

  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. "private_key":"-----BEGIN RSA PRIVATE KEY-----your private key-----END RSA PRIVATE KEY-----"
  9. }
  10. }
  11. },
  12. "upstream": {
  13. "type": "roundrobin",
  14. "nodes": {
  15. "127.0.0.1:1980": 1
  16. }
  17. },
  18. "uri": "/hello"
  19. }'

测试插件

你可以通过以下命令向 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. }'