receivers:
  otlp:
    protocols:
      grpc:
        endpoint: 0.0.0.0:4317
      http:
        endpoint: 0.0.0.0:4318
  syslog/sqlpage:
    protocol: rfc5424
    udp:
      listen_address: 0.0.0.0:1514
  syslog/nginx:
    protocol: rfc5424
    udp:
      listen_address: 0.0.0.0:1516
  filelog/postgresql:
    include:
      - /var/log/postgresql/postgresql.json
    start_at: end
    include_file_path: true
    operators:
      - type: json_parser
        parse_from: body
        parse_to: attributes
  postgresql:
    endpoint: postgres:5432
    transport: tcp
    username: sqlpage
    password: sqlpage
    tls:
      insecure: true
    databases:
      - sqlpage
    collection_interval: 1s

processors:
  transform/sqlpage_logs:
    error_mode: ignore
    log_statements:
      - context: resource
        statements:
          - set(resource.attributes["service.name"], "sqlpage")
      - context: log
        statements:
          - set(log.body, log.attributes["message"]) where log.attributes["message"] != nil
          - merge_maps(log.cache, ExtractPatterns(log.body, "level=(?P<level>[^ ]+)"), "upsert") where IsString(log.body)
          - merge_maps(log.cache, ExtractPatterns(log.body, "trace_id=(?P<trace_id>[0-9a-f]+)"), "upsert") where IsString(log.body)
          - set(log.attributes["level"], log.cache["level"]) where log.cache["level"] != nil
          - set(log.attributes["trace_id"], log.cache["trace_id"]) where log.cache["trace_id"] != nil
          - set(log.severity_text, "TRACE") where log.cache["level"] == "trace"
          - set(log.severity_number, SEVERITY_NUMBER_TRACE) where log.cache["level"] == "trace"
          - set(log.severity_text, "DEBUG") where log.cache["level"] == "debug"
          - set(log.severity_number, SEVERITY_NUMBER_DEBUG) where log.cache["level"] == "debug"
          - set(log.severity_text, "INFO") where log.cache["level"] == "info"
          - set(log.severity_number, SEVERITY_NUMBER_INFO) where log.cache["level"] == "info"
          - set(log.severity_text, "WARN") where log.cache["level"] == "warn"
          - set(log.severity_number, SEVERITY_NUMBER_WARN) where log.cache["level"] == "warn"
          - set(log.severity_text, "ERROR") where log.cache["level"] == "error"
          - set(log.severity_number, SEVERITY_NUMBER_ERROR) where log.cache["level"] == "error"
  transform/postgresql_logs:
    error_mode: ignore
    log_statements:
      - context: resource
        statements:
          - set(resource.attributes["service.name"], "postgresql")
      - context: log
        statements:
          - set(log.body, log.attributes["message"]) where log.attributes["message"] != nil
          - merge_maps(log.cache, ExtractPatterns(log.attributes["application_name"], "00-(?P<trace_id>[0-9a-f]{32})-[0-9a-f]{16}-[0-9a-f]{2}"), "upsert") where IsString(log.attributes["application_name"])
          - set(log.attributes["level"], log.attributes["error_severity"]) where log.attributes["error_severity"] != nil
          - set(log.attributes["trace_id"], log.cache["trace_id"]) where log.cache["trace_id"] != nil
          - set(log.severity_text, "DEBUG") where log.attributes["level"] == "DEBUG1" or log.attributes["level"] == "DEBUG2" or log.attributes["level"] == "DEBUG3" or log.attributes["level"] == "DEBUG4" or log.attributes["level"] == "DEBUG5" or log.attributes["level"] == "DEBUG"
          - set(log.severity_number, SEVERITY_NUMBER_DEBUG) where log.severity_text == "DEBUG"
          - set(log.severity_text, "INFO") where log.attributes["level"] == "LOG" or log.attributes["level"] == "INFO" or log.attributes["level"] == "NOTICE"
          - set(log.severity_number, SEVERITY_NUMBER_INFO) where log.severity_text == "INFO"
          - set(log.severity_text, "WARN") where log.attributes["level"] == "WARNING"
          - set(log.severity_number, SEVERITY_NUMBER_WARN) where log.severity_text == "WARN"
          - set(log.severity_text, "ERROR") where log.attributes["level"] == "ERROR" or log.attributes["level"] == "FATAL" or log.attributes["level"] == "PANIC"
          - set(log.severity_number, SEVERITY_NUMBER_ERROR) where log.severity_text == "ERROR"
  transform/nginx_logs:
    error_mode: ignore
    log_statements:
      - context: resource
        statements:
          - set(resource.attributes["service.name"], "nginx")
      - context: log
        statements:
          - set(log.body, log.attributes["message"]) where log.attributes["message"] != nil
          - merge_maps(log.cache, ExtractPatterns(log.body, "level=(?P<level>[^ ]+)"), "upsert") where IsString(log.body)
          - merge_maps(log.cache, ExtractPatterns(log.body, "\\[(?P<nginx_level>info|error|warn|notice|debug)\\]"), "upsert") where IsString(log.body)
          - merge_maps(log.cache, ExtractPatterns(log.body, "trace_id=(?P<trace_id>[0-9a-f]{32})"), "upsert") where IsString(log.body)
          - merge_maps(log.cache, ExtractPatterns(log.body, "span_id=(?P<span_id>[0-9a-f]{16})"), "upsert") where IsString(log.body)
          - set(log.attributes["trace_id"], log.cache["trace_id"]) where log.cache["trace_id"] != nil
          - set(log.attributes["span_id"], log.cache["span_id"]) where log.cache["span_id"] != nil
          - set(log.attributes["level"], log.cache["level"]) where log.cache["level"] != nil
          - set(log.attributes["level"], log.cache["nginx_level"]) where log.cache["level"] == nil and log.cache["nginx_level"] != nil
          - set(log.severity_text, "DEBUG") where log.attributes["level"] == "debug"
          - set(log.severity_number, SEVERITY_NUMBER_DEBUG) where log.attributes["level"] == "debug"
          - set(log.severity_text, "INFO") where log.attributes["level"] == "info" or log.attributes["level"] == "notice"
          - set(log.severity_number, SEVERITY_NUMBER_INFO) where log.attributes["level"] == "info" or log.attributes["level"] == "notice"
          - set(log.severity_text, "WARN") where log.attributes["level"] == "warn"
          - set(log.severity_number, SEVERITY_NUMBER_WARN) where log.attributes["level"] == "warn"
          - set(log.severity_text, "ERROR") where log.attributes["level"] == "error"
          - set(log.severity_number, SEVERITY_NUMBER_ERROR) where log.attributes["level"] == "error"
  batch:
    timeout: 1s

exporters:
  otlp_grpc/tempo:
    endpoint: tempo:4317
    tls:
      insecure: true
  otlp_http/loki:
    endpoint: http://loki:3100/otlp
  prometheus:
    endpoint: 0.0.0.0:9464

service:
  pipelines:
    traces:
      receivers:
        - otlp
      processors:
        - batch
      exporters:
        - otlp_grpc/tempo
    metrics:
      receivers:
        - otlp
        - postgresql
      processors:
        - batch
      exporters:
        - prometheus
    logs/sqlpage:
      receivers:
        - syslog/sqlpage
      processors:
        - transform/sqlpage_logs
        - batch
      exporters:
        - otlp_http/loki
    logs/postgresql:
      receivers:
        - filelog/postgresql
      processors:
        - transform/postgresql_logs
        - batch
      exporters:
        - otlp_http/loki
    logs/nginx:
      receivers:
        - syslog/nginx
      processors:
        - transform/nginx_logs
        - batch
      exporters:
        - otlp_http/loki
