Skip to content

Instantly share code, notes, and snippets.

@enixdark
Forked from ruanbekker/promtail_docker_logs.md
Created November 2, 2022 16:36
Show Gist options
  • Save enixdark/cec6b1c744bc35bf52e4fc3e22e37c60 to your computer and use it in GitHub Desktop.
Save enixdark/cec6b1c744bc35bf52e4fc3e22e37c60 to your computer and use it in GitHub Desktop.

Revisions

  1. @ruanbekker ruanbekker revised this gist May 8, 2020. 1 changed file with 64 additions and 8 deletions.
    72 changes: 64 additions & 8 deletions promtail_docker_logs.md
    Original file line number Diff line number Diff line change
    @@ -49,13 +49,13 @@ clients:
    scrape_configs:
    #- job_name: system
    # static_configs:
    # - targets:
    # - localhost
    # labels:
    # job: varlogs
    # __path__: /var/log/*log
    - job_name: system
    static_configs:
    - targets:
    - localhost
    labels:
    job: varlogs
    __path__: /var/log/*log
    - job_name: containers
    entry_parser: raw
    @@ -100,4 +100,60 @@ $ curl http://localhost:8080/?foo=bar

    Screenshot:

    ![](https://user-images.githubusercontent.com/30043398/81418029-5a7e7380-914c-11ea-94cc-b7b1f8024309.png)
    ![](https://user-images.githubusercontent.com/30043398/81418029-5a7e7380-914c-11ea-94cc-b7b1f8024309.png)


    Setup with less labels:

    - docker-config.yml

    ```
    server:
    http_listen_address: 0.0.0.0
    http_listen_port: 9080
    positions:
    filename: /tmp/positions.yaml
    clients:
    - url: http://loki:3100/loki/api/v1/push
    scrape_configs:
    - job_name: containers
    entry_parser: raw
    static_configs:
    - targets:
    - localhost
    labels:
    job: containerlogs
    cluster: multipass-cluster
    __path__: /var/lib/docker/containers/*/*log
    # --log-opt tag="{{.Name}}"
    pipeline_stages:
    - json:
    expressions:
    stream: stream
    attrs: attrs
    tag: attrs.tag
    - regex:
    expression: (?P<container_name>(?:[^|]*[^|]))
    source: "tag"
    - labels:
    #tag:
    stream:
    container_name:
    ```

    ```
    $ docker run -itd --name nginxapp3 -p 8080:80 --log-driver json-file --log-opt tag="{{.Name}}" nginx
    $ curl -XGET -A "Mozilla" --refer http://bot.com/scrape.html http://localhost:8080/?foo=barx
    ```

    Screenshot:

    ![](https://user-images.githubusercontent.com/30043398/81419351-69662580-914e-11ea-9463-7fc2b910849d.png)
  2. @ruanbekker ruanbekker created this gist May 8, 2020.
    103 changes: 103 additions & 0 deletions promtail_docker_logs.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,103 @@
    Inspired By: https://github.com/grafana/loki/issues/333

    - docker-compose.yml

    ```
    version: "3"
    networks:
    loki:
    services:
    loki:
    image: grafana/loki:1.4.1
    ports:
    - "3100:3100"
    command: -config.file=/etc/loki/local-config.yaml
    networks:
    - loki
    promtail:
    image: grafana/promtail:1.4.1
    volumes:
    - /var/lib/docker/containers:/var/lib/docker/containers
    - /home/ubuntu/docker-config.yml:/etc/promtail/docker-config.yml
    command: -config.file=/etc/promtail/docker-config.yml
    networks:
    - loki
    grafana:
    image: grafana/grafana:master
    ports:
    - "3000:3000"
    networks:
    - loki
    ```

    - docker-config.yml

    ```
    server:
    http_listen_address: 0.0.0.0
    http_listen_port: 9080
    positions:
    filename: /tmp/positions.yaml
    clients:
    - url: http://loki:3100/loki/api/v1/push
    scrape_configs:
    #- job_name: system
    # static_configs:
    # - targets:
    # - localhost
    # labels:
    # job: varlogs
    # __path__: /var/log/*log
    - job_name: containers
    entry_parser: raw
    static_configs:
    - targets:
    - localhost
    labels:
    job: containerlogs
    __path__: /var/lib/docker/containers/*/*log
    # --log-opt tag="{{.ImageName}}|{{.Name}}|{{.ImageFullID}}|{{.FullID}}"
    pipeline_stages:
    - json:
    expressions:
    stream: stream
    attrs: attrs
    tag: attrs.tag
    - regex:
    expression: (?P<image_name>(?:[^|]*[^|])).(?P<container_name>(?:[^|]*[^|])).(?P<image_id>(?:[^|]*[^|])).(?P<container_id>(?:[^|]*[^|]))
    source: "tag"
    - labels:
    tag:
    stream:
    image_name:
    container_name:
    image_id:
    container_id:
    ```

    ```
    $ docker-compose up -d
    ```

    ```
    $ docker run -itd --name nginxapp -p 8080:80 --log-driver json-file --log-opt tag="{{.ImageName}}|{{.Name}}|{{.ImageFullID}}|{{.FullID}}" nginx
    $ curl http://localhost:8080/?foo=bar
    ```

    Screenshot:

    ![](https://user-images.githubusercontent.com/30043398/81418029-5a7e7380-914c-11ea-94cc-b7b1f8024309.png)