# =============================================================================
# Promtail Configuration for RE Workflow
# Ships logs from application log files to Loki
# =============================================================================

server:
  http_listen_port: 9080
  grpc_listen_port: 0

# Positions file (tracks what's been read)
positions:
  filename: /tmp/promtail/positions.yaml

# Loki client configuration
clients:
  - url: http://loki:3100/loki/api/v1/push
    batchwait: 1s
    batchsize: 1048576  # 1MB
    timeout: 10s

# Scrape configurations
scrape_configs:
  # ============================================
  # RE Workflow Backend Application Logs
  # ============================================
  - job_name: re-workflow-app
    static_configs:
      - targets:
          - localhost
        labels:
          job: re-workflow
          app: re-workflow
          service: backend
          __path__: /var/log/app/*.log

    pipeline_stages:
      # Parse JSON logs
      - json:
          expressions:
            level: level
            message: message
            timestamp: timestamp
            requestId: requestId
            userId: userId
            method: method
            url: url
            statusCode: statusCode
            duration: duration
            workflowEvent: workflowEvent
            tatEvent: tatEvent
            authEvent: authEvent
            error: error

      # Set log level as label
      - labels:
          level:
          requestId:
          workflowEvent:
          tatEvent:
          authEvent:

      # Timestamp parsing
      - timestamp:
          source: timestamp
          format: "2006-01-02 15:04:05"
          fallback_formats:
            - RFC3339

      # Output stage
      - output:
          source: message

  # ============================================
  # Docker Container Logs (if running in Docker)
  # ============================================
  - job_name: docker-containers
    static_configs:
      - targets:
          - localhost
        labels:
          job: docker
          __path__: /var/lib/docker/containers/*/*-json.log

    pipeline_stages:
      # Parse Docker JSON format
      - json:
          expressions:
            output: log
            stream: stream
            time: time

      # Extract container info from path
      - regex:
          source: filename
          expression: '/var/lib/docker/containers/(?P<container_id>[a-f0-9]+)/.*'

      # Add labels
      - labels:
          stream:
          container_id:

      # Parse application JSON from log field
      - json:
          source: output
          expressions:
            level: level
            message: message
            service: service

      # Add level as label if present
      - labels:
          level:
          service:

      # Output the log message
      - output:
          source: output

  # ============================================
  # System Logs (optional - for infrastructure monitoring)
  # ============================================
  # - job_name: system
  #   static_configs:
  #     - targets:
  #         - localhost
  #       labels:
  #         job: system
  #         __path__: /var/log/syslog

