# =============================================================================
# Promtail Configuration - 日志收集代理
# =============================================================================
# 功能：
#   - 自动发现 Docker 容器日志
#   - 解析并标签化日志
#   - 推送到 Loki
# =============================================================================

server:
  http_listen_port: 9080
  grpc_listen_port: 0

positions:
  filename: /tmp/positions.yaml

clients:
  - url: http://loki:3100/loki/api/v1/push
    tenant_id: rwa

scrape_configs:
  # ===========================================================================
  # Docker 容器日志收集
  # ===========================================================================
  - job_name: docker
    docker_sd_configs:
      - host: unix:///var/run/docker.sock
        refresh_interval: 5s
        filters:
          - name: label
            values: ["com.docker.compose.project"]

    relabel_configs:
      # 使用容器名作为日志标签
      - source_labels: ['__meta_docker_container_name']
        regex: '/(.*)'
        target_label: 'container'

      # 使用 compose 项目名
      - source_labels: ['__meta_docker_container_label_com_docker_compose_project']
        target_label: 'project'

      # 使用 compose 服务名
      - source_labels: ['__meta_docker_container_label_com_docker_compose_service']
        target_label: 'service'

      # 容器 ID
      - source_labels: ['__meta_docker_container_id']
        target_label: 'container_id'

    pipeline_stages:
      # 解析 JSON 格式日志 (NestJS 默认格式)
      - json:
          expressions:
            level: level
            message: message
            context: context
            timestamp: timestamp
            trace_id: traceId
            span_id: spanId

      # 设置日志级别标签
      - labels:
          level:
          context:
          trace_id:
          span_id:

      # 时间戳解析
      - timestamp:
          source: timestamp
          format: RFC3339Nano
          fallback_formats:
            - RFC3339
            - UnixMs

      # 过滤健康检查日志 (可选，减少噪音)
      - match:
          selector: '{service=~".+"}'
          stages:
            - regex:
                expression: '.*(health|healthcheck|ready|live).*'
            - drop:
                expression: '.*(health|healthcheck|ready|live).*'
                drop_counter_reason: healthcheck_noise

  # ===========================================================================
  # RWA 微服务日志 (直接从服务器采集)
  # ===========================================================================
  - job_name: rwa-services
    static_configs:
      - targets:
          - localhost
        labels:
          job: rwa-backend
          __path__: /var/log/rwa/*.log

    pipeline_stages:
      - json:
          expressions:
            level: level
            message: message
            context: context
            service: service
      - labels:
          level:
          context:
          service:

  # ===========================================================================
  # 系统日志 (可选)
  # ===========================================================================
  - job_name: system
    static_configs:
      - targets:
          - localhost
        labels:
          job: system
          __path__: /var/log/syslog

    pipeline_stages:
      - regex:
          expression: '^(?P<timestamp>\w+\s+\d+\s+\d+:\d+:\d+)\s+(?P<host>\S+)\s+(?P<process>\S+):\s+(?P<message>.*)$'
      - labels:
          host:
          process:
      - timestamp:
          source: timestamp
          format: 'Jan 02 15:04:05'
