blob: 5622b80390990a359a81eb074df4edc4465a633b [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>
Willy Tarreau762d7a52020-06-04 11:23:07 +020022#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020023#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020024#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020025#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020029#include <haproxy/listener.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020030#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020031#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020032#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020033#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020034#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020035#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020036#include <haproxy/stconn.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
Willy Tarreauae1747d2022-05-03 17:33:00 +020063/* Prometheus exporter flags (ctx->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
Willy Tarreauae1747d2022-05-03 17:33:00 +020083/* the context of the applet */
84struct promex_ctx {
85 struct proxy *px; /* current proxy */
86 struct stktable *st; /* current table */
87 struct listener *li; /* current listener */
88 struct server *sv; /* current server */
89 unsigned int flags; /* PROMEX_FL_* */
Willy Tarreaude58d242022-05-03 18:05:23 +020090 unsigned field_num; /* current field number (ST_F_* etc) */
Willy Tarreauae1747d2022-05-03 17:33:00 +020091 int obj_state; /* current state among PROMEX_{FRONT|BACK|SRV|LI}_STATE_* */
92};
93
Christopher Faulet0312c0d2021-01-20 15:19:12 +010094/* Promtheus metric type (gauge or counter) */
95enum promex_mt_type {
96 PROMEX_MT_GAUGE = 1,
97 PROMEX_MT_COUNTER = 2,
98};
99
Christopher Fauletf959d082019-02-07 15:38:42 +0100100/* The max length for metrics name. It is a hard limit but it should be
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500101 * enough.
Christopher Fauletf959d082019-02-07 15:38:42 +0100102 */
103#define PROMEX_MAX_NAME_LEN 128
104
105/* The expected max length for a metric dump, including its header lines. It is
106 * just a soft limit to avoid extra work. We don't try to dump a metric if less
107 * than this size is available in the HTX.
108 */
109#define PROMEX_MAX_METRIC_LENGTH 512
110
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100111/* The max number of labels per metric */
112#define PROMEX_MAX_LABELS 8
William Dauchy5a982a72021-01-08 13:18:06 +0100113
Christopher Faulet0312c0d2021-01-20 15:19:12 +0100114/* Describe a prometheus metric */
115struct promex_metric {
116 const struct ist n; /* The metric name */
117 enum promex_mt_type type; /* The metric type (gauge or counter) */
118 unsigned int flags; /* PROMEX_FL_* flags */
119};
120
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100121/* Describe a prometheus metric label. It is just a key/value pair */
122struct promex_label {
123 struct ist name;
124 struct ist value;
125};
126
Christopher Faulet37286a52021-01-20 15:20:53 +0100127/* Global metrics */
128const struct promex_metric promex_global_metrics[INF_TOTAL_FIELDS] = {
129 //[INF_NAME] ignored
130 //[INF_VERSION], ignored
131 //[INF_RELEASE_DATE] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100132 [INF_NBTHREAD] = { .n = IST("nbthread"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
133 [INF_NBPROC] = { .n = IST("nbproc"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
134 [INF_PROCESS_NUM] = { .n = IST("relative_process_id"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
135 //[INF_PID] ignored
136 //[INF_UPTIME] ignored
137 [INF_UPTIME_SEC] = { .n = IST("uptime_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
138 [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 +0100139 //[INF_MEMMAX_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100140 [INF_MEMMAX_BYTES] = { .n = IST("max_memory_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100141 //[INF_POOL_ALLOC_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100142 [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 +0100143 //[INF_POOL_USED_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100144 [INF_POOL_USED_BYTES] = { .n = IST("pool_used_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
145 [INF_POOL_FAILED] = { .n = IST("pool_failures_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
146 [INF_ULIMIT_N] = { .n = IST("max_fds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
147 [INF_MAXSOCK] = { .n = IST("max_sockets"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
148 [INF_MAXCONN] = { .n = IST("max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
149 [INF_HARD_MAXCONN] = { .n = IST("hard_max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
150 [INF_CURR_CONN] = { .n = IST("current_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
151 [INF_CUM_CONN] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
152 [INF_CUM_REQ] = { .n = IST("requests_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
153 [INF_MAX_SSL_CONNS] = { .n = IST("max_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
154 [INF_CURR_SSL_CONNS] = { .n = IST("current_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
155 [INF_CUM_SSL_CONNS] = { .n = IST("ssl_connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
156 [INF_MAXPIPES] = { .n = IST("max_pipes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
157 [INF_PIPES_USED] = { .n = IST("pipes_used_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
158 [INF_PIPES_FREE] = { .n = IST("pipes_free_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
159 [INF_CONN_RATE] = { .n = IST("current_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
160 [INF_CONN_RATE_LIMIT] = { .n = IST("limit_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
161 [INF_MAX_CONN_RATE] = { .n = IST("max_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
162 [INF_SESS_RATE] = { .n = IST("current_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
163 [INF_SESS_RATE_LIMIT] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
164 [INF_MAX_SESS_RATE] = { .n = IST("max_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
165 [INF_SSL_RATE] = { .n = IST("current_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
166 [INF_SSL_RATE_LIMIT] = { .n = IST("limit_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
167 [INF_MAX_SSL_RATE] = { .n = IST("max_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
168 [INF_SSL_FRONTEND_KEY_RATE] = { .n = IST("current_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
169 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .n = IST("max_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
170 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .n = IST("frontend_ssl_reuse"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
171 [INF_SSL_BACKEND_KEY_RATE] = { .n = IST("current_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
172 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .n = IST("max_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
173 [INF_SSL_CACHE_LOOKUPS] = { .n = IST("ssl_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
174 [INF_SSL_CACHE_MISSES] = { .n = IST("ssl_cache_misses_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
175 [INF_COMPRESS_BPS_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
176 [INF_COMPRESS_BPS_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
177 [INF_COMPRESS_BPS_RATE_LIM] = { .n = IST("limit_http_comp"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
178 [INF_ZLIB_MEM_USAGE] = { .n = IST("current_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
179 [INF_MAX_ZLIB_MEM_USAGE] = { .n = IST("max_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
180 [INF_TASKS] = { .n = IST("current_tasks"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
181 [INF_RUN_QUEUE] = { .n = IST("current_run_queue"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
182 [INF_IDLE_PCT] = { .n = IST("idle_time_percent"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
183 //[INF_NODE] ignored
184 //[INF_DESCRIPTION] ignored
185 [INF_STOPPING] = { .n = IST("stopping"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
186 [INF_JOBS] = { .n = IST("jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
187 [INF_UNSTOPPABLE_JOBS] = { .n = IST("unstoppable_jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
188 [INF_LISTENERS] = { .n = IST("listeners"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
189 [INF_ACTIVE_PEERS] = { .n = IST("active_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
190 [INF_CONNECTED_PEERS] = { .n = IST("connected_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
191 [INF_DROPPED_LOGS] = { .n = IST("dropped_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
192 [INF_BUSY_POLLING] = { .n = IST("busy_polling_enabled"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
193 [INF_FAILED_RESOLUTIONS] = { .n = IST("failed_resolutions"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
194 [INF_TOTAL_BYTES_OUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
195 [INF_TOTAL_SPLICED_BYTES_OUT] = { .n = IST("spliced_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
196 [INF_BYTES_OUT_RATE] = { .n = IST("bytes_out_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
197 //[INF_DEBUG_COMMANDS_ISSUED] ignored
William Dauchy7741c332021-02-01 13:11:57 +0100198 [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 +0100199 [INF_BUILD_INFO] = { .n = IST("build_info"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
Christopher Fauletf959d082019-02-07 15:38:42 +0100200};
201
Christopher Faulet37286a52021-01-20 15:20:53 +0100202/* frontend/backend/server fields */
203const struct promex_metric promex_st_metrics[ST_F_TOTAL_FIELDS] = {
William Dauchy42d7c402021-11-07 10:18:47 +0100204 //[ST_F_PXNAME] ignored
205 //[ST_F_SVNAME] ignored
206 [ST_F_QCUR] = { .n = IST("current_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
207 [ST_F_QMAX] = { .n = IST("max_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
208 [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) },
209 [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) },
210 [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) },
211 [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) },
212 [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) },
213 [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) },
214 [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 ) },
215 [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) },
216 [ST_F_EREQ] = { .n = IST("request_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
217 [ST_F_ECON] = { .n = IST("connection_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
218 [ST_F_ERESP] = { .n = IST("response_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
219 [ST_F_WRETR] = { .n = IST("retry_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
220 [ST_F_WREDIS] = { .n = IST("redispatch_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
221 [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) },
222 [ST_F_WEIGHT] = { .n = IST("weight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
223 [ST_F_ACT] = { .n = IST("active_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
224 [ST_F_BCK] = { .n = IST("backup_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
225 [ST_F_CHKFAIL] = { .n = IST("check_failures_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_SRV_METRIC) },
226 [ST_F_CHKDOWN] = { .n = IST("check_up_down_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
227 [ST_F_LASTCHG] = { .n = IST("check_last_change_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
228 [ST_F_DOWNTIME] = { .n = IST("downtime_seconds_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
229 [ST_F_QLIMIT] = { .n = IST("queue_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
230 //[ST_F_PID] ignored
231 //[ST_F_IID] ignored
232 //[ST_F_SID] ignored
233 [ST_F_THROTTLE] = { .n = IST("current_throttle"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
234 [ST_F_LBTOT] = { .n = IST("loadbalanced_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
235 //[ST_F_TRACKED] ignored
236 //[ST_F_TYPE] ignored
237 //[ST_F_RATE] ignored
238 [ST_F_RATE_LIM] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
239 [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) },
240 [ST_F_CHECK_STATUS] = { .n = IST("check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
241 [ST_F_CHECK_CODE] = { .n = IST("check_code"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
242 [ST_F_CHECK_DURATION] = { .n = IST("check_duration_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
243 [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) },
244 [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) },
245 [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) },
246 [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) },
247 [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) },
248 [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) },
249 //[ST_F_HANAFAIL] ignored
250 //[ST_F_REQ_RATE] ignored
251 [ST_F_REQ_RATE_MAX] = { .n = IST("http_requests_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
252 [ST_F_REQ_TOT] = { .n = IST("http_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
253 [ST_F_CLI_ABRT] = { .n = IST("client_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
254 [ST_F_SRV_ABRT] = { .n = IST("server_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
255 [ST_F_COMP_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
256 [ST_F_COMP_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
257 [ST_F_COMP_BYP] = { .n = IST("http_comp_bytes_bypassed_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
258 [ST_F_COMP_RSP] = { .n = IST("http_comp_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
259 [ST_F_LASTSESS] = { .n = IST("last_session_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
260 //[ST_F_LAST_CHK] ignored
261 //[ST_F_LAST_AGT] ignored
262 [ST_F_QTIME] = { .n = IST("queue_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
263 [ST_F_CTIME] = { .n = IST("connect_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
264 [ST_F_RTIME] = { .n = IST("response_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
265 [ST_F_TTIME] = { .n = IST("total_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
266 //[ST_F_AGENT_STATUS] ignored
267 //[ST_F_AGENT_CODE] ignored
268 //[ST_F_AGENT_DURATION] ignored
269 //[ST_F_CHECK_DESC] ignored
270 //[ST_F_AGENT_DESC] ignored
271 //[ST_F_CHECK_RISE] ignored
272 //[ST_F_CHECK_FALL] ignored
273 //[ST_F_CHECK_HEALTH] ignored
274 //[ST_F_AGENT_RISE] ignored
275 //[ST_F_AGENT_FALL] ignored
276 //[ST_F_AGENT_HEALTH] ignored
277 //[ST_F_ADDR] ignored
278 //[ST_F_COOKIE] ignored
279 //[ST_F_MODE] ignored
280 //[ST_F_ALGO] ignored
281 //[ST_F_CONN_RATE] ignored
282 [ST_F_CONN_RATE_MAX] = { .n = IST("connections_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
283 [ST_F_CONN_TOT] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
284 [ST_F_INTERCEPTED] = { .n = IST("intercepted_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
285 [ST_F_DCON] = { .n = IST("denied_connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
286 [ST_F_DSES] = { .n = IST("denied_sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
287 [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) },
288 [ST_F_CONNECT] = { .n = IST("connection_attempts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
289 [ST_F_REUSE] = { .n = IST("connection_reuses_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
290 [ST_F_CACHE_LOOKUPS] = { .n = IST("http_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
291 [ST_F_CACHE_HITS] = { .n = IST("http_cache_hits_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
292 [ST_F_SRV_ICUR] = { .n = IST("idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
293 [ST_F_SRV_ILIM] = { .n = IST("idle_connections_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
294 [ST_F_QT_MAX] = { .n = IST("max_queue_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
295 [ST_F_CT_MAX] = { .n = IST("max_connect_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
296 [ST_F_RT_MAX] = { .n = IST("max_response_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
297 [ST_F_TT_MAX] = { .n = IST("max_total_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
298 [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) },
299 [ST_F_IDLE_CONN_CUR] = { .n = IST("unsafe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
300 [ST_F_SAFE_CONN_CUR] = { .n = IST("safe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
301 [ST_F_USED_CONN_CUR] = { .n = IST("used_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
302 [ST_F_NEED_CONN_EST] = { .n = IST("need_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
303 [ST_F_UWEIGHT] = { .n = IST("uweight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
304 [ST_F_AGG_SRV_CHECK_STATUS] = { .n = IST("agg_server_check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
Cedric Paillet7d6644e2022-12-08 09:17:00 +0000305 [ST_F_AGG_SRV_STATUS ] = { .n = IST("agg_server_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
Cedric Paillete06e31e2022-12-08 09:17:01 +0000306 [ST_F_AGG_CHECK_STATUS] = { .n = IST("agg_check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
Christopher Fauletf959d082019-02-07 15:38:42 +0100307};
308
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500309/* Description of overridden stats fields */
Christopher Fauletf959d082019-02-07 15:38:42 +0100310const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
William Dauchya1da7ba2021-02-01 13:11:52 +0100311 [ST_F_STATUS] = IST("Current status of the service, per state label value."),
William Dauchyde3c3262021-02-01 13:11:51 +0100312 [ST_F_CHECK_STATUS] = IST("Status of last health check, per state label value."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100313 [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."),
Christopher Faulet2711e512020-02-27 16:12:07 +0100314 [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100315 [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."),
316 [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."),
317 [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."),
318 [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100319 [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"),
320 [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"),
321 [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"),
322 [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100323};
324
William Dauchy69164222021-02-07 20:42:38 +0100325/* stick table base fields */
326enum sticktable_field {
327 STICKTABLE_SIZE = 0,
328 STICKTABLE_USED,
329 /* must always be the last one */
330 STICKTABLE_TOTAL_FIELDS
331};
332
333const struct promex_metric promex_sticktable_metrics[STICKTABLE_TOTAL_FIELDS] = {
334 [STICKTABLE_SIZE] = { .n = IST("size"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC },
335 [STICKTABLE_USED] = { .n = IST("used"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC },
336};
337
338/* stick table base description */
339const struct ist promex_sticktable_metric_desc[STICKTABLE_TOTAL_FIELDS] = {
340 [STICKTABLE_SIZE] = IST("Stick table size."),
341 [STICKTABLE_USED] = IST("Number of entries used in this stick table."),
342};
343
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100344/* Specific labels for all ST_F_HRSP_* fields */
345const struct ist promex_hrsp_code[1 + ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = {
346 [ST_F_HRSP_1XX - ST_F_HRSP_1XX] = IST("1xx"),
347 [ST_F_HRSP_2XX - ST_F_HRSP_1XX] = IST("2xx"),
348 [ST_F_HRSP_3XX - ST_F_HRSP_1XX] = IST("3xx"),
349 [ST_F_HRSP_4XX - ST_F_HRSP_1XX] = IST("4xx"),
350 [ST_F_HRSP_5XX - ST_F_HRSP_1XX] = IST("5xx"),
351 [ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = IST("other"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100352};
353
William Dauchy54938212021-01-27 22:40:16 +0100354enum promex_front_state {
355 PROMEX_FRONT_STATE_DOWN = 0,
356 PROMEX_FRONT_STATE_UP,
357
358 PROMEX_FRONT_STATE_COUNT /* must be last */
359};
360
361const struct ist promex_front_st[PROMEX_FRONT_STATE_COUNT] = {
362 [PROMEX_FRONT_STATE_DOWN] = IST("DOWN"),
363 [PROMEX_FRONT_STATE_UP] = IST("UP"),
364};
365
366enum promex_back_state {
367 PROMEX_BACK_STATE_DOWN = 0,
368 PROMEX_BACK_STATE_UP,
369
370 PROMEX_BACK_STATE_COUNT /* must be last */
371};
372
373const struct ist promex_back_st[PROMEX_BACK_STATE_COUNT] = {
374 [PROMEX_BACK_STATE_DOWN] = IST("DOWN"),
375 [PROMEX_BACK_STATE_UP] = IST("UP"),
376};
377
378enum promex_srv_state {
379 PROMEX_SRV_STATE_DOWN = 0,
380 PROMEX_SRV_STATE_UP,
381 PROMEX_SRV_STATE_MAINT,
382 PROMEX_SRV_STATE_DRAIN,
383 PROMEX_SRV_STATE_NOLB,
384
385 PROMEX_SRV_STATE_COUNT /* must be last */
386};
387
388const struct ist promex_srv_st[PROMEX_SRV_STATE_COUNT] = {
389 [PROMEX_SRV_STATE_DOWN] = IST("DOWN"),
390 [PROMEX_SRV_STATE_UP] = IST("UP"),
391 [PROMEX_SRV_STATE_MAINT] = IST("MAINT"),
392 [PROMEX_SRV_STATE_DRAIN] = IST("DRAIN"),
393 [PROMEX_SRV_STATE_NOLB] = IST("NOLB"),
394};
395
396/* Return the server status. */
397enum promex_srv_state promex_srv_status(struct server *sv)
Christopher Fauletf959d082019-02-07 15:38:42 +0100398{
William Dauchy54938212021-01-27 22:40:16 +0100399 int state = PROMEX_SRV_STATE_DOWN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100400
Christopher Fauletf959d082019-02-07 15:38:42 +0100401 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Dauchy54938212021-01-27 22:40:16 +0100402 state = PROMEX_SRV_STATE_UP;
Christopher Fauletf959d082019-02-07 15:38:42 +0100403 if (sv->cur_admin & SRV_ADMF_DRAIN)
William Dauchy54938212021-01-27 22:40:16 +0100404 state = PROMEX_SRV_STATE_DRAIN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100405 }
Christopher Fauletd45d1052019-09-06 16:10:19 +0200406 else if (sv->cur_state == SRV_ST_STOPPING)
William Dauchy54938212021-01-27 22:40:16 +0100407 state = PROMEX_SRV_STATE_NOLB;
Christopher Fauletd45d1052019-09-06 16:10:19 +0200408
409 if (sv->cur_admin & SRV_ADMF_MAINT)
William Dauchy54938212021-01-27 22:40:16 +0100410 state = PROMEX_SRV_STATE_MAINT;
Christopher Fauletf959d082019-02-07 15:38:42 +0100411
412 return state;
413}
414
415/* Convert a field to its string representation and write it in <out>, followed
416 * by a newline, if there is enough space. non-numeric value are converted in
William Dauchy18a2c6e2021-01-22 21:09:47 +0100417 * "NaN" because Prometheus only support numerical values (but it is unexepceted
Christopher Fauletf959d082019-02-07 15:38:42 +0100418 * to process this kind of value). It returns 1 on success. Otherwise, it
419 * returns 0. The buffer's length must not exceed <max> value.
420 */
421static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
422{
423 int ret = 0;
424
425 switch (field_format(f, 0)) {
William Dauchy18a2c6e2021-01-22 21:09:47 +0100426 case FF_EMPTY: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100427 case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break;
428 case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break;
429 case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break;
430 case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200431 case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break;
William Dauchy18a2c6e2021-01-22 21:09:47 +0100432 case FF_STR: ret = chunk_strcat(out, "NaN\n"); break;
433 default: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100434 }
435 if (!ret || out->data > max)
436 return 0;
437 return 1;
438}
439
Christopher Fauletf959d082019-02-07 15:38:42 +0100440/* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
441 * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
442 */
443static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
Christopher Faulet37286a52021-01-20 15:20:53 +0100444 const struct promex_metric *metric, const struct ist name,
445 struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100446{
Willy Tarreaude58d242022-05-03 18:05:23 +0200447 struct promex_ctx *ctx = appctx->svcctx;
Christopher Faulet37286a52021-01-20 15:20:53 +0100448 struct ist type;
William Dauchy82b2ce22021-02-01 13:11:55 +0100449 struct ist desc;
Christopher Faulet37286a52021-01-20 15:20:53 +0100450
451 switch (metric->type) {
452 case PROMEX_MT_COUNTER:
453 type = ist("counter");
454 break;
455 default:
456 type = ist("gauge");
457 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100458
William Dauchya191b772021-01-15 22:41:39 +0100459 if (istcat(out, ist("# HELP "), max) == -1 ||
460 istcat(out, name, max) == -1 ||
461 istcat(out, ist(" "), max) == -1)
462 goto full;
463
William Dauchy82b2ce22021-02-01 13:11:55 +0100464 if (metric->flags & PROMEX_FL_INFO_METRIC)
Willy Tarreaude58d242022-05-03 18:05:23 +0200465 desc = ist(info_fields[ctx->field_num].desc);
William Dauchy69164222021-02-07 20:42:38 +0100466 else if (metric->flags & PROMEX_FL_STICKTABLE_METRIC)
Willy Tarreaude58d242022-05-03 18:05:23 +0200467 desc = promex_sticktable_metric_desc[ctx->field_num];
468 else if (!isttest(promex_st_metric_desc[ctx->field_num]))
469 desc = ist(stat_fields[ctx->field_num].desc);
William Dauchy82b2ce22021-02-01 13:11:55 +0100470 else
Willy Tarreaude58d242022-05-03 18:05:23 +0200471 desc = promex_st_metric_desc[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100472
William Dauchy82b2ce22021-02-01 13:11:55 +0100473 if (istcat(out, desc, max) == -1 ||
474 istcat(out, ist("\n# TYPE "), max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100475 istcat(out, name, max) == -1 ||
476 istcat(out, ist(" "), max) == -1 ||
Christopher Faulet37286a52021-01-20 15:20:53 +0100477 istcat(out, type, max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100478 istcat(out, ist("\n"), max) == -1)
479 goto full;
480
481 return 1;
482
483 full:
484 return 0;
485}
486
487/* Dump the line for <metric>. It starts by the metric name followed by its
488 * labels (proxy name, server name...) between braces and finally its value. If
489 * not already done, the header lines are dumped first. It returns 1 on
490 * success. Otherwise if <out> length exceeds <max>, it returns 0.
491 */
Christopher Faulet37286a52021-01-20 15:20:53 +0100492static int promex_dump_metric(struct appctx *appctx, struct htx *htx, struct ist prefix,
William Dauchyc6464592021-01-27 22:40:17 +0100493 const struct promex_metric *metric, struct field *val,
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100494 struct promex_label *labels, struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100495{
496 struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
Willy Tarreauae1747d2022-05-03 17:33:00 +0200497 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100498 size_t len = out->len;
499
500 if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
501 return 0;
502
Christopher Faulet37286a52021-01-20 15:20:53 +0100503 /* Fill the metric name */
504 istcat(&name, prefix, PROMEX_MAX_NAME_LEN);
505 istcat(&name, metric->n, PROMEX_MAX_NAME_LEN);
506
507
Willy Tarreauae1747d2022-05-03 17:33:00 +0200508 if ((ctx->flags & PROMEX_FL_METRIC_HDR) &&
Christopher Faulet37286a52021-01-20 15:20:53 +0100509 !promex_dump_metric_header(appctx, htx, metric, name, out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100510 goto full;
511
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100512 if (istcat(out, name, max) == -1)
513 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100514
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100515 if (isttest(labels[0].name)) {
516 int i;
517
518 if (istcat(out, ist("{"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100519 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100520
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100521 for (i = 0; isttest(labels[i].name); i++) {
522 if (!isttest(labels[i].value))
523 continue;
524
525 if ((i && istcat(out, ist(","), max) == -1) ||
526 istcat(out, labels[i].name, max) == -1 ||
527 istcat(out, ist("=\""), max) == -1 ||
528 istcat(out, labels[i].value, max) == -1 ||
529 istcat(out, ist("\""), max) == -1)
530 goto full;
531 }
532
533 if (istcat(out, ist("}"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100534 goto full;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100535
Christopher Fauletf959d082019-02-07 15:38:42 +0100536 }
537
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100538 if (istcat(out, ist(" "), max) == -1)
539 goto full;
540
Christopher Fauletf959d082019-02-07 15:38:42 +0100541 trash.data = out->len;
Christopher Faulet37286a52021-01-20 15:20:53 +0100542 if (!promex_metric_to_str(&trash, val, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100543 goto full;
544 out->len = trash.data;
545
Willy Tarreauae1747d2022-05-03 17:33:00 +0200546 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100547 return 1;
548 full:
549 // Restore previous length
550 out->len = len;
551 return 0;
552
553}
554
555
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500556/* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100557 * 0 if <htx> is full and -1 in case of any error. */
558static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
559{
560 static struct ist prefix = IST("haproxy_process_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200561 struct promex_ctx *ctx = appctx->svcctx;
Christopher Faulet37286a52021-01-20 15:20:53 +0100562 struct field val;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200563 struct channel *chn = sc_ic(appctx_sc(appctx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100564 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200565 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100566 int ret = 1;
567
Willy Tarreau0b26b382021-05-08 07:43:53 +0200568 if (!stats_fill_info(info, INF_TOTAL_FIELDS, 0))
William Dauchy5d9b8f32021-01-11 20:07:49 +0100569 return -1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100570
Willy Tarreaude58d242022-05-03 18:05:23 +0200571 for (; ctx->field_num < INF_TOTAL_FIELDS; ctx->field_num++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100572 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
573
Willy Tarreaude58d242022-05-03 18:05:23 +0200574 if (!(promex_global_metrics[ctx->field_num].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100575 continue;
576
Willy Tarreaude58d242022-05-03 18:05:23 +0200577 switch (ctx->field_num) {
William Dauchy5a982a72021-01-08 13:18:06 +0100578 case INF_BUILD_INFO:
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100579 labels[0].name = ist("version");
580 labels[0].value = ist(HAPROXY_VERSION);
Christopher Faulet37286a52021-01-20 15:20:53 +0100581 val = mkf_u32(FN_GAUGE, 1);
William Dauchy5a982a72021-01-08 13:18:06 +0100582 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100583
584 default:
Willy Tarreaude58d242022-05-03 18:05:23 +0200585 val = info[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100586 }
587
Willy Tarreaude58d242022-05-03 18:05:23 +0200588 if (!promex_dump_metric(appctx, htx, prefix, &promex_global_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100589 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100590 goto full;
591
Willy Tarreauae1747d2022-05-03 17:33:00 +0200592 ctx->flags |= PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100593 }
594
595 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200596 if (out.len) {
597 if (!htx_add_data_atonce(htx, out))
598 return -1; /* Unexpected and unrecoverable error */
599 channel_add_input(chn, out.len);
600 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100601 return ret;
602 full:
603 ret = 0;
604 goto end;
605}
606
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500607/* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100608 * 0 if <htx> is full and -1 in case of any error. */
609static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
610{
611 static struct ist prefix = IST("haproxy_frontend_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200612 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100613 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100614 struct field val;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200615 struct channel *chn = sc_ic(appctx_sc(appctx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100616 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200617 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchyb9577452021-01-17 18:27:46 +0100618 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100619 int ret = 1;
William Dauchyc6464592021-01-27 22:40:17 +0100620 enum promex_front_state state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100621
Willy Tarreaude58d242022-05-03 18:05:23 +0200622 for (;ctx->field_num < ST_F_TOTAL_FIELDS; ctx->field_num++) {
623 if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100624 continue;
625
Willy Tarreauae1747d2022-05-03 17:33:00 +0200626 while (ctx->px) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100627 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
628
Willy Tarreauae1747d2022-05-03 17:33:00 +0200629 px = ctx->px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100630
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100631 labels[0].name = ist("proxy");
632 labels[0].value = ist2(px->id, strlen(px->id));
633
Christopher Fauletf959d082019-02-07 15:38:42 +0100634 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200635 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100636 goto next_px;
637
Willy Tarreaude58d242022-05-03 18:05:23 +0200638 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(ctx->field_num)))
William Dauchyb9577452021-01-17 18:27:46 +0100639 return -1;
640
Willy Tarreaude58d242022-05-03 18:05:23 +0200641 switch (ctx->field_num) {
Christopher Fauletf959d082019-02-07 15:38:42 +0100642 case ST_F_STATUS:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200643 state = !(px->flags & PR_FL_STOPPED);
Willy Tarreauae1747d2022-05-03 17:33:00 +0200644 for (; ctx->obj_state < PROMEX_FRONT_STATE_COUNT; ctx->obj_state++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100645 labels[1].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200646 labels[1].value = promex_front_st[ctx->obj_state];
647 val = mkf_u32(FO_STATUS, state == ctx->obj_state);
Willy Tarreaude58d242022-05-03 18:05:23 +0200648 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100649 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100650 goto full;
651 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200652 ctx->obj_state = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100653 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100654 case ST_F_REQ_RATE_MAX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100655 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100656 case ST_F_INTERCEPTED:
Christopher Fauletf959d082019-02-07 15:38:42 +0100657 case ST_F_CACHE_LOOKUPS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100658 case ST_F_CACHE_HITS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100659 case ST_F_COMP_IN:
Christopher Fauletf959d082019-02-07 15:38:42 +0100660 case ST_F_COMP_OUT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100661 case ST_F_COMP_BYP:
William Dauchyb9577452021-01-17 18:27:46 +0100662 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100663 if (px->mode != PR_MODE_HTTP)
664 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +0200665 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100666 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100667 case ST_F_HRSP_1XX:
William Dauchyb9577452021-01-17 18:27:46 +0100668 case ST_F_HRSP_2XX:
669 case ST_F_HRSP_3XX:
670 case ST_F_HRSP_4XX:
671 case ST_F_HRSP_5XX:
672 case ST_F_HRSP_OTHER:
Christopher Fauletf959d082019-02-07 15:38:42 +0100673 if (px->mode != PR_MODE_HTTP)
674 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +0200675 if (ctx->field_num != ST_F_HRSP_1XX)
Willy Tarreauae1747d2022-05-03 17:33:00 +0200676 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100677 labels[1].name = ist("code");
Willy Tarreaude58d242022-05-03 18:05:23 +0200678 labels[1].value = promex_hrsp_code[ctx->field_num - ST_F_HRSP_1XX];
679 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100680 break;
681
682 default:
Willy Tarreaude58d242022-05-03 18:05:23 +0200683 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100684 }
685
Willy Tarreaude58d242022-05-03 18:05:23 +0200686 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100687 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100688 goto full;
689 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +0200690 ctx->px = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100691 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200692 ctx->flags |= PROMEX_FL_METRIC_HDR;
693 ctx->px = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100694 }
695
696 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200697 if (out.len) {
698 if (!htx_add_data_atonce(htx, out))
699 return -1; /* Unexpected and unrecoverable error */
700 channel_add_input(chn, out.len);
701 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100702 return ret;
703 full:
704 ret = 0;
705 goto end;
706}
707
William Dauchye3f7bd52021-02-14 23:22:56 +0100708/* Dump listener metrics (prefixed by "haproxy_listen_"). It returns 1 on
709 * success, 0 if <htx> is full and -1 in case of any error. */
710static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx)
711{
712 static struct ist prefix = IST("haproxy_listener_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200713 struct promex_ctx *ctx = appctx->svcctx;
William Dauchye3f7bd52021-02-14 23:22:56 +0100714 struct proxy *px;
715 struct field val;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200716 struct channel *chn = sc_ic(appctx_sc(appctx));
William Dauchye3f7bd52021-02-14 23:22:56 +0100717 struct ist out = ist2(trash.area, 0);
718 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
719 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
720 struct listener *li;
721 int ret = 1;
722 enum li_status status;
723
Willy Tarreaude58d242022-05-03 18:05:23 +0200724 for (;ctx->field_num < ST_F_TOTAL_FIELDS; ctx->field_num++) {
725 if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
William Dauchye3f7bd52021-02-14 23:22:56 +0100726 continue;
727
Willy Tarreauae1747d2022-05-03 17:33:00 +0200728 while (ctx->px) {
William Dauchye3f7bd52021-02-14 23:22:56 +0100729 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
730
Willy Tarreauae1747d2022-05-03 17:33:00 +0200731 px = ctx->px;
William Dauchye3f7bd52021-02-14 23:22:56 +0100732
733 labels[0].name = ist("proxy");
734 labels[0].value = ist2(px->id, strlen(px->id));
735
736 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200737 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
William Dauchye3f7bd52021-02-14 23:22:56 +0100738 goto next_px;
739
Willy Tarreauae1747d2022-05-03 17:33:00 +0200740 li = ctx->li;
William Dauchye3f7bd52021-02-14 23:22:56 +0100741 list_for_each_entry_from(li, &px->conf.listeners, by_fe) {
742
William Dauchye3f7bd52021-02-14 23:22:56 +0100743 if (!li->counters)
744 continue;
745
William Dauchybaf22732021-02-25 00:53:13 +0100746 labels[1].name = ist("listener");
747 labels[1].value = ist2(li->name, strlen(li->name));
748
William Dauchye3f7bd52021-02-14 23:22:56 +0100749 if (!stats_fill_li_stats(px, li, 0, stats,
Willy Tarreaude58d242022-05-03 18:05:23 +0200750 ST_F_TOTAL_FIELDS, &(ctx->field_num)))
William Dauchye3f7bd52021-02-14 23:22:56 +0100751 return -1;
752
Willy Tarreaude58d242022-05-03 18:05:23 +0200753 switch (ctx->field_num) {
William Dauchye3f7bd52021-02-14 23:22:56 +0100754 case ST_F_STATUS:
755 status = get_li_status(li);
Willy Tarreauae1747d2022-05-03 17:33:00 +0200756 for (; ctx->obj_state < LI_STATE_COUNT; ctx->obj_state++) {
757 val = mkf_u32(FO_STATUS, status == ctx->obj_state);
William Dauchye3f7bd52021-02-14 23:22:56 +0100758 labels[2].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200759 labels[2].value = ist(li_status_st[ctx->obj_state]);
Willy Tarreaude58d242022-05-03 18:05:23 +0200760 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
William Dauchye3f7bd52021-02-14 23:22:56 +0100761 &val, labels, &out, max))
762 goto full;
763 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200764 ctx->obj_state = 0;
William Dauchye3f7bd52021-02-14 23:22:56 +0100765 continue;
766 default:
Willy Tarreaude58d242022-05-03 18:05:23 +0200767 val = stats[ctx->field_num];
William Dauchye3f7bd52021-02-14 23:22:56 +0100768 }
769
770 if (!promex_dump_metric(appctx, htx, prefix,
Willy Tarreaude58d242022-05-03 18:05:23 +0200771 &promex_st_metrics[ctx->field_num],
William Dauchye3f7bd52021-02-14 23:22:56 +0100772 &val, labels, &out, max))
773 goto full;
774 }
775
776 next_px:
777 px = px->next;
Willy Tarreauae1747d2022-05-03 17:33:00 +0200778 ctx->px = px;
779 ctx->li = (px ? LIST_NEXT(&px->conf.listeners, struct listener *, by_fe) : NULL);
William Dauchye3f7bd52021-02-14 23:22:56 +0100780 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200781 ctx->flags |= PROMEX_FL_METRIC_HDR;
782 ctx->px = proxies_list;
783 ctx->li = LIST_NEXT(&proxies_list->conf.listeners, struct listener *, by_fe);
William Dauchye3f7bd52021-02-14 23:22:56 +0100784 }
785
786 end:
787 if (out.len) {
788 if (!htx_add_data_atonce(htx, out))
789 return -1; /* Unexpected and unrecoverable error */
790 channel_add_input(chn, out.len);
791 }
792 return ret;
793 full:
Willy Tarreauae1747d2022-05-03 17:33:00 +0200794 ctx->li = li;
William Dauchye3f7bd52021-02-14 23:22:56 +0100795 ret = 0;
796 goto end;
797}
798
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500799/* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100800 * 0 if <htx> is full and -1 in case of any error. */
801static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
802{
803 static struct ist prefix = IST("haproxy_backend_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200804 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100805 struct proxy *px;
William Dauchy42d7c402021-11-07 10:18:47 +0100806 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100807 struct field val;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200808 struct channel *chn = sc_ic(appctx_sc(appctx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100809 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200810 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchy3c6f0062021-01-25 17:29:02 +0100811 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100812 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200813 double secs;
William Dauchy42d7c402021-11-07 10:18:47 +0100814 enum promex_back_state bkd_state;
815 enum promex_srv_state srv_state;
Cedric Paillete06e31e2022-12-08 09:17:01 +0000816 enum healthcheck_status srv_check_status;
Christopher Fauletf959d082019-02-07 15:38:42 +0100817
Willy Tarreaude58d242022-05-03 18:05:23 +0200818 for (;ctx->field_num < ST_F_TOTAL_FIELDS; ctx->field_num++) {
819 if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100820 continue;
821
Willy Tarreauae1747d2022-05-03 17:33:00 +0200822 while (ctx->px) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100823 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
William Dauchy42d7c402021-11-07 10:18:47 +0100824 unsigned int srv_state_count[PROMEX_SRV_STATE_COUNT] = { 0 };
Cedric Paillete06e31e2022-12-08 09:17:01 +0000825 unsigned int srv_check_count[HCHK_STATUS_SIZE] = { 0 };
826 const char *check_state;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100827
Willy Tarreauae1747d2022-05-03 17:33:00 +0200828 px = ctx->px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100829
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100830 labels[0].name = ist("proxy");
831 labels[0].value = ist2(px->id, strlen(px->id));
832
Christopher Fauletf959d082019-02-07 15:38:42 +0100833 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200834 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100835 goto next_px;
836
Willy Tarreaude58d242022-05-03 18:05:23 +0200837 if (!stats_fill_be_stats(px, 0, stats, ST_F_TOTAL_FIELDS, &(ctx->field_num)))
William Dauchy3c6f0062021-01-25 17:29:02 +0100838 return -1;
839
Willy Tarreaude58d242022-05-03 18:05:23 +0200840 switch (ctx->field_num) {
Cedric Paillet7d6644e2022-12-08 09:17:00 +0000841 case ST_F_AGG_SRV_CHECK_STATUS: // DEPRECATED
842 case ST_F_AGG_SRV_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +0100843 if (!px->srv)
844 goto next_px;
845 sv = px->srv;
846 while (sv) {
847 srv_state = promex_srv_status(sv);
848 srv_state_count[srv_state] += 1;
849 sv = sv->next;
850 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200851 for (; ctx->obj_state < PROMEX_SRV_STATE_COUNT; ctx->obj_state++) {
852 val = mkf_u32(FN_GAUGE, srv_state_count[ctx->obj_state]);
William Dauchy42d7c402021-11-07 10:18:47 +0100853 labels[1].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200854 labels[1].value = promex_srv_st[ctx->obj_state];
Willy Tarreaude58d242022-05-03 18:05:23 +0200855 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
William Dauchy42d7c402021-11-07 10:18:47 +0100856 &val, labels, &out, max))
857 goto full;
858 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200859 ctx->obj_state = 0;
William Dauchy42d7c402021-11-07 10:18:47 +0100860 goto next_px;
Cedric Paillete06e31e2022-12-08 09:17:01 +0000861 case ST_F_AGG_CHECK_STATUS:
862 if (!px->srv)
863 goto next_px;
864 sv = px->srv;
865 while (sv) {
866 srv_check_status = sv->check.status;
867 srv_check_count[srv_check_status] += 1;
868 sv = sv->next;
869 }
870 for (; ctx->obj_state < HCHK_STATUS_SIZE; ctx->obj_state++) {
871 if (get_check_status_result(ctx->obj_state) < CHK_RES_FAILED)
872 continue;
873 val = mkf_u32(FO_STATUS, srv_check_count[ctx->obj_state]);
874 check_state = get_check_status_info(ctx->obj_state);
875 labels[1].name = ist("state");
876 labels[1].value = ist(check_state);
877 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
878 &val, labels, &out, max))
879 goto full;
880 }
881 ctx->obj_state = 0;
882 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100883 case ST_F_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +0100884 bkd_state = ((px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0);
Willy Tarreauae1747d2022-05-03 17:33:00 +0200885 for (; ctx->obj_state < PROMEX_BACK_STATE_COUNT; ctx->obj_state++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100886 labels[1].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200887 labels[1].value = promex_back_st[ctx->obj_state];
888 val = mkf_u32(FO_STATUS, bkd_state == ctx->obj_state);
Willy Tarreaude58d242022-05-03 18:05:23 +0200889 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100890 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100891 goto full;
892 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200893 ctx->obj_state = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100894 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100895 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200896 secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100897 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100898 break;
899 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200900 secs = (double)swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100901 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100902 break;
903 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200904 secs = (double)swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100905 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100906 break;
907 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200908 secs = (double)swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100909 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100910 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100911 case ST_F_QT_MAX:
912 secs = (double)px->be_counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100913 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100914 break;
915 case ST_F_CT_MAX:
916 secs = (double)px->be_counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100917 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100918 break;
919 case ST_F_RT_MAX:
920 secs = (double)px->be_counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100921 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100922 break;
923 case ST_F_TT_MAX:
924 secs = (double)px->be_counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100925 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100926 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100927 case ST_F_REQ_TOT:
William Dauchy3c6f0062021-01-25 17:29:02 +0100928 case ST_F_CACHE_LOOKUPS:
929 case ST_F_CACHE_HITS:
930 case ST_F_COMP_IN:
931 case ST_F_COMP_OUT:
932 case ST_F_COMP_BYP:
933 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100934 if (px->mode != PR_MODE_HTTP)
935 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +0200936 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100937 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100938 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100939 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100940 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100941 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100942 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100943 case ST_F_HRSP_OTHER:
944 if (px->mode != PR_MODE_HTTP)
945 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +0200946 if (ctx->field_num != ST_F_HRSP_1XX)
Willy Tarreauae1747d2022-05-03 17:33:00 +0200947 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100948 labels[1].name = ist("code");
Willy Tarreaude58d242022-05-03 18:05:23 +0200949 labels[1].value = promex_hrsp_code[ctx->field_num - ST_F_HRSP_1XX];
950 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100951 break;
952
953 default:
Willy Tarreaude58d242022-05-03 18:05:23 +0200954 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100955 }
956
Willy Tarreaude58d242022-05-03 18:05:23 +0200957 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100958 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100959 goto full;
960 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +0200961 ctx->px = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100962 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200963 ctx->flags |= PROMEX_FL_METRIC_HDR;
964 ctx->px = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100965 }
966
967 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200968 if (out.len) {
969 if (!htx_add_data_atonce(htx, out))
970 return -1; /* Unexpected and unrecoverable error */
971 channel_add_input(chn, out.len);
972 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100973 return ret;
974 full:
975 ret = 0;
976 goto end;
977}
978
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500979/* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100980 * 0 if <htx> is full and -1 in case of any error. */
981static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
982{
983 static struct ist prefix = IST("haproxy_server_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200984 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100985 struct proxy *px;
986 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100987 struct field val;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200988 struct channel *chn = sc_ic(appctx_sc(appctx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100989 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200990 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchybde2bf62021-01-25 17:29:04 +0100991 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100992 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200993 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100994 enum promex_srv_state state;
William Dauchyde3c3262021-02-01 13:11:51 +0100995 const char *check_state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100996
Willy Tarreaude58d242022-05-03 18:05:23 +0200997 for (;ctx->field_num < ST_F_TOTAL_FIELDS; ctx->field_num++) {
998 if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100999 continue;
1000
Willy Tarreauae1747d2022-05-03 17:33:00 +02001001 while (ctx->px) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001002 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
1003
Willy Tarreauae1747d2022-05-03 17:33:00 +02001004 px = ctx->px;
Christopher Fauletf959d082019-02-07 15:38:42 +01001005
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001006 labels[0].name = ist("proxy");
1007 labels[0].value = ist2(px->id, strlen(px->id));
1008
Christopher Fauletf959d082019-02-07 15:38:42 +01001009 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +02001010 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +01001011 goto next_px;
1012
Willy Tarreauae1747d2022-05-03 17:33:00 +02001013 while (ctx->sv) {
1014 sv = ctx->sv;
Christopher Fauletf959d082019-02-07 15:38:42 +01001015
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001016 labels[1].name = ist("server");
1017 labels[1].value = ist2(sv->id, strlen(sv->id));
1018
Willy Tarreaude58d242022-05-03 18:05:23 +02001019 if (!stats_fill_sv_stats(px, sv, 0, stats, ST_F_TOTAL_FIELDS, &(ctx->field_num)))
William Dauchybde2bf62021-01-25 17:29:04 +01001020 return -1;
1021
Willy Tarreauae1747d2022-05-03 17:33:00 +02001022 if ((ctx->flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
Christopher Fauleteba22942019-11-19 14:18:24 +01001023 goto next_sv;
1024
Willy Tarreaude58d242022-05-03 18:05:23 +02001025 switch (ctx->field_num) {
Christopher Fauletf959d082019-02-07 15:38:42 +01001026 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +01001027 state = promex_srv_status(sv);
Willy Tarreauae1747d2022-05-03 17:33:00 +02001028 for (; ctx->obj_state < PROMEX_SRV_STATE_COUNT; ctx->obj_state++) {
1029 val = mkf_u32(FO_STATUS, state == ctx->obj_state);
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001030 labels[2].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +02001031 labels[2].value = promex_srv_st[ctx->obj_state];
Willy Tarreaude58d242022-05-03 18:05:23 +02001032 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001033 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +01001034 goto full;
1035 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001036 ctx->obj_state = 0;
William Dauchyc6464592021-01-27 22:40:17 +01001037 goto next_sv;
Christopher Fauletf959d082019-02-07 15:38:42 +01001038 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001039 secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001040 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001041 break;
1042 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001043 secs = (double)swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001044 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001045 break;
1046 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001047 secs = (double)swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001048 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001049 break;
1050 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001051 secs = (double)swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001052 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001053 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001054 case ST_F_QT_MAX:
1055 secs = (double)sv->counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001056 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001057 break;
1058 case ST_F_CT_MAX:
1059 secs = (double)sv->counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001060 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001061 break;
1062 case ST_F_RT_MAX:
1063 secs = (double)sv->counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001064 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001065 break;
1066 case ST_F_TT_MAX:
1067 secs = (double)sv->counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001068 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001069 break;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001070 case ST_F_CHECK_STATUS:
1071 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1072 goto next_sv;
William Dauchyde3c3262021-02-01 13:11:51 +01001073
Willy Tarreauae1747d2022-05-03 17:33:00 +02001074 for (; ctx->obj_state < HCHK_STATUS_SIZE; ctx->obj_state++) {
1075 if (get_check_status_result(ctx->obj_state) < CHK_RES_FAILED)
William Dauchyde3c3262021-02-01 13:11:51 +01001076 continue;
Willy Tarreauae1747d2022-05-03 17:33:00 +02001077 val = mkf_u32(FO_STATUS, sv->check.status == ctx->obj_state);
1078 check_state = get_check_status_info(ctx->obj_state);
William Dauchyde3c3262021-02-01 13:11:51 +01001079 labels[2].name = ist("state");
Tim Duesterhusb113b5c2021-09-15 13:58:44 +02001080 labels[2].value = ist(check_state);
Willy Tarreaude58d242022-05-03 18:05:23 +02001081 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
William Dauchyde3c3262021-02-01 13:11:51 +01001082 &val, labels, &out, max))
1083 goto full;
1084 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001085 ctx->obj_state = 0;
William Dauchyde3c3262021-02-01 13:11:51 +01001086 goto next_sv;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001087 case ST_F_CHECK_CODE:
1088 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1089 goto next_sv;
Christopher Faulet37286a52021-01-20 15:20:53 +01001090 val = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code);
Christopher Fauletcf403f32019-11-21 14:35:46 +01001091 break;
Christopher Faulet2711e512020-02-27 16:12:07 +01001092 case ST_F_CHECK_DURATION:
1093 if (sv->check.status < HCHK_STATUS_CHECKED)
1094 goto next_sv;
1095 secs = (double)sv->check.duration / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001096 val = mkf_flt(FN_DURATION, secs);
Christopher Faulet2711e512020-02-27 16:12:07 +01001097 break;
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001098 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +01001099 if (px->mode != PR_MODE_HTTP)
1100 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +02001101 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +01001102 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +01001103 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001104 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001105 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001106 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001107 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001108 case ST_F_HRSP_OTHER:
1109 if (px->mode != PR_MODE_HTTP)
1110 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +02001111 if (ctx->field_num != ST_F_HRSP_1XX)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001112 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001113 labels[2].name = ist("code");
Willy Tarreaude58d242022-05-03 18:05:23 +02001114 labels[2].value = promex_hrsp_code[ctx->field_num - ST_F_HRSP_1XX];
1115 val = stats[ctx->field_num];
Christopher Fauletc55a6262020-07-10 15:39:39 +02001116 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001117
1118 default:
Willy Tarreaude58d242022-05-03 18:05:23 +02001119 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +01001120 }
1121
Willy Tarreaude58d242022-05-03 18:05:23 +02001122 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001123 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +01001124 goto full;
Christopher Fauleteba22942019-11-19 14:18:24 +01001125 next_sv:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001126 ctx->sv = sv->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001127 }
1128
1129 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001130 ctx->px = px->next;
1131 ctx->sv = (ctx->px ? ctx->px->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001132 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001133 ctx->flags |= PROMEX_FL_METRIC_HDR;
1134 ctx->px = proxies_list;
1135 ctx->sv = (ctx->px ? ctx->px->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001136 }
1137
1138
1139 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001140 if (out.len) {
1141 if (!htx_add_data_atonce(htx, out))
1142 return -1; /* Unexpected and unrecoverable error */
1143 channel_add_input(chn, out.len);
1144 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001145 return ret;
1146 full:
1147 ret = 0;
1148 goto end;
1149}
1150
William Dauchy69164222021-02-07 20:42:38 +01001151/* Dump stick table metrics (prefixed by "haproxy_sticktable_"). It returns 1 on success,
1152 * 0 if <htx> is full and -1 in case of any error. */
1153static int promex_dump_sticktable_metrics(struct appctx *appctx, struct htx *htx)
1154{
1155 static struct ist prefix = IST("haproxy_sticktable_");
Willy Tarreauae1747d2022-05-03 17:33:00 +02001156 struct promex_ctx *ctx = appctx->svcctx;
William Dauchy69164222021-02-07 20:42:38 +01001157 struct field val;
Willy Tarreauc12b3212022-05-27 11:08:15 +02001158 struct channel *chn = sc_ic(appctx_sc(appctx));
William Dauchy69164222021-02-07 20:42:38 +01001159 struct ist out = ist2(trash.area, 0);
1160 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
1161 int ret = 1;
1162 struct stktable *t;
1163
Willy Tarreaude58d242022-05-03 18:05:23 +02001164 for (; ctx->field_num < STICKTABLE_TOTAL_FIELDS; ctx->field_num++) {
1165 if (!(promex_sticktable_metrics[ctx->field_num].flags & ctx->flags))
William Dauchy69164222021-02-07 20:42:38 +01001166 continue;
1167
Willy Tarreauae1747d2022-05-03 17:33:00 +02001168 while (ctx->st) {
William Dauchy69164222021-02-07 20:42:38 +01001169 struct promex_label labels[PROMEX_MAX_LABELS - 1] = {};
1170
Willy Tarreauae1747d2022-05-03 17:33:00 +02001171 t = ctx->st;
William Dauchy69164222021-02-07 20:42:38 +01001172 if (!t->size)
1173 goto next_px;
1174
1175 labels[0].name = ist("name");
1176 labels[0].value = ist2(t->id, strlen(t->id));
1177 labels[1].name = ist("type");
1178 labels[1].value = ist2(stktable_types[t->type].kw, strlen(stktable_types[t->type].kw));
Willy Tarreaude58d242022-05-03 18:05:23 +02001179 switch (ctx->field_num) {
William Dauchy69164222021-02-07 20:42:38 +01001180 case STICKTABLE_SIZE:
1181 val = mkf_u32(FN_GAUGE, t->size);
1182 break;
1183 case STICKTABLE_USED:
1184 val = mkf_u32(FN_GAUGE, t->current);
1185 break;
1186 default:
1187 goto next_px;
1188 }
1189
1190 if (!promex_dump_metric(appctx, htx, prefix,
Willy Tarreaude58d242022-05-03 18:05:23 +02001191 &promex_sticktable_metrics[ctx->field_num],
William Dauchy69164222021-02-07 20:42:38 +01001192 &val, labels, &out, max))
1193 goto full;
1194
1195 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001196 ctx->st = t->next;
William Dauchy69164222021-02-07 20:42:38 +01001197 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001198 ctx->flags |= PROMEX_FL_METRIC_HDR;
1199 ctx->st = stktables_list;
William Dauchy69164222021-02-07 20:42:38 +01001200 }
1201
1202 end:
1203 if (out.len) {
1204 if (!htx_add_data_atonce(htx, out))
1205 return -1; /* Unexpected and unrecoverable error */
1206 channel_add_input(chn, out.len);
1207 }
1208 return ret;
1209 full:
1210 ret = 0;
1211 goto end;
1212}
1213
Christopher Fauletf959d082019-02-07 15:38:42 +01001214/* Dump all metrics (global, frontends, backends and servers) depending on the
1215 * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001216 * -1 in case of any error.
Willy Tarreauae1747d2022-05-03 17:33:00 +02001217 * Uses <appctx.ctx.stats.px> as a pointer to the current proxy and <sv>/<li>
1218 * as pointers to the current server/listener respectively.
1219 */
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001220static int promex_dump_metrics(struct appctx *appctx, struct stconn *sc, struct htx *htx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001221{
Willy Tarreauae1747d2022-05-03 17:33:00 +02001222 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +01001223 int ret;
1224
1225 switch (appctx->st1) {
1226 case PROMEX_DUMPER_INIT:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001227 ctx->px = NULL;
1228 ctx->st = NULL;
1229 ctx->li = NULL;
1230 ctx->sv = NULL;
1231 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
1232 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001233 ctx->field_num = INF_NAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001234 appctx->st1 = PROMEX_DUMPER_GLOBAL;
Willy Tarreau913bea52022-11-14 07:37:45 +01001235 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001236
1237 case PROMEX_DUMPER_GLOBAL:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001238 if (ctx->flags & PROMEX_FL_SCOPE_GLOBAL) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001239 ret = promex_dump_global_metrics(appctx, htx);
1240 if (ret <= 0) {
1241 if (ret == -1)
1242 goto error;
1243 goto full;
1244 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001245 }
1246
Willy Tarreauae1747d2022-05-03 17:33:00 +02001247 ctx->px = proxies_list;
1248 ctx->st = NULL;
1249 ctx->li = NULL;
1250 ctx->sv = NULL;
1251 ctx->flags &= ~PROMEX_FL_INFO_METRIC;
1252 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC);
1253 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001254 ctx->field_num = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001255 appctx->st1 = PROMEX_DUMPER_FRONT;
Willy Tarreau913bea52022-11-14 07:37:45 +01001256 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001257
1258 case PROMEX_DUMPER_FRONT:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001259 if (ctx->flags & PROMEX_FL_SCOPE_FRONT) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001260 ret = promex_dump_front_metrics(appctx, htx);
1261 if (ret <= 0) {
1262 if (ret == -1)
1263 goto error;
1264 goto full;
1265 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001266 }
1267
Willy Tarreauae1747d2022-05-03 17:33:00 +02001268 ctx->px = proxies_list;
1269 ctx->st = NULL;
1270 ctx->li = LIST_NEXT(&proxies_list->conf.listeners, struct listener *, by_fe);
1271 ctx->sv = NULL;
1272 ctx->flags &= ~PROMEX_FL_FRONT_METRIC;
1273 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_LI_METRIC);
1274 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001275 ctx->field_num = ST_F_PXNAME;
William Dauchye3f7bd52021-02-14 23:22:56 +01001276 appctx->st1 = PROMEX_DUMPER_LI;
Willy Tarreau913bea52022-11-14 07:37:45 +01001277 __fallthrough;
William Dauchye3f7bd52021-02-14 23:22:56 +01001278
1279 case PROMEX_DUMPER_LI:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001280 if (ctx->flags & PROMEX_FL_SCOPE_LI) {
William Dauchye3f7bd52021-02-14 23:22:56 +01001281 ret = promex_dump_listener_metrics(appctx, htx);
1282 if (ret <= 0) {
1283 if (ret == -1)
1284 goto error;
1285 goto full;
1286 }
1287 }
1288
Willy Tarreauae1747d2022-05-03 17:33:00 +02001289 ctx->px = proxies_list;
1290 ctx->st = NULL;
1291 ctx->li = NULL;
1292 ctx->sv = NULL;
1293 ctx->flags &= ~PROMEX_FL_LI_METRIC;
1294 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC);
1295 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001296 ctx->field_num = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001297 appctx->st1 = PROMEX_DUMPER_BACK;
Willy Tarreau913bea52022-11-14 07:37:45 +01001298 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001299
1300 case PROMEX_DUMPER_BACK:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001301 if (ctx->flags & PROMEX_FL_SCOPE_BACK) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001302 ret = promex_dump_back_metrics(appctx, htx);
1303 if (ret <= 0) {
1304 if (ret == -1)
1305 goto error;
1306 goto full;
1307 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001308 }
1309
Willy Tarreauae1747d2022-05-03 17:33:00 +02001310 ctx->px = proxies_list;
1311 ctx->st = NULL;
1312 ctx->li = NULL;
1313 ctx->sv = ctx->px ? ctx->px->srv : NULL;
1314 ctx->flags &= ~PROMEX_FL_BACK_METRIC;
1315 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
1316 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001317 ctx->field_num = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001318 appctx->st1 = PROMEX_DUMPER_SRV;
Willy Tarreau913bea52022-11-14 07:37:45 +01001319 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001320
1321 case PROMEX_DUMPER_SRV:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001322 if (ctx->flags & PROMEX_FL_SCOPE_SERVER) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001323 ret = promex_dump_srv_metrics(appctx, htx);
1324 if (ret <= 0) {
1325 if (ret == -1)
1326 goto error;
1327 goto full;
1328 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001329 }
1330
Willy Tarreauae1747d2022-05-03 17:33:00 +02001331 ctx->px = NULL;
1332 ctx->st = stktables_list;
1333 ctx->li = NULL;
1334 ctx->sv = NULL;
1335 ctx->flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
1336 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC);
Willy Tarreaude58d242022-05-03 18:05:23 +02001337 ctx->field_num = STICKTABLE_SIZE;
William Dauchy69164222021-02-07 20:42:38 +01001338 appctx->st1 = PROMEX_DUMPER_STICKTABLE;
Willy Tarreau913bea52022-11-14 07:37:45 +01001339 __fallthrough;
William Dauchy69164222021-02-07 20:42:38 +01001340
1341 case PROMEX_DUMPER_STICKTABLE:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001342 if (ctx->flags & PROMEX_FL_SCOPE_STICKTABLE) {
William Dauchy69164222021-02-07 20:42:38 +01001343 ret = promex_dump_sticktable_metrics(appctx, htx);
1344 if (ret <= 0) {
1345 if (ret == -1)
1346 goto error;
1347 goto full;
1348 }
1349 }
1350
Willy Tarreauae1747d2022-05-03 17:33:00 +02001351 ctx->px = NULL;
1352 ctx->st = NULL;
1353 ctx->li = NULL;
1354 ctx->sv = NULL;
1355 ctx->flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC);
Willy Tarreaude58d242022-05-03 18:05:23 +02001356 ctx->field_num = 0;
Christopher Fauletf959d082019-02-07 15:38:42 +01001357 appctx->st1 = PROMEX_DUMPER_DONE;
Willy Tarreau913bea52022-11-14 07:37:45 +01001358 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001359
1360 case PROMEX_DUMPER_DONE:
1361 default:
1362 break;
1363 }
1364
1365 return 1;
1366
1367 full:
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001368 sc_need_room(sc);
Christopher Fauletf959d082019-02-07 15:38:42 +01001369 return 0;
1370 error:
1371 /* unrecoverable error */
Willy Tarreauae1747d2022-05-03 17:33:00 +02001372 ctx->px = NULL;
1373 ctx->st = NULL;
1374 ctx->li = NULL;
1375 ctx->sv = NULL;
1376 ctx->flags = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001377 ctx->field_num = 0;
Christopher Fauletf959d082019-02-07 15:38:42 +01001378 appctx->st1 = PROMEX_DUMPER_DONE;
1379 return -1;
1380}
1381
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001382/* Parse the query string of request URI to filter the metrics. It returns 1 on
Christopher Faulet78407ce2019-11-18 14:47:08 +01001383 * success and -1 on error. */
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001384static int promex_parse_uri(struct appctx *appctx, struct stconn *sc)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001385{
Willy Tarreauae1747d2022-05-03 17:33:00 +02001386 struct promex_ctx *ctx = appctx->svcctx;
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001387 struct channel *req = sc_oc(sc);
1388 struct channel *res = sc_ic(sc);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001389 struct htx *req_htx, *res_htx;
1390 struct htx_sl *sl;
William Dauchyc65f6562019-11-26 12:56:26 +01001391 char *p, *key, *value;
1392 const char *end;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001393 struct buffer *err;
1394 int default_scopes = PROMEX_FL_SCOPE_ALL;
1395 int len;
1396
1397 /* Get the query-string */
1398 req_htx = htxbuf(&req->buf);
1399 sl = http_get_stline(req_htx);
1400 if (!sl)
1401 goto error;
1402 p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?');
1403 if (!p)
1404 goto end;
1405 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001406
William Dauchyc65f6562019-11-26 12:56:26 +01001407 /* copy the query-string */
1408 len = end - p;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001409 chunk_reset(&trash);
1410 memcpy(trash.area, p, len);
1411 trash.area[len] = 0;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001412 p = trash.area;
William Dauchyc65f6562019-11-26 12:56:26 +01001413 end = trash.area + len;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001414
1415 /* Parse the query-string */
William Dauchyc65f6562019-11-26 12:56:26 +01001416 while (p < end && *p && *p != '#') {
1417 value = NULL;
1418
1419 /* decode parameter name */
1420 key = p;
1421 while (p < end && *p != '=' && *p != '&' && *p != '#')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001422 ++p;
William Dauchyc65f6562019-11-26 12:56:26 +01001423 /* found a value */
1424 if (*p == '=') {
1425 *(p++) = 0;
1426 value = p;
1427 }
1428 else if (*p == '&')
1429 *(p++) = 0;
1430 else if (*p == '#')
1431 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001432 len = url_decode(key, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001433 if (len == -1)
1434 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001435
William Dauchyc65f6562019-11-26 12:56:26 +01001436 /* decode value */
1437 if (value) {
1438 while (p < end && *p != '=' && *p != '&' && *p != '#')
1439 ++p;
1440 if (*p == '=')
1441 goto error;
1442 if (*p == '&')
1443 *(p++) = 0;
1444 else if (*p == '#')
1445 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001446 len = url_decode(value, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001447 if (len == -1)
1448 goto error;
1449 }
1450
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001451 if (strcmp(key, "scope") == 0) {
William Dauchyc65f6562019-11-26 12:56:26 +01001452 default_scopes = 0; /* at least a scope defined, unset default scopes */
1453 if (!value)
1454 goto error;
1455 else if (*value == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001456 ctx->flags &= ~PROMEX_FL_SCOPE_ALL;
William Dauchyc65f6562019-11-26 12:56:26 +01001457 else if (*value == '*')
Willy Tarreauae1747d2022-05-03 17:33:00 +02001458 ctx->flags |= PROMEX_FL_SCOPE_ALL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001459 else if (strcmp(value, "global") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001460 ctx->flags |= PROMEX_FL_SCOPE_GLOBAL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001461 else if (strcmp(value, "server") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001462 ctx->flags |= PROMEX_FL_SCOPE_SERVER;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001463 else if (strcmp(value, "backend") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001464 ctx->flags |= PROMEX_FL_SCOPE_BACK;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001465 else if (strcmp(value, "frontend") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001466 ctx->flags |= PROMEX_FL_SCOPE_FRONT;
William Dauchye3f7bd52021-02-14 23:22:56 +01001467 else if (strcmp(value, "listener") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001468 ctx->flags |= PROMEX_FL_SCOPE_LI;
William Dauchy69164222021-02-07 20:42:38 +01001469 else if (strcmp(value, "sticktable") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001470 ctx->flags |= PROMEX_FL_SCOPE_STICKTABLE;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001471 else
1472 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001473 }
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001474 else if (strcmp(key, "no-maint") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001475 ctx->flags |= PROMEX_FL_NO_MAINT_SRV;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001476 }
1477
1478 end:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001479 ctx->flags |= default_scopes;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001480 return 1;
1481
1482 error:
1483 err = &http_err_chunks[HTTP_ERR_400];
1484 channel_erase(res);
1485 res->buf.data = b_data(err);
1486 memcpy(res->buf.area, b_head(err), b_data(err));
1487 res_htx = htx_from_buf(&res->buf);
1488 channel_add_input(res, res_htx->data);
1489 appctx->st0 = PROMEX_ST_END;
1490 return -1;
1491}
1492
Christopher Fauletf959d082019-02-07 15:38:42 +01001493/* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is
1494 * full. */
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001495static int promex_send_headers(struct appctx *appctx, struct stconn *sc, struct htx *htx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001496{
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001497 struct channel *chn = sc_ic(sc);
Christopher Fauletf959d082019-02-07 15:38:42 +01001498 struct htx_sl *sl;
1499 unsigned int flags;
1500
1501 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);
1502 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
1503 if (!sl)
1504 goto full;
1505 sl->info.res.status = 200;
1506 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001507 !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) ||
1508 !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) ||
1509 !htx_add_endof(htx, HTX_BLK_EOH))
1510 goto full;
1511
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001512 channel_add_input(chn, htx->data);
Christopher Fauletf959d082019-02-07 15:38:42 +01001513 return 1;
1514 full:
1515 htx_reset(htx);
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001516 sc_need_room(sc);
Christopher Fauletf959d082019-02-07 15:38:42 +01001517 return 0;
1518}
1519
1520/* The function returns 1 if the initialisation is complete, 0 if
1521 * an errors occurs and -1 if more data are required for initializing
1522 * the applet.
1523 */
Christopher Faulet4aa1d282022-01-13 16:01:35 +01001524static int promex_appctx_init(struct appctx *appctx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001525{
Willy Tarreauae1747d2022-05-03 17:33:00 +02001526 applet_reserve_svcctx(appctx, sizeof(struct promex_ctx));
Christopher Fauletf959d082019-02-07 15:38:42 +01001527 appctx->st0 = PROMEX_ST_INIT;
Christopher Fauletc9929382022-05-12 11:52:27 +02001528 return 0;
Christopher Fauletf959d082019-02-07 15:38:42 +01001529}
1530
1531/* The main I/O handler for the promex applet. */
1532static void promex_appctx_handle_io(struct appctx *appctx)
1533{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001534 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001535 struct stream *s = __sc_strm(sc);
1536 struct channel *req = sc_oc(sc);
1537 struct channel *res = sc_ic(sc);
Christopher Fauletf959d082019-02-07 15:38:42 +01001538 struct htx *req_htx, *res_htx;
1539 int ret;
1540
1541 res_htx = htx_from_buf(&res->buf);
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001542 if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO))
Christopher Fauletf959d082019-02-07 15:38:42 +01001543 goto out;
1544
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001545 /* Check if the input buffer is available. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001546 if (!b_size(&res->buf)) {
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001547 sc_need_room(sc);
Christopher Fauletf959d082019-02-07 15:38:42 +01001548 goto out;
1549 }
1550
1551 switch (appctx->st0) {
1552 case PROMEX_ST_INIT:
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001553 ret = promex_parse_uri(appctx, sc);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001554 if (ret <= 0) {
1555 if (ret == -1)
1556 goto error;
1557 goto out;
1558 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001559 appctx->st0 = PROMEX_ST_HEAD;
1560 appctx->st1 = PROMEX_DUMPER_INIT;
Willy Tarreau913bea52022-11-14 07:37:45 +01001561 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001562
1563 case PROMEX_ST_HEAD:
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001564 if (!promex_send_headers(appctx, sc, res_htx))
Christopher Fauletf959d082019-02-07 15:38:42 +01001565 goto out;
1566 appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP);
Willy Tarreau913bea52022-11-14 07:37:45 +01001567 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001568
1569 case PROMEX_ST_DUMP:
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001570 ret = promex_dump_metrics(appctx, sc, res_htx);
Christopher Fauletf959d082019-02-07 15:38:42 +01001571 if (ret <= 0) {
1572 if (ret == -1)
1573 goto error;
1574 goto out;
1575 }
1576 appctx->st0 = PROMEX_ST_DONE;
Willy Tarreau913bea52022-11-14 07:37:45 +01001577 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001578
1579 case PROMEX_ST_DONE:
Christopher Fauletbe69cbd2022-04-07 10:19:46 +02001580 /* no more data are expected. If the response buffer is
1581 * empty, be sure to add something (EOT block in this
1582 * case) to have something to send. It is important to
1583 * be sure the EOM flags will be handled by the
1584 * endpoint.
1585 */
1586 if (htx_is_empty(res_htx)) {
1587 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001588 sc_need_room(sc);
Christopher Fauletbe69cbd2022-04-07 10:19:46 +02001589 goto out;
1590 }
1591 channel_add_input(res, 1);
1592 }
1593 res_htx->flags |= HTX_FL_EOM;
Christopher Fauletbef64b22022-03-07 15:56:20 +01001594 res->flags |= CF_EOI;
Willy Tarreaud869e132022-05-17 18:05:31 +02001595 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001596 appctx->st0 = PROMEX_ST_END;
Willy Tarreau913bea52022-11-14 07:37:45 +01001597 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001598
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001599 case PROMEX_ST_END:
1600 if (!(res->flags & CF_SHUTR)) {
1601 res->flags |= CF_READ_NULL;
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001602 sc_shutr(sc);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001603 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001604 }
1605
Christopher Fauletf959d082019-02-07 15:38:42 +01001606 out:
1607 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001608
1609 /* eat the whole request */
1610 if (co_data(req)) {
1611 req_htx = htx_from_buf(&req->buf);
1612 co_htx_skip(req, req_htx, co_data(req));
1613 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001614 return;
1615
1616 error:
1617 res->flags |= CF_READ_NULL;
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001618 sc_shutr(sc);
1619 sc_shutw(sc);
Christopher Fauletf959d082019-02-07 15:38:42 +01001620}
1621
1622struct applet promex_applet = {
1623 .obj_type = OBJ_TYPE_APPLET,
1624 .name = "<PROMEX>", /* used for logging */
1625 .init = promex_appctx_init,
1626 .fct = promex_appctx_handle_io,
1627};
1628
1629static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px,
1630 struct act_rule *rule, char **err)
1631{
1632 /* Prometheus exporter service is only available on "http-request" rulesets */
1633 if (rule->from != ACT_F_HTTP_REQ) {
1634 memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets");
1635 return ACT_RET_PRS_ERR;
1636 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001637
1638 /* Add applet pointer in the rule. */
1639 rule->applet = promex_applet;
1640
1641 return ACT_RET_PRS_OK;
1642}
1643static void promex_register_build_options(void)
1644{
1645 char *ptr = NULL;
1646
1647 memprintf(&ptr, "Built with the Prometheus exporter as a service");
1648 hap_register_build_opts(ptr, 1);
1649}
1650
1651
1652static struct action_kw_list service_actions = { ILH, {
1653 { "prometheus-exporter", service_parse_prometheus_exporter },
1654 { /* END */ }
1655}};
1656
1657INITCALL1(STG_REGISTER, service_keywords_register, &service_actions);
1658INITCALL0(STG_REGISTER, promex_register_build_options);