/etc/google-cloud-ops-agent/config.yaml
logging:
receivers:
app:
type: files
include_paths: [/www/app-*.log]
processors:
monolog:
type: parse_regex
field: message
regex: "^\[(?<time>[^\]]+)\]\s+(?<environment>\w+)\.(?<severity>\w+):\s+(?<msg>.*?)(?<context>{.*})?\s*$"
context:
type: parse_json
field: context
service:
pipelines:
default_pipeline:
receivers: [app]
processors: [monolog,context]
我正在尝试配置
pipeline
,以便:
但是,这不起作用。生成的日志仅包含该块中的JSON,它会删除所有其他数据。我究竟做错了什么?怎么解决呢? Fluentbit 文档没有帮助,Google 文档是最低限度的信息。
我有在 Google Cloud 上运行 docker compose 映像的虚拟机。容器以以下格式在
/var/log/syslog
吐出日志(这就是它们发送到日志资源管理器(以前的堆栈驱动程序)的方式:
Mar 8 20:32:12 service-instance-beta-f14506b-1 docker[4020]: service-cron-beta-3 | {"message":"subscribing: TASK_CANCELLATION_BETA_CRON_SUB","severity":"INFO","timestamp":{"seconds":1709929932,"nanos":973725672}}
然后,我构建了一个正则表达式,用于将日志条目构造为 json 格式。我在 https://rubular.com 上测试了它,它有效:
正则表达式:
^(?<timestamp>\w{3}\s+\d{1,2}\s\d{2}:\d{2}:\d{2})\s(?<host>[\w-]+)\s[\w-]+\[\d+\]:\s(?<service>[\w-]+)\s+\|\s(?<message>{.*})$
它如何在配置上工作?
首先,我使用
parse_regex
类型的处理器以结构化 json 格式处理和构建我的平面日志条目。
然后,我使用另一个类型为
parse_json
的处理器来收集该结构化 json 并将其注入到 StackDriver 日志条目的 jsonPayload
对象中。
这是最终的工作配置:
logging:
receivers:
syslog:
type: files
include_paths:
- /var/log/syslog
processors:
extract_json:
type: parse_regex
field: message
regex: "/^(?<timestamp>\w{3}\s+\d{1,2}\s\d{2}:\d{2}:\d{2})\s(?<host>[\w-]+)\s[\w-]+\[\d+\]:\s(?<service>[\w-]+)\s+\|\s(?<message>{.*})$/"
parse_message:
type: parse_json
field: message
service:
pipelines:
default_pipeline:
receivers: [syslog]
processors: [extract_json,parse_message]