apiVersion: batch/v1
kind: Job
metadata:
  labels:
    jobName: metrics-collector
  name: metrics-collector
spec:
  template:
    metadata:
      annotations:
        sidecar.istio.io/inject: "false"
      labels:
        jobName: metrics-collector
      name: metrics-collector
    spec:
      containers:
      - command:
        - bash
        - -c
        - |
          export PROM_API="http://monitoring-prometheus.kyma-system.svc.cluster.local:9090/api/v1"
          
          configured_avg=$(curl -s "$PROM_API/query?query=avg(function_configured_status_duration_millisecond)" | jq '.data.result[0].value[1]' -r)
          built_avg=$(curl -s "$PROM_API/query?query=avg(function_built_status_duration_millisecond)" | jq '.data.result[0].value[1]' -r)
          running_avg=$(curl -s "$PROM_API/query?query=avg(function_running_status_duration_millisecond)" | jq '.data.result[0].value[1]' -r)
          
          echo "--------------------------------------------------------------------------------"
          echo "Collecting Serverless Function Metrics..."
          echo "--------------------------------------------------------------------------------"
          
          cat << EOF | jq -c . 
          {
          "serverlessFunctionMetrics": {
              "timestamp": "$(date --rfc-3339=seconds)",
              "configured": ${configured_avg:-0},
              "built": ${built_avg:-0},
              "running": ${running_avg:-0}
          }
          }
          EOF
        image: eu.gcr.io/kyma-project/tpi/k8s-tools:20211022-85284bf9
        name: metrics-collector
        resources: {}
      dnsPolicy: ClusterFirst
      restartPolicy: OnFailure

