7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Prometheusでinstance名をホスト名にしたい

Posted at

Prometheus.yamlでtargetにIPアドレスを指定すると、メトリクスのinstanceラベルにもIPアドレスで記録されてしまいます。

解決方法は3つあります。

  • 方法①:DNSサーバで名前解決できるようにして、prometheus.yamlにホスト名を書く
  • 方法②:Prometheusサーバの/etc/hostsにホスト名とIPアドレスの対応を書き、prometheus.yamlにホスト名を書く
  • 方法③:relabel_configsを使う

この記事では、簡単に試せる方法③を紹介します。

結論だけ。

prometheus.yaml
global:
  scrape_interval:     15s
  evaluation_interval: 15s
  external_labels:
      monitor: 'codelab-monitor'

scrape_configs:
  - job_name: 'linux'
    static_configs:
      - targets:
        - 192.168.1.101:9100:server1   #<IP>:<PORT>:<HOSTNAME>
        - 192.168.1.102:9100:server2
    relabel_configs:
      - source_labels: [__address__]  # targetsエントリを分解してinstanceラベルの値を生成する
        regex: '([^:]+):(\d+):(.*)'
        target_label: instance
        replacement: '${3}:${2}'      # instance=<HOSTNAME>:<PORT>
      - source_labels: [__address__]  # targetsエントリを分解して、実際のアクセス先を生成する
        regex: '([^:]+):(\d+):(.*)'
        target_label: __address__
        replacement: '${1}:${2}'      # __address__=<IP>:<PORT> 
7
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?