blob: 99c3ca70f400b8dfa79b206ca3a1c43f98a91abd [file] [log] [blame]
Christopher Fauletf959d082019-02-07 15:38:42 +01001/*
2 * Promex is a Prometheus exporter for HAProxy
3 *
4 * It is highly inspired by the official Prometheus exporter.
5 * See: https://github.com/prometheus/haproxy_exporter
6 *
7 * Copyright 2019 Christopher Faulet <cfaulet@haproxy.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 */
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020018#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020019#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
William Dauchyde3c3262021-02-01 13:11:51 +010021#include <haproxy/check.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010022#include <haproxy/conn_stream.h>
23#include <haproxy/cs_utils.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020024#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020025#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020026#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020027#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020028#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020029#include <haproxy/htx.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020030#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020031#include <haproxy/listener.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020032#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020033#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020034#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020035#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020036#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020038#include <haproxy/task.h>
Willy Tarreau0fd04fd2021-05-08 12:58:12 +020039#include <haproxy/tools.h>
William Dauchy5a982a72021-01-08 13:18:06 +010040#include <haproxy/version.h>
Christopher Fauletf959d082019-02-07 15:38:42 +010041
42/* Prometheus exporter applet states (appctx->st0) */
43enum {
44 PROMEX_ST_INIT = 0, /* initialized */
45 PROMEX_ST_HEAD, /* send headers before dump */
46 PROMEX_ST_DUMP, /* dumping stats */
47 PROMEX_ST_DONE, /* finished */
Christopher Faulet9744f7c2019-03-27 15:48:53 +010048 PROMEX_ST_END, /* treatment terminated */
Christopher Fauletf959d082019-02-07 15:38:42 +010049};
50
51/* Prometheus exporter dumper states (appctx->st1) */
52enum {
William Dauchy69164222021-02-07 20:42:38 +010053 PROMEX_DUMPER_INIT = 0, /* initialized */
54 PROMEX_DUMPER_GLOBAL, /* dump metrics of globals */
55 PROMEX_DUMPER_FRONT, /* dump metrics of frontend proxies */
56 PROMEX_DUMPER_BACK, /* dump metrics of backend proxies */
57 PROMEX_DUMPER_LI, /* dump metrics of listeners */
58 PROMEX_DUMPER_SRV, /* dump metrics of servers */
59 PROMEX_DUMPER_STICKTABLE, /* dump metrics of stick tables */
60 PROMEX_DUMPER_DONE, /* finished */
Christopher Fauletf959d082019-02-07 15:38:42 +010061};
62
63/* Prometheus exporter flags (appctx->ctx.stats.flags) */
William Dauchy69164222021-02-07 20:42:38 +010064#define PROMEX_FL_METRIC_HDR 0x00000001
65#define PROMEX_FL_INFO_METRIC 0x00000002
66#define PROMEX_FL_FRONT_METRIC 0x00000004
67#define PROMEX_FL_BACK_METRIC 0x00000008
68#define PROMEX_FL_SRV_METRIC 0x00000010
William Dauchye3f7bd52021-02-14 23:22:56 +010069#define PROMEX_FL_LI_METRIC 0x00000020
70#define PROMEX_FL_STICKTABLE_METRIC 0x00000040
71#define PROMEX_FL_SCOPE_GLOBAL 0x00000080
72#define PROMEX_FL_SCOPE_FRONT 0x00000100
73#define PROMEX_FL_SCOPE_BACK 0x00000200
74#define PROMEX_FL_SCOPE_SERVER 0x00000400
75#define PROMEX_FL_SCOPE_LI 0x00000800
76#define PROMEX_FL_SCOPE_STICKTABLE 0x00001000
77#define PROMEX_FL_NO_MAINT_SRV 0x00002000
Christopher Faulet78407ce2019-11-18 14:47:08 +010078
William Dauchy69164222021-02-07 20:42:38 +010079#define PROMEX_FL_SCOPE_ALL (PROMEX_FL_SCOPE_GLOBAL | PROMEX_FL_SCOPE_FRONT | \
William Dauchye3f7bd52021-02-14 23:22:56 +010080 PROMEX_FL_SCOPE_LI | PROMEX_FL_SCOPE_BACK | \
81 PROMEX_FL_SCOPE_SERVER | PROMEX_FL_SCOPE_STICKTABLE)
Christopher Fauletf959d082019-02-07 15:38:42 +010082
Christopher Faulet0312c0d2021-01-20 15:19:12 +010083/* Promtheus metric type (gauge or counter) */
84enum promex_mt_type {
85 PROMEX_MT_GAUGE = 1,
86 PROMEX_MT_COUNTER = 2,
87};
88
Christopher Fauletf959d082019-02-07 15:38:42 +010089/* The max length for metrics name. It is a hard limit but it should be
Ilya Shipitsince7b00f2020-03-23 22:28:40 +050090 * enough.
Christopher Fauletf959d082019-02-07 15:38:42 +010091 */
92#define PROMEX_MAX_NAME_LEN 128
93
94/* The expected max length for a metric dump, including its header lines. It is
95 * just a soft limit to avoid extra work. We don't try to dump a metric if less
96 * than this size is available in the HTX.
97 */
98#define PROMEX_MAX_METRIC_LENGTH 512
99
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100100/* The max number of labels per metric */
101#define PROMEX_MAX_LABELS 8
William Dauchy5a982a72021-01-08 13:18:06 +0100102
Christopher Faulet0312c0d2021-01-20 15:19:12 +0100103/* Describe a prometheus metric */
104struct promex_metric {
105 const struct ist n; /* The metric name */
106 enum promex_mt_type type; /* The metric type (gauge or counter) */
107 unsigned int flags; /* PROMEX_FL_* flags */
108};
109
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100110/* Describe a prometheus metric label. It is just a key/value pair */
111struct promex_label {
112 struct ist name;
113 struct ist value;
114};
115
Christopher Faulet37286a52021-01-20 15:20:53 +0100116/* Global metrics */
117const struct promex_metric promex_global_metrics[INF_TOTAL_FIELDS] = {
118 //[INF_NAME] ignored
119 //[INF_VERSION], ignored
120 //[INF_RELEASE_DATE] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100121 [INF_NBTHREAD] = { .n = IST("nbthread"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
122 [INF_NBPROC] = { .n = IST("nbproc"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
123 [INF_PROCESS_NUM] = { .n = IST("relative_process_id"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
124 //[INF_PID] ignored
125 //[INF_UPTIME] ignored
126 [INF_UPTIME_SEC] = { .n = IST("uptime_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
127 [INF_START_TIME_SEC] = { .n = IST("start_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100128 //[INF_MEMMAX_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100129 [INF_MEMMAX_BYTES] = { .n = IST("max_memory_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100130 //[INF_POOL_ALLOC_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100131 [INF_POOL_ALLOC_BYTES] = { .n = IST("pool_allocated_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100132 //[INF_POOL_USED_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100133 [INF_POOL_USED_BYTES] = { .n = IST("pool_used_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
134 [INF_POOL_FAILED] = { .n = IST("pool_failures_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
135 [INF_ULIMIT_N] = { .n = IST("max_fds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
136 [INF_MAXSOCK] = { .n = IST("max_sockets"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
137 [INF_MAXCONN] = { .n = IST("max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
138 [INF_HARD_MAXCONN] = { .n = IST("hard_max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
139 [INF_CURR_CONN] = { .n = IST("current_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
140 [INF_CUM_CONN] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
141 [INF_CUM_REQ] = { .n = IST("requests_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
142 [INF_MAX_SSL_CONNS] = { .n = IST("max_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
143 [INF_CURR_SSL_CONNS] = { .n = IST("current_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
144 [INF_CUM_SSL_CONNS] = { .n = IST("ssl_connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
145 [INF_MAXPIPES] = { .n = IST("max_pipes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
146 [INF_PIPES_USED] = { .n = IST("pipes_used_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
147 [INF_PIPES_FREE] = { .n = IST("pipes_free_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
148 [INF_CONN_RATE] = { .n = IST("current_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
149 [INF_CONN_RATE_LIMIT] = { .n = IST("limit_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
150 [INF_MAX_CONN_RATE] = { .n = IST("max_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
151 [INF_SESS_RATE] = { .n = IST("current_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
152 [INF_SESS_RATE_LIMIT] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
153 [INF_MAX_SESS_RATE] = { .n = IST("max_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
154 [INF_SSL_RATE] = { .n = IST("current_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
155 [INF_SSL_RATE_LIMIT] = { .n = IST("limit_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
156 [INF_MAX_SSL_RATE] = { .n = IST("max_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
157 [INF_SSL_FRONTEND_KEY_RATE] = { .n = IST("current_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
158 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .n = IST("max_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
159 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .n = IST("frontend_ssl_reuse"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
160 [INF_SSL_BACKEND_KEY_RATE] = { .n = IST("current_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
161 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .n = IST("max_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
162 [INF_SSL_CACHE_LOOKUPS] = { .n = IST("ssl_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
163 [INF_SSL_CACHE_MISSES] = { .n = IST("ssl_cache_misses_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
164 [INF_COMPRESS_BPS_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
165 [INF_COMPRESS_BPS_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
166 [INF_COMPRESS_BPS_RATE_LIM] = { .n = IST("limit_http_comp"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
167 [INF_ZLIB_MEM_USAGE] = { .n = IST("current_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
168 [INF_MAX_ZLIB_MEM_USAGE] = { .n = IST("max_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
169 [INF_TASKS] = { .n = IST("current_tasks"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
170 [INF_RUN_QUEUE] = { .n = IST("current_run_queue"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
171 [INF_IDLE_PCT] = { .n = IST("idle_time_percent"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
172 //[INF_NODE] ignored
173 //[INF_DESCRIPTION] ignored
174 [INF_STOPPING] = { .n = IST("stopping"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
175 [INF_JOBS] = { .n = IST("jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
176 [INF_UNSTOPPABLE_JOBS] = { .n = IST("unstoppable_jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
177 [INF_LISTENERS] = { .n = IST("listeners"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
178 [INF_ACTIVE_PEERS] = { .n = IST("active_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
179 [INF_CONNECTED_PEERS] = { .n = IST("connected_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
180 [INF_DROPPED_LOGS] = { .n = IST("dropped_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
181 [INF_BUSY_POLLING] = { .n = IST("busy_polling_enabled"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
182 [INF_FAILED_RESOLUTIONS] = { .n = IST("failed_resolutions"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
183 [INF_TOTAL_BYTES_OUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
184 [INF_TOTAL_SPLICED_BYTES_OUT] = { .n = IST("spliced_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
185 [INF_BYTES_OUT_RATE] = { .n = IST("bytes_out_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
186 //[INF_DEBUG_COMMANDS_ISSUED] ignored
William Dauchy7741c332021-02-01 13:11:57 +0100187 [INF_CUM_LOG_MSGS] = { .n = IST("recv_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100188 [INF_BUILD_INFO] = { .n = IST("build_info"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
Christopher Fauletf959d082019-02-07 15:38:42 +0100189};
190
Christopher Faulet37286a52021-01-20 15:20:53 +0100191/* frontend/backend/server fields */
192const struct promex_metric promex_st_metrics[ST_F_TOTAL_FIELDS] = {
William Dauchy42d7c402021-11-07 10:18:47 +0100193 //[ST_F_PXNAME] ignored
194 //[ST_F_SVNAME] ignored
195 [ST_F_QCUR] = { .n = IST("current_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
196 [ST_F_QMAX] = { .n = IST("max_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
197 [ST_F_SCUR] = { .n = IST("current_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
198 [ST_F_SMAX] = { .n = IST("max_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
199 [ST_F_SLIM] = { .n = IST("limit_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
200 [ST_F_STOT] = { .n = IST("sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
201 [ST_F_BIN] = { .n = IST("bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
202 [ST_F_BOUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
203 [ST_F_DREQ] = { .n = IST("requests_denied_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC ) },
204 [ST_F_DRESP] = { .n = IST("responses_denied_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
205 [ST_F_EREQ] = { .n = IST("request_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
206 [ST_F_ECON] = { .n = IST("connection_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
207 [ST_F_ERESP] = { .n = IST("response_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
208 [ST_F_WRETR] = { .n = IST("retry_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
209 [ST_F_WREDIS] = { .n = IST("redispatch_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
210 [ST_F_STATUS] = { .n = IST("status"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
211 [ST_F_WEIGHT] = { .n = IST("weight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
212 [ST_F_ACT] = { .n = IST("active_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
213 [ST_F_BCK] = { .n = IST("backup_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
214 [ST_F_CHKFAIL] = { .n = IST("check_failures_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_SRV_METRIC) },
215 [ST_F_CHKDOWN] = { .n = IST("check_up_down_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
216 [ST_F_LASTCHG] = { .n = IST("check_last_change_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
217 [ST_F_DOWNTIME] = { .n = IST("downtime_seconds_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
218 [ST_F_QLIMIT] = { .n = IST("queue_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
219 //[ST_F_PID] ignored
220 //[ST_F_IID] ignored
221 //[ST_F_SID] ignored
222 [ST_F_THROTTLE] = { .n = IST("current_throttle"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
223 [ST_F_LBTOT] = { .n = IST("loadbalanced_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
224 //[ST_F_TRACKED] ignored
225 //[ST_F_TYPE] ignored
226 //[ST_F_RATE] ignored
227 [ST_F_RATE_LIM] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
228 [ST_F_RATE_MAX] = { .n = IST("max_session_rate"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
229 [ST_F_CHECK_STATUS] = { .n = IST("check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
230 [ST_F_CHECK_CODE] = { .n = IST("check_code"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
231 [ST_F_CHECK_DURATION] = { .n = IST("check_duration_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
232 [ST_F_HRSP_1XX] = { .n = IST("http_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
233 [ST_F_HRSP_2XX] = { .n = IST("http_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
234 [ST_F_HRSP_3XX] = { .n = IST("http_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
235 [ST_F_HRSP_4XX] = { .n = IST("http_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
236 [ST_F_HRSP_5XX] = { .n = IST("http_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
237 [ST_F_HRSP_OTHER] = { .n = IST("http_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
238 //[ST_F_HANAFAIL] ignored
239 //[ST_F_REQ_RATE] ignored
240 [ST_F_REQ_RATE_MAX] = { .n = IST("http_requests_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
241 [ST_F_REQ_TOT] = { .n = IST("http_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
242 [ST_F_CLI_ABRT] = { .n = IST("client_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
243 [ST_F_SRV_ABRT] = { .n = IST("server_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
244 [ST_F_COMP_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
245 [ST_F_COMP_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
246 [ST_F_COMP_BYP] = { .n = IST("http_comp_bytes_bypassed_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
247 [ST_F_COMP_RSP] = { .n = IST("http_comp_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
248 [ST_F_LASTSESS] = { .n = IST("last_session_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
249 //[ST_F_LAST_CHK] ignored
250 //[ST_F_LAST_AGT] ignored
251 [ST_F_QTIME] = { .n = IST("queue_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
252 [ST_F_CTIME] = { .n = IST("connect_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
253 [ST_F_RTIME] = { .n = IST("response_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
254 [ST_F_TTIME] = { .n = IST("total_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
255 //[ST_F_AGENT_STATUS] ignored
256 //[ST_F_AGENT_CODE] ignored
257 //[ST_F_AGENT_DURATION] ignored
258 //[ST_F_CHECK_DESC] ignored
259 //[ST_F_AGENT_DESC] ignored
260 //[ST_F_CHECK_RISE] ignored
261 //[ST_F_CHECK_FALL] ignored
262 //[ST_F_CHECK_HEALTH] ignored
263 //[ST_F_AGENT_RISE] ignored
264 //[ST_F_AGENT_FALL] ignored
265 //[ST_F_AGENT_HEALTH] ignored
266 //[ST_F_ADDR] ignored
267 //[ST_F_COOKIE] ignored
268 //[ST_F_MODE] ignored
269 //[ST_F_ALGO] ignored
270 //[ST_F_CONN_RATE] ignored
271 [ST_F_CONN_RATE_MAX] = { .n = IST("connections_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
272 [ST_F_CONN_TOT] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
273 [ST_F_INTERCEPTED] = { .n = IST("intercepted_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
274 [ST_F_DCON] = { .n = IST("denied_connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
275 [ST_F_DSES] = { .n = IST("denied_sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
276 [ST_F_WREW] = { .n = IST("failed_header_rewriting_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
277 [ST_F_CONNECT] = { .n = IST("connection_attempts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
278 [ST_F_REUSE] = { .n = IST("connection_reuses_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
279 [ST_F_CACHE_LOOKUPS] = { .n = IST("http_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
280 [ST_F_CACHE_HITS] = { .n = IST("http_cache_hits_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
281 [ST_F_SRV_ICUR] = { .n = IST("idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
282 [ST_F_SRV_ILIM] = { .n = IST("idle_connections_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
283 [ST_F_QT_MAX] = { .n = IST("max_queue_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
284 [ST_F_CT_MAX] = { .n = IST("max_connect_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
285 [ST_F_RT_MAX] = { .n = IST("max_response_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
286 [ST_F_TT_MAX] = { .n = IST("max_total_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
287 [ST_F_EINT] = { .n = IST("internal_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
288 [ST_F_IDLE_CONN_CUR] = { .n = IST("unsafe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
289 [ST_F_SAFE_CONN_CUR] = { .n = IST("safe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
290 [ST_F_USED_CONN_CUR] = { .n = IST("used_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
291 [ST_F_NEED_CONN_EST] = { .n = IST("need_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
292 [ST_F_UWEIGHT] = { .n = IST("uweight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
293 [ST_F_AGG_SRV_CHECK_STATUS] = { .n = IST("agg_server_check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
Christopher Fauletf959d082019-02-07 15:38:42 +0100294};
295
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500296/* Description of overridden stats fields */
Christopher Fauletf959d082019-02-07 15:38:42 +0100297const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
William Dauchya1da7ba2021-02-01 13:11:52 +0100298 [ST_F_STATUS] = IST("Current status of the service, per state label value."),
William Dauchyde3c3262021-02-01 13:11:51 +0100299 [ST_F_CHECK_STATUS] = IST("Status of last health check, per state label value."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100300 [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."),
Christopher Faulet2711e512020-02-27 16:12:07 +0100301 [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100302 [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."),
303 [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."),
304 [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."),
305 [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100306 [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"),
307 [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"),
308 [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"),
309 [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100310};
311
William Dauchy69164222021-02-07 20:42:38 +0100312/* stick table base fields */
313enum sticktable_field {
314 STICKTABLE_SIZE = 0,
315 STICKTABLE_USED,
316 /* must always be the last one */
317 STICKTABLE_TOTAL_FIELDS
318};
319
320const struct promex_metric promex_sticktable_metrics[STICKTABLE_TOTAL_FIELDS] = {
321 [STICKTABLE_SIZE] = { .n = IST("size"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC },
322 [STICKTABLE_USED] = { .n = IST("used"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC },
323};
324
325/* stick table base description */
326const struct ist promex_sticktable_metric_desc[STICKTABLE_TOTAL_FIELDS] = {
327 [STICKTABLE_SIZE] = IST("Stick table size."),
328 [STICKTABLE_USED] = IST("Number of entries used in this stick table."),
329};
330
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100331/* Specific labels for all ST_F_HRSP_* fields */
332const struct ist promex_hrsp_code[1 + ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = {
333 [ST_F_HRSP_1XX - ST_F_HRSP_1XX] = IST("1xx"),
334 [ST_F_HRSP_2XX - ST_F_HRSP_1XX] = IST("2xx"),
335 [ST_F_HRSP_3XX - ST_F_HRSP_1XX] = IST("3xx"),
336 [ST_F_HRSP_4XX - ST_F_HRSP_1XX] = IST("4xx"),
337 [ST_F_HRSP_5XX - ST_F_HRSP_1XX] = IST("5xx"),
338 [ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = IST("other"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100339};
340
William Dauchy54938212021-01-27 22:40:16 +0100341enum promex_front_state {
342 PROMEX_FRONT_STATE_DOWN = 0,
343 PROMEX_FRONT_STATE_UP,
344
345 PROMEX_FRONT_STATE_COUNT /* must be last */
346};
347
348const struct ist promex_front_st[PROMEX_FRONT_STATE_COUNT] = {
349 [PROMEX_FRONT_STATE_DOWN] = IST("DOWN"),
350 [PROMEX_FRONT_STATE_UP] = IST("UP"),
351};
352
353enum promex_back_state {
354 PROMEX_BACK_STATE_DOWN = 0,
355 PROMEX_BACK_STATE_UP,
356
357 PROMEX_BACK_STATE_COUNT /* must be last */
358};
359
360const struct ist promex_back_st[PROMEX_BACK_STATE_COUNT] = {
361 [PROMEX_BACK_STATE_DOWN] = IST("DOWN"),
362 [PROMEX_BACK_STATE_UP] = IST("UP"),
363};
364
365enum promex_srv_state {
366 PROMEX_SRV_STATE_DOWN = 0,
367 PROMEX_SRV_STATE_UP,
368 PROMEX_SRV_STATE_MAINT,
369 PROMEX_SRV_STATE_DRAIN,
370 PROMEX_SRV_STATE_NOLB,
371
372 PROMEX_SRV_STATE_COUNT /* must be last */
373};
374
375const struct ist promex_srv_st[PROMEX_SRV_STATE_COUNT] = {
376 [PROMEX_SRV_STATE_DOWN] = IST("DOWN"),
377 [PROMEX_SRV_STATE_UP] = IST("UP"),
378 [PROMEX_SRV_STATE_MAINT] = IST("MAINT"),
379 [PROMEX_SRV_STATE_DRAIN] = IST("DRAIN"),
380 [PROMEX_SRV_STATE_NOLB] = IST("NOLB"),
381};
382
383/* Return the server status. */
384enum promex_srv_state promex_srv_status(struct server *sv)
Christopher Fauletf959d082019-02-07 15:38:42 +0100385{
William Dauchy54938212021-01-27 22:40:16 +0100386 int state = PROMEX_SRV_STATE_DOWN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100387
Christopher Fauletf959d082019-02-07 15:38:42 +0100388 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Dauchy54938212021-01-27 22:40:16 +0100389 state = PROMEX_SRV_STATE_UP;
Christopher Fauletf959d082019-02-07 15:38:42 +0100390 if (sv->cur_admin & SRV_ADMF_DRAIN)
William Dauchy54938212021-01-27 22:40:16 +0100391 state = PROMEX_SRV_STATE_DRAIN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100392 }
Christopher Fauletd45d1052019-09-06 16:10:19 +0200393 else if (sv->cur_state == SRV_ST_STOPPING)
William Dauchy54938212021-01-27 22:40:16 +0100394 state = PROMEX_SRV_STATE_NOLB;
Christopher Fauletd45d1052019-09-06 16:10:19 +0200395
396 if (sv->cur_admin & SRV_ADMF_MAINT)
William Dauchy54938212021-01-27 22:40:16 +0100397 state = PROMEX_SRV_STATE_MAINT;
Christopher Fauletf959d082019-02-07 15:38:42 +0100398
399 return state;
400}
401
402/* Convert a field to its string representation and write it in <out>, followed
403 * by a newline, if there is enough space. non-numeric value are converted in
William Dauchy18a2c6e2021-01-22 21:09:47 +0100404 * "NaN" because Prometheus only support numerical values (but it is unexepceted
Christopher Fauletf959d082019-02-07 15:38:42 +0100405 * to process this kind of value). It returns 1 on success. Otherwise, it
406 * returns 0. The buffer's length must not exceed <max> value.
407 */
408static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
409{
410 int ret = 0;
411
412 switch (field_format(f, 0)) {
William Dauchy18a2c6e2021-01-22 21:09:47 +0100413 case FF_EMPTY: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100414 case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break;
415 case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break;
416 case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break;
417 case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200418 case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break;
William Dauchy18a2c6e2021-01-22 21:09:47 +0100419 case FF_STR: ret = chunk_strcat(out, "NaN\n"); break;
420 default: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100421 }
422 if (!ret || out->data > max)
423 return 0;
424 return 1;
425}
426
Christopher Fauletf959d082019-02-07 15:38:42 +0100427/* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
428 * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
429 */
430static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
Christopher Faulet37286a52021-01-20 15:20:53 +0100431 const struct promex_metric *metric, const struct ist name,
432 struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100433{
Christopher Faulet37286a52021-01-20 15:20:53 +0100434 struct ist type;
William Dauchy82b2ce22021-02-01 13:11:55 +0100435 struct ist desc;
Christopher Faulet37286a52021-01-20 15:20:53 +0100436
437 switch (metric->type) {
438 case PROMEX_MT_COUNTER:
439 type = ist("counter");
440 break;
441 default:
442 type = ist("gauge");
443 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100444
William Dauchya191b772021-01-15 22:41:39 +0100445 if (istcat(out, ist("# HELP "), max) == -1 ||
446 istcat(out, name, max) == -1 ||
447 istcat(out, ist(" "), max) == -1)
448 goto full;
449
William Dauchy82b2ce22021-02-01 13:11:55 +0100450 if (metric->flags & PROMEX_FL_INFO_METRIC)
451 desc = ist(info_fields[appctx->st2].desc);
William Dauchy69164222021-02-07 20:42:38 +0100452 else if (metric->flags & PROMEX_FL_STICKTABLE_METRIC)
453 desc = promex_sticktable_metric_desc[appctx->st2];
William Dauchy82b2ce22021-02-01 13:11:55 +0100454 else if (!isttest(promex_st_metric_desc[appctx->st2]))
455 desc = ist(stat_fields[appctx->st2].desc);
456 else
457 desc = promex_st_metric_desc[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100458
William Dauchy82b2ce22021-02-01 13:11:55 +0100459 if (istcat(out, desc, max) == -1 ||
460 istcat(out, ist("\n# TYPE "), max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100461 istcat(out, name, max) == -1 ||
462 istcat(out, ist(" "), max) == -1 ||
Christopher Faulet37286a52021-01-20 15:20:53 +0100463 istcat(out, type, max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100464 istcat(out, ist("\n"), max) == -1)
465 goto full;
466
467 return 1;
468
469 full:
470 return 0;
471}
472
473/* Dump the line for <metric>. It starts by the metric name followed by its
474 * labels (proxy name, server name...) between braces and finally its value. If
475 * not already done, the header lines are dumped first. It returns 1 on
476 * success. Otherwise if <out> length exceeds <max>, it returns 0.
477 */
Christopher Faulet37286a52021-01-20 15:20:53 +0100478static int promex_dump_metric(struct appctx *appctx, struct htx *htx, struct ist prefix,
William Dauchyc6464592021-01-27 22:40:17 +0100479 const struct promex_metric *metric, struct field *val,
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100480 struct promex_label *labels, struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100481{
482 struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
483 size_t len = out->len;
484
485 if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
486 return 0;
487
Christopher Faulet37286a52021-01-20 15:20:53 +0100488 /* Fill the metric name */
489 istcat(&name, prefix, PROMEX_MAX_NAME_LEN);
490 istcat(&name, metric->n, PROMEX_MAX_NAME_LEN);
491
492
Christopher Fauletf959d082019-02-07 15:38:42 +0100493 if ((appctx->ctx.stats.flags & PROMEX_FL_METRIC_HDR) &&
Christopher Faulet37286a52021-01-20 15:20:53 +0100494 !promex_dump_metric_header(appctx, htx, metric, name, out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100495 goto full;
496
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100497 if (istcat(out, name, max) == -1)
498 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100499
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100500 if (isttest(labels[0].name)) {
501 int i;
502
503 if (istcat(out, ist("{"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100504 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100505
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100506 for (i = 0; isttest(labels[i].name); i++) {
507 if (!isttest(labels[i].value))
508 continue;
509
510 if ((i && istcat(out, ist(","), max) == -1) ||
511 istcat(out, labels[i].name, max) == -1 ||
512 istcat(out, ist("=\""), max) == -1 ||
513 istcat(out, labels[i].value, max) == -1 ||
514 istcat(out, ist("\""), max) == -1)
515 goto full;
516 }
517
518 if (istcat(out, ist("}"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100519 goto full;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100520
Christopher Fauletf959d082019-02-07 15:38:42 +0100521 }
522
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100523 if (istcat(out, ist(" "), max) == -1)
524 goto full;
525
Christopher Fauletf959d082019-02-07 15:38:42 +0100526 trash.data = out->len;
Christopher Faulet37286a52021-01-20 15:20:53 +0100527 if (!promex_metric_to_str(&trash, val, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100528 goto full;
529 out->len = trash.data;
530
531 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
532 return 1;
533 full:
534 // Restore previous length
535 out->len = len;
536 return 0;
537
538}
539
540
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500541/* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100542 * 0 if <htx> is full and -1 in case of any error. */
543static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
544{
545 static struct ist prefix = IST("haproxy_process_");
Christopher Faulet37286a52021-01-20 15:20:53 +0100546 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100547 struct channel *chn = cs_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +0100548 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200549 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100550 int ret = 1;
551
Willy Tarreau0b26b382021-05-08 07:43:53 +0200552 if (!stats_fill_info(info, INF_TOTAL_FIELDS, 0))
William Dauchy5d9b8f32021-01-11 20:07:49 +0100553 return -1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100554
Christopher Faulet37286a52021-01-20 15:20:53 +0100555 for (; appctx->st2 < INF_TOTAL_FIELDS; appctx->st2++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100556 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
557
Christopher Faulet37286a52021-01-20 15:20:53 +0100558 if (!(promex_global_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
559 continue;
560
Christopher Fauletf959d082019-02-07 15:38:42 +0100561 switch (appctx->st2) {
William Dauchy5a982a72021-01-08 13:18:06 +0100562 case INF_BUILD_INFO:
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100563 labels[0].name = ist("version");
564 labels[0].value = ist(HAPROXY_VERSION);
Christopher Faulet37286a52021-01-20 15:20:53 +0100565 val = mkf_u32(FN_GAUGE, 1);
William Dauchy5a982a72021-01-08 13:18:06 +0100566 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100567
568 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100569 val = info[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100570 }
571
William Dauchyc6464592021-01-27 22:40:17 +0100572 if (!promex_dump_metric(appctx, htx, prefix, &promex_global_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100573 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100574 goto full;
575
Christopher Fauletf959d082019-02-07 15:38:42 +0100576 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100577 }
578
579 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200580 if (out.len) {
581 if (!htx_add_data_atonce(htx, out))
582 return -1; /* Unexpected and unrecoverable error */
583 channel_add_input(chn, out.len);
584 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100585 return ret;
586 full:
587 ret = 0;
588 goto end;
589}
590
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500591/* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100592 * 0 if <htx> is full and -1 in case of any error. */
593static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
594{
595 static struct ist prefix = IST("haproxy_frontend_");
596 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100597 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100598 struct channel *chn = cs_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +0100599 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200600 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchyb9577452021-01-17 18:27:46 +0100601 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100602 int ret = 1;
William Dauchyc6464592021-01-27 22:40:17 +0100603 enum promex_front_state state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100604
Christopher Faulet37286a52021-01-20 15:20:53 +0100605 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
606 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
607 continue;
608
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200609 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100610 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
611
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200612 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100613
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100614 labels[0].name = ist("proxy");
615 labels[0].value = ist2(px->id, strlen(px->id));
616
Christopher Fauletf959d082019-02-07 15:38:42 +0100617 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200618 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100619 goto next_px;
620
William Dauchyb9577452021-01-17 18:27:46 +0100621 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
622 return -1;
623
Christopher Fauletf959d082019-02-07 15:38:42 +0100624 switch (appctx->st2) {
625 case ST_F_STATUS:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200626 state = !(px->flags & PR_FL_STOPPED);
Christopher Faulet040b1192021-02-01 15:05:21 +0100627 for (; appctx->ctx.stats.st_code < PROMEX_FRONT_STATE_COUNT; appctx->ctx.stats.st_code++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100628 labels[1].name = ist("state");
Christopher Faulet040b1192021-02-01 15:05:21 +0100629 labels[1].value = promex_front_st[appctx->ctx.stats.st_code];
630 val = mkf_u32(FO_STATUS, state == appctx->ctx.stats.st_code);
William Dauchyc6464592021-01-27 22:40:17 +0100631 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100632 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100633 goto full;
634 }
Christopher Faulet040b1192021-02-01 15:05:21 +0100635 appctx->ctx.stats.st_code = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100636 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100637 case ST_F_REQ_RATE_MAX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100638 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100639 case ST_F_INTERCEPTED:
Christopher Fauletf959d082019-02-07 15:38:42 +0100640 case ST_F_CACHE_LOOKUPS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100641 case ST_F_CACHE_HITS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100642 case ST_F_COMP_IN:
Christopher Fauletf959d082019-02-07 15:38:42 +0100643 case ST_F_COMP_OUT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100644 case ST_F_COMP_BYP:
William Dauchyb9577452021-01-17 18:27:46 +0100645 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100646 if (px->mode != PR_MODE_HTTP)
647 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100648 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100649 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100650 case ST_F_HRSP_1XX:
William Dauchyb9577452021-01-17 18:27:46 +0100651 case ST_F_HRSP_2XX:
652 case ST_F_HRSP_3XX:
653 case ST_F_HRSP_4XX:
654 case ST_F_HRSP_5XX:
655 case ST_F_HRSP_OTHER:
Christopher Fauletf959d082019-02-07 15:38:42 +0100656 if (px->mode != PR_MODE_HTTP)
657 goto next_px;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100658 if (appctx->st2 != ST_F_HRSP_1XX)
659 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100660 labels[1].name = ist("code");
661 labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
662 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100663 break;
664
665 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100666 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100667 }
668
William Dauchyc6464592021-01-27 22:40:17 +0100669 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100670 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100671 goto full;
672 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200673 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100674 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100675 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200676 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100677 }
678
679 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200680 if (out.len) {
681 if (!htx_add_data_atonce(htx, out))
682 return -1; /* Unexpected and unrecoverable error */
683 channel_add_input(chn, out.len);
684 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100685 return ret;
686 full:
687 ret = 0;
688 goto end;
689}
690
William Dauchye3f7bd52021-02-14 23:22:56 +0100691/* Dump listener metrics (prefixed by "haproxy_listen_"). It returns 1 on
692 * success, 0 if <htx> is full and -1 in case of any error. */
693static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx)
694{
695 static struct ist prefix = IST("haproxy_listener_");
696 struct proxy *px;
697 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100698 struct channel *chn = cs_ic(appctx->owner);
William Dauchye3f7bd52021-02-14 23:22:56 +0100699 struct ist out = ist2(trash.area, 0);
700 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
701 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
702 struct listener *li;
703 int ret = 1;
704 enum li_status status;
705
706 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
707 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
708 continue;
709
710 while (appctx->ctx.stats.obj1) {
711 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
712
713 px = appctx->ctx.stats.obj1;
714
715 labels[0].name = ist("proxy");
716 labels[0].value = ist2(px->id, strlen(px->id));
717
718 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200719 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
William Dauchye3f7bd52021-02-14 23:22:56 +0100720 goto next_px;
721
722 li = appctx->ctx.stats.obj2;
723 list_for_each_entry_from(li, &px->conf.listeners, by_fe) {
724
William Dauchye3f7bd52021-02-14 23:22:56 +0100725 if (!li->counters)
726 continue;
727
William Dauchybaf22732021-02-25 00:53:13 +0100728 labels[1].name = ist("listener");
729 labels[1].value = ist2(li->name, strlen(li->name));
730
William Dauchye3f7bd52021-02-14 23:22:56 +0100731 if (!stats_fill_li_stats(px, li, 0, stats,
732 ST_F_TOTAL_FIELDS, &(appctx->st2)))
733 return -1;
734
735 switch (appctx->st2) {
736 case ST_F_STATUS:
737 status = get_li_status(li);
738 for (; appctx->ctx.stats.st_code < LI_STATE_COUNT; appctx->ctx.stats.st_code++) {
739 val = mkf_u32(FO_STATUS, status == appctx->ctx.stats.st_code);
740 labels[2].name = ist("state");
741 labels[2].value = ist(li_status_st[appctx->ctx.stats.st_code]);
742 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
743 &val, labels, &out, max))
744 goto full;
745 }
746 appctx->ctx.stats.st_code = 0;
747 continue;
748 default:
749 val = stats[appctx->st2];
750 }
751
752 if (!promex_dump_metric(appctx, htx, prefix,
753 &promex_st_metrics[appctx->st2],
754 &val, labels, &out, max))
755 goto full;
756 }
757
758 next_px:
759 px = px->next;
760 appctx->ctx.stats.obj1 = px;
761 appctx->ctx.stats.obj2 = (px ? LIST_NEXT(&px->conf.listeners, struct listener *, by_fe) : NULL);
762 }
763 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
764 appctx->ctx.stats.obj1 = proxies_list;
765 appctx->ctx.stats.obj2 = LIST_NEXT(&proxies_list->conf.listeners, struct listener *, by_fe);
766 }
767
768 end:
769 if (out.len) {
770 if (!htx_add_data_atonce(htx, out))
771 return -1; /* Unexpected and unrecoverable error */
772 channel_add_input(chn, out.len);
773 }
774 return ret;
775 full:
776 appctx->ctx.stats.obj2 = li;
777 ret = 0;
778 goto end;
779}
780
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500781/* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100782 * 0 if <htx> is full and -1 in case of any error. */
783static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
784{
785 static struct ist prefix = IST("haproxy_backend_");
786 struct proxy *px;
William Dauchy42d7c402021-11-07 10:18:47 +0100787 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100788 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100789 struct channel *chn = cs_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +0100790 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200791 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchy3c6f0062021-01-25 17:29:02 +0100792 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100793 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200794 double secs;
William Dauchy42d7c402021-11-07 10:18:47 +0100795 enum promex_back_state bkd_state;
796 enum promex_srv_state srv_state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100797
Christopher Faulet37286a52021-01-20 15:20:53 +0100798 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
799 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
800 continue;
801
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200802 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100803 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
William Dauchy42d7c402021-11-07 10:18:47 +0100804 unsigned int srv_state_count[PROMEX_SRV_STATE_COUNT] = { 0 };
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100805
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200806 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100807
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100808 labels[0].name = ist("proxy");
809 labels[0].value = ist2(px->id, strlen(px->id));
810
Christopher Fauletf959d082019-02-07 15:38:42 +0100811 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200812 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100813 goto next_px;
814
William Dauchy3c6f0062021-01-25 17:29:02 +0100815 if (!stats_fill_be_stats(px, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
816 return -1;
817
Christopher Fauletf959d082019-02-07 15:38:42 +0100818 switch (appctx->st2) {
William Dauchy42d7c402021-11-07 10:18:47 +0100819 case ST_F_AGG_SRV_CHECK_STATUS:
820 if (!px->srv)
821 goto next_px;
822 sv = px->srv;
823 while (sv) {
824 srv_state = promex_srv_status(sv);
825 srv_state_count[srv_state] += 1;
826 sv = sv->next;
827 }
828 for (; appctx->ctx.stats.st_code < PROMEX_SRV_STATE_COUNT; appctx->ctx.stats.st_code++) {
829 val = mkf_u32(FN_GAUGE, srv_state_count[appctx->ctx.stats.st_code]);
830 labels[1].name = ist("state");
831 labels[1].value = promex_srv_st[appctx->ctx.stats.st_code];
832 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
833 &val, labels, &out, max))
834 goto full;
835 }
836 appctx->ctx.stats.st_code = 0;
837 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100838 case ST_F_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +0100839 bkd_state = ((px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0);
Christopher Faulet040b1192021-02-01 15:05:21 +0100840 for (; appctx->ctx.stats.st_code < PROMEX_BACK_STATE_COUNT; appctx->ctx.stats.st_code++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100841 labels[1].name = ist("state");
Christopher Faulet040b1192021-02-01 15:05:21 +0100842 labels[1].value = promex_back_st[appctx->ctx.stats.st_code];
William Dauchy42d7c402021-11-07 10:18:47 +0100843 val = mkf_u32(FO_STATUS, bkd_state == appctx->ctx.stats.st_code);
William Dauchyc6464592021-01-27 22:40:17 +0100844 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100845 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100846 goto full;
847 }
Christopher Faulet040b1192021-02-01 15:05:21 +0100848 appctx->ctx.stats.st_code = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100849 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100850 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200851 secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100852 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100853 break;
854 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200855 secs = (double)swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100856 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100857 break;
858 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200859 secs = (double)swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100860 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100861 break;
862 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200863 secs = (double)swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100864 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100865 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100866 case ST_F_QT_MAX:
867 secs = (double)px->be_counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100868 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100869 break;
870 case ST_F_CT_MAX:
871 secs = (double)px->be_counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100872 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100873 break;
874 case ST_F_RT_MAX:
875 secs = (double)px->be_counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100876 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100877 break;
878 case ST_F_TT_MAX:
879 secs = (double)px->be_counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100880 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100881 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100882 case ST_F_REQ_TOT:
William Dauchy3c6f0062021-01-25 17:29:02 +0100883 case ST_F_CACHE_LOOKUPS:
884 case ST_F_CACHE_HITS:
885 case ST_F_COMP_IN:
886 case ST_F_COMP_OUT:
887 case ST_F_COMP_BYP:
888 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100889 if (px->mode != PR_MODE_HTTP)
890 goto next_px;
William Dauchy3c6f0062021-01-25 17:29:02 +0100891 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100892 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100893 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100894 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100895 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100896 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100897 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100898 case ST_F_HRSP_OTHER:
899 if (px->mode != PR_MODE_HTTP)
900 goto next_px;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100901 if (appctx->st2 != ST_F_HRSP_1XX)
902 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100903 labels[1].name = ist("code");
904 labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
William Dauchy3c6f0062021-01-25 17:29:02 +0100905 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100906 break;
907
908 default:
William Dauchy3c6f0062021-01-25 17:29:02 +0100909 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100910 }
911
William Dauchy3c6f0062021-01-25 17:29:02 +0100912 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100913 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100914 goto full;
915 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200916 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100917 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100918 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200919 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100920 }
921
922 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200923 if (out.len) {
924 if (!htx_add_data_atonce(htx, out))
925 return -1; /* Unexpected and unrecoverable error */
926 channel_add_input(chn, out.len);
927 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100928 return ret;
929 full:
930 ret = 0;
931 goto end;
932}
933
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500934/* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100935 * 0 if <htx> is full and -1 in case of any error. */
936static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
937{
938 static struct ist prefix = IST("haproxy_server_");
939 struct proxy *px;
940 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100941 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100942 struct channel *chn = cs_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +0100943 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200944 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchybde2bf62021-01-25 17:29:04 +0100945 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100946 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200947 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100948 enum promex_srv_state state;
William Dauchyde3c3262021-02-01 13:11:51 +0100949 const char *check_state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100950
Christopher Faulet37286a52021-01-20 15:20:53 +0100951 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
952 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
953 continue;
954
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200955 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100956 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
957
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200958 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100959
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100960 labels[0].name = ist("proxy");
961 labels[0].value = ist2(px->id, strlen(px->id));
962
Christopher Fauletf959d082019-02-07 15:38:42 +0100963 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200964 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100965 goto next_px;
966
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200967 while (appctx->ctx.stats.obj2) {
968 sv = appctx->ctx.stats.obj2;
Christopher Fauletf959d082019-02-07 15:38:42 +0100969
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100970 labels[1].name = ist("server");
971 labels[1].value = ist2(sv->id, strlen(sv->id));
972
William Dauchybde2bf62021-01-25 17:29:04 +0100973 if (!stats_fill_sv_stats(px, sv, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
974 return -1;
975
Christopher Fauleteba22942019-11-19 14:18:24 +0100976 if ((appctx->ctx.stats.flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
977 goto next_sv;
978
Christopher Fauletf959d082019-02-07 15:38:42 +0100979 switch (appctx->st2) {
980 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100981 state = promex_srv_status(sv);
William Dauchy64a38052021-02-14 22:26:24 +0100982 for (; appctx->ctx.stats.st_code < PROMEX_SRV_STATE_COUNT; appctx->ctx.stats.st_code++) {
Christopher Faulet040b1192021-02-01 15:05:21 +0100983 val = mkf_u32(FO_STATUS, state == appctx->ctx.stats.st_code);
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100984 labels[2].name = ist("state");
Christopher Faulet040b1192021-02-01 15:05:21 +0100985 labels[2].value = promex_srv_st[appctx->ctx.stats.st_code];
William Dauchyc6464592021-01-27 22:40:17 +0100986 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100987 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100988 goto full;
989 }
Christopher Faulet040b1192021-02-01 15:05:21 +0100990 appctx->ctx.stats.st_code = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100991 goto next_sv;
Christopher Fauletf959d082019-02-07 15:38:42 +0100992 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200993 secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100994 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100995 break;
996 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200997 secs = (double)swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100998 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100999 break;
1000 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001001 secs = (double)swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001002 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001003 break;
1004 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001005 secs = (double)swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001006 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001007 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001008 case ST_F_QT_MAX:
1009 secs = (double)sv->counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001010 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001011 break;
1012 case ST_F_CT_MAX:
1013 secs = (double)sv->counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001014 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001015 break;
1016 case ST_F_RT_MAX:
1017 secs = (double)sv->counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001018 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001019 break;
1020 case ST_F_TT_MAX:
1021 secs = (double)sv->counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001022 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001023 break;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001024 case ST_F_CHECK_STATUS:
1025 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1026 goto next_sv;
William Dauchyde3c3262021-02-01 13:11:51 +01001027
Christopher Faulet040b1192021-02-01 15:05:21 +01001028 for (; appctx->ctx.stats.st_code < HCHK_STATUS_SIZE; appctx->ctx.stats.st_code++) {
1029 if (get_check_status_result(appctx->ctx.stats.st_code) < CHK_RES_FAILED)
William Dauchyde3c3262021-02-01 13:11:51 +01001030 continue;
Christopher Faulet040b1192021-02-01 15:05:21 +01001031 val = mkf_u32(FO_STATUS, sv->check.status == appctx->ctx.stats.st_code);
1032 check_state = get_check_status_info(appctx->ctx.stats.st_code);
William Dauchyde3c3262021-02-01 13:11:51 +01001033 labels[2].name = ist("state");
Tim Duesterhusb113b5c2021-09-15 13:58:44 +02001034 labels[2].value = ist(check_state);
William Dauchyde3c3262021-02-01 13:11:51 +01001035 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
1036 &val, labels, &out, max))
1037 goto full;
1038 }
Christopher Faulet040b1192021-02-01 15:05:21 +01001039 appctx->ctx.stats.st_code = 0;
William Dauchyde3c3262021-02-01 13:11:51 +01001040 goto next_sv;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001041 case ST_F_CHECK_CODE:
1042 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1043 goto next_sv;
Christopher Faulet37286a52021-01-20 15:20:53 +01001044 val = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code);
Christopher Fauletcf403f32019-11-21 14:35:46 +01001045 break;
Christopher Faulet2711e512020-02-27 16:12:07 +01001046 case ST_F_CHECK_DURATION:
1047 if (sv->check.status < HCHK_STATUS_CHECKED)
1048 goto next_sv;
1049 secs = (double)sv->check.duration / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001050 val = mkf_flt(FN_DURATION, secs);
Christopher Faulet2711e512020-02-27 16:12:07 +01001051 break;
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001052 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +01001053 if (px->mode != PR_MODE_HTTP)
1054 goto next_px;
William Dauchybde2bf62021-01-25 17:29:04 +01001055 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001056 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +01001057 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001058 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001059 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001060 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001061 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001062 case ST_F_HRSP_OTHER:
1063 if (px->mode != PR_MODE_HTTP)
1064 goto next_px;
Christopher Faulet32ef48e2021-02-01 14:55:37 +01001065 if (appctx->st2 != ST_F_HRSP_1XX)
1066 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001067 labels[2].name = ist("code");
1068 labels[2].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
William Dauchybde2bf62021-01-25 17:29:04 +01001069 val = stats[appctx->st2];
Christopher Fauletc55a6262020-07-10 15:39:39 +02001070 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001071
1072 default:
William Dauchybde2bf62021-01-25 17:29:04 +01001073 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001074 }
1075
William Dauchyc6464592021-01-27 22:40:17 +01001076 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001077 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +01001078 goto full;
Christopher Fauleteba22942019-11-19 14:18:24 +01001079 next_sv:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001080 appctx->ctx.stats.obj2 = sv->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001081 }
1082
1083 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001084 appctx->ctx.stats.obj1 = px->next;
1085 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001086 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001087 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001088 appctx->ctx.stats.obj1 = proxies_list;
1089 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001090 }
1091
1092
1093 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001094 if (out.len) {
1095 if (!htx_add_data_atonce(htx, out))
1096 return -1; /* Unexpected and unrecoverable error */
1097 channel_add_input(chn, out.len);
1098 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001099 return ret;
1100 full:
1101 ret = 0;
1102 goto end;
1103}
1104
William Dauchy69164222021-02-07 20:42:38 +01001105/* Dump stick table metrics (prefixed by "haproxy_sticktable_"). It returns 1 on success,
1106 * 0 if <htx> is full and -1 in case of any error. */
1107static int promex_dump_sticktable_metrics(struct appctx *appctx, struct htx *htx)
1108{
1109 static struct ist prefix = IST("haproxy_sticktable_");
1110 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +01001111 struct channel *chn = cs_ic(appctx->owner);
William Dauchy69164222021-02-07 20:42:38 +01001112 struct ist out = ist2(trash.area, 0);
1113 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
1114 int ret = 1;
1115 struct stktable *t;
1116
1117 for (; appctx->st2 < STICKTABLE_TOTAL_FIELDS; appctx->st2++) {
1118 if (!(promex_sticktable_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
1119 continue;
1120
1121 while (appctx->ctx.stats.obj1) {
1122 struct promex_label labels[PROMEX_MAX_LABELS - 1] = {};
1123
1124 t = appctx->ctx.stats.obj1;
1125 if (!t->size)
1126 goto next_px;
1127
1128 labels[0].name = ist("name");
1129 labels[0].value = ist2(t->id, strlen(t->id));
1130 labels[1].name = ist("type");
1131 labels[1].value = ist2(stktable_types[t->type].kw, strlen(stktable_types[t->type].kw));
1132 switch (appctx->st2) {
1133 case STICKTABLE_SIZE:
1134 val = mkf_u32(FN_GAUGE, t->size);
1135 break;
1136 case STICKTABLE_USED:
1137 val = mkf_u32(FN_GAUGE, t->current);
1138 break;
1139 default:
1140 goto next_px;
1141 }
1142
1143 if (!promex_dump_metric(appctx, htx, prefix,
1144 &promex_sticktable_metrics[appctx->st2],
1145 &val, labels, &out, max))
1146 goto full;
1147
1148 next_px:
1149 appctx->ctx.stats.obj1 = t->next;
1150 }
1151 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
1152 appctx->ctx.stats.obj1 = stktables_list;
1153 }
1154
1155 end:
1156 if (out.len) {
1157 if (!htx_add_data_atonce(htx, out))
1158 return -1; /* Unexpected and unrecoverable error */
1159 channel_add_input(chn, out.len);
1160 }
1161 return ret;
1162 full:
1163 ret = 0;
1164 goto end;
1165}
1166
Christopher Fauletf959d082019-02-07 15:38:42 +01001167/* Dump all metrics (global, frontends, backends and servers) depending on the
1168 * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001169 * -1 in case of any error.
1170 * Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
1171 * a pointer to the current server/listener. */
Christopher Faulet908628c2022-03-25 16:43:49 +01001172static int promex_dump_metrics(struct appctx *appctx, struct conn_stream *cs, struct htx *htx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001173{
1174 int ret;
1175
1176 switch (appctx->st1) {
1177 case PROMEX_DUMPER_INIT:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001178 appctx->ctx.stats.obj1 = NULL;
1179 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001180 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
Christopher Faulet040b1192021-02-01 15:05:21 +01001181 appctx->ctx.stats.st_code = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001182 appctx->st2 = INF_NAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001183 appctx->st1 = PROMEX_DUMPER_GLOBAL;
1184 /* fall through */
1185
1186 case PROMEX_DUMPER_GLOBAL:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001187 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_GLOBAL) {
1188 ret = promex_dump_global_metrics(appctx, htx);
1189 if (ret <= 0) {
1190 if (ret == -1)
1191 goto error;
1192 goto full;
1193 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001194 }
1195
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001196 appctx->ctx.stats.obj1 = proxies_list;
1197 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001198 appctx->ctx.stats.flags &= ~PROMEX_FL_INFO_METRIC;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001199 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC);
Christopher Faulet040b1192021-02-01 15:05:21 +01001200 appctx->ctx.stats.st_code = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001201 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001202 appctx->st1 = PROMEX_DUMPER_FRONT;
1203 /* fall through */
1204
1205 case PROMEX_DUMPER_FRONT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001206 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_FRONT) {
1207 ret = promex_dump_front_metrics(appctx, htx);
1208 if (ret <= 0) {
1209 if (ret == -1)
1210 goto error;
1211 goto full;
1212 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001213 }
1214
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001215 appctx->ctx.stats.obj1 = proxies_list;
William Dauchye3f7bd52021-02-14 23:22:56 +01001216 appctx->ctx.stats.obj2 = LIST_NEXT(&proxies_list->conf.listeners, struct listener *, by_fe);
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001217 appctx->ctx.stats.flags &= ~PROMEX_FL_FRONT_METRIC;
William Dauchye3f7bd52021-02-14 23:22:56 +01001218 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_LI_METRIC);
1219 appctx->ctx.stats.st_code = 0;
1220 appctx->st2 = ST_F_PXNAME;
1221 appctx->st1 = PROMEX_DUMPER_LI;
1222 /* fall through */
1223
1224 case PROMEX_DUMPER_LI:
1225 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_LI) {
1226 ret = promex_dump_listener_metrics(appctx, htx);
1227 if (ret <= 0) {
1228 if (ret == -1)
1229 goto error;
1230 goto full;
1231 }
1232 }
1233
1234 appctx->ctx.stats.obj1 = proxies_list;
1235 appctx->ctx.stats.obj2 = NULL;
1236 appctx->ctx.stats.flags &= ~PROMEX_FL_LI_METRIC;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001237 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC);
Christopher Faulet040b1192021-02-01 15:05:21 +01001238 appctx->ctx.stats.st_code = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001239 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001240 appctx->st1 = PROMEX_DUMPER_BACK;
1241 /* fall through */
1242
1243 case PROMEX_DUMPER_BACK:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001244 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_BACK) {
1245 ret = promex_dump_back_metrics(appctx, htx);
1246 if (ret <= 0) {
1247 if (ret == -1)
1248 goto error;
1249 goto full;
1250 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001251 }
1252
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001253 appctx->ctx.stats.obj1 = proxies_list;
1254 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001255 appctx->ctx.stats.flags &= ~PROMEX_FL_BACK_METRIC;
1256 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Faulet040b1192021-02-01 15:05:21 +01001257 appctx->ctx.stats.st_code = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001258 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001259 appctx->st1 = PROMEX_DUMPER_SRV;
1260 /* fall through */
1261
1262 case PROMEX_DUMPER_SRV:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001263 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_SERVER) {
1264 ret = promex_dump_srv_metrics(appctx, htx);
1265 if (ret <= 0) {
1266 if (ret == -1)
1267 goto error;
1268 goto full;
1269 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001270 }
1271
William Dauchy69164222021-02-07 20:42:38 +01001272 appctx->ctx.stats.obj1 = stktables_list;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001273 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001274 appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
William Dauchy69164222021-02-07 20:42:38 +01001275 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC);
1276 appctx->st2 = STICKTABLE_SIZE;
1277 appctx->st1 = PROMEX_DUMPER_STICKTABLE;
1278 /* fall through */
1279
1280 case PROMEX_DUMPER_STICKTABLE:
1281 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_STICKTABLE) {
1282 ret = promex_dump_sticktable_metrics(appctx, htx);
1283 if (ret <= 0) {
1284 if (ret == -1)
1285 goto error;
1286 goto full;
1287 }
1288 }
1289
1290 appctx->ctx.stats.obj1 = NULL;
1291 appctx->ctx.stats.obj2 = NULL;
1292 appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001293 appctx->st2 = 0;
1294 appctx->st1 = PROMEX_DUMPER_DONE;
1295 /* fall through */
1296
1297 case PROMEX_DUMPER_DONE:
1298 default:
1299 break;
1300 }
1301
1302 return 1;
1303
1304 full:
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001305 cs_rx_room_blk(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001306 return 0;
1307 error:
1308 /* unrecoverable error */
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001309 appctx->ctx.stats.obj1 = NULL;
1310 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletf959d082019-02-07 15:38:42 +01001311 appctx->ctx.stats.flags = 0;
1312 appctx->st2 = 0;
1313 appctx->st1 = PROMEX_DUMPER_DONE;
1314 return -1;
1315}
1316
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001317/* Parse the query string of request URI to filter the metrics. It returns 1 on
Christopher Faulet78407ce2019-11-18 14:47:08 +01001318 * success and -1 on error. */
Christopher Faulet908628c2022-03-25 16:43:49 +01001319static int promex_parse_uri(struct appctx *appctx, struct conn_stream *cs)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001320{
Christopher Faulet908628c2022-03-25 16:43:49 +01001321 struct channel *req = cs_oc(cs);
1322 struct channel *res = cs_ic(cs);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001323 struct htx *req_htx, *res_htx;
1324 struct htx_sl *sl;
William Dauchyc65f6562019-11-26 12:56:26 +01001325 char *p, *key, *value;
1326 const char *end;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001327 struct buffer *err;
1328 int default_scopes = PROMEX_FL_SCOPE_ALL;
1329 int len;
1330
1331 /* Get the query-string */
1332 req_htx = htxbuf(&req->buf);
1333 sl = http_get_stline(req_htx);
1334 if (!sl)
1335 goto error;
1336 p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?');
1337 if (!p)
1338 goto end;
1339 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001340
William Dauchyc65f6562019-11-26 12:56:26 +01001341 /* copy the query-string */
1342 len = end - p;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001343 chunk_reset(&trash);
1344 memcpy(trash.area, p, len);
1345 trash.area[len] = 0;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001346 p = trash.area;
William Dauchyc65f6562019-11-26 12:56:26 +01001347 end = trash.area + len;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001348
1349 /* Parse the query-string */
William Dauchyc65f6562019-11-26 12:56:26 +01001350 while (p < end && *p && *p != '#') {
1351 value = NULL;
1352
1353 /* decode parameter name */
1354 key = p;
1355 while (p < end && *p != '=' && *p != '&' && *p != '#')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001356 ++p;
William Dauchyc65f6562019-11-26 12:56:26 +01001357 /* found a value */
1358 if (*p == '=') {
1359 *(p++) = 0;
1360 value = p;
1361 }
1362 else if (*p == '&')
1363 *(p++) = 0;
1364 else if (*p == '#')
1365 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001366 len = url_decode(key, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001367 if (len == -1)
1368 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001369
William Dauchyc65f6562019-11-26 12:56:26 +01001370 /* decode value */
1371 if (value) {
1372 while (p < end && *p != '=' && *p != '&' && *p != '#')
1373 ++p;
1374 if (*p == '=')
1375 goto error;
1376 if (*p == '&')
1377 *(p++) = 0;
1378 else if (*p == '#')
1379 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001380 len = url_decode(value, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001381 if (len == -1)
1382 goto error;
1383 }
1384
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001385 if (strcmp(key, "scope") == 0) {
William Dauchyc65f6562019-11-26 12:56:26 +01001386 default_scopes = 0; /* at least a scope defined, unset default scopes */
1387 if (!value)
1388 goto error;
1389 else if (*value == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001390 appctx->ctx.stats.flags &= ~PROMEX_FL_SCOPE_ALL;
William Dauchyc65f6562019-11-26 12:56:26 +01001391 else if (*value == '*')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001392 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_ALL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001393 else if (strcmp(value, "global") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001394 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_GLOBAL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001395 else if (strcmp(value, "server") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001396 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_SERVER;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001397 else if (strcmp(value, "backend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001398 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_BACK;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001399 else if (strcmp(value, "frontend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001400 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_FRONT;
William Dauchye3f7bd52021-02-14 23:22:56 +01001401 else if (strcmp(value, "listener") == 0)
1402 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_LI;
William Dauchy69164222021-02-07 20:42:38 +01001403 else if (strcmp(value, "sticktable") == 0)
1404 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_STICKTABLE;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001405 else
1406 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001407 }
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001408 else if (strcmp(key, "no-maint") == 0)
Christopher Fauleteba22942019-11-19 14:18:24 +01001409 appctx->ctx.stats.flags |= PROMEX_FL_NO_MAINT_SRV;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001410 }
1411
1412 end:
1413 appctx->ctx.stats.flags |= default_scopes;
1414 return 1;
1415
1416 error:
1417 err = &http_err_chunks[HTTP_ERR_400];
1418 channel_erase(res);
1419 res->buf.data = b_data(err);
1420 memcpy(res->buf.area, b_head(err), b_data(err));
1421 res_htx = htx_from_buf(&res->buf);
1422 channel_add_input(res, res_htx->data);
1423 appctx->st0 = PROMEX_ST_END;
1424 return -1;
1425}
1426
Christopher Fauletf959d082019-02-07 15:38:42 +01001427/* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is
1428 * full. */
Christopher Faulet908628c2022-03-25 16:43:49 +01001429static int promex_send_headers(struct appctx *appctx, struct conn_stream *cs, struct htx *htx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001430{
Christopher Faulet908628c2022-03-25 16:43:49 +01001431 struct channel *chn = cs_ic(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001432 struct htx_sl *sl;
1433 unsigned int flags;
1434
1435 flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11|HTX_SL_F_XFER_ENC|HTX_SL_F_XFER_LEN|HTX_SL_F_CHNK);
1436 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
1437 if (!sl)
1438 goto full;
1439 sl->info.res.status = 200;
1440 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001441 !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) ||
1442 !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) ||
1443 !htx_add_endof(htx, HTX_BLK_EOH))
1444 goto full;
1445
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001446 channel_add_input(chn, htx->data);
Christopher Fauletf959d082019-02-07 15:38:42 +01001447 return 1;
1448 full:
1449 htx_reset(htx);
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001450 cs_rx_room_blk(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001451 return 0;
1452}
1453
1454/* The function returns 1 if the initialisation is complete, 0 if
1455 * an errors occurs and -1 if more data are required for initializing
1456 * the applet.
1457 */
Christopher Faulet4aa1d282022-01-13 16:01:35 +01001458static int promex_appctx_init(struct appctx *appctx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001459{
1460 appctx->st0 = PROMEX_ST_INIT;
1461 return 1;
1462}
1463
1464/* The main I/O handler for the promex applet. */
1465static void promex_appctx_handle_io(struct appctx *appctx)
1466{
Christopher Faulet908628c2022-03-25 16:43:49 +01001467 struct conn_stream *cs = appctx->owner;
1468 struct stream *s = __cs_strm(cs);
1469 struct channel *req = cs_oc(cs);
1470 struct channel *res = cs_ic(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001471 struct htx *req_htx, *res_htx;
1472 int ret;
1473
1474 res_htx = htx_from_buf(&res->buf);
Christopher Faulet62e75742022-03-31 09:16:34 +02001475 if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO))
Christopher Fauletf959d082019-02-07 15:38:42 +01001476 goto out;
1477
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001478 /* Check if the input buffer is available. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001479 if (!b_size(&res->buf)) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001480 cs_rx_room_blk(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001481 goto out;
1482 }
1483
1484 switch (appctx->st0) {
1485 case PROMEX_ST_INIT:
Christopher Faulet908628c2022-03-25 16:43:49 +01001486 ret = promex_parse_uri(appctx, cs);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001487 if (ret <= 0) {
1488 if (ret == -1)
1489 goto error;
1490 goto out;
1491 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001492 appctx->st0 = PROMEX_ST_HEAD;
1493 appctx->st1 = PROMEX_DUMPER_INIT;
1494 /* fall through */
1495
1496 case PROMEX_ST_HEAD:
Christopher Faulet908628c2022-03-25 16:43:49 +01001497 if (!promex_send_headers(appctx, cs, res_htx))
Christopher Fauletf959d082019-02-07 15:38:42 +01001498 goto out;
1499 appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP);
1500 /* fall through */
1501
1502 case PROMEX_ST_DUMP:
Christopher Faulet908628c2022-03-25 16:43:49 +01001503 ret = promex_dump_metrics(appctx, cs, res_htx);
Christopher Fauletf959d082019-02-07 15:38:42 +01001504 if (ret <= 0) {
1505 if (ret == -1)
1506 goto error;
1507 goto out;
1508 }
1509 appctx->st0 = PROMEX_ST_DONE;
1510 /* fall through */
1511
1512 case PROMEX_ST_DONE:
Christopher Fauletbe69cbd2022-04-07 10:19:46 +02001513 /* no more data are expected. If the response buffer is
1514 * empty, be sure to add something (EOT block in this
1515 * case) to have something to send. It is important to
1516 * be sure the EOM flags will be handled by the
1517 * endpoint.
1518 */
1519 if (htx_is_empty(res_htx)) {
1520 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001521 cs_rx_room_blk(cs);
Christopher Fauletbe69cbd2022-04-07 10:19:46 +02001522 goto out;
1523 }
1524 channel_add_input(res, 1);
1525 }
1526 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet908628c2022-03-25 16:43:49 +01001527 cs->endp->flags |= CS_EP_EOI;
Christopher Fauletbef64b22022-03-07 15:56:20 +01001528 res->flags |= CF_EOI;
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001529 appctx->st0 = PROMEX_ST_END;
1530 /* fall through */
Christopher Fauletf959d082019-02-07 15:38:42 +01001531
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001532 case PROMEX_ST_END:
1533 if (!(res->flags & CF_SHUTR)) {
1534 res->flags |= CF_READ_NULL;
Christopher Fauletda098e62022-03-31 17:44:45 +02001535 cs_shutr(cs);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001536 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001537 }
1538
Christopher Fauletf959d082019-02-07 15:38:42 +01001539 out:
1540 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001541
1542 /* eat the whole request */
1543 if (co_data(req)) {
1544 req_htx = htx_from_buf(&req->buf);
1545 co_htx_skip(req, req_htx, co_data(req));
1546 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001547 return;
1548
1549 error:
1550 res->flags |= CF_READ_NULL;
Christopher Fauletda098e62022-03-31 17:44:45 +02001551 cs_shutr(cs);
1552 cs_shutw(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001553}
1554
1555struct applet promex_applet = {
1556 .obj_type = OBJ_TYPE_APPLET,
1557 .name = "<PROMEX>", /* used for logging */
1558 .init = promex_appctx_init,
1559 .fct = promex_appctx_handle_io,
1560};
1561
1562static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px,
1563 struct act_rule *rule, char **err)
1564{
1565 /* Prometheus exporter service is only available on "http-request" rulesets */
1566 if (rule->from != ACT_F_HTTP_REQ) {
1567 memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets");
1568 return ACT_RET_PRS_ERR;
1569 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001570
1571 /* Add applet pointer in the rule. */
1572 rule->applet = promex_applet;
1573
1574 return ACT_RET_PRS_OK;
1575}
1576static void promex_register_build_options(void)
1577{
1578 char *ptr = NULL;
1579
1580 memprintf(&ptr, "Built with the Prometheus exporter as a service");
1581 hap_register_build_opts(ptr, 1);
1582}
1583
1584
1585static struct action_kw_list service_actions = { ILH, {
1586 { "prometheus-exporter", service_parse_prometheus_exporter },
1587 { /* END */ }
1588}};
1589
1590INITCALL1(STG_REGISTER, service_keywords_register, &service_actions);
1591INITCALL0(STG_REGISTER, promex_register_build_options);