# Useful examples on how to configure Prometheus
# * https://www.weave.works/prometheus-and-kubernetes-monitoring-your-applications/
# * https://grafana.net/dashboards/162
# * https://raw.githubusercontent.com/prometheus/prometheus/master/documentation/examples/prometheus-kubernetes.yml
apiVersion: v1
kind: ConfigMap
metadata:
  name: prometheus-configmap
  namespace: monitoring
data:
  prometheus.yml: |
    global:
      scrape_interval: 15s
    # rule_files:
      # - "/etc/prometheus-rules/alert.rules"
    scrape_configs:
      - job_name: kubernetes-service-endpoints
        kubernetes_sd_configs:
          - role: endpoints
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
        relabel_configs:
          - source_labels:
              - __meta_kubernetes_service_label_component
            regex: apiserver
            action: replace
            target_label: __scheme__
            replacement: https
          - source_labels:
              - __meta_kubernetes_service_label_kubernetes_io_cluster_service
            action: drop
            regex: 'true'
          - source_labels:
              - __meta_kubernetes_service_annotation_prometheus_io_scrape
            action: drop
            regex: 'false'
          - source_labels:
              - __meta_kubernetes_pod_container_port_name
            action: drop
            regex: .*-noscrape
          - source_labels:
              - __meta_kubernetes_service_annotation_prometheus_io_scheme
            action: replace
            target_label: __scheme__
            regex: ^(https?)$
            replacement: $1
          - source_labels:
              - __meta_kubernetes_service_annotation_prometheus_io_path
            action: replace
            target_label: __metrics_path__
            regex: ^(.+)$
            replacement: $1
          - source_labels:
              - __address__
              - __meta_kubernetes_service_annotation_prometheus_io_port
            action: replace
            target_label: __address__
            regex: '^(.+)(?::\d+);(\d+)$'
            replacement: '$1:$2'
          - action: labelmap
            regex: ^__meta_kubernetes_service_label_(.+)$
            replacement: $1
          - source_labels: [__meta_kubernetes_namespace]
            action: replace
            target_label: namespace
          - source_labels: [__meta_kubernetes_pod_name]
            action: replace
            target_label: pod_name
          - source_labels:
              - __meta_kubernetes_pod_node_name
            target_label: instance
          # - source_labels:
          #     - __meta_kubernetes_namespace
          #     - __meta_kubernetes_service_name
          #   separator: /
          #   target_label: job
      - job_name: kubernetes-pods
        kubernetes_sd_configs:
          - role: pod
        relabel_configs:
          - source_labels:
              - __meta_kubernetes_pod_annotation_prometheus_io_scrape
            action: keep
            regex: 'true'
          # - source_labels:
          #     - __meta_kubernetes_namespace
          #     - __meta_kubernetes_pod_label_name
          #   separator: /
          #   target_label: job
          - source_labels:
              - __meta_kubernetes_pod_node_name
            target_label: node
          - source_labels: [__meta_kubernetes_namespace]
            action: replace
            target_label: namespace
          - source_labels: [__meta_kubernetes_pod_name]
            action: replace
            target_label: pod_name
      - job_name: kubernetes-nodes
        kubernetes_sd_configs:
          - role: node
        tls_config:
          insecure_skip_verify: true
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
        relabel_configs:
          - target_label: __scheme__
            replacement: http
          - source_labels:
              - __meta_kubernetes_node_label_kubernetes_io_hostname
            target_label: instance
          - source_labels: [__address__]
            action: replace
            target_label: __address__
            regex: '^(.+?)(?::\d+)?$'
            replacement: '$1:10255'
      - job_name: weave
        kubernetes_sd_configs:
          - role: pod
        relabel_configs:
          - source_labels:
              - __meta_kubernetes_namespace
              - __meta_kubernetes_pod_label_name
            action: keep
            regex: ^kube-system;weave-net$
          - source_labels:
              - __meta_kubernetes_pod_container_name
              - __address__
            action: replace
            target_label: __address__
            regex: '^weave;(.+?)(?::\d+)?$'
            replacement: '$1:6782'
          - source_labels:
              - __meta_kubernetes_pod_container_name
              - __address__
            action: replace
            target_label: __address__
            regex: '^weave-npc;(.+?)(?::\d+)?$'
            replacement: '$1:6781'
          - source_labels:
              - __meta_kubernetes_pod_container_name
            action: replace
            target_label: job
      # Scrape config for Kubelet cAdvisor.
      - job_name: 'kubernetes-cadvisor'
        scheme: https
      
        tls_config:
          ca_file: /var/run/secrets/kubernetes.io/serviceaccount/ca.crt
        bearer_token_file: /var/run/secrets/kubernetes.io/serviceaccount/token
      
        kubernetes_sd_configs:
        - role: node
      
        relabel_configs:
        - action: labelmap
          regex: __meta_kubernetes_node_label_(.+)
        - target_label: __address__
          replacement: kubernetes.default.svc:443
        - source_labels: [__meta_kubernetes_node_name]
          regex: (.+)
          target_label: __metrics_path__
          replacement: /api/v1/nodes/${1}/proxy/metrics/cadvisor

      # Mixer scrapping. Defaults to Prometheus and mixer on same namespace.
      #
      - job_name: 'istio-mesh'
        kubernetes_sd_configs:
        - role: endpoints
          namespaces:
            names:
            - istio-system
        relabel_configs:
        - source_labels: [__meta_kubernetes_service_name, __meta_kubernetes_endpoint_port_name]
          action: keep
          regex: istio-telemetry;prometheus
      # Scrape config for envoy stats
      - job_name: 'envoy-stats'
        metrics_path: /stats/prometheus
        kubernetes_sd_configs:
        - role: pod

        relabel_configs:
        - source_labels: [__meta_kubernetes_pod_container_port_name]
          action: keep
          regex: '.*-envoy-prom'
        - source_labels: [__address__, __meta_kubernetes_pod_annotation_prometheus_io_port]
          action: replace
          regex: ([^:]+)(?::\d+)?;(\d+)
          replacement: $1:15090
          target_label: __address__
        - action: labeldrop
          regex: __meta_kubernetes_pod_label_(.+)
        - source_labels: [__meta_kubernetes_namespace]
          action: replace
          target_label: namespace
        - source_labels: [__meta_kubernetes_pod_name]
          action: replace
          target_label: pod_name