# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-config
  namespace: apigee
data:
  prometheus.yaml: |
    global:
      scrape_interval:     15s
      evaluation_interval: 30s
    
    scrape_configs:
    - job_name: 'kubernetes-pods'

      tls_config:
        insecure_skip_verify: true
    
      kubernetes_sd_configs:
      - role: pod
    
      relabel_configs:
      # Relabel to scrape only pods that have
      # "prometheus.io/scrape = true" annotation.
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scrape]
        action: keep
        regex: true

      - source_labels: [__meta_kubernetes_pod_name]
        action: replace
        target_label: kubernetes_pod_name

      # Relabel to customize metric path based on pod
      # "prometheus.io/path = <metric path>" annotation.
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_path]
        action: replace
        target_label: __metrics_path__
        regex: (.+)

      # Relabel to customize scrape scheme based on pod
      # "prometheus.io/scheme = <scheme>" annotation.
      - source_labels: [__meta_kubernetes_pod_annotation_prometheus_io_scheme]
        action: replace
        target_label: __scheme__
        regex: (.+)

      # Relabel to scrape only single, desired port for the pod
      # based on pod "prometheus.io/port = <port>" annotation.
      - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
        action: replace
        regex: ([^:]+)(?::\d+)?;(\d+)
        replacement: $1:$2
        target_label: __address__

      # Replace the job name with the container name
      - source_labels: [__meta_kubernetes_pod_container_name]
        target_label: job