<p>I have some kubernetes applications that log to files rather than stdout/stderr, and I collect them with Promtail sidecars. But since the sidecars execute with &quot;localhost&quot; target, I don't have a <code>kubernetes_sd_config</code> that will apply pod metadata to labels for me. So I'm stuck statically declaring my labels.</p>
<pre class="lang-yaml prettyprint-override"><code># ConfigMap
apiVersion: v1
kind: ConfigMap
metadata:
  labels:
    app: promtail
  name: sidecar-promtail
data:
  config.yml: |
    client:
      url: http://loki.loki.svc.cluster.local:3100/loki/api/v1/push
      backoff_config:
        max_period: 5m
        max_retries: 10
        min_period: 500ms
      batchsize: 1048576
      batchwait: 1s
      external_labels: {}
      timeout: 10s
    positions:
      filename: /tmp/promtail-positions.yaml
    server:
      http_listen_port: 3101
    target_config:
      sync_period: 10s
    scrape_configs:
    - job_name: sidecar-logs
      static_configs:
        - targets:
          - localhost
          labels:
            job: sidecar-logs
            __path__: &quot;/sidecar-logs/*.log&quot;

----
# Deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: test-logger
spec:
  selector:
    matchLabels:
      run: test-logger
  template:
    metadata:
      labels:
        run: test-logger
    spec:
      volumes:
        - name: nfs
          persistentVolumeClaim:
            claimName: nfs-claim
        - name: promtail-config
          configMap:
            name: sidecar-promtail
      containers:
        - name: sidecar-promtail
          image: grafana/promtail:2.1.0
          volumeMounts:
            - name: nfs
              mountPath: /sidecar-logs
            - mountPath: /etc/promtail
              name: promtail-config
        - name: simple-logger         
          image: foo/simple-logger
          volumeMounts:
            - name: nfs
              mountPath: /logs
</code></pre>
<p>What is the best way to label the collected logs based on the parent pod's metadata?</p>

user274892
<p>You can do the following:</p>
<p>In the sidecar container, expose the pod name, node name and other information you need as environment variables, then add the flag <code>'-config.expand-env'</code> to enable environment expansion inside promtail config file, e.g.:</p>
<pre class="lang-yaml prettyprint-override"><code>...
- name: sidecar-promtail
  image: grafana/promtail:2.1.0
  # image: grafana/promtail:2.4.1 # use this one if environment expansion is not available in 2.1.0
  args:
    # Enable environment expansion in promtail config file
    - '-config.expand-env'
  env:
    - name: NODE_NAME
      valueFrom:
        fieldRef:
          fieldPath: spec.nodeName
    - name: POD_NAME
      valueFrom:
        fieldRef:
          fieldPath: metadata.name
...
</code></pre>
<p>Then in your configMap, add the environment variables in your static_config labels as such:</p>
<pre class="lang-yaml prettyprint-override"><code>...
    scrape_configs:
    - job_name: sidecar-logs
      static_configs:
        - targets:
          - localhost
          labels:
            job: sidecar-logs
            pod: ${POD_NAME}
            node_name: ${NODE_NAME}
            __path__: &quot;/sidecar-logs/*.log&quot;
...
</code></pre>
