# Metrics Collection & Monitoring
# Collect and route system metrics to multiple destinations
#
# Source: https://docs.expanso.io/examples/metrics-collector
#
# Usage:
#   curl -o config.yaml https://docs.expanso.io/examples/metrics-collector.yaml
#   expanso-edge run -f config.yaml

input:
  generate:
    interval: 30s
    mapping: |
      # Collect system metrics
      root.timestamp = now()
      root.host = env("HOSTNAME").or("localhost")
      root.metrics.cpu_usage = random_int(min: 0, max: 100)
      root.metrics.memory_usage = random_int(min: 0, max: 100)
      root.metrics.disk_usage = random_int(min: 0, max: 100)

pipeline:
  processors:
    - mapping: |
        # Add metadata
        root.node_id = env("NODE_ID").or("unknown")
        root.edge_version = env("EDGE_VERSION").or("dev")

        # Calculate health score
        let cpu = this.metrics.cpu_usage
        let mem = this.metrics.memory_usage
        let disk = this.metrics.disk_usage

        root.health_score = if cpu > 90 || mem > 90 || disk > 90 {
          "critical"
        } else if cpu > 70 || mem > 70 || disk > 70 {
          "warning"
        } else {
          "healthy"
        }

output:
  broker:
    pattern: fan_out
    outputs:
      - stdout:
          codec: lines

      - file:
          path: /var/log/expanso/metrics.json
          codec: lines
