systemd_exporter v0.7.0がリリースされてsystemd-resolvedのメトリクスが取れるようになりました

タイトルまま、これは記念エントリです。

github.com

これ。

systemd_exporterにsystemd-resolvedのメトリクスを取得するPRを投げ、マージされてから1年と2ヶ月経過してやっとこ新バージョンがリリースされました。

github.com

これです。

こんなのが取れます

systemd resolved dashboard

使う人向け情報

systemd_exporter --systemd.collector.enable-resolved

でsystemd_resolvedのメトリクスを取得するようになります。

READMEへの変更はマージされてないのでアレですが --help で確認できます

$ curl -s http://localhost:9558/metrics | grep resolve
# HELP systemd_resolved_cache_hits_total Resolved Total Cache Hits
# TYPE systemd_resolved_cache_hits_total counter
systemd_resolved_cache_hits_total 236418
# HELP systemd_resolved_cache_misses_total Resolved Total Cache Misses
# TYPE systemd_resolved_cache_misses_total counter
systemd_resolved_cache_misses_total 247689
# HELP systemd_resolved_current_cache_size Resolved Current Cache Size
# TYPE systemd_resolved_current_cache_size gauge
systemd_resolved_current_cache_size 48
# HELP systemd_resolved_current_transactions Resolved Current Transactions
# TYPE systemd_resolved_current_transactions gauge
systemd_resolved_current_transactions 0
# HELP systemd_resolved_dnssec_bogus_total Resolved Total number of DNSSEC Verdicts Boguss
# TYPE systemd_resolved_dnssec_bogus_total counter
systemd_resolved_dnssec_bogus_total 0
# HELP systemd_resolved_dnssec_indeterminate_total Resolved Total number of DNSSEC Verdicts Indeterminat
# TYPE systemd_resolved_dnssec_indeterminate_total counter
systemd_resolved_dnssec_indeterminate_total 0
# HELP systemd_resolved_dnssec_insecure_total Resolved Total number of DNSSEC Verdicts Insecure
# TYPE systemd_resolved_dnssec_insecure_total counter
systemd_resolved_dnssec_insecure_total 0
# HELP systemd_resolved_dnssec_secure_total Resolved Total number of DNSSEC Verdicts Secure
# TYPE systemd_resolved_dnssec_secure_total counter
systemd_resolved_dnssec_secure_total 0
# HELP systemd_resolved_transactions_total Resolved Total Transactions
# TYPE systemd_resolved_transactions_total counter
systemd_resolved_transactions_total 476851

特権は特に必要ないので

[Unit]
Description=Systemd Exporter

[Service]
Type=simple
User=nobody
ExecStart=/usr/local/bin/systemd_exporter --systemd.collector.enable-resolved --systemd.collector.unit-include=(apache2|.*exporter.*|prometheus|grafana)\.service

[Install]
WantedBy=multi-user.target

自分はこんな感じのsystemd unitファイルを書いてます。

systemd_exporterはunitごとのメトリクスを取るためカーディナリティが高いので、systemd.collector.unit-includeでunitを絞ったり、prometheusのrelabel_configでよしなにdropするなどすると良いと思います(有償のマネージドサービスを利用する場合は特に)

収集されたメトリクスの可視化はGrafanaダッシュボードで行えます。

grafana.com

に基本的なものを用意してあります(とりあえずtimeseries graphに置き換えたので最近のGrafanaでも使えるはず)。

systemd-resolvedについて

systemd-resolved - ArchWiki

ローカル DNSタブリスナによるネットワーク名前解決をローカルアプリケーションに提供する systemd サービスです

少なくとも最近のubuntuではデフォルトで動作するようになっており、名前解決結果をキャッシュします。

k8s環境では残念ながらおそらくそれほど使われていない(node localなキャッシュは別で提供されますしね・・)ですが、VMで利用している方は意識せずとも使われてる環境が多いかと思います。

統計情報がD-Busインターフェース(とそれを利用するcli)により提供されています。

統計情報はカウンターなのでそのままでは扱いづらいですが、定期的に収集、rateを計算することでキャッシュヒット率など意味のある指標を得ることができます。

systemd_exporterの systemd.collector.enable-resolved optionはD-Bus経由で統計情報を取得します。

systemd_exporterとマージされるまでの流れ

なんというか特にすごい機能というわけではないものの、当初の実装からマージされてリリースされるまでだいぶかかったなーという思いの記録。

  • systemd_exporterはもともとPovilas Versockas氏により開発、メンテされていた
  • systemd-resolvedのメトリクス収集についてはOct 22, 2020からissueが立てられていた
  • Mar 5, 2022に個人で作成していたsystemd_resolved_exporterをベースにPR出そうか?と自分がコメントしたもののプロジェクトの状況が思わしくなさそうなので放置
  • そうこうしているうちに、レポジトリがprometheus-communityに移行する案が提案され、Jul 19, 2022に移行完了
  • コミュニティベースになったのなら今後も期待できるかなと思い、コメントをきっかけにsystemd-resolved対応のPRを出したのがDec 22, 2023、マージされたのがJan 12, 2024
  • 何回かメンテナ氏を突っついてみるもののしばらく音沙汰なし
  • 今日(2025-03-14)唐突に新バージョンがリリースされる

prometheus-community orgに移されたあとは割と期待したのですが、そうはいってもメンテナが少なくなかなかリリースサイクルがかかっている感じがします。

もうちょっと気軽にこのあたり関われればと思いつつ、なかなか悩ましいところ。

さておきこれで人にもすすめやすくなったなーと思うので、VM環境でPrometheus利用各位は是非お試しください🙌