blob: e247920cdcd34b8a2516ab4bb1cb3420276521d8 [file] [log] [blame]
Christopher Fauletf959d082019-02-07 15:38:42 +01001/*
2 * Promex is a Prometheus exporter for HAProxy
3 *
4 * It is highly inspired by the official Prometheus exporter.
5 * See: https://github.com/prometheus/haproxy_exporter
6 *
7 * Copyright 2019 Christopher Faulet <cfaulet@haproxy.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 */
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020018#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020019#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
William Dauchyde3c3262021-02-01 13:11:51 +010021#include <haproxy/check.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010022#include <haproxy/conn_stream.h>
23#include <haproxy/cs_utils.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020024#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020025#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020026#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020027#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020028#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020029#include <haproxy/htx.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020030#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020031#include <haproxy/listener.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020032#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020033#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020034#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020035#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020036#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020038#include <haproxy/task.h>
Willy Tarreau0fd04fd2021-05-08 12:58:12 +020039#include <haproxy/tools.h>
William Dauchy5a982a72021-01-08 13:18:06 +010040#include <haproxy/version.h>
Christopher Fauletf959d082019-02-07 15:38:42 +010041
42/* Prometheus exporter applet states (appctx->st0) */
43enum {
44 PROMEX_ST_INIT = 0, /* initialized */
45 PROMEX_ST_HEAD, /* send headers before dump */
46 PROMEX_ST_DUMP, /* dumping stats */
47 PROMEX_ST_DONE, /* finished */
Christopher Faulet9744f7c2019-03-27 15:48:53 +010048 PROMEX_ST_END, /* treatment terminated */
Christopher Fauletf959d082019-02-07 15:38:42 +010049};
50
51/* Prometheus exporter dumper states (appctx->st1) */
52enum {
William Dauchy69164222021-02-07 20:42:38 +010053 PROMEX_DUMPER_INIT = 0, /* initialized */
54 PROMEX_DUMPER_GLOBAL, /* dump metrics of globals */
55 PROMEX_DUMPER_FRONT, /* dump metrics of frontend proxies */
56 PROMEX_DUMPER_BACK, /* dump metrics of backend proxies */
57 PROMEX_DUMPER_LI, /* dump metrics of listeners */
58 PROMEX_DUMPER_SRV, /* dump metrics of servers */
59 PROMEX_DUMPER_STICKTABLE, /* dump metrics of stick tables */
60 PROMEX_DUMPER_DONE, /* finished */
Christopher Fauletf959d082019-02-07 15:38:42 +010061};
62
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 ) },
Christopher Fauletf959d082019-02-07 15:38:42 +0100305};
306
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500307/* Description of overridden stats fields */
Christopher Fauletf959d082019-02-07 15:38:42 +0100308const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
William Dauchya1da7ba2021-02-01 13:11:52 +0100309 [ST_F_STATUS] = IST("Current status of the service, per state label value."),
William Dauchyde3c3262021-02-01 13:11:51 +0100310 [ST_F_CHECK_STATUS] = IST("Status of last health check, per state label value."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100311 [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."),
Christopher Faulet2711e512020-02-27 16:12:07 +0100312 [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100313 [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."),
314 [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."),
315 [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."),
316 [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100317 [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"),
318 [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"),
319 [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"),
320 [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100321};
322
William Dauchy69164222021-02-07 20:42:38 +0100323/* stick table base fields */
324enum sticktable_field {
325 STICKTABLE_SIZE = 0,
326 STICKTABLE_USED,
327 /* must always be the last one */
328 STICKTABLE_TOTAL_FIELDS
329};
330
331const struct promex_metric promex_sticktable_metrics[STICKTABLE_TOTAL_FIELDS] = {
332 [STICKTABLE_SIZE] = { .n = IST("size"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC },
333 [STICKTABLE_USED] = { .n = IST("used"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC },
334};
335
336/* stick table base description */
337const struct ist promex_sticktable_metric_desc[STICKTABLE_TOTAL_FIELDS] = {
338 [STICKTABLE_SIZE] = IST("Stick table size."),
339 [STICKTABLE_USED] = IST("Number of entries used in this stick table."),
340};
341
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100342/* Specific labels for all ST_F_HRSP_* fields */
343const struct ist promex_hrsp_code[1 + ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = {
344 [ST_F_HRSP_1XX - ST_F_HRSP_1XX] = IST("1xx"),
345 [ST_F_HRSP_2XX - ST_F_HRSP_1XX] = IST("2xx"),
346 [ST_F_HRSP_3XX - ST_F_HRSP_1XX] = IST("3xx"),
347 [ST_F_HRSP_4XX - ST_F_HRSP_1XX] = IST("4xx"),
348 [ST_F_HRSP_5XX - ST_F_HRSP_1XX] = IST("5xx"),
349 [ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = IST("other"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100350};
351
William Dauchy54938212021-01-27 22:40:16 +0100352enum promex_front_state {
353 PROMEX_FRONT_STATE_DOWN = 0,
354 PROMEX_FRONT_STATE_UP,
355
356 PROMEX_FRONT_STATE_COUNT /* must be last */
357};
358
359const struct ist promex_front_st[PROMEX_FRONT_STATE_COUNT] = {
360 [PROMEX_FRONT_STATE_DOWN] = IST("DOWN"),
361 [PROMEX_FRONT_STATE_UP] = IST("UP"),
362};
363
364enum promex_back_state {
365 PROMEX_BACK_STATE_DOWN = 0,
366 PROMEX_BACK_STATE_UP,
367
368 PROMEX_BACK_STATE_COUNT /* must be last */
369};
370
371const struct ist promex_back_st[PROMEX_BACK_STATE_COUNT] = {
372 [PROMEX_BACK_STATE_DOWN] = IST("DOWN"),
373 [PROMEX_BACK_STATE_UP] = IST("UP"),
374};
375
376enum promex_srv_state {
377 PROMEX_SRV_STATE_DOWN = 0,
378 PROMEX_SRV_STATE_UP,
379 PROMEX_SRV_STATE_MAINT,
380 PROMEX_SRV_STATE_DRAIN,
381 PROMEX_SRV_STATE_NOLB,
382
383 PROMEX_SRV_STATE_COUNT /* must be last */
384};
385
386const struct ist promex_srv_st[PROMEX_SRV_STATE_COUNT] = {
387 [PROMEX_SRV_STATE_DOWN] = IST("DOWN"),
388 [PROMEX_SRV_STATE_UP] = IST("UP"),
389 [PROMEX_SRV_STATE_MAINT] = IST("MAINT"),
390 [PROMEX_SRV_STATE_DRAIN] = IST("DRAIN"),
391 [PROMEX_SRV_STATE_NOLB] = IST("NOLB"),
392};
393
394/* Return the server status. */
395enum promex_srv_state promex_srv_status(struct server *sv)
Christopher Fauletf959d082019-02-07 15:38:42 +0100396{
William Dauchy54938212021-01-27 22:40:16 +0100397 int state = PROMEX_SRV_STATE_DOWN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100398
Christopher Fauletf959d082019-02-07 15:38:42 +0100399 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Dauchy54938212021-01-27 22:40:16 +0100400 state = PROMEX_SRV_STATE_UP;
Christopher Fauletf959d082019-02-07 15:38:42 +0100401 if (sv->cur_admin & SRV_ADMF_DRAIN)
William Dauchy54938212021-01-27 22:40:16 +0100402 state = PROMEX_SRV_STATE_DRAIN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100403 }
Christopher Fauletd45d1052019-09-06 16:10:19 +0200404 else if (sv->cur_state == SRV_ST_STOPPING)
William Dauchy54938212021-01-27 22:40:16 +0100405 state = PROMEX_SRV_STATE_NOLB;
Christopher Fauletd45d1052019-09-06 16:10:19 +0200406
407 if (sv->cur_admin & SRV_ADMF_MAINT)
William Dauchy54938212021-01-27 22:40:16 +0100408 state = PROMEX_SRV_STATE_MAINT;
Christopher Fauletf959d082019-02-07 15:38:42 +0100409
410 return state;
411}
412
413/* Convert a field to its string representation and write it in <out>, followed
414 * by a newline, if there is enough space. non-numeric value are converted in
William Dauchy18a2c6e2021-01-22 21:09:47 +0100415 * "NaN" because Prometheus only support numerical values (but it is unexepceted
Christopher Fauletf959d082019-02-07 15:38:42 +0100416 * to process this kind of value). It returns 1 on success. Otherwise, it
417 * returns 0. The buffer's length must not exceed <max> value.
418 */
419static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
420{
421 int ret = 0;
422
423 switch (field_format(f, 0)) {
William Dauchy18a2c6e2021-01-22 21:09:47 +0100424 case FF_EMPTY: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100425 case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break;
426 case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break;
427 case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break;
428 case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200429 case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break;
William Dauchy18a2c6e2021-01-22 21:09:47 +0100430 case FF_STR: ret = chunk_strcat(out, "NaN\n"); break;
431 default: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100432 }
433 if (!ret || out->data > max)
434 return 0;
435 return 1;
436}
437
Christopher Fauletf959d082019-02-07 15:38:42 +0100438/* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
439 * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
440 */
441static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
Christopher Faulet37286a52021-01-20 15:20:53 +0100442 const struct promex_metric *metric, const struct ist name,
443 struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100444{
Willy Tarreaude58d242022-05-03 18:05:23 +0200445 struct promex_ctx *ctx = appctx->svcctx;
Christopher Faulet37286a52021-01-20 15:20:53 +0100446 struct ist type;
William Dauchy82b2ce22021-02-01 13:11:55 +0100447 struct ist desc;
Christopher Faulet37286a52021-01-20 15:20:53 +0100448
449 switch (metric->type) {
450 case PROMEX_MT_COUNTER:
451 type = ist("counter");
452 break;
453 default:
454 type = ist("gauge");
455 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100456
William Dauchya191b772021-01-15 22:41:39 +0100457 if (istcat(out, ist("# HELP "), max) == -1 ||
458 istcat(out, name, max) == -1 ||
459 istcat(out, ist(" "), max) == -1)
460 goto full;
461
William Dauchy82b2ce22021-02-01 13:11:55 +0100462 if (metric->flags & PROMEX_FL_INFO_METRIC)
Willy Tarreaude58d242022-05-03 18:05:23 +0200463 desc = ist(info_fields[ctx->field_num].desc);
William Dauchy69164222021-02-07 20:42:38 +0100464 else if (metric->flags & PROMEX_FL_STICKTABLE_METRIC)
Willy Tarreaude58d242022-05-03 18:05:23 +0200465 desc = promex_sticktable_metric_desc[ctx->field_num];
466 else if (!isttest(promex_st_metric_desc[ctx->field_num]))
467 desc = ist(stat_fields[ctx->field_num].desc);
William Dauchy82b2ce22021-02-01 13:11:55 +0100468 else
Willy Tarreaude58d242022-05-03 18:05:23 +0200469 desc = promex_st_metric_desc[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100470
William Dauchy82b2ce22021-02-01 13:11:55 +0100471 if (istcat(out, desc, max) == -1 ||
472 istcat(out, ist("\n# TYPE "), max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100473 istcat(out, name, max) == -1 ||
474 istcat(out, ist(" "), max) == -1 ||
Christopher Faulet37286a52021-01-20 15:20:53 +0100475 istcat(out, type, max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100476 istcat(out, ist("\n"), max) == -1)
477 goto full;
478
479 return 1;
480
481 full:
482 return 0;
483}
484
485/* Dump the line for <metric>. It starts by the metric name followed by its
486 * labels (proxy name, server name...) between braces and finally its value. If
487 * not already done, the header lines are dumped first. It returns 1 on
488 * success. Otherwise if <out> length exceeds <max>, it returns 0.
489 */
Christopher Faulet37286a52021-01-20 15:20:53 +0100490static int promex_dump_metric(struct appctx *appctx, struct htx *htx, struct ist prefix,
William Dauchyc6464592021-01-27 22:40:17 +0100491 const struct promex_metric *metric, struct field *val,
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100492 struct promex_label *labels, struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100493{
494 struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
Willy Tarreauae1747d2022-05-03 17:33:00 +0200495 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100496 size_t len = out->len;
497
498 if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
499 return 0;
500
Christopher Faulet37286a52021-01-20 15:20:53 +0100501 /* Fill the metric name */
502 istcat(&name, prefix, PROMEX_MAX_NAME_LEN);
503 istcat(&name, metric->n, PROMEX_MAX_NAME_LEN);
504
505
Willy Tarreauae1747d2022-05-03 17:33:00 +0200506 if ((ctx->flags & PROMEX_FL_METRIC_HDR) &&
Christopher Faulet37286a52021-01-20 15:20:53 +0100507 !promex_dump_metric_header(appctx, htx, metric, name, out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100508 goto full;
509
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100510 if (istcat(out, name, max) == -1)
511 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100512
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100513 if (isttest(labels[0].name)) {
514 int i;
515
516 if (istcat(out, ist("{"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100517 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100518
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100519 for (i = 0; isttest(labels[i].name); i++) {
520 if (!isttest(labels[i].value))
521 continue;
522
523 if ((i && istcat(out, ist(","), max) == -1) ||
524 istcat(out, labels[i].name, max) == -1 ||
525 istcat(out, ist("=\""), max) == -1 ||
526 istcat(out, labels[i].value, max) == -1 ||
527 istcat(out, ist("\""), max) == -1)
528 goto full;
529 }
530
531 if (istcat(out, ist("}"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100532 goto full;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100533
Christopher Fauletf959d082019-02-07 15:38:42 +0100534 }
535
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100536 if (istcat(out, ist(" "), max) == -1)
537 goto full;
538
Christopher Fauletf959d082019-02-07 15:38:42 +0100539 trash.data = out->len;
Christopher Faulet37286a52021-01-20 15:20:53 +0100540 if (!promex_metric_to_str(&trash, val, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100541 goto full;
542 out->len = trash.data;
543
Willy Tarreauae1747d2022-05-03 17:33:00 +0200544 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100545 return 1;
546 full:
547 // Restore previous length
548 out->len = len;
549 return 0;
550
551}
552
553
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500554/* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100555 * 0 if <htx> is full and -1 in case of any error. */
556static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
557{
558 static struct ist prefix = IST("haproxy_process_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200559 struct promex_ctx *ctx = appctx->svcctx;
Christopher Faulet37286a52021-01-20 15:20:53 +0100560 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100561 struct channel *chn = cs_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +0100562 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200563 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100564 int ret = 1;
565
Willy Tarreau0b26b382021-05-08 07:43:53 +0200566 if (!stats_fill_info(info, INF_TOTAL_FIELDS, 0))
William Dauchy5d9b8f32021-01-11 20:07:49 +0100567 return -1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100568
Willy Tarreaude58d242022-05-03 18:05:23 +0200569 for (; ctx->field_num < INF_TOTAL_FIELDS; ctx->field_num++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100570 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
571
Willy Tarreaude58d242022-05-03 18:05:23 +0200572 if (!(promex_global_metrics[ctx->field_num].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100573 continue;
574
Willy Tarreaude58d242022-05-03 18:05:23 +0200575 switch (ctx->field_num) {
William Dauchy5a982a72021-01-08 13:18:06 +0100576 case INF_BUILD_INFO:
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100577 labels[0].name = ist("version");
578 labels[0].value = ist(HAPROXY_VERSION);
Christopher Faulet37286a52021-01-20 15:20:53 +0100579 val = mkf_u32(FN_GAUGE, 1);
William Dauchy5a982a72021-01-08 13:18:06 +0100580 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100581
582 default:
Willy Tarreaude58d242022-05-03 18:05:23 +0200583 val = info[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100584 }
585
Willy Tarreaude58d242022-05-03 18:05:23 +0200586 if (!promex_dump_metric(appctx, htx, prefix, &promex_global_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100587 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100588 goto full;
589
Willy Tarreauae1747d2022-05-03 17:33:00 +0200590 ctx->flags |= PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100591 }
592
593 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200594 if (out.len) {
595 if (!htx_add_data_atonce(htx, out))
596 return -1; /* Unexpected and unrecoverable error */
597 channel_add_input(chn, out.len);
598 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100599 return ret;
600 full:
601 ret = 0;
602 goto end;
603}
604
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500605/* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100606 * 0 if <htx> is full and -1 in case of any error. */
607static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
608{
609 static struct ist prefix = IST("haproxy_frontend_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200610 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100611 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100612 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100613 struct channel *chn = cs_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +0100614 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200615 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchyb9577452021-01-17 18:27:46 +0100616 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100617 int ret = 1;
William Dauchyc6464592021-01-27 22:40:17 +0100618 enum promex_front_state state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100619
Willy Tarreaude58d242022-05-03 18:05:23 +0200620 for (;ctx->field_num < ST_F_TOTAL_FIELDS; ctx->field_num++) {
621 if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100622 continue;
623
Willy Tarreauae1747d2022-05-03 17:33:00 +0200624 while (ctx->px) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100625 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
626
Willy Tarreauae1747d2022-05-03 17:33:00 +0200627 px = ctx->px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100628
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100629 labels[0].name = ist("proxy");
630 labels[0].value = ist2(px->id, strlen(px->id));
631
Christopher Fauletf959d082019-02-07 15:38:42 +0100632 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200633 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100634 goto next_px;
635
Willy Tarreaude58d242022-05-03 18:05:23 +0200636 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(ctx->field_num)))
William Dauchyb9577452021-01-17 18:27:46 +0100637 return -1;
638
Willy Tarreaude58d242022-05-03 18:05:23 +0200639 switch (ctx->field_num) {
Christopher Fauletf959d082019-02-07 15:38:42 +0100640 case ST_F_STATUS:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200641 state = !(px->flags & PR_FL_STOPPED);
Willy Tarreauae1747d2022-05-03 17:33:00 +0200642 for (; ctx->obj_state < PROMEX_FRONT_STATE_COUNT; ctx->obj_state++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100643 labels[1].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200644 labels[1].value = promex_front_st[ctx->obj_state];
645 val = mkf_u32(FO_STATUS, state == ctx->obj_state);
Willy Tarreaude58d242022-05-03 18:05:23 +0200646 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100647 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100648 goto full;
649 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200650 ctx->obj_state = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100651 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100652 case ST_F_REQ_RATE_MAX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100653 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100654 case ST_F_INTERCEPTED:
Christopher Fauletf959d082019-02-07 15:38:42 +0100655 case ST_F_CACHE_LOOKUPS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100656 case ST_F_CACHE_HITS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100657 case ST_F_COMP_IN:
Christopher Fauletf959d082019-02-07 15:38:42 +0100658 case ST_F_COMP_OUT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100659 case ST_F_COMP_BYP:
William Dauchyb9577452021-01-17 18:27:46 +0100660 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100661 if (px->mode != PR_MODE_HTTP)
662 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +0200663 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100664 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100665 case ST_F_HRSP_1XX:
William Dauchyb9577452021-01-17 18:27:46 +0100666 case ST_F_HRSP_2XX:
667 case ST_F_HRSP_3XX:
668 case ST_F_HRSP_4XX:
669 case ST_F_HRSP_5XX:
670 case ST_F_HRSP_OTHER:
Christopher Fauletf959d082019-02-07 15:38:42 +0100671 if (px->mode != PR_MODE_HTTP)
672 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +0200673 if (ctx->field_num != ST_F_HRSP_1XX)
Willy Tarreauae1747d2022-05-03 17:33:00 +0200674 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100675 labels[1].name = ist("code");
Willy Tarreaude58d242022-05-03 18:05:23 +0200676 labels[1].value = promex_hrsp_code[ctx->field_num - ST_F_HRSP_1XX];
677 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100678 break;
679
680 default:
Willy Tarreaude58d242022-05-03 18:05:23 +0200681 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100682 }
683
Willy Tarreaude58d242022-05-03 18:05:23 +0200684 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100685 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100686 goto full;
687 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +0200688 ctx->px = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100689 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200690 ctx->flags |= PROMEX_FL_METRIC_HDR;
691 ctx->px = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100692 }
693
694 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200695 if (out.len) {
696 if (!htx_add_data_atonce(htx, out))
697 return -1; /* Unexpected and unrecoverable error */
698 channel_add_input(chn, out.len);
699 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100700 return ret;
701 full:
702 ret = 0;
703 goto end;
704}
705
William Dauchye3f7bd52021-02-14 23:22:56 +0100706/* Dump listener metrics (prefixed by "haproxy_listen_"). It returns 1 on
707 * success, 0 if <htx> is full and -1 in case of any error. */
708static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx)
709{
710 static struct ist prefix = IST("haproxy_listener_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200711 struct promex_ctx *ctx = appctx->svcctx;
William Dauchye3f7bd52021-02-14 23:22:56 +0100712 struct proxy *px;
713 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100714 struct channel *chn = cs_ic(appctx->owner);
William Dauchye3f7bd52021-02-14 23:22:56 +0100715 struct ist out = ist2(trash.area, 0);
716 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
717 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
718 struct listener *li;
719 int ret = 1;
720 enum li_status status;
721
Willy Tarreaude58d242022-05-03 18:05:23 +0200722 for (;ctx->field_num < ST_F_TOTAL_FIELDS; ctx->field_num++) {
723 if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
William Dauchye3f7bd52021-02-14 23:22:56 +0100724 continue;
725
Willy Tarreauae1747d2022-05-03 17:33:00 +0200726 while (ctx->px) {
William Dauchye3f7bd52021-02-14 23:22:56 +0100727 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
728
Willy Tarreauae1747d2022-05-03 17:33:00 +0200729 px = ctx->px;
William Dauchye3f7bd52021-02-14 23:22:56 +0100730
731 labels[0].name = ist("proxy");
732 labels[0].value = ist2(px->id, strlen(px->id));
733
734 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200735 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
William Dauchye3f7bd52021-02-14 23:22:56 +0100736 goto next_px;
737
Willy Tarreauae1747d2022-05-03 17:33:00 +0200738 li = ctx->li;
William Dauchye3f7bd52021-02-14 23:22:56 +0100739 list_for_each_entry_from(li, &px->conf.listeners, by_fe) {
740
William Dauchye3f7bd52021-02-14 23:22:56 +0100741 if (!li->counters)
742 continue;
743
William Dauchybaf22732021-02-25 00:53:13 +0100744 labels[1].name = ist("listener");
745 labels[1].value = ist2(li->name, strlen(li->name));
746
William Dauchye3f7bd52021-02-14 23:22:56 +0100747 if (!stats_fill_li_stats(px, li, 0, stats,
Willy Tarreaude58d242022-05-03 18:05:23 +0200748 ST_F_TOTAL_FIELDS, &(ctx->field_num)))
William Dauchye3f7bd52021-02-14 23:22:56 +0100749 return -1;
750
Willy Tarreaude58d242022-05-03 18:05:23 +0200751 switch (ctx->field_num) {
William Dauchye3f7bd52021-02-14 23:22:56 +0100752 case ST_F_STATUS:
753 status = get_li_status(li);
Willy Tarreauae1747d2022-05-03 17:33:00 +0200754 for (; ctx->obj_state < LI_STATE_COUNT; ctx->obj_state++) {
755 val = mkf_u32(FO_STATUS, status == ctx->obj_state);
William Dauchye3f7bd52021-02-14 23:22:56 +0100756 labels[2].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200757 labels[2].value = ist(li_status_st[ctx->obj_state]);
Willy Tarreaude58d242022-05-03 18:05:23 +0200758 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
William Dauchye3f7bd52021-02-14 23:22:56 +0100759 &val, labels, &out, max))
760 goto full;
761 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200762 ctx->obj_state = 0;
William Dauchye3f7bd52021-02-14 23:22:56 +0100763 continue;
764 default:
Willy Tarreaude58d242022-05-03 18:05:23 +0200765 val = stats[ctx->field_num];
William Dauchye3f7bd52021-02-14 23:22:56 +0100766 }
767
768 if (!promex_dump_metric(appctx, htx, prefix,
Willy Tarreaude58d242022-05-03 18:05:23 +0200769 &promex_st_metrics[ctx->field_num],
William Dauchye3f7bd52021-02-14 23:22:56 +0100770 &val, labels, &out, max))
771 goto full;
772 }
773
774 next_px:
775 px = px->next;
Willy Tarreauae1747d2022-05-03 17:33:00 +0200776 ctx->px = px;
777 ctx->li = (px ? LIST_NEXT(&px->conf.listeners, struct listener *, by_fe) : NULL);
William Dauchye3f7bd52021-02-14 23:22:56 +0100778 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200779 ctx->flags |= PROMEX_FL_METRIC_HDR;
780 ctx->px = proxies_list;
781 ctx->li = LIST_NEXT(&proxies_list->conf.listeners, struct listener *, by_fe);
William Dauchye3f7bd52021-02-14 23:22:56 +0100782 }
783
784 end:
785 if (out.len) {
786 if (!htx_add_data_atonce(htx, out))
787 return -1; /* Unexpected and unrecoverable error */
788 channel_add_input(chn, out.len);
789 }
790 return ret;
791 full:
Willy Tarreauae1747d2022-05-03 17:33:00 +0200792 ctx->li = li;
William Dauchye3f7bd52021-02-14 23:22:56 +0100793 ret = 0;
794 goto end;
795}
796
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500797/* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100798 * 0 if <htx> is full and -1 in case of any error. */
799static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
800{
801 static struct ist prefix = IST("haproxy_backend_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200802 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100803 struct proxy *px;
William Dauchy42d7c402021-11-07 10:18:47 +0100804 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100805 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100806 struct channel *chn = cs_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +0100807 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200808 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchy3c6f0062021-01-25 17:29:02 +0100809 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100810 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200811 double secs;
William Dauchy42d7c402021-11-07 10:18:47 +0100812 enum promex_back_state bkd_state;
813 enum promex_srv_state srv_state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100814
Willy Tarreaude58d242022-05-03 18:05:23 +0200815 for (;ctx->field_num < ST_F_TOTAL_FIELDS; ctx->field_num++) {
816 if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100817 continue;
818
Willy Tarreauae1747d2022-05-03 17:33:00 +0200819 while (ctx->px) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100820 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
William Dauchy42d7c402021-11-07 10:18:47 +0100821 unsigned int srv_state_count[PROMEX_SRV_STATE_COUNT] = { 0 };
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100822
Willy Tarreauae1747d2022-05-03 17:33:00 +0200823 px = ctx->px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100824
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100825 labels[0].name = ist("proxy");
826 labels[0].value = ist2(px->id, strlen(px->id));
827
Christopher Fauletf959d082019-02-07 15:38:42 +0100828 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200829 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100830 goto next_px;
831
Willy Tarreaude58d242022-05-03 18:05:23 +0200832 if (!stats_fill_be_stats(px, 0, stats, ST_F_TOTAL_FIELDS, &(ctx->field_num)))
William Dauchy3c6f0062021-01-25 17:29:02 +0100833 return -1;
834
Willy Tarreaude58d242022-05-03 18:05:23 +0200835 switch (ctx->field_num) {
William Dauchy42d7c402021-11-07 10:18:47 +0100836 case ST_F_AGG_SRV_CHECK_STATUS:
837 if (!px->srv)
838 goto next_px;
839 sv = px->srv;
840 while (sv) {
841 srv_state = promex_srv_status(sv);
842 srv_state_count[srv_state] += 1;
843 sv = sv->next;
844 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200845 for (; ctx->obj_state < PROMEX_SRV_STATE_COUNT; ctx->obj_state++) {
846 val = mkf_u32(FN_GAUGE, srv_state_count[ctx->obj_state]);
William Dauchy42d7c402021-11-07 10:18:47 +0100847 labels[1].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200848 labels[1].value = promex_srv_st[ctx->obj_state];
Willy Tarreaude58d242022-05-03 18:05:23 +0200849 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
William Dauchy42d7c402021-11-07 10:18:47 +0100850 &val, labels, &out, max))
851 goto full;
852 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200853 ctx->obj_state = 0;
William Dauchy42d7c402021-11-07 10:18:47 +0100854 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100855 case ST_F_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +0100856 bkd_state = ((px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0);
Willy Tarreauae1747d2022-05-03 17:33:00 +0200857 for (; ctx->obj_state < PROMEX_BACK_STATE_COUNT; ctx->obj_state++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100858 labels[1].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200859 labels[1].value = promex_back_st[ctx->obj_state];
860 val = mkf_u32(FO_STATUS, bkd_state == ctx->obj_state);
Willy Tarreaude58d242022-05-03 18:05:23 +0200861 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100862 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100863 goto full;
864 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200865 ctx->obj_state = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100866 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100867 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200868 secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100869 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100870 break;
871 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200872 secs = (double)swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100873 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100874 break;
875 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200876 secs = (double)swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100877 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100878 break;
879 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200880 secs = (double)swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100881 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100882 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100883 case ST_F_QT_MAX:
884 secs = (double)px->be_counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100885 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100886 break;
887 case ST_F_CT_MAX:
888 secs = (double)px->be_counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100889 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100890 break;
891 case ST_F_RT_MAX:
892 secs = (double)px->be_counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100893 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100894 break;
895 case ST_F_TT_MAX:
896 secs = (double)px->be_counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100897 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100898 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100899 case ST_F_REQ_TOT:
William Dauchy3c6f0062021-01-25 17:29:02 +0100900 case ST_F_CACHE_LOOKUPS:
901 case ST_F_CACHE_HITS:
902 case ST_F_COMP_IN:
903 case ST_F_COMP_OUT:
904 case ST_F_COMP_BYP:
905 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100906 if (px->mode != PR_MODE_HTTP)
907 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +0200908 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100909 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100910 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100911 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100912 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100913 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100914 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100915 case ST_F_HRSP_OTHER:
916 if (px->mode != PR_MODE_HTTP)
917 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +0200918 if (ctx->field_num != ST_F_HRSP_1XX)
Willy Tarreauae1747d2022-05-03 17:33:00 +0200919 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100920 labels[1].name = ist("code");
Willy Tarreaude58d242022-05-03 18:05:23 +0200921 labels[1].value = promex_hrsp_code[ctx->field_num - ST_F_HRSP_1XX];
922 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100923 break;
924
925 default:
Willy Tarreaude58d242022-05-03 18:05:23 +0200926 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100927 }
928
Willy Tarreaude58d242022-05-03 18:05:23 +0200929 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100930 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100931 goto full;
932 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +0200933 ctx->px = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100934 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200935 ctx->flags |= PROMEX_FL_METRIC_HDR;
936 ctx->px = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100937 }
938
939 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200940 if (out.len) {
941 if (!htx_add_data_atonce(htx, out))
942 return -1; /* Unexpected and unrecoverable error */
943 channel_add_input(chn, out.len);
944 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100945 return ret;
946 full:
947 ret = 0;
948 goto end;
949}
950
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500951/* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100952 * 0 if <htx> is full and -1 in case of any error. */
953static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
954{
955 static struct ist prefix = IST("haproxy_server_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200956 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100957 struct proxy *px;
958 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100959 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100960 struct channel *chn = cs_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +0100961 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200962 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchybde2bf62021-01-25 17:29:04 +0100963 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100964 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200965 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100966 enum promex_srv_state state;
William Dauchyde3c3262021-02-01 13:11:51 +0100967 const char *check_state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100968
Willy Tarreaude58d242022-05-03 18:05:23 +0200969 for (;ctx->field_num < ST_F_TOTAL_FIELDS; ctx->field_num++) {
970 if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100971 continue;
972
Willy Tarreauae1747d2022-05-03 17:33:00 +0200973 while (ctx->px) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100974 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
975
Willy Tarreauae1747d2022-05-03 17:33:00 +0200976 px = ctx->px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100977
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100978 labels[0].name = ist("proxy");
979 labels[0].value = ist2(px->id, strlen(px->id));
980
Christopher Fauletf959d082019-02-07 15:38:42 +0100981 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200982 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100983 goto next_px;
984
Willy Tarreauae1747d2022-05-03 17:33:00 +0200985 while (ctx->sv) {
986 sv = ctx->sv;
Christopher Fauletf959d082019-02-07 15:38:42 +0100987
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100988 labels[1].name = ist("server");
989 labels[1].value = ist2(sv->id, strlen(sv->id));
990
Willy Tarreaude58d242022-05-03 18:05:23 +0200991 if (!stats_fill_sv_stats(px, sv, 0, stats, ST_F_TOTAL_FIELDS, &(ctx->field_num)))
William Dauchybde2bf62021-01-25 17:29:04 +0100992 return -1;
993
Willy Tarreauae1747d2022-05-03 17:33:00 +0200994 if ((ctx->flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
Christopher Fauleteba22942019-11-19 14:18:24 +0100995 goto next_sv;
996
Willy Tarreaude58d242022-05-03 18:05:23 +0200997 switch (ctx->field_num) {
Christopher Fauletf959d082019-02-07 15:38:42 +0100998 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100999 state = promex_srv_status(sv);
Willy Tarreauae1747d2022-05-03 17:33:00 +02001000 for (; ctx->obj_state < PROMEX_SRV_STATE_COUNT; ctx->obj_state++) {
1001 val = mkf_u32(FO_STATUS, state == ctx->obj_state);
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001002 labels[2].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +02001003 labels[2].value = promex_srv_st[ctx->obj_state];
Willy Tarreaude58d242022-05-03 18:05:23 +02001004 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001005 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +01001006 goto full;
1007 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001008 ctx->obj_state = 0;
William Dauchyc6464592021-01-27 22:40:17 +01001009 goto next_sv;
Christopher Fauletf959d082019-02-07 15:38:42 +01001010 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001011 secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001012 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001013 break;
1014 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001015 secs = (double)swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001016 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001017 break;
1018 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001019 secs = (double)swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001020 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001021 break;
1022 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001023 secs = (double)swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001024 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001025 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001026 case ST_F_QT_MAX:
1027 secs = (double)sv->counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001028 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001029 break;
1030 case ST_F_CT_MAX:
1031 secs = (double)sv->counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001032 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001033 break;
1034 case ST_F_RT_MAX:
1035 secs = (double)sv->counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001036 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001037 break;
1038 case ST_F_TT_MAX:
1039 secs = (double)sv->counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001040 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001041 break;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001042 case ST_F_CHECK_STATUS:
1043 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1044 goto next_sv;
William Dauchyde3c3262021-02-01 13:11:51 +01001045
Willy Tarreauae1747d2022-05-03 17:33:00 +02001046 for (; ctx->obj_state < HCHK_STATUS_SIZE; ctx->obj_state++) {
1047 if (get_check_status_result(ctx->obj_state) < CHK_RES_FAILED)
William Dauchyde3c3262021-02-01 13:11:51 +01001048 continue;
Willy Tarreauae1747d2022-05-03 17:33:00 +02001049 val = mkf_u32(FO_STATUS, sv->check.status == ctx->obj_state);
1050 check_state = get_check_status_info(ctx->obj_state);
William Dauchyde3c3262021-02-01 13:11:51 +01001051 labels[2].name = ist("state");
Tim Duesterhusb113b5c2021-09-15 13:58:44 +02001052 labels[2].value = ist(check_state);
Willy Tarreaude58d242022-05-03 18:05:23 +02001053 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
William Dauchyde3c3262021-02-01 13:11:51 +01001054 &val, labels, &out, max))
1055 goto full;
1056 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001057 ctx->obj_state = 0;
William Dauchyde3c3262021-02-01 13:11:51 +01001058 goto next_sv;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001059 case ST_F_CHECK_CODE:
1060 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1061 goto next_sv;
Christopher Faulet37286a52021-01-20 15:20:53 +01001062 val = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code);
Christopher Fauletcf403f32019-11-21 14:35:46 +01001063 break;
Christopher Faulet2711e512020-02-27 16:12:07 +01001064 case ST_F_CHECK_DURATION:
1065 if (sv->check.status < HCHK_STATUS_CHECKED)
1066 goto next_sv;
1067 secs = (double)sv->check.duration / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001068 val = mkf_flt(FN_DURATION, secs);
Christopher Faulet2711e512020-02-27 16:12:07 +01001069 break;
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001070 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +01001071 if (px->mode != PR_MODE_HTTP)
1072 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +02001073 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +01001074 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +01001075 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001076 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001077 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001078 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001079 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001080 case ST_F_HRSP_OTHER:
1081 if (px->mode != PR_MODE_HTTP)
1082 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +02001083 if (ctx->field_num != ST_F_HRSP_1XX)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001084 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001085 labels[2].name = ist("code");
Willy Tarreaude58d242022-05-03 18:05:23 +02001086 labels[2].value = promex_hrsp_code[ctx->field_num - ST_F_HRSP_1XX];
1087 val = stats[ctx->field_num];
Christopher Fauletc55a6262020-07-10 15:39:39 +02001088 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001089
1090 default:
Willy Tarreaude58d242022-05-03 18:05:23 +02001091 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +01001092 }
1093
Willy Tarreaude58d242022-05-03 18:05:23 +02001094 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001095 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +01001096 goto full;
Christopher Fauleteba22942019-11-19 14:18:24 +01001097 next_sv:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001098 ctx->sv = sv->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001099 }
1100
1101 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001102 ctx->px = px->next;
1103 ctx->sv = (ctx->px ? ctx->px->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001104 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001105 ctx->flags |= PROMEX_FL_METRIC_HDR;
1106 ctx->px = proxies_list;
1107 ctx->sv = (ctx->px ? ctx->px->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001108 }
1109
1110
1111 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001112 if (out.len) {
1113 if (!htx_add_data_atonce(htx, out))
1114 return -1; /* Unexpected and unrecoverable error */
1115 channel_add_input(chn, out.len);
1116 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001117 return ret;
1118 full:
1119 ret = 0;
1120 goto end;
1121}
1122
William Dauchy69164222021-02-07 20:42:38 +01001123/* Dump stick table metrics (prefixed by "haproxy_sticktable_"). It returns 1 on success,
1124 * 0 if <htx> is full and -1 in case of any error. */
1125static int promex_dump_sticktable_metrics(struct appctx *appctx, struct htx *htx)
1126{
1127 static struct ist prefix = IST("haproxy_sticktable_");
Willy Tarreauae1747d2022-05-03 17:33:00 +02001128 struct promex_ctx *ctx = appctx->svcctx;
William Dauchy69164222021-02-07 20:42:38 +01001129 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +01001130 struct channel *chn = cs_ic(appctx->owner);
William Dauchy69164222021-02-07 20:42:38 +01001131 struct ist out = ist2(trash.area, 0);
1132 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
1133 int ret = 1;
1134 struct stktable *t;
1135
Willy Tarreaude58d242022-05-03 18:05:23 +02001136 for (; ctx->field_num < STICKTABLE_TOTAL_FIELDS; ctx->field_num++) {
1137 if (!(promex_sticktable_metrics[ctx->field_num].flags & ctx->flags))
William Dauchy69164222021-02-07 20:42:38 +01001138 continue;
1139
Willy Tarreauae1747d2022-05-03 17:33:00 +02001140 while (ctx->st) {
William Dauchy69164222021-02-07 20:42:38 +01001141 struct promex_label labels[PROMEX_MAX_LABELS - 1] = {};
1142
Willy Tarreauae1747d2022-05-03 17:33:00 +02001143 t = ctx->st;
William Dauchy69164222021-02-07 20:42:38 +01001144 if (!t->size)
1145 goto next_px;
1146
1147 labels[0].name = ist("name");
1148 labels[0].value = ist2(t->id, strlen(t->id));
1149 labels[1].name = ist("type");
1150 labels[1].value = ist2(stktable_types[t->type].kw, strlen(stktable_types[t->type].kw));
Willy Tarreaude58d242022-05-03 18:05:23 +02001151 switch (ctx->field_num) {
William Dauchy69164222021-02-07 20:42:38 +01001152 case STICKTABLE_SIZE:
1153 val = mkf_u32(FN_GAUGE, t->size);
1154 break;
1155 case STICKTABLE_USED:
1156 val = mkf_u32(FN_GAUGE, t->current);
1157 break;
1158 default:
1159 goto next_px;
1160 }
1161
1162 if (!promex_dump_metric(appctx, htx, prefix,
Willy Tarreaude58d242022-05-03 18:05:23 +02001163 &promex_sticktable_metrics[ctx->field_num],
William Dauchy69164222021-02-07 20:42:38 +01001164 &val, labels, &out, max))
1165 goto full;
1166
1167 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001168 ctx->st = t->next;
William Dauchy69164222021-02-07 20:42:38 +01001169 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001170 ctx->flags |= PROMEX_FL_METRIC_HDR;
1171 ctx->st = stktables_list;
William Dauchy69164222021-02-07 20:42:38 +01001172 }
1173
1174 end:
1175 if (out.len) {
1176 if (!htx_add_data_atonce(htx, out))
1177 return -1; /* Unexpected and unrecoverable error */
1178 channel_add_input(chn, out.len);
1179 }
1180 return ret;
1181 full:
1182 ret = 0;
1183 goto end;
1184}
1185
Christopher Fauletf959d082019-02-07 15:38:42 +01001186/* Dump all metrics (global, frontends, backends and servers) depending on the
1187 * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001188 * -1 in case of any error.
Willy Tarreauae1747d2022-05-03 17:33:00 +02001189 * Uses <appctx.ctx.stats.px> as a pointer to the current proxy and <sv>/<li>
1190 * as pointers to the current server/listener respectively.
1191 */
Christopher Faulet908628c2022-03-25 16:43:49 +01001192static int promex_dump_metrics(struct appctx *appctx, struct conn_stream *cs, struct htx *htx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001193{
Willy Tarreauae1747d2022-05-03 17:33:00 +02001194 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +01001195 int ret;
1196
1197 switch (appctx->st1) {
1198 case PROMEX_DUMPER_INIT:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001199 ctx->px = NULL;
1200 ctx->st = NULL;
1201 ctx->li = NULL;
1202 ctx->sv = NULL;
1203 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
1204 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001205 ctx->field_num = INF_NAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001206 appctx->st1 = PROMEX_DUMPER_GLOBAL;
1207 /* fall through */
1208
1209 case PROMEX_DUMPER_GLOBAL:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001210 if (ctx->flags & PROMEX_FL_SCOPE_GLOBAL) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001211 ret = promex_dump_global_metrics(appctx, htx);
1212 if (ret <= 0) {
1213 if (ret == -1)
1214 goto error;
1215 goto full;
1216 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001217 }
1218
Willy Tarreauae1747d2022-05-03 17:33:00 +02001219 ctx->px = proxies_list;
1220 ctx->st = NULL;
1221 ctx->li = NULL;
1222 ctx->sv = NULL;
1223 ctx->flags &= ~PROMEX_FL_INFO_METRIC;
1224 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC);
1225 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001226 ctx->field_num = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001227 appctx->st1 = PROMEX_DUMPER_FRONT;
1228 /* fall through */
1229
1230 case PROMEX_DUMPER_FRONT:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001231 if (ctx->flags & PROMEX_FL_SCOPE_FRONT) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001232 ret = promex_dump_front_metrics(appctx, htx);
1233 if (ret <= 0) {
1234 if (ret == -1)
1235 goto error;
1236 goto full;
1237 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001238 }
1239
Willy Tarreauae1747d2022-05-03 17:33:00 +02001240 ctx->px = proxies_list;
1241 ctx->st = NULL;
1242 ctx->li = LIST_NEXT(&proxies_list->conf.listeners, struct listener *, by_fe);
1243 ctx->sv = NULL;
1244 ctx->flags &= ~PROMEX_FL_FRONT_METRIC;
1245 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_LI_METRIC);
1246 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001247 ctx->field_num = ST_F_PXNAME;
William Dauchye3f7bd52021-02-14 23:22:56 +01001248 appctx->st1 = PROMEX_DUMPER_LI;
1249 /* fall through */
1250
1251 case PROMEX_DUMPER_LI:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001252 if (ctx->flags & PROMEX_FL_SCOPE_LI) {
William Dauchye3f7bd52021-02-14 23:22:56 +01001253 ret = promex_dump_listener_metrics(appctx, htx);
1254 if (ret <= 0) {
1255 if (ret == -1)
1256 goto error;
1257 goto full;
1258 }
1259 }
1260
Willy Tarreauae1747d2022-05-03 17:33:00 +02001261 ctx->px = proxies_list;
1262 ctx->st = NULL;
1263 ctx->li = NULL;
1264 ctx->sv = NULL;
1265 ctx->flags &= ~PROMEX_FL_LI_METRIC;
1266 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC);
1267 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001268 ctx->field_num = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001269 appctx->st1 = PROMEX_DUMPER_BACK;
1270 /* fall through */
1271
1272 case PROMEX_DUMPER_BACK:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001273 if (ctx->flags & PROMEX_FL_SCOPE_BACK) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001274 ret = promex_dump_back_metrics(appctx, htx);
1275 if (ret <= 0) {
1276 if (ret == -1)
1277 goto error;
1278 goto full;
1279 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001280 }
1281
Willy Tarreauae1747d2022-05-03 17:33:00 +02001282 ctx->px = proxies_list;
1283 ctx->st = NULL;
1284 ctx->li = NULL;
1285 ctx->sv = ctx->px ? ctx->px->srv : NULL;
1286 ctx->flags &= ~PROMEX_FL_BACK_METRIC;
1287 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
1288 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001289 ctx->field_num = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001290 appctx->st1 = PROMEX_DUMPER_SRV;
1291 /* fall through */
1292
1293 case PROMEX_DUMPER_SRV:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001294 if (ctx->flags & PROMEX_FL_SCOPE_SERVER) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001295 ret = promex_dump_srv_metrics(appctx, htx);
1296 if (ret <= 0) {
1297 if (ret == -1)
1298 goto error;
1299 goto full;
1300 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001301 }
1302
Willy Tarreauae1747d2022-05-03 17:33:00 +02001303 ctx->px = NULL;
1304 ctx->st = stktables_list;
1305 ctx->li = NULL;
1306 ctx->sv = NULL;
1307 ctx->flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
1308 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC);
Willy Tarreaude58d242022-05-03 18:05:23 +02001309 ctx->field_num = STICKTABLE_SIZE;
William Dauchy69164222021-02-07 20:42:38 +01001310 appctx->st1 = PROMEX_DUMPER_STICKTABLE;
1311 /* fall through */
1312
1313 case PROMEX_DUMPER_STICKTABLE:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001314 if (ctx->flags & PROMEX_FL_SCOPE_STICKTABLE) {
William Dauchy69164222021-02-07 20:42:38 +01001315 ret = promex_dump_sticktable_metrics(appctx, htx);
1316 if (ret <= 0) {
1317 if (ret == -1)
1318 goto error;
1319 goto full;
1320 }
1321 }
1322
Willy Tarreauae1747d2022-05-03 17:33:00 +02001323 ctx->px = NULL;
1324 ctx->st = NULL;
1325 ctx->li = NULL;
1326 ctx->sv = NULL;
1327 ctx->flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC);
Willy Tarreaude58d242022-05-03 18:05:23 +02001328 ctx->field_num = 0;
Christopher Fauletf959d082019-02-07 15:38:42 +01001329 appctx->st1 = PROMEX_DUMPER_DONE;
1330 /* fall through */
1331
1332 case PROMEX_DUMPER_DONE:
1333 default:
1334 break;
1335 }
1336
1337 return 1;
1338
1339 full:
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001340 cs_rx_room_blk(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001341 return 0;
1342 error:
1343 /* unrecoverable error */
Willy Tarreauae1747d2022-05-03 17:33:00 +02001344 ctx->px = NULL;
1345 ctx->st = NULL;
1346 ctx->li = NULL;
1347 ctx->sv = NULL;
1348 ctx->flags = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001349 ctx->field_num = 0;
Christopher Fauletf959d082019-02-07 15:38:42 +01001350 appctx->st1 = PROMEX_DUMPER_DONE;
1351 return -1;
1352}
1353
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001354/* Parse the query string of request URI to filter the metrics. It returns 1 on
Christopher Faulet78407ce2019-11-18 14:47:08 +01001355 * success and -1 on error. */
Christopher Faulet908628c2022-03-25 16:43:49 +01001356static int promex_parse_uri(struct appctx *appctx, struct conn_stream *cs)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001357{
Willy Tarreauae1747d2022-05-03 17:33:00 +02001358 struct promex_ctx *ctx = appctx->svcctx;
Christopher Faulet908628c2022-03-25 16:43:49 +01001359 struct channel *req = cs_oc(cs);
1360 struct channel *res = cs_ic(cs);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001361 struct htx *req_htx, *res_htx;
1362 struct htx_sl *sl;
William Dauchyc65f6562019-11-26 12:56:26 +01001363 char *p, *key, *value;
1364 const char *end;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001365 struct buffer *err;
1366 int default_scopes = PROMEX_FL_SCOPE_ALL;
1367 int len;
1368
1369 /* Get the query-string */
1370 req_htx = htxbuf(&req->buf);
1371 sl = http_get_stline(req_htx);
1372 if (!sl)
1373 goto error;
1374 p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?');
1375 if (!p)
1376 goto end;
1377 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001378
William Dauchyc65f6562019-11-26 12:56:26 +01001379 /* copy the query-string */
1380 len = end - p;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001381 chunk_reset(&trash);
1382 memcpy(trash.area, p, len);
1383 trash.area[len] = 0;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001384 p = trash.area;
William Dauchyc65f6562019-11-26 12:56:26 +01001385 end = trash.area + len;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001386
1387 /* Parse the query-string */
William Dauchyc65f6562019-11-26 12:56:26 +01001388 while (p < end && *p && *p != '#') {
1389 value = NULL;
1390
1391 /* decode parameter name */
1392 key = p;
1393 while (p < end && *p != '=' && *p != '&' && *p != '#')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001394 ++p;
William Dauchyc65f6562019-11-26 12:56:26 +01001395 /* found a value */
1396 if (*p == '=') {
1397 *(p++) = 0;
1398 value = p;
1399 }
1400 else if (*p == '&')
1401 *(p++) = 0;
1402 else if (*p == '#')
1403 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001404 len = url_decode(key, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001405 if (len == -1)
1406 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001407
William Dauchyc65f6562019-11-26 12:56:26 +01001408 /* decode value */
1409 if (value) {
1410 while (p < end && *p != '=' && *p != '&' && *p != '#')
1411 ++p;
1412 if (*p == '=')
1413 goto error;
1414 if (*p == '&')
1415 *(p++) = 0;
1416 else if (*p == '#')
1417 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001418 len = url_decode(value, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001419 if (len == -1)
1420 goto error;
1421 }
1422
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001423 if (strcmp(key, "scope") == 0) {
William Dauchyc65f6562019-11-26 12:56:26 +01001424 default_scopes = 0; /* at least a scope defined, unset default scopes */
1425 if (!value)
1426 goto error;
1427 else if (*value == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001428 ctx->flags &= ~PROMEX_FL_SCOPE_ALL;
William Dauchyc65f6562019-11-26 12:56:26 +01001429 else if (*value == '*')
Willy Tarreauae1747d2022-05-03 17:33:00 +02001430 ctx->flags |= PROMEX_FL_SCOPE_ALL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001431 else if (strcmp(value, "global") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001432 ctx->flags |= PROMEX_FL_SCOPE_GLOBAL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001433 else if (strcmp(value, "server") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001434 ctx->flags |= PROMEX_FL_SCOPE_SERVER;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001435 else if (strcmp(value, "backend") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001436 ctx->flags |= PROMEX_FL_SCOPE_BACK;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001437 else if (strcmp(value, "frontend") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001438 ctx->flags |= PROMEX_FL_SCOPE_FRONT;
William Dauchye3f7bd52021-02-14 23:22:56 +01001439 else if (strcmp(value, "listener") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001440 ctx->flags |= PROMEX_FL_SCOPE_LI;
William Dauchy69164222021-02-07 20:42:38 +01001441 else if (strcmp(value, "sticktable") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001442 ctx->flags |= PROMEX_FL_SCOPE_STICKTABLE;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001443 else
1444 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001445 }
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001446 else if (strcmp(key, "no-maint") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001447 ctx->flags |= PROMEX_FL_NO_MAINT_SRV;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001448 }
1449
1450 end:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001451 ctx->flags |= default_scopes;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001452 return 1;
1453
1454 error:
1455 err = &http_err_chunks[HTTP_ERR_400];
1456 channel_erase(res);
1457 res->buf.data = b_data(err);
1458 memcpy(res->buf.area, b_head(err), b_data(err));
1459 res_htx = htx_from_buf(&res->buf);
1460 channel_add_input(res, res_htx->data);
1461 appctx->st0 = PROMEX_ST_END;
1462 return -1;
1463}
1464
Christopher Fauletf959d082019-02-07 15:38:42 +01001465/* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is
1466 * full. */
Christopher Faulet908628c2022-03-25 16:43:49 +01001467static int promex_send_headers(struct appctx *appctx, struct conn_stream *cs, struct htx *htx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001468{
Christopher Faulet908628c2022-03-25 16:43:49 +01001469 struct channel *chn = cs_ic(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001470 struct htx_sl *sl;
1471 unsigned int flags;
1472
1473 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);
1474 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
1475 if (!sl)
1476 goto full;
1477 sl->info.res.status = 200;
1478 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001479 !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) ||
1480 !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) ||
1481 !htx_add_endof(htx, HTX_BLK_EOH))
1482 goto full;
1483
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001484 channel_add_input(chn, htx->data);
Christopher Fauletf959d082019-02-07 15:38:42 +01001485 return 1;
1486 full:
1487 htx_reset(htx);
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001488 cs_rx_room_blk(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001489 return 0;
1490}
1491
1492/* The function returns 1 if the initialisation is complete, 0 if
1493 * an errors occurs and -1 if more data are required for initializing
1494 * the applet.
1495 */
Christopher Faulet4aa1d282022-01-13 16:01:35 +01001496static int promex_appctx_init(struct appctx *appctx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001497{
Willy Tarreauae1747d2022-05-03 17:33:00 +02001498 applet_reserve_svcctx(appctx, sizeof(struct promex_ctx));
Christopher Fauletf959d082019-02-07 15:38:42 +01001499 appctx->st0 = PROMEX_ST_INIT;
1500 return 1;
1501}
1502
1503/* The main I/O handler for the promex applet. */
1504static void promex_appctx_handle_io(struct appctx *appctx)
1505{
Christopher Faulet908628c2022-03-25 16:43:49 +01001506 struct conn_stream *cs = appctx->owner;
1507 struct stream *s = __cs_strm(cs);
1508 struct channel *req = cs_oc(cs);
1509 struct channel *res = cs_ic(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001510 struct htx *req_htx, *res_htx;
1511 int ret;
1512
1513 res_htx = htx_from_buf(&res->buf);
Christopher Faulet62e75742022-03-31 09:16:34 +02001514 if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO))
Christopher Fauletf959d082019-02-07 15:38:42 +01001515 goto out;
1516
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001517 /* Check if the input buffer is available. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001518 if (!b_size(&res->buf)) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001519 cs_rx_room_blk(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001520 goto out;
1521 }
1522
1523 switch (appctx->st0) {
1524 case PROMEX_ST_INIT:
Christopher Faulet908628c2022-03-25 16:43:49 +01001525 ret = promex_parse_uri(appctx, cs);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001526 if (ret <= 0) {
1527 if (ret == -1)
1528 goto error;
1529 goto out;
1530 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001531 appctx->st0 = PROMEX_ST_HEAD;
1532 appctx->st1 = PROMEX_DUMPER_INIT;
1533 /* fall through */
1534
1535 case PROMEX_ST_HEAD:
Christopher Faulet908628c2022-03-25 16:43:49 +01001536 if (!promex_send_headers(appctx, cs, res_htx))
Christopher Fauletf959d082019-02-07 15:38:42 +01001537 goto out;
1538 appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP);
1539 /* fall through */
1540
1541 case PROMEX_ST_DUMP:
Christopher Faulet908628c2022-03-25 16:43:49 +01001542 ret = promex_dump_metrics(appctx, cs, res_htx);
Christopher Fauletf959d082019-02-07 15:38:42 +01001543 if (ret <= 0) {
1544 if (ret == -1)
1545 goto error;
1546 goto out;
1547 }
1548 appctx->st0 = PROMEX_ST_DONE;
1549 /* fall through */
1550
1551 case PROMEX_ST_DONE:
Christopher Fauletbe69cbd2022-04-07 10:19:46 +02001552 /* no more data are expected. If the response buffer is
1553 * empty, be sure to add something (EOT block in this
1554 * case) to have something to send. It is important to
1555 * be sure the EOM flags will be handled by the
1556 * endpoint.
1557 */
1558 if (htx_is_empty(res_htx)) {
1559 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001560 cs_rx_room_blk(cs);
Christopher Fauletbe69cbd2022-04-07 10:19:46 +02001561 goto out;
1562 }
1563 channel_add_input(res, 1);
1564 }
1565 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet908628c2022-03-25 16:43:49 +01001566 cs->endp->flags |= CS_EP_EOI;
Christopher Fauletbef64b22022-03-07 15:56:20 +01001567 res->flags |= CF_EOI;
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001568 appctx->st0 = PROMEX_ST_END;
1569 /* fall through */
Christopher Fauletf959d082019-02-07 15:38:42 +01001570
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001571 case PROMEX_ST_END:
1572 if (!(res->flags & CF_SHUTR)) {
1573 res->flags |= CF_READ_NULL;
Christopher Fauletda098e62022-03-31 17:44:45 +02001574 cs_shutr(cs);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001575 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001576 }
1577
Christopher Fauletf959d082019-02-07 15:38:42 +01001578 out:
1579 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001580
1581 /* eat the whole request */
1582 if (co_data(req)) {
1583 req_htx = htx_from_buf(&req->buf);
1584 co_htx_skip(req, req_htx, co_data(req));
1585 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001586 return;
1587
1588 error:
1589 res->flags |= CF_READ_NULL;
Christopher Fauletda098e62022-03-31 17:44:45 +02001590 cs_shutr(cs);
1591 cs_shutw(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001592}
1593
1594struct applet promex_applet = {
1595 .obj_type = OBJ_TYPE_APPLET,
1596 .name = "<PROMEX>", /* used for logging */
1597 .init = promex_appctx_init,
1598 .fct = promex_appctx_handle_io,
1599};
1600
1601static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px,
1602 struct act_rule *rule, char **err)
1603{
1604 /* Prometheus exporter service is only available on "http-request" rulesets */
1605 if (rule->from != ACT_F_HTTP_REQ) {
1606 memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets");
1607 return ACT_RET_PRS_ERR;
1608 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001609
1610 /* Add applet pointer in the rule. */
1611 rule->applet = promex_applet;
1612
1613 return ACT_RET_PRS_OK;
1614}
1615static void promex_register_build_options(void)
1616{
1617 char *ptr = NULL;
1618
1619 memprintf(&ptr, "Built with the Prometheus exporter as a service");
1620 hap_register_build_opts(ptr, 1);
1621}
1622
1623
1624static struct action_kw_list service_actions = { ILH, {
1625 { "prometheus-exporter", service_parse_prometheus_exporter },
1626 { /* END */ }
1627}};
1628
1629INITCALL1(STG_REGISTER, service_keywords_register, &service_actions);
1630INITCALL0(STG_REGISTER, promex_register_build_options);