# OpenTelemetry Collector configuration for Mattermost and PostgreSQL log collection
# This version uses script-driven labeling and explicit severity mapping.

receivers:
  filelog/mattermost:
    include: [ /opt/mattermost/logs/mattermost.log ]
    start_at: beginning

  filelog/postgres:
    include: [ /var/log/postgresql/*.json ]
    start_at: beginning
    operators:
      - type: json_parser

processors:
  batch:
    send_batch_size: 10000
    timeout: 10s

  resource:
    attributes:
      - key: host.name
        value: "<HOSTNAME>"
        action: upsert
      - key: service.instance.id
        value: "<HOSTNAME>"
        action: upsert
      - key: service.name
        value: "<SERVICE_NAME>"
        action: upsert

  transform/severity:
    log_statements:
      - context: log
        statements:
          # Postgres severity mapping
          - set(log.severity_text, log.attributes["error_severity"]) where log.attributes["error_severity"] != nil
          - set(log.severity_number, 9) where log.attributes["error_severity"] == "LOG" or log.attributes["error_severity"] == "DETAIL" or log.attributes["error_severity"] == "STATEMENT"
          - set(log.severity_number, 13) where log.attributes["error_severity"] == "WARNING"
          - set(log.severity_number, 17) where log.attributes["error_severity"] == "ERROR" or log.attributes["error_severity"] == "FATAL" or log.attributes["error_severity"] == "PANIC"

          # Mattermost severity mapping
          - set(log.severity_text, log.attributes["level"]) where log.attributes["level"] != nil
          - set(log.severity_number, 9) where log.attributes["level"] == "info"
          - set(log.severity_number, 13) where log.attributes["level"] == "warn" or log.attributes["level"] == "warning"
          - set(log.severity_number, 17) where log.attributes["level"] == "error"

exporters:
  otlp_http/loki:
    endpoint: "http://<LOKI_HOST>:3100/otlp"
    tls:
      insecure: true

service:
  telemetry:
    logs:
      level: "info"
  pipelines:
    logs/mattermost:
      receivers: [ filelog/mattermost ]
      processors: [ resource, transform/severity, batch ]
      exporters: [ otlp_http/loki ]
    logs/postgres:
      receivers: [ filelog/postgres ]
      processors: [ resource, transform/severity, batch ]
      exporters: [ otlp_http/loki ]
