How to add variable hostname label to static_config in Promtail?

How do I get the hostname added as a label when using “static_config”? I have tried starting promtail with --client.external-labels=hostname=$(hostname) but I couldn’t get that to work. Can anyone post their config?

scrape_configs:
  - job_name: app_log
    static_configs:
      - targets:
          - localhost
        labels:
          job: syslog
          __path__: /var/log/app.log
          hostname: ???
promtail, version 2.5.0 (branch: HEAD, revision: 2d9d0ee23)
  build user:       root@4779f4b48f3a
  build date:       2022-04-07T21:50:00Z
  go version:       go1.17.6
  platform:         linux/amd64

might this help?

Maybe I’m missing something but that doesn’t mention how to add the hostname without explicitly adding it.

host: yourhost # A `host` label will help identify logs from this machine vs others

‘yourhost’ I would need to replace this with the actual hostname manually. Is there not a way to use $HOSTNAME or something else? I know for the journal you can use __journal__hostname but I can’t figure out how to to use that when using static_configs.

I tried:

sudo ./promtail-linux-amd64 --client.url=http://xxxx:3100/loki/api/v1/push --client.external-labels=hostname=${hostname} --config.file=./config.yaml

and

sudo ./promtail-linux-amd64 --client.url=http://xxxx:3100/loki/api/v1/push --config.expand-env=true --config.file=./config.yaml

With this config

server:
  disable: true

positions:
  filename: /opt/promtail/positions.yaml

scrape_configs:
  - job_name: syslog
    static_configs:
      - targets:
          - localhost
        labels:
          job: syslog
          __path__: /var/log/syslog
          ex1: hostname
          ex2: ${hostname}
          ex3: ${HOSTNAME}
          ex4: HOSTNAME

In Loki the labels show up as I have them in the config and doesn’t actually grab the hostname.

This seems like a very basic thing and I have to be missing something.

What OS is the host this is running on? never mind I see

promtail-linux-amd64

Linux version 4.1.0-cl-7-amd64
gcc version 4.9.2 (Debian 4.9.2-10+deb8u2)  
Cumulus 4.1.33-1+cl3u24 (2019-10-16)
1 Like

so in windows

%COMPUTERNAME%

give you the host name

is there such a environment variable for the distro you are running in? one of these?

hostname
OR
hostnamectl

or maybe even auto generate your config file (deployment as code) so that it stuffs the hostname in it via python or helm or whatever.

using

pip install ruamel.yaml
# Regular imports
from copy import deepcopy
import socket


# Yaml loaders and dumpers
from ruamel.yaml.main import \
    round_trip_load as yaml_load, \
    round_trip_dump as yaml_dump

# Yaml commentary
from ruamel.yaml.comments import \
    CommentedMap as OrderedDict, \
    CommentedSeq as OrderedList

# For manual creation of tokens
from ruamel.yaml.tokens import CommentToken
from ruamel.yaml.error import CommentMark
# Globals
# Number of spaces for an indent 
INDENTATION = 2 
# Used to reset comment objects
tsRESET_COMMENT_LIST = [None, [], None, None]

lokiconfig = OrderedDict({
    "server": OrderedDict({
        "disable": True
    }),
    "positions": OrderedDict({
        "filename": '/opt/promtail/positions.yaml',
        "hostname": socket.gethostname()
    }),
    
})
# Dump the yaml file
print(yaml_dump(lokiconfig))

yeah for Linux it’s $HOSTNAME`

I could generate the configs but what I’m trying to do is documented with both --client.external-labels and --config.expand-env=true. I can’t get either one to work and I can only imagine someone else out there is doing this.

At this point I’m planning on generating each devices config which now adds a bit more complexity. I have over 3k devices that this will be running on and having to generate the configs just because I can’t get the hostname added in seems silly.

1 Like

Agreed on it should work as documented. If it is documented it should work. But in none of your settings do you have it as

$HOSTNAME
ex2: ${hostname}
ex3: ${HOSTNAME}
ex4: HOSTNAME

--client.external-labels=hostname=${hostname}

So are you manually carving these configs on each device?

also check this out interesting

I left off some of my config, I had $HOSTNAME as a test as well. It doesn’t work either.

Besides wanting the hostname label to be unique, the config is the same across all of the devices.

Yes, I saw that link. That’s where I got the --client.external-labels idea from.

Did you see the part that says (hostname) rather than {hostname}

Finally got this working! Thank you @yosiasz for the help on this. Using “()” instead of “{}” was the issue when using --client.external-labels.

Any one in the future that searches for this, this is the full config I used:

config.yaml

server:
  disable: true

positions:
  filename: /opt/promtail/positions.yaml

scrape_configs:
  - job_name: syslog
    static_configs:
      - targets:
          - localhost
        labels:
          job: syslog
          __path__: /var/log/syslog
sudo ./promtail-linux-amd64 --client.url=http://xxxx:3100/loki/api/v1/push --client.external-labels=hostname=$(hostname) --config.file=./config.yaml
3 Likes

If you’re using the helm chart like me. Use the suggested way in the chart to do it. It’s a bit hidden in the values file here it’s quite similar to the solution but capitalizied. I think it takes the env variable HOSTNAME from the pod nodeName field.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.