Skip to content

Shipping logs to Loki with Promtail — config, pipelines, and the labels you should not set

A working Promtail config that tails syslog, journald, and container logs, parses common formats, and ships to Loki with the right labels for query speed.

Promtail is the agent most teams reach for to push host and container logs into Loki. The defaults are fine until they aren’t: a pipeline that pulls request_id into a label, a __path__ that grabs every rotated log under /var/log, or a container scrape that uses the image name as the label and turns a deploy into a million-stream cardinality event. This article gives a working Promtail config for syslog, journald, and Docker, plus the pipeline patterns that extract structured fields without exploding the index.

How to verify

After installing Promtail, the metrics endpoint tells you whether logs are being read, parsed, and accepted by Loki.

systemctl status promtail --no-pager
ss -lntp | grep 9080
curl -fsS http://127.0.0.1:9080/metrics | grep -E '^promtail_(read_lines_total|sent_entries_total|dropped_entries_total|targets)'
curl -fsS http://127.0.0.1:9080/targets | head -50
# Confirm Loki accepted the labels
curl -fsS 'http://loki:3100/loki/api/v1/labels' | jq
curl -fsS 'http://loki:3100/loki/api/v1/label/instance/values' | jq

A promtail_dropped_entries_total that climbs steadily means Loki is rejecting writes — usually 429 too many streams (cardinality), 400 entry out of order (clock skew), or 429 ingestion rate exceeded (a spike). Each has a different fix; the metric name does not tell you which, so check Promtail’s logs.

What’s happening

Promtail watches files (or journald, or Docker, or syslog), turns each line into an entry, runs the entry through a pipeline that can parse, mutate, and label it, then ships batches to Loki via the HTTP push API. The labels attached at the end of the pipeline determine the stream the entry lands in.

Two things to internalize. First, every label increases the stream count combinatorially — a label with 100 values and another with 50 creates up to 5,000 streams per service. Promtail will happily extract a field from JSON and turn it into a label, even if that field is a request ID; you have to actively prevent it. Second, the order of pipeline stages matters: json before labels, labels before output. A stage that runs in the wrong order silently produces empty extractions.

The procedure

  1. Install Promtail.

    PROMTAIL_VERSION="3.2.0"
    sudo useradd --system --no-create-home --shell /usr/sbin/nologin promtail
    sudo usermod -aG systemd-journal promtail
    sudo mkdir -p /etc/promtail /var/lib/promtail
    sudo chown -R promtail:promtail /var/lib/promtail
    curl -fLO "https://github.com/grafana/loki/releases/download/v${PROMTAIL_VERSION}/promtail-linux-amd64.zip"
    unzip promtail-linux-amd64.zip
    sudo install -m 0755 promtail-linux-amd64 /usr/local/bin/promtail
  2. Write the base config. The structure is server, clients (where to push), scrape_configs (where to read), positions (a file tracking how far Promtail has read).

    # /etc/promtail/config.yaml
    server:
      http_listen_address: 127.0.0.1
      http_listen_port: 9080
      grpc_listen_port: 0
    
    positions:
      filename: /var/lib/promtail/positions.yaml
    
    clients:
      - url: http://loki.internal:3100/loki/api/v1/push
        backoff_config:
          min_period: 1s
          max_period: 5m
          max_retries: 10
        batchwait: 1s
        batchsize: 1048576
  3. Add a journald scrape. This is the cleanest source on systemd hosts — every service log goes through journald, fields like _SYSTEMD_UNIT and _HOSTNAME are reliable, and there is no log rotation to deal with.

    scrape_configs:
      - job_name: journal
        journal:
          max_age: 12h
          path: /var/log/journal
          labels:
            job: systemd-journal
            env: prod
            cluster: ca-central-1
        relabel_configs:
          - source_labels: ['__journal__systemd_unit']
            target_label: unit
          - source_labels: ['__journal__hostname']
            target_label: instance
          - source_labels: ['__journal_priority_keyword']
            target_label: level
          - source_labels: ['unit']
            regex: '(ssh-session-.*|user@\d+\.service)'
            action: drop

    The drop action excludes ephemeral session units that would otherwise create one stream per SSH login.

  4. Add a Docker container scrape. Use the Docker SD provider, not file globs on /var/lib/docker/containers/*/-style paths — Docker rotates and renames those files and the file-glob approach loses lines.

      - job_name: docker
        docker_sd_configs:
          - host: unix:///var/run/docker.sock
            refresh_interval: 10s
        relabel_configs:
          - source_labels: ['__meta_docker_container_name']
            regex: '/(.*)'
            target_label: container
          - source_labels: ['__meta_docker_container_label_com_docker_compose_service']
            target_label: service
          - source_labels: ['__meta_docker_container_label_logging']
            regex: 'false'
            action: drop
        pipeline_stages:
          - cri: {}
          - json:
              expressions:
                level: level
                msg: msg
          - labels:
              level:
          - output:
              source: msg

    The cri: stage strips the container runtime prefix (2024-01-01T12:00:00.000Z stdout F <line>) before downstream stages see the JSON. The labels: block promotes level (low cardinality) but not msg (the full line).

  5. Add a syslog/file scrape for non-systemd services. Nginx access logs are the canonical example.

      - job_name: nginx-access
        static_configs:
          - targets: ['localhost']
            labels:
              job: nginx-access
              env: prod
              instance: web01.internal
              __path__: /var/log/nginx/access.log
        pipeline_stages:
          - regex:
              expression: '^(?P<remote>\S+) - (?P<user>\S+) \[(?P<ts>[^\]]+)\] "(?P<method>\S+) (?P<path>\S+) (?P<proto>\S+)" (?P<status>\d+) (?P<size>\d+)'
          - timestamp:
              source: ts
              format: '02/Jan/2006:15:04:05 -0700'
          - labels:
              method:
              status:

    Notably absent from labels:: path, remote, user. Those go into the log body as parsed fields — query-time extraction via | regexp or | json is the right tool for them.

  6. Run as a systemd service.

    # /etc/systemd/system/promtail.service
    [Unit]
    Description=Promtail
    After=network-online.target
    [Service]
    User=promtail
    Group=promtail
    ExecStart=/usr/local/bin/promtail -config.file=/etc/promtail/config.yaml
    Restart=on-failure
    LimitNOFILE=65536
    [Install]
    WantedBy=multi-user.target

Common pitfalls

  • Promoting a JSON field to a label without checking its cardinality first; one bad commit takes streams from hundreds to millions.
  • Forgetting positions.filename is a state file — rebuilding a host without persisting it causes Promtail to re-ship the entire max_age window.
  • Using file globs on container logs instead of docker_sd_configs; rotation breaks tailing and entries are lost silently.
  • A batchsize larger than Loki’s ingestion_rate_mb; pushes are throttled and dropped_entries_total climbs.
  • Reading /var/log/journal without adding promtail to the systemd-journal group; the agent appears to work but reads only what its own UID can.

For the receiving side, see loki-log-aggregation-intro. For host log structure choices, see journalctl-filtering-deep.


Stack Harbor ships logs with Promtail this way on the managed operations tier — pipelines reviewed for cardinality, scrape configs in version control, and an alert on promtail_dropped_entries_total so a misshapen pipeline does not silently lose a day of logs.