# ═══════════════════════════════════════════════════════════
# 🐍 OPHION - OpenTelemetry Collector Configuration
# Central collector for all instrumented applications
# ═══════════════════════════════════════════════════════════

receivers:
  # OTLP receiver - accepts data from any OTLP-compatible SDK
  otlp:
    protocols:
      grpc:
        endpoint: 0.0.0.0:4317
      http:
        endpoint: 0.0.0.0:4318
        cors:
          allowed_origins:
            - "*"
          allowed_headers:
            - "*"

  # Prometheus receiver for scraping metrics
  prometheus:
    config:
      scrape_configs:
        - job_name: 'otel-collector'
          scrape_interval: 15s
          static_configs:
            - targets: ['localhost:8888']

processors:
  # Batch processor for better performance
  batch:
    timeout: 5s
    send_batch_size: 512
    send_batch_max_size: 1024

  # Memory limiter to prevent OOM
  memory_limiter:
    check_interval: 1s
    limit_percentage: 80
    spike_limit_percentage: 25

  # Resource processor to add common attributes
  resource:
    attributes:
      - key: collector.name
        value: ophion-collector
        action: upsert

  # Transform processor for data enrichment
  transform:
    trace_statements:
      - context: span
        statements:
          - set(attributes["ophion.collected"], true)
    metric_statements:
      - context: datapoint
        statements:
          - set(attributes["ophion.collected"], true)

exporters:
  # Export traces to Ophion server via HTTP
  otlphttp/traces:
    endpoint: http://server:8080
    headers:
      Authorization: "Bearer ${env:AGENT_KEY}"
      X-Ophion-Source: otel-collector
    compression: gzip
    retry_on_failure:
      enabled: true
      initial_interval: 5s
      max_interval: 30s
      max_elapsed_time: 300s

  # Export metrics to Ophion server
  otlphttp/metrics:
    endpoint: http://server:8080
    headers:
      Authorization: "Bearer ${env:AGENT_KEY}"
      X-Ophion-Source: otel-collector
    compression: gzip

  # Export logs to Ophion server
  otlphttp/logs:
    endpoint: http://server:8080
    headers:
      Authorization: "Bearer ${env:AGENT_KEY}"
      X-Ophion-Source: otel-collector
    compression: gzip

  # Debug exporter for troubleshooting
  debug:
    verbosity: basic
    sampling_initial: 5
    sampling_thereafter: 200

  # Prometheus exporter for collector's own metrics
  prometheus:
    endpoint: 0.0.0.0:8889
    namespace: ophion_collector

extensions:
  # Health check extension
  health_check:
    endpoint: 0.0.0.0:13133
    path: /health

  # Performance profiler
  pprof:
    endpoint: 0.0.0.0:1777

  # zPages for debugging
  zpages:
    endpoint: 0.0.0.0:55679

service:
  extensions: [health_check, pprof, zpages]
  
  pipelines:
    # Traces pipeline
    traces:
      receivers: [otlp]
      processors: [memory_limiter, batch, resource, transform]
      exporters: [otlphttp/traces, debug]

    # Metrics pipeline
    metrics:
      receivers: [otlp, prometheus]
      processors: [memory_limiter, batch, resource]
      exporters: [otlphttp/metrics, debug]

    # Logs pipeline
    logs:
      receivers: [otlp]
      processors: [memory_limiter, batch, resource]
      exporters: [otlphttp/logs, debug]

  telemetry:
    logs:
      level: info
      encoding: json
    metrics:
      level: detailed
      address: 0.0.0.0:8888
