From: Willy Tarreau Date: Tue, 15 Sep 2026 11:48:11 +0000 (+0200) Subject: MINOR: sample: add the "master_id" and "worker_id" sample fetches X-Git-Tag: v3.5-dev7~9 X-Git-Url: https://git.kaiwu.me/$%7BISSUES%7D/static/gitweb.js?a=commitdiff_plain;h=cca6bb4bf6bb347e6237177ac9bb29052f6be9a7;p=haproxy.git MINOR: sample: add the "master_id" and "worker_id" sample fetches These two new sample fetch functions return the unique identifiers of the master and of the current worker process, so that they can be placed in logs, in response headers, or be used in any other expression. The "master_id" one fails when there is no master, i.e. when not running in master-worker mode, so that a log or a header will simply not report anything there instead of an inaccurate value. Note that these functions rely on internal storage, not environment variables, so they are unaffected by "setenv" directives. --- diff --git a/doc/configuration.txt b/doc/configuration.txt index 6662168f6..47b31639f 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -1071,7 +1071,8 @@ The variables in question are the following: next master upon reload, so that it remains stable for the whole life of the master, and it is also exposed to the worker. Its format is the same as the one of the proxies' and servers' "guid": at most 127 characters among - letters, digits, '.', ':', '-' and '_'. + letters, digits, '.', ':', '-' and '_'. See also the "master_id" sample + fetch function. * HAPROXY_WORKER_ID: unique identifier of the worker process. It is randomly generated at boot on the worker process, and may be overwritten by the @@ -1082,7 +1083,8 @@ The variables in question are the following: set before the worker reads the rest of its configuration, so it may be referenced from there, and being part of the process' environment it is also visible to Lua (os.getenv()) and inherited by the processes launched - from the worker, such as external health checks. + from the worker, such as external health checks. See also the "worker_id" + sample fetch function. * HAPROXY_STARTUP_VERSION: contains the version used to start, in master- worker mode this is the version which was used to start the master, even @@ -3846,7 +3848,9 @@ worker-id worker-id "pop7-node1-inst3-${HAPROXY_WORKER_ID}" The identifier is exported as the HAPROXY_WORKER_ID environment variable (see - section 2.3). See also HAPROXY_MASTER_ID in section 2.3. + section 2.3) and is returned by the "worker_id" sample fetch function. See + also HAPROXY_MASTER_ID in section 2.3, and the "worker_id" sample fetch + function. wurfl-cache-size Sets the WURFL Useragent cache size. For faster lookups, already processed user @@ -24025,6 +24029,7 @@ last_rule_file string last_rule_line integer lat_ns_avg integer lat_ns_tot integer +master_id string meth() method nbsrv([]) integer pid integer @@ -24055,6 +24060,7 @@ uuid([]) string var([,]) undefined wait_end boolean waiting_entity string +worker_id string -------------------------------------------------+------------- Detailed list: @@ -24413,6 +24419,15 @@ lat_ns_tot : integer processing many HTTP chunks, and for this reason it is often preferred to log lat_ns_avg instead, which is a more relevant performance indicator. +master_id : string + Returns the unique identifier of the master process, as found in the + HAPROXY_MASTER_ID environment variable (see section 2.3). This identifier is + preserved across reloads, so it designates this installation for as long as + the master lives. This sample fetch function returns nothing when there is no + master, i.e. when not running in master-worker mode. See also "worker_id". + Note that this string has its own storage, and that changing the contents of + HAPROXY_MASTER_ID via "setenv" will not affect it. + meth() : method Returns a method. @@ -24726,6 +24741,21 @@ waiting_entity : string # Log the waiting entity, if any, and only if an error is reported log-format "$HAPROXY_HTTP_LOG_FMT %{Q}[waiting_entity,when(error)] +worker_id : string + Returns the unique identifier of the current worker process, either the one + set with the "worker-id" keyword in the global section, or the random one + which was generated at boot. In the latter case it changes on every reload, + so that a newly started worker may be told from the previous ones. Combined + with "master_id", which does not change across reloads, this permits an + external monitoring system to correlate the traffic it sees with the process + it collects its metrics from, for example: + + log-format "$HAPROXY_HTTP_LOG_FMT mid=%[master_id] wid=%[worker_id]" + + Note that this string has its own storage, and that changing the contents of + HAPROXY_WORKER_ID via "setenv" will not affect it. See also the "master_id" + sample fetch function, and the "worker-id" global keyword. + 7.3.3. Fetching samples at Layer 4 ---------------------------------- diff --git a/src/sample.c b/src/sample.c index 9d29baa57..a39b5ce2e 100644 --- a/src/sample.c +++ b/src/sample.c @@ -5177,6 +5177,36 @@ smp_fetch_hostname(const struct arg *args, struct sample *smp, const char *kw, v return 1; } +/* returns the unique identifier of the master process. Fails when there is no + * master, i.e. when not running in master-worker mode. + */ +static int +smp_fetch_master_id(const struct arg *args, struct sample *smp, const char *kw, void *private) +{ + if (!global.master_id) + return 0; + + smp->data.type = SMP_T_STR; + smp->flags = SMP_F_CONST; + smp->data.u.str.area = global.master_id; + smp->data.u.str.data = strlen(global.master_id); + return 1; +} + +/* returns the unique identifier of the current worker process */ +static int +smp_fetch_worker_id(const struct arg *args, struct sample *smp, const char *kw, void *private) +{ + if (!global.worker_id) + return 0; + + smp->data.type = SMP_T_STR; + smp->flags = SMP_F_CONST; + smp->data.u.str.area = global.worker_id; + smp->data.u.str.data = strlen(global.worker_id); + return 1; +} + /* returns the number of processes */ static int smp_fetch_nbproc(const struct arg *args, struct sample *smp, const char *kw, void *private) @@ -5834,6 +5864,7 @@ static struct sample_fetch_kw_list smp_kws = {ILH, { { "date", smp_fetch_date, ARG2(0,SINT,STR), smp_check_date_unit, SMP_T_SINT, SMP_USE_CONST }, { "date_us", smp_fetch_date_us, 0, NULL, SMP_T_SINT, SMP_USE_CONST }, { "hostname", smp_fetch_hostname, 0, NULL, SMP_T_STR, SMP_USE_CONST }, + { "master_id", smp_fetch_master_id, 0, NULL, SMP_T_STR, SMP_USE_CONST }, { "nbproc", smp_fetch_nbproc,0, NULL, SMP_T_SINT, SMP_USE_CONST }, { "pid", smp_fetch_pid, 0, NULL, SMP_T_SINT, SMP_USE_CONST }, { "proc", smp_fetch_proc, 0, NULL, SMP_T_SINT, SMP_USE_CONST }, @@ -5844,6 +5875,7 @@ static struct sample_fetch_kw_list smp_kws = {ILH, { { "stopping", smp_fetch_stopping, 0, NULL, SMP_T_BOOL, SMP_USE_INTRN }, { "uptime", smp_fetch_uptime, 0, NULL, SMP_T_SINT, SMP_USE_CONST }, { "uuid", smp_fetch_uuid, ARG1(0, SINT), smp_check_uuid, SMP_T_STR, SMP_USE_CONST }, + { "worker_id", smp_fetch_worker_id, 0, NULL, SMP_T_STR, SMP_USE_CONST }, { "cpu_calls", smp_fetch_cpu_calls, 0, NULL, SMP_T_SINT, SMP_USE_INTRN }, { "cpu_ns_avg", smp_fetch_cpu_ns_avg, 0, NULL, SMP_T_SINT, SMP_USE_INTRN },