# Prometheus Adapter rules for exposing rdev metrics for HPA.
# These rules make rdev_pending_tasks available as an external metric.
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-adapter-config
  namespace: monitoring  # Adjust to match your prometheus-adapter namespace
  labels:
    app.kubernetes.io/name: prometheus-adapter
    app.kubernetes.io/part-of: rdev
data:
  config.yaml: |
    # Default rules from prometheus-adapter
    rules:
    - seriesQuery: '{__name__=~"^container_.*",container!="POD",namespace!="",pod!=""}'
      seriesFilters: []
      resources:
        overrides:
          namespace:
            resource: namespace
          pod:
            resource: pod
      name:
        matches: ^container_(.*)_seconds_total$
        as: ""
      metricsQuery: sum(rate(<<.Series>>{<<.LabelMatchers>>,container!="POD"}[5m])) by (<<.GroupBy>>)
    - seriesQuery: '{__name__=~"^container_.*",container!="POD",namespace!="",pod!=""}'
      seriesFilters:
      - isNot: ^container_.*_seconds_total$
      resources:
        overrides:
          namespace:
            resource: namespace
          pod:
            resource: pod
      name:
        matches: ^container_(.*)_total$
        as: ""
      metricsQuery: sum(rate(<<.Series>>{<<.LabelMatchers>>,container!="POD"}[5m])) by (<<.GroupBy>>)
    - seriesQuery: '{__name__=~"^container_.*",container!="POD",namespace!="",pod!=""}'
      seriesFilters:
      - isNot: ^container_.*_total$
      resources:
        overrides:
          namespace:
            resource: namespace
          pod:
            resource: pod
      name:
        matches: ^container_(.*)$
        as: ""
      metricsQuery: sum(<<.Series>>{<<.LabelMatchers>>,container!="POD"}) by (<<.GroupBy>>)

    # rdev external metrics for HPA
    externalRules:
    - seriesQuery: 'rdev_work_queue_pending_tasks'
      resources:
        namespaced: false
      name:
        matches: "rdev_work_queue_pending_tasks"
        as: "rdev_pending_tasks"
      metricsQuery: sum(rdev_work_queue_pending_tasks)

    - seriesQuery: 'rdev_work_queue_running_tasks'
      resources:
        namespaced: false
      name:
        matches: "rdev_work_queue_running_tasks"
        as: "rdev_running_tasks"
      metricsQuery: sum(rdev_work_queue_running_tasks)

    - seriesQuery: 'rdev_workers_idle'
      resources:
        namespaced: false
      name:
        matches: "rdev_workers_idle"
        as: "rdev_idle_workers"
      metricsQuery: sum(rdev_workers_idle)
