]> git.kaiwu.me - haproxy.git/commitdiff
MINOR: sample: add the "master_id" and "worker_id" sample fetches
authorWilly Tarreau <w@1wt.eu>
Tue, 15 Sep 2026 11:48:11 +0000 (13:48 +0200)
committerWilly Tarreau <w@1wt.eu>
Wed, 16 Sep 2026 16:00:51 +0000 (18:00 +0200)
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.

doc/configuration.txt
src/sample.c

index 6662168f6e461562eb23fc6dc6b6cc2d50b9262d..47b31639fbdf079226a7e01816e5e80390c16648 100644 (file)
@@ -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 <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 <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>)                                     method
 nbsrv([<backend>])                                 integer
 pid                                                integer
@@ -24055,6 +24060,7 @@ uuid([<version>])                                  string
 var(<var-name>[,<default>])                        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>) : 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
 ----------------------------------
index 9d29baa57f97c1f279f63b2a39ae94d9a60e8e84..a39b5ce2e9ed43991a53ad2d39abdeee4fe84273 100644 (file)
@@ -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 },