Skip to content

Commit bbf5bdf

Browse files
erl-100yorickdowne
andauthored
Track eth-docker version in metrics (#2662)
* export eth-docker version * Scope Alloy Docker discovery to this install's own Compose project * update comment * Read eth-docker version from README.md instead of nearest git tag * add COMPOSE_PROJECT_NAME to .env and bump version * Move version metric out of ./.eth into ./prometheus * Make Alloy project-name scoping opt-in via ALLOY_FILTER_BY_PROJECT_NAME Replace the automatic COMPOSE_PROJECT_NAME pinning with an opt-in boolean. The old approach hijacked Compose's own COMPOSE_PROJECT_NAME and set it for everyone, risking broken volume references and changing the default scrape-all behavior. ALLOY_FILTER_BY_PROJECT_NAME (default false) now leaves collection unchanged. When true, ethd resolves this install's project name read-only via `docker compose config` and writes it to ALLOY_PROJECT_NAME, which config.alloy uses to scope Docker discovery. Alloy has no conditional expressions, so the decision is made in ethd and coalesced to ".*" when empty. COMPOSE_PROJECT_NAME is never touched. Also drop the version-metric write from start(); it belongs only in update(), where the version can actually change. * pr comments: read env var into __value, use ${__project_name} * Read NODE_EXPORTER_COLLECTOR_MOUNT_PATH into __value, not a local * Update ethd * Update ethd * Update ethd * Update ethd --------- Co-authored-by: erl-100 <erl-100> Co-authored-by: Yorick Downe <71337066+yorickdowne@users.noreply.github.com>
1 parent f487448 commit bbf5bdf

7 files changed

Lines changed: 98 additions & 3 deletions

File tree

alloy/config.alloy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,18 @@ discovery.docker "docker_containers" {
1111
discovery.relabel "docker_logs" {
1212
targets = discovery.docker.docker_containers.targets
1313

14+
// discovery.docker sees every container on the host, not just this
15+
// project's. ALLOY_PROJECT_NAME holds this install's Compose project name
16+
// when ALLOY_FILTER_BY_PROJECT_NAME=true (ethd resolves and sets it); we
17+
// then keep only our own, so multiple Eth Docker installs on one host don't
18+
// ship each other's logs. Empty (the default) coalesces to ".*", matching
19+
// every container on the host and preserving the scrape-all behavior.
20+
rule {
21+
source_labels = ["__meta_docker_container_label_com_docker_compose_project"]
22+
regex = coalesce(sys.env("ALLOY_PROJECT_NAME"), ".*")
23+
action = "keep"
24+
}
25+
1426
rule {
1527
source_labels = ["__meta_docker_container_label_logs_collect"]
1628
regex = "true"
@@ -37,6 +49,18 @@ loki.source.docker "docker_logs" {
3749
discovery.relabel "docker_metrics" {
3850
targets = discovery.docker.docker_containers.targets
3951

52+
// discovery.docker sees every container on the host, not just this
53+
// project's. ALLOY_PROJECT_NAME holds this install's Compose project name
54+
// when ALLOY_FILTER_BY_PROJECT_NAME=true (ethd resolves and sets it); we
55+
// then keep only our own, so multiple Eth Docker installs on one host don't
56+
// scrape/ship each other's metrics. Empty (the default) coalesces to ".*",
57+
// matching every container on the host and preserving the scrape-all behavior.
58+
rule {
59+
source_labels = ["__meta_docker_container_label_com_docker_compose_project"]
60+
regex = coalesce(sys.env("ALLOY_PROJECT_NAME"), ".*")
61+
action = "keep"
62+
}
63+
4064
rule {
4165
source_labels = ["__meta_docker_container_label_metrics_scrape"]
4266
regex = "true"

default.env

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,10 +562,14 @@ DDNS_TAG=v2
562562
NODE_EXPORTER_COLLECTOR_MOUNT_PATH=
563563
# For the Node Dashboard, define a regex of mount points to ignore for the diskspace check.
564564
NODE_EXPORTER_IGNORE_MOUNT_REGEX='^/(dev|proc|sys|run|var/snap/.+|var/lib/docker.+)($|/)'
565+
# Set true to scope Alloy collection to this install's own containers, needed for a correct Eth Docker version metric when running multiple instances on one host. false scrapes every container
566+
ALLOY_FILTER_BY_PROJECT_NAME=false
565567
# And the Docker root so promtail scrapes logs from the right location. This is updated by ethd
566568
DOCKER_ROOT=/var/lib/docker
567569
# Docker socket location. Updated by ethd on Ubuntu/Debian; on macOS set to <home-dir>/.colima/default/docker.sock if using Colima
568570
DOCKER_SOCK=/var/run/docker.sock
571+
# This install's Compose project name, resolved by ethd when ALLOY_FILTER_BY_PROJECT_NAME is true. Updated by ethd - do not set by hand
572+
ALLOY_PROJECT_NAME=
569573

570574
# Used by ethd update - please do not adjust
571-
ENV_VERSION=61
575+
ENV_VERSION=62

ethd

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2698,6 +2698,8 @@ reset to defaults."
26982698
echo "Please make sure to run compatible client versions."
26992699
fi
27002700

2701+
__write_eth_docker_version_metric
2702+
27012703
if [[ ! "$OSTYPE" = "darwin"* ]]; then
27022704
# Release lock and remove lock file
27032705
exec 200<&-
@@ -4322,12 +4324,69 @@ __adjust_grandine_permissions() {
43224324
}
43234325

43244326

4327+
# Writes the currently checked-out Eth Docker version as a node-exporter
4328+
# textfile-collector metric (see grafana.yml), so it can be tracked the same
4329+
# way the EL/CL client versions already are. "tag" comes from README.md's
4330+
# "This is Eth Docker vX.Y.Z" line (the same source `version` reads). A
4331+
# release commit bumps that line straight to "vX.Y.Z-dev" for the next
4332+
# version, so this stays accurate on main between releases. Writes into
4333+
# the same host directory node-exporter's textfile collector is already
4334+
# mounted from (NODE_EXPORTER_COLLECTOR_MOUNT_PATH, defaulting to
4335+
# ./prometheus/node-exporter-text) - that mount is read-only inside the
4336+
# container, so the file has to land there from the host side.
4337+
__write_eth_docker_version_metric() {
4338+
local tag
4339+
local commit
4340+
local var
4341+
local textfile_dir
4342+
4343+
tag=$(grep -m1 "^This is ${__project_name} v" README.md 2>/dev/null | sed -E "s/^This is ${__project_name} //") || tag=""
4344+
commit=$(${__as_owner} git rev-parse --short HEAD 2>/dev/null) || commit=""
4345+
4346+
var=NODE_EXPORTER_COLLECTOR_MOUNT_PATH
4347+
__get_value_from_env "${var}" "${__env_file}" "__value"
4348+
textfile_dir="${__value:-./prometheus/node-exporter-text}"
4349+
4350+
${__as_owner} mkdir -p "${textfile_dir}"
4351+
{
4352+
echo "# HELP eth_docker_version_info Currently checked out ${__project_name} version"
4353+
echo "# TYPE eth_docker_version_info gauge"
4354+
echo "eth_docker_version_info{tag=\"${tag}\",commit=\"${commit}\"} 1"
4355+
} | ${__as_owner} tee "${textfile_dir}/eth-docker-version.prom.tmp" >/dev/null
4356+
${__as_owner} mv "${textfile_dir}/eth-docker-version.prom.tmp" "${textfile_dir}/eth-docker-version.prom"
4357+
}
4358+
4359+
4360+
# Sets ALLOY_PROJECT_NAME in .env so config.alloy can scope Alloy's Docker
4361+
# service discovery to this install's own containers (see grafana.yml). When
4362+
# ALLOY_FILTER_BY_PROJECT_NAME is true, the name is read back from Compose's
4363+
# own resolution (docker compose config), never computed independently and
4364+
# never written to COMPOSE_PROJECT_NAME. Otherwise it is cleared, and Alloy
4365+
# coalesces the empty value to ".*" - the scrape-all default. Runs on every
4366+
# start so toggling ALLOY_FILTER_BY_PROJECT_NAME takes effect on the next up.
4367+
__set_alloy_project_name() {
4368+
local var
4369+
4370+
var=ALLOY_FILTER_BY_PROJECT_NAME
4371+
__get_value_from_env "${var}" "${__env_file}" "__value"
4372+
if [[ "${__value}" = "true" ]]; then
4373+
ALLOY_PROJECT_NAME=$(__docompose config 2>/dev/null | awk -F': ' '/^name:/{print $2; exit}') || ALLOY_PROJECT_NAME=""
4374+
else
4375+
ALLOY_PROJECT_NAME=""
4376+
fi
4377+
4378+
var=ALLOY_PROJECT_NAME
4379+
__update_value_in_env "${var}" "${!var}" "${__env_file}"
4380+
}
4381+
4382+
43254383
upgrade() {
43264384
update
43274385
}
43284386

43294387

43304388
start() {
4389+
__set_alloy_project_name
43314390
__adjust_grandine_permissions
43324391
__docompose up -d --remove-orphans "$@"
43334392
}

grafana-cloud.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ services:
5252
- /sys:/host/sys:ro,rslave
5353
- /etc/hostname:/etc/nodename:ro
5454
- /etc/localtime:/etc/localtime:ro
55-
- ${NODE_EXPORTER_COLLECTOR_MOUNT_PATH:-/tmp/dummy-nodeexp-text}:/tmp/text-collector:ro
55+
- ${NODE_EXPORTER_COLLECTOR_MOUNT_PATH:-./prometheus/node-exporter-text}:/tmp/text-collector:ro
5656
<<: *logging
5757
labels:
5858
- metrics.scrape=true
@@ -120,6 +120,8 @@ services:
120120
alloy:
121121
image: grafana/alloy:latest
122122
restart: unless-stopped
123+
environment:
124+
- ALLOY_PROJECT_NAME=${ALLOY_PROJECT_NAME:-}
123125
volumes:
124126
- alloy-data:/var/lib/alloy
125127
- ./alloy:/etc/alloy

grafana-rootless.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ services:
9494
alloy:
9595
image: grafana/alloy:latest
9696
restart: unless-stopped
97+
environment:
98+
- ALLOY_PROJECT_NAME=${ALLOY_PROJECT_NAME:-}
9799
volumes:
98100
- alloy-data:/var/lib/alloy
99101
- ./alloy:/etc/alloy

grafana.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ services:
7575
- /sys:/host/sys:ro,rslave
7676
- /etc/hostname:/etc/nodename:ro
7777
- /etc/localtime:/etc/localtime:ro
78-
- ${NODE_EXPORTER_COLLECTOR_MOUNT_PATH:-/tmp/dummy-nodeexp-text}:/tmp/text-collector:ro
78+
- ${NODE_EXPORTER_COLLECTOR_MOUNT_PATH:-./prometheus/node-exporter-text}:/tmp/text-collector:ro
7979
command:
8080
- '--path.rootfs=/rootfs'
8181
- '--path.sysfs=/host/sys'
@@ -151,6 +151,8 @@ services:
151151
alloy:
152152
image: grafana/alloy:latest
153153
restart: unless-stopped
154+
environment:
155+
- ALLOY_PROJECT_NAME=${ALLOY_PROJECT_NAME:-}
154156
volumes:
155157
- alloy-data:/var/lib/alloy
156158
- ./alloy:/etc/alloy

prometheus/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
prometheus.yml
33
custom-prom.yml
44
conf.d
5+
# node-exporter textfile-collector metrics written by ethd
6+
node-exporter-text

0 commit comments

Comments
 (0)