github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/production/ksonnet/promtail/scrape_config.libsonnet (about)

     1  local config = import 'config.libsonnet';
     2  
     3  config {
     4    local gen_scrape_config(job_name, pod_uid) = {
     5      job_name: job_name,
     6      pipeline_stages: $._config.promtail_config.pipeline_stages,
     7      kubernetes_sd_configs: [{
     8        role: 'pod',
     9      }],
    10  
    11      relabel_configs: self.prelabel_config + [
    12        // Only scrape local pods; Promtail will drop targets with a __host__ label
    13        // that does not match the current host name.
    14        {
    15          source_labels: ['__meta_kubernetes_pod_node_name'],
    16          target_label: '__host__',
    17        },
    18  
    19        // Drop pods without a __service__ label.
    20        {
    21          source_labels: ['__service__'],
    22          action: 'drop',
    23          regex: '',
    24        },
    25  
    26        // Include all the other labels on the pod.
    27        // Perform this mapping before applying additional label replacement rules
    28        // to prevent a supplied label from overwriting any of the following labels.
    29        {
    30          action: 'labelmap',
    31          regex: '__meta_kubernetes_pod_label_(.+)',
    32        },
    33  
    34        // Rename jobs to be <namespace>/<service>.
    35        {
    36          source_labels: ['__meta_kubernetes_namespace', '__service__'],
    37          action: 'replace',
    38          separator: '/',
    39          target_label: 'job',
    40          replacement: '$1',
    41        },
    42  
    43        // But also include the namespace, pod, container as separate
    44        // labels. They uniquely identify a container. They are also
    45        // identical to the target labels configured in Prometheus
    46        // (but note that Loki does not use an instance label).
    47        {
    48          source_labels: ['__meta_kubernetes_namespace'],
    49          action: 'replace',
    50          target_label: 'namespace',
    51        },
    52        {
    53          source_labels: ['__meta_kubernetes_pod_name'],
    54          action: 'replace',
    55          target_label: 'pod',  // Not 'pod_name', which disappeared in K8s 1.16.
    56        },
    57        {
    58          source_labels: ['__meta_kubernetes_pod_container_name'],
    59          action: 'replace',
    60          target_label: 'container',  // Not 'container_name', which disappeared in K8s 1.16.
    61        },
    62  
    63        // Kubernetes puts logs under subdirectories keyed pod UID and container_name.
    64        {
    65          source_labels: [pod_uid, '__meta_kubernetes_pod_container_name'],
    66          target_label: '__path__',
    67          separator: '/',
    68          replacement: '/var/log/pods/*$1/*.log',
    69        },
    70      ],
    71    },
    72  
    73    promtail_config:: {
    74      scrape_configs: [
    75        // Scrape config to scrape any pods with a 'name' label.
    76        gen_scrape_config('kubernetes-pods-name', '__meta_kubernetes_pod_uid') {
    77          prelabel_config:: [
    78            // Use name label as __service__.
    79            {
    80              source_labels: ['__meta_kubernetes_pod_label_name'],
    81              target_label: '__service__',
    82            },
    83          ],
    84        },
    85  
    86        // Scrape config to scrape any pods with an 'app' label.
    87        gen_scrape_config('kubernetes-pods-app', '__meta_kubernetes_pod_uid') {
    88          prelabel_config:: [
    89            // Drop pods with a 'name' label.  They will have already been added by
    90            // the scrape_config that matches on the 'name' label
    91            {
    92              source_labels: ['__meta_kubernetes_pod_label_name'],
    93              action: 'drop',
    94              regex: '.+',
    95            },
    96  
    97            // Use app label as the __service__.
    98            {
    99              source_labels: ['__meta_kubernetes_pod_label_app'],
   100              target_label: '__service__',
   101            },
   102          ],
   103        },
   104  
   105        // Scrape config to scrape any pods with a direct controller (eg
   106        // StatefulSets).
   107        gen_scrape_config('kubernetes-pods-direct-controllers', '__meta_kubernetes_pod_uid') {
   108          prelabel_config:: [
   109            // Drop pods with a 'name' or 'app' label.  They will have already been added by
   110            // the scrape_config that matches above.
   111            {
   112              source_labels: ['__meta_kubernetes_pod_label_name', '__meta_kubernetes_pod_label_app'],
   113              separator: '',
   114              action: 'drop',
   115              regex: '.+',
   116            },
   117  
   118            // Drop pods with an indirect controller. eg Deployments create replicaSets
   119            // which then create pods.
   120            {
   121              source_labels: ['__meta_kubernetes_pod_controller_name'],
   122              action: 'drop',
   123              regex: '[0-9a-z-.]+-[0-9a-f]{8,10}',
   124            },
   125  
   126            // Use controller name as __service__.
   127            {
   128              source_labels: ['__meta_kubernetes_pod_controller_name'],
   129              target_label: '__service__',
   130            },
   131          ],
   132        },
   133  
   134        // Scrape config to scrape any pods with an indirect controller (eg
   135        // Deployments).
   136        gen_scrape_config('kubernetes-pods-indirect-controller', '__meta_kubernetes_pod_uid') {
   137          prelabel_config:: [
   138            // Drop pods with a 'name' or 'app' label.  They will have already been added by
   139            // the scrape_config that matches above.
   140            {
   141              source_labels: ['__meta_kubernetes_pod_label_name', '__meta_kubernetes_pod_label_app'],
   142              separator: '',
   143              action: 'drop',
   144              regex: '.+',
   145            },
   146  
   147            // Drop pods not from an indirect controller. eg StatefulSets, DaemonSets
   148            {
   149              source_labels: ['__meta_kubernetes_pod_controller_name'],
   150              regex: '[0-9a-z-.]+-[0-9a-f]{8,10}',
   151              action: 'keep',
   152            },
   153  
   154            // Put the indirect controller name into a temp label.
   155            {
   156              source_labels: ['__meta_kubernetes_pod_controller_name'],
   157              action: 'replace',
   158              regex: '([0-9a-z-.]+)-[0-9a-f]{8,10}',
   159              target_label: '__service__',
   160            },
   161          ],
   162        },
   163  
   164        // Scrape config to scrape any control plane static pods (e.g. kube-apiserver
   165        // etcd, kube-controller-manager & kube-scheduler)
   166        gen_scrape_config('kubernetes-pods-static', '__meta_kubernetes_pod_annotation_kubernetes_io_config_mirror') {
   167          prelabel_config:: [
   168            // Ignore pods that aren't mirror pods
   169            {
   170              action: 'drop',
   171              source_labels: ['__meta_kubernetes_pod_annotation_kubernetes_io_config_mirror'],
   172              regex: '',
   173            },
   174  
   175            // Static control plane pods usually have a component label that identifies them
   176            {
   177              action: 'replace',
   178              source_labels: ['__meta_kubernetes_pod_label_component'],
   179              target_label: '__service__',
   180            },
   181          ],
   182        },
   183      ],
   184    },
   185  }