You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用Ops Agent采集GCE VM中Python ETL本地日志到Google Logging

配置问题排查与修复方案

核心配置错误点

  • YAML缩进错误:etl_log_processor 是 logging.processors 的子属性,你的配置中没有缩进,直接和 processors 平级,导致配置解析失败,处理器完全不生效。
  • 非法HTML转义字符:你配置中的 " 是HTML转义后的双引号,YAML文件不识别该转义符,导致正则表达式解析完全错误,无法匹配日志格式。
  • 多余的log_level配置:logging.service.pipelines下的log_level是Ops Agent自身程序的日志输出级别,不是用来过滤采集的业务日志级别的,多余配置会干扰采集逻辑。
  • 冗余的Metrics配置:Metrics部分重复声明了3个功能完全相同的pipeline,虽然不影响日志采集,但属于无效配置,建议删除冗余项。
  • 潜在的权限问题:Ops Agent默认以google-cloud-ops-agent系统用户运行,需要确保你的ETL日志目录/home/user/test_logging/以及目录下的所有日志文件对该用户有可读权限,否则会读取失败无法采集。

修正后的完整配置

logging:
  receivers:
    syslog:
      type: files
      include_paths:
      - /var/log/messages
      - /var/log/syslog
    etl-error-logs:
      type: files
      include_paths:
      - /home/user/test_logging/err_*
    etl-info-logs:
      type: files
      include_paths:
      - /home/user/test_logging/out_*
  processors:
    etl_log_processor:
      type: parse_regex
      field: message
      # 用单引号包裹正则避免YAML转义问题,可直接匹配你的日志样例
      regex: '(?<time>\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\s(?<severity>INFO|ERROR)\s(?<message>.*)'
      time_key: time
      time_format: "%Y-%m-%d %H:%M:%S"
  service:
    pipelines:
      default_pipeline:
        receivers: [syslog]
      error_pipeline:
        receivers: [etl-error-logs]
        processors: [etl_log_processor]
      info_pipeline:
        receivers: [etl-info-logs]
        processors: [etl_log_processor]
metrics:
  receivers:
    hostmetrics:
      type: hostmetrics
      collection_interval: 60s
  processors:
    metrics_filter:
      type: exclude_metrics
      metrics_pattern: []
  service:
    pipelines:
      default_pipeline:
        receivers: [hostmetrics]
        processors: [metrics_filter]

生效与验证步骤

  1. 将修正后的配置覆盖到/etc/google-cloud-ops-agent/config.yaml
  2. 重启Ops Agent服务:sudo systemctl restart google-cloud-ops-agent
  3. 检查服务运行状态:sudo systemctl status google-cloud-ops-agent,确保没有报错
  4. 权限验证:如果出现读取日志文件失败的错误,执行授权命令:
    sudo chmod -R o+r /home/user/test_logging/
    sudo find /home/user/test_logging/ -type f -exec chmod o+r {} \;
    
  5. 测试日志采集:手动写入一条测试日志到对应文件,等待1-2分钟即可在GCP Log Explorer中查询到
    echo "2024-01-01 12:00:00 INFO 测试日志采集" >> /home/user/test_logging/out_test
    

内容的提问来源于stack exchange,提问作者nivsk98

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.25 00:15:08