Cấu hình Docker container log vào GCP Logging

Newsletter Mar 5, 2024

TLDR: Cấu hình Ops Agent để collect log từ container đẩy lên GCP Logging

  1. Setup Google Cloud Ops Agent: https://cloud.google.com/stackdriver/docs/solutions/agents/ops-agent
  2. Config Ops Agent
# /etc/google-cloud-ops-agent/config.yaml

logging:
  receivers:
    docker_logs:
      type: files
      include_paths:
        - "/var/lib/docker/containers/*/*.log"
      record_log_file_path: true
  processors:
    docker_json_parser:
      type: parse_json
      field: log
      time_key: time
      time_format: "%Y-%m-%dT%H:%M:%S.%L"
    # Additional processor for extracting container ID from the log metadata if available
    # This is hypothetical and depends on your logging setup to include such metadata
    extract_container_id:
      type: parse_regex
      regex: '^/var/lib/docker/containers/(?<container_id>[^/]+)/.+$'
      # This assumes that your logs include a container_id field or similar; adjust as necessary
  exporters:
    google:
      type: google_cloud_logging
  service:
    pipelines:
      logs:
        receivers: [docker_logs]
        processors: [docker_json_parser]
        exporters: [google]
  1. Restart Agent
sudo systemctl restart google-cloud-ops-agent
sudo systemctl status google-cloud-ops-agent
  1. Check log trên Logs Explorer

Tags