blob: 9330a860cd8826cd2effdd14a4439e82752d2db7 [file] [log] [blame]
Christopher Fauletf959d082019-02-07 15:38:42 +01001/*
2 * Promex is a Prometheus exporter for HAProxy
3 *
4 * It is highly inspired by the official Prometheus exporter.
5 * See: https://github.com/prometheus/haproxy_exporter
6 *
7 * Copyright 2019 Christopher Faulet <cfaulet@haproxy.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 */
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020018#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020019#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
William Dauchyde3c3262021-02-01 13:11:51 +010021#include <haproxy/check.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020022#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020023#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020024#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020025#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020029#include <haproxy/listener.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020030#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020031#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020032#include <haproxy/sample.h>
Willy Tarreau5edca2f2022-05-27 09:25:10 +020033#include <haproxy/sc_strm.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020034#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020035#include <haproxy/stats.h>
Willy Tarreaucb086c62022-05-27 09:47:12 +020036#include <haproxy/stconn.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020038#include <haproxy/task.h>
Willy Tarreau0fd04fd2021-05-08 12:58:12 +020039#include <haproxy/tools.h>
William Dauchy5a982a72021-01-08 13:18:06 +010040#include <haproxy/version.h>
Christopher Fauletf959d082019-02-07 15:38:42 +010041
42/* Prometheus exporter applet states (appctx->st0) */
43enum {
44 PROMEX_ST_INIT = 0, /* initialized */
45 PROMEX_ST_HEAD, /* send headers before dump */
46 PROMEX_ST_DUMP, /* dumping stats */
47 PROMEX_ST_DONE, /* finished */
Christopher Faulet9744f7c2019-03-27 15:48:53 +010048 PROMEX_ST_END, /* treatment terminated */
Christopher Fauletf959d082019-02-07 15:38:42 +010049};
50
51/* Prometheus exporter dumper states (appctx->st1) */
52enum {
William Dauchy69164222021-02-07 20:42:38 +010053 PROMEX_DUMPER_INIT = 0, /* initialized */
54 PROMEX_DUMPER_GLOBAL, /* dump metrics of globals */
55 PROMEX_DUMPER_FRONT, /* dump metrics of frontend proxies */
56 PROMEX_DUMPER_BACK, /* dump metrics of backend proxies */
57 PROMEX_DUMPER_LI, /* dump metrics of listeners */
58 PROMEX_DUMPER_SRV, /* dump metrics of servers */
59 PROMEX_DUMPER_STICKTABLE, /* dump metrics of stick tables */
60 PROMEX_DUMPER_DONE, /* finished */
Christopher Fauletf959d082019-02-07 15:38:42 +010061};
62
Willy Tarreauae1747d2022-05-03 17:33:00 +020063/* Prometheus exporter flags (ctx->flags) */
William Dauchy69164222021-02-07 20:42:38 +010064#define PROMEX_FL_METRIC_HDR 0x00000001
65#define PROMEX_FL_INFO_METRIC 0x00000002
66#define PROMEX_FL_FRONT_METRIC 0x00000004
67#define PROMEX_FL_BACK_METRIC 0x00000008
68#define PROMEX_FL_SRV_METRIC 0x00000010
William Dauchye3f7bd52021-02-14 23:22:56 +010069#define PROMEX_FL_LI_METRIC 0x00000020
70#define PROMEX_FL_STICKTABLE_METRIC 0x00000040
71#define PROMEX_FL_SCOPE_GLOBAL 0x00000080
72#define PROMEX_FL_SCOPE_FRONT 0x00000100
73#define PROMEX_FL_SCOPE_BACK 0x00000200
74#define PROMEX_FL_SCOPE_SERVER 0x00000400
75#define PROMEX_FL_SCOPE_LI 0x00000800
76#define PROMEX_FL_SCOPE_STICKTABLE 0x00001000
77#define PROMEX_FL_NO_MAINT_SRV 0x00002000
Christopher Faulet78407ce2019-11-18 14:47:08 +010078
William Dauchy69164222021-02-07 20:42:38 +010079#define PROMEX_FL_SCOPE_ALL (PROMEX_FL_SCOPE_GLOBAL | PROMEX_FL_SCOPE_FRONT | \
William Dauchye3f7bd52021-02-14 23:22:56 +010080 PROMEX_FL_SCOPE_LI | PROMEX_FL_SCOPE_BACK | \
81 PROMEX_FL_SCOPE_SERVER | PROMEX_FL_SCOPE_STICKTABLE)
Christopher Fauletf959d082019-02-07 15:38:42 +010082
Willy Tarreauae1747d2022-05-03 17:33:00 +020083/* the context of the applet */
84struct promex_ctx {
85 struct proxy *px; /* current proxy */
86 struct stktable *st; /* current table */
87 struct listener *li; /* current listener */
88 struct server *sv; /* current server */
89 unsigned int flags; /* PROMEX_FL_* */
Willy Tarreaude58d242022-05-03 18:05:23 +020090 unsigned field_num; /* current field number (ST_F_* etc) */
Willy Tarreauae1747d2022-05-03 17:33:00 +020091 int obj_state; /* current state among PROMEX_{FRONT|BACK|SRV|LI}_STATE_* */
92};
93
Christopher Faulet0312c0d2021-01-20 15:19:12 +010094/* Promtheus metric type (gauge or counter) */
95enum promex_mt_type {
96 PROMEX_MT_GAUGE = 1,
97 PROMEX_MT_COUNTER = 2,
98};
99
Christopher Fauletf959d082019-02-07 15:38:42 +0100100/* The max length for metrics name. It is a hard limit but it should be
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500101 * enough.
Christopher Fauletf959d082019-02-07 15:38:42 +0100102 */
103#define PROMEX_MAX_NAME_LEN 128
104
105/* The expected max length for a metric dump, including its header lines. It is
106 * just a soft limit to avoid extra work. We don't try to dump a metric if less
107 * than this size is available in the HTX.
108 */
109#define PROMEX_MAX_METRIC_LENGTH 512
110
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100111/* The max number of labels per metric */
112#define PROMEX_MAX_LABELS 8
William Dauchy5a982a72021-01-08 13:18:06 +0100113
Christopher Faulet0312c0d2021-01-20 15:19:12 +0100114/* Describe a prometheus metric */
115struct promex_metric {
116 const struct ist n; /* The metric name */
117 enum promex_mt_type type; /* The metric type (gauge or counter) */
118 unsigned int flags; /* PROMEX_FL_* flags */
119};
120
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100121/* Describe a prometheus metric label. It is just a key/value pair */
122struct promex_label {
123 struct ist name;
124 struct ist value;
125};
126
Christopher Faulet37286a52021-01-20 15:20:53 +0100127/* Global metrics */
128const struct promex_metric promex_global_metrics[INF_TOTAL_FIELDS] = {
129 //[INF_NAME] ignored
130 //[INF_VERSION], ignored
131 //[INF_RELEASE_DATE] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100132 [INF_NBTHREAD] = { .n = IST("nbthread"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
133 [INF_NBPROC] = { .n = IST("nbproc"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
134 [INF_PROCESS_NUM] = { .n = IST("relative_process_id"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
135 //[INF_PID] ignored
136 //[INF_UPTIME] ignored
137 [INF_UPTIME_SEC] = { .n = IST("uptime_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
138 [INF_START_TIME_SEC] = { .n = IST("start_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100139 //[INF_MEMMAX_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100140 [INF_MEMMAX_BYTES] = { .n = IST("max_memory_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100141 //[INF_POOL_ALLOC_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100142 [INF_POOL_ALLOC_BYTES] = { .n = IST("pool_allocated_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100143 //[INF_POOL_USED_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100144 [INF_POOL_USED_BYTES] = { .n = IST("pool_used_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
145 [INF_POOL_FAILED] = { .n = IST("pool_failures_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
146 [INF_ULIMIT_N] = { .n = IST("max_fds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
147 [INF_MAXSOCK] = { .n = IST("max_sockets"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
148 [INF_MAXCONN] = { .n = IST("max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
149 [INF_HARD_MAXCONN] = { .n = IST("hard_max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
150 [INF_CURR_CONN] = { .n = IST("current_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
151 [INF_CUM_CONN] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
152 [INF_CUM_REQ] = { .n = IST("requests_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
153 [INF_MAX_SSL_CONNS] = { .n = IST("max_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
154 [INF_CURR_SSL_CONNS] = { .n = IST("current_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
155 [INF_CUM_SSL_CONNS] = { .n = IST("ssl_connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
156 [INF_MAXPIPES] = { .n = IST("max_pipes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
157 [INF_PIPES_USED] = { .n = IST("pipes_used_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
158 [INF_PIPES_FREE] = { .n = IST("pipes_free_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
159 [INF_CONN_RATE] = { .n = IST("current_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
160 [INF_CONN_RATE_LIMIT] = { .n = IST("limit_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
161 [INF_MAX_CONN_RATE] = { .n = IST("max_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
162 [INF_SESS_RATE] = { .n = IST("current_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
163 [INF_SESS_RATE_LIMIT] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
164 [INF_MAX_SESS_RATE] = { .n = IST("max_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
165 [INF_SSL_RATE] = { .n = IST("current_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
166 [INF_SSL_RATE_LIMIT] = { .n = IST("limit_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
167 [INF_MAX_SSL_RATE] = { .n = IST("max_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
168 [INF_SSL_FRONTEND_KEY_RATE] = { .n = IST("current_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
169 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .n = IST("max_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
170 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .n = IST("frontend_ssl_reuse"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
171 [INF_SSL_BACKEND_KEY_RATE] = { .n = IST("current_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
172 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .n = IST("max_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
173 [INF_SSL_CACHE_LOOKUPS] = { .n = IST("ssl_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
174 [INF_SSL_CACHE_MISSES] = { .n = IST("ssl_cache_misses_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
175 [INF_COMPRESS_BPS_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
176 [INF_COMPRESS_BPS_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
177 [INF_COMPRESS_BPS_RATE_LIM] = { .n = IST("limit_http_comp"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
178 [INF_ZLIB_MEM_USAGE] = { .n = IST("current_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
179 [INF_MAX_ZLIB_MEM_USAGE] = { .n = IST("max_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
180 [INF_TASKS] = { .n = IST("current_tasks"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
181 [INF_RUN_QUEUE] = { .n = IST("current_run_queue"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
182 [INF_IDLE_PCT] = { .n = IST("idle_time_percent"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
183 //[INF_NODE] ignored
184 //[INF_DESCRIPTION] ignored
185 [INF_STOPPING] = { .n = IST("stopping"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
186 [INF_JOBS] = { .n = IST("jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
187 [INF_UNSTOPPABLE_JOBS] = { .n = IST("unstoppable_jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
188 [INF_LISTENERS] = { .n = IST("listeners"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
189 [INF_ACTIVE_PEERS] = { .n = IST("active_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
190 [INF_CONNECTED_PEERS] = { .n = IST("connected_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
191 [INF_DROPPED_LOGS] = { .n = IST("dropped_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
192 [INF_BUSY_POLLING] = { .n = IST("busy_polling_enabled"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
193 [INF_FAILED_RESOLUTIONS] = { .n = IST("failed_resolutions"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
194 [INF_TOTAL_BYTES_OUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
195 [INF_TOTAL_SPLICED_BYTES_OUT] = { .n = IST("spliced_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
196 [INF_BYTES_OUT_RATE] = { .n = IST("bytes_out_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
197 //[INF_DEBUG_COMMANDS_ISSUED] ignored
William Dauchy7741c332021-02-01 13:11:57 +0100198 [INF_CUM_LOG_MSGS] = { .n = IST("recv_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100199 [INF_BUILD_INFO] = { .n = IST("build_info"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
Christopher Fauletf959d082019-02-07 15:38:42 +0100200};
201
Christopher Faulet37286a52021-01-20 15:20:53 +0100202/* frontend/backend/server fields */
203const struct promex_metric promex_st_metrics[ST_F_TOTAL_FIELDS] = {
William Dauchy42d7c402021-11-07 10:18:47 +0100204 //[ST_F_PXNAME] ignored
205 //[ST_F_SVNAME] ignored
206 [ST_F_QCUR] = { .n = IST("current_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
207 [ST_F_QMAX] = { .n = IST("max_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
208 [ST_F_SCUR] = { .n = IST("current_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
209 [ST_F_SMAX] = { .n = IST("max_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
210 [ST_F_SLIM] = { .n = IST("limit_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
211 [ST_F_STOT] = { .n = IST("sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
212 [ST_F_BIN] = { .n = IST("bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
213 [ST_F_BOUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
214 [ST_F_DREQ] = { .n = IST("requests_denied_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC ) },
215 [ST_F_DRESP] = { .n = IST("responses_denied_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
216 [ST_F_EREQ] = { .n = IST("request_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
217 [ST_F_ECON] = { .n = IST("connection_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
218 [ST_F_ERESP] = { .n = IST("response_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
219 [ST_F_WRETR] = { .n = IST("retry_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
220 [ST_F_WREDIS] = { .n = IST("redispatch_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
221 [ST_F_STATUS] = { .n = IST("status"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
222 [ST_F_WEIGHT] = { .n = IST("weight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
223 [ST_F_ACT] = { .n = IST("active_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
224 [ST_F_BCK] = { .n = IST("backup_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
225 [ST_F_CHKFAIL] = { .n = IST("check_failures_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_SRV_METRIC) },
226 [ST_F_CHKDOWN] = { .n = IST("check_up_down_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
227 [ST_F_LASTCHG] = { .n = IST("check_last_change_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
228 [ST_F_DOWNTIME] = { .n = IST("downtime_seconds_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
229 [ST_F_QLIMIT] = { .n = IST("queue_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
230 //[ST_F_PID] ignored
231 //[ST_F_IID] ignored
232 //[ST_F_SID] ignored
233 [ST_F_THROTTLE] = { .n = IST("current_throttle"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
234 [ST_F_LBTOT] = { .n = IST("loadbalanced_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
235 //[ST_F_TRACKED] ignored
236 //[ST_F_TYPE] ignored
237 //[ST_F_RATE] ignored
238 [ST_F_RATE_LIM] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
239 [ST_F_RATE_MAX] = { .n = IST("max_session_rate"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
240 [ST_F_CHECK_STATUS] = { .n = IST("check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
241 [ST_F_CHECK_CODE] = { .n = IST("check_code"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
242 [ST_F_CHECK_DURATION] = { .n = IST("check_duration_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
243 [ST_F_HRSP_1XX] = { .n = IST("http_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
244 [ST_F_HRSP_2XX] = { .n = IST("http_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
245 [ST_F_HRSP_3XX] = { .n = IST("http_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
246 [ST_F_HRSP_4XX] = { .n = IST("http_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
247 [ST_F_HRSP_5XX] = { .n = IST("http_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
248 [ST_F_HRSP_OTHER] = { .n = IST("http_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
249 //[ST_F_HANAFAIL] ignored
250 //[ST_F_REQ_RATE] ignored
251 [ST_F_REQ_RATE_MAX] = { .n = IST("http_requests_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
252 [ST_F_REQ_TOT] = { .n = IST("http_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
253 [ST_F_CLI_ABRT] = { .n = IST("client_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
254 [ST_F_SRV_ABRT] = { .n = IST("server_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
255 [ST_F_COMP_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
256 [ST_F_COMP_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
257 [ST_F_COMP_BYP] = { .n = IST("http_comp_bytes_bypassed_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
258 [ST_F_COMP_RSP] = { .n = IST("http_comp_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
259 [ST_F_LASTSESS] = { .n = IST("last_session_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
260 //[ST_F_LAST_CHK] ignored
261 //[ST_F_LAST_AGT] ignored
262 [ST_F_QTIME] = { .n = IST("queue_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
263 [ST_F_CTIME] = { .n = IST("connect_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
264 [ST_F_RTIME] = { .n = IST("response_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
265 [ST_F_TTIME] = { .n = IST("total_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
266 //[ST_F_AGENT_STATUS] ignored
267 //[ST_F_AGENT_CODE] ignored
268 //[ST_F_AGENT_DURATION] ignored
269 //[ST_F_CHECK_DESC] ignored
270 //[ST_F_AGENT_DESC] ignored
271 //[ST_F_CHECK_RISE] ignored
272 //[ST_F_CHECK_FALL] ignored
273 //[ST_F_CHECK_HEALTH] ignored
274 //[ST_F_AGENT_RISE] ignored
275 //[ST_F_AGENT_FALL] ignored
276 //[ST_F_AGENT_HEALTH] ignored
277 //[ST_F_ADDR] ignored
278 //[ST_F_COOKIE] ignored
279 //[ST_F_MODE] ignored
280 //[ST_F_ALGO] ignored
281 //[ST_F_CONN_RATE] ignored
282 [ST_F_CONN_RATE_MAX] = { .n = IST("connections_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
283 [ST_F_CONN_TOT] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
284 [ST_F_INTERCEPTED] = { .n = IST("intercepted_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
285 [ST_F_DCON] = { .n = IST("denied_connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
286 [ST_F_DSES] = { .n = IST("denied_sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
287 [ST_F_WREW] = { .n = IST("failed_header_rewriting_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
288 [ST_F_CONNECT] = { .n = IST("connection_attempts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
289 [ST_F_REUSE] = { .n = IST("connection_reuses_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
290 [ST_F_CACHE_LOOKUPS] = { .n = IST("http_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
291 [ST_F_CACHE_HITS] = { .n = IST("http_cache_hits_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
292 [ST_F_SRV_ICUR] = { .n = IST("idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
293 [ST_F_SRV_ILIM] = { .n = IST("idle_connections_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
294 [ST_F_QT_MAX] = { .n = IST("max_queue_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
295 [ST_F_CT_MAX] = { .n = IST("max_connect_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
296 [ST_F_RT_MAX] = { .n = IST("max_response_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
297 [ST_F_TT_MAX] = { .n = IST("max_total_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
298 [ST_F_EINT] = { .n = IST("internal_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
299 [ST_F_IDLE_CONN_CUR] = { .n = IST("unsafe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
300 [ST_F_SAFE_CONN_CUR] = { .n = IST("safe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
301 [ST_F_USED_CONN_CUR] = { .n = IST("used_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
302 [ST_F_NEED_CONN_EST] = { .n = IST("need_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
303 [ST_F_UWEIGHT] = { .n = IST("uweight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
304 [ST_F_AGG_SRV_CHECK_STATUS] = { .n = IST("agg_server_check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
Cedric Paillet7d6644e2022-12-08 09:17:00 +0000305 [ST_F_AGG_SRV_STATUS ] = { .n = IST("agg_server_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
Christopher Fauletf959d082019-02-07 15:38:42 +0100306};
307
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500308/* Description of overridden stats fields */
Christopher Fauletf959d082019-02-07 15:38:42 +0100309const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
William Dauchya1da7ba2021-02-01 13:11:52 +0100310 [ST_F_STATUS] = IST("Current status of the service, per state label value."),
William Dauchyde3c3262021-02-01 13:11:51 +0100311 [ST_F_CHECK_STATUS] = IST("Status of last health check, per state label value."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100312 [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."),
Christopher Faulet2711e512020-02-27 16:12:07 +0100313 [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100314 [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."),
315 [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."),
316 [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."),
317 [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100318 [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"),
319 [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"),
320 [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"),
321 [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100322};
323
William Dauchy69164222021-02-07 20:42:38 +0100324/* stick table base fields */
325enum sticktable_field {
326 STICKTABLE_SIZE = 0,
327 STICKTABLE_USED,
328 /* must always be the last one */
329 STICKTABLE_TOTAL_FIELDS
330};
331
332const struct promex_metric promex_sticktable_metrics[STICKTABLE_TOTAL_FIELDS] = {
333 [STICKTABLE_SIZE] = { .n = IST("size"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC },
334 [STICKTABLE_USED] = { .n = IST("used"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC },
335};
336
337/* stick table base description */
338const struct ist promex_sticktable_metric_desc[STICKTABLE_TOTAL_FIELDS] = {
339 [STICKTABLE_SIZE] = IST("Stick table size."),
340 [STICKTABLE_USED] = IST("Number of entries used in this stick table."),
341};
342
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100343/* Specific labels for all ST_F_HRSP_* fields */
344const struct ist promex_hrsp_code[1 + ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = {
345 [ST_F_HRSP_1XX - ST_F_HRSP_1XX] = IST("1xx"),
346 [ST_F_HRSP_2XX - ST_F_HRSP_1XX] = IST("2xx"),
347 [ST_F_HRSP_3XX - ST_F_HRSP_1XX] = IST("3xx"),
348 [ST_F_HRSP_4XX - ST_F_HRSP_1XX] = IST("4xx"),
349 [ST_F_HRSP_5XX - ST_F_HRSP_1XX] = IST("5xx"),
350 [ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = IST("other"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100351};
352
William Dauchy54938212021-01-27 22:40:16 +0100353enum promex_front_state {
354 PROMEX_FRONT_STATE_DOWN = 0,
355 PROMEX_FRONT_STATE_UP,
356
357 PROMEX_FRONT_STATE_COUNT /* must be last */
358};
359
360const struct ist promex_front_st[PROMEX_FRONT_STATE_COUNT] = {
361 [PROMEX_FRONT_STATE_DOWN] = IST("DOWN"),
362 [PROMEX_FRONT_STATE_UP] = IST("UP"),
363};
364
365enum promex_back_state {
366 PROMEX_BACK_STATE_DOWN = 0,
367 PROMEX_BACK_STATE_UP,
368
369 PROMEX_BACK_STATE_COUNT /* must be last */
370};
371
372const struct ist promex_back_st[PROMEX_BACK_STATE_COUNT] = {
373 [PROMEX_BACK_STATE_DOWN] = IST("DOWN"),
374 [PROMEX_BACK_STATE_UP] = IST("UP"),
375};
376
377enum promex_srv_state {
378 PROMEX_SRV_STATE_DOWN = 0,
379 PROMEX_SRV_STATE_UP,
380 PROMEX_SRV_STATE_MAINT,
381 PROMEX_SRV_STATE_DRAIN,
382 PROMEX_SRV_STATE_NOLB,
383
384 PROMEX_SRV_STATE_COUNT /* must be last */
385};
386
387const struct ist promex_srv_st[PROMEX_SRV_STATE_COUNT] = {
388 [PROMEX_SRV_STATE_DOWN] = IST("DOWN"),
389 [PROMEX_SRV_STATE_UP] = IST("UP"),
390 [PROMEX_SRV_STATE_MAINT] = IST("MAINT"),
391 [PROMEX_SRV_STATE_DRAIN] = IST("DRAIN"),
392 [PROMEX_SRV_STATE_NOLB] = IST("NOLB"),
393};
394
395/* Return the server status. */
396enum promex_srv_state promex_srv_status(struct server *sv)
Christopher Fauletf959d082019-02-07 15:38:42 +0100397{
William Dauchy54938212021-01-27 22:40:16 +0100398 int state = PROMEX_SRV_STATE_DOWN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100399
Christopher Fauletf959d082019-02-07 15:38:42 +0100400 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Dauchy54938212021-01-27 22:40:16 +0100401 state = PROMEX_SRV_STATE_UP;
Christopher Fauletf959d082019-02-07 15:38:42 +0100402 if (sv->cur_admin & SRV_ADMF_DRAIN)
William Dauchy54938212021-01-27 22:40:16 +0100403 state = PROMEX_SRV_STATE_DRAIN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100404 }
Christopher Fauletd45d1052019-09-06 16:10:19 +0200405 else if (sv->cur_state == SRV_ST_STOPPING)
William Dauchy54938212021-01-27 22:40:16 +0100406 state = PROMEX_SRV_STATE_NOLB;
Christopher Fauletd45d1052019-09-06 16:10:19 +0200407
408 if (sv->cur_admin & SRV_ADMF_MAINT)
William Dauchy54938212021-01-27 22:40:16 +0100409 state = PROMEX_SRV_STATE_MAINT;
Christopher Fauletf959d082019-02-07 15:38:42 +0100410
411 return state;
412}
413
414/* Convert a field to its string representation and write it in <out>, followed
415 * by a newline, if there is enough space. non-numeric value are converted in
William Dauchy18a2c6e2021-01-22 21:09:47 +0100416 * "NaN" because Prometheus only support numerical values (but it is unexepceted
Christopher Fauletf959d082019-02-07 15:38:42 +0100417 * to process this kind of value). It returns 1 on success. Otherwise, it
418 * returns 0. The buffer's length must not exceed <max> value.
419 */
420static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
421{
422 int ret = 0;
423
424 switch (field_format(f, 0)) {
William Dauchy18a2c6e2021-01-22 21:09:47 +0100425 case FF_EMPTY: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100426 case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break;
427 case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break;
428 case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break;
429 case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200430 case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break;
William Dauchy18a2c6e2021-01-22 21:09:47 +0100431 case FF_STR: ret = chunk_strcat(out, "NaN\n"); break;
432 default: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100433 }
434 if (!ret || out->data > max)
435 return 0;
436 return 1;
437}
438
Christopher Fauletf959d082019-02-07 15:38:42 +0100439/* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
440 * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
441 */
442static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
Christopher Faulet37286a52021-01-20 15:20:53 +0100443 const struct promex_metric *metric, const struct ist name,
444 struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100445{
Willy Tarreaude58d242022-05-03 18:05:23 +0200446 struct promex_ctx *ctx = appctx->svcctx;
Christopher Faulet37286a52021-01-20 15:20:53 +0100447 struct ist type;
William Dauchy82b2ce22021-02-01 13:11:55 +0100448 struct ist desc;
Christopher Faulet37286a52021-01-20 15:20:53 +0100449
450 switch (metric->type) {
451 case PROMEX_MT_COUNTER:
452 type = ist("counter");
453 break;
454 default:
455 type = ist("gauge");
456 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100457
William Dauchya191b772021-01-15 22:41:39 +0100458 if (istcat(out, ist("# HELP "), max) == -1 ||
459 istcat(out, name, max) == -1 ||
460 istcat(out, ist(" "), max) == -1)
461 goto full;
462
William Dauchy82b2ce22021-02-01 13:11:55 +0100463 if (metric->flags & PROMEX_FL_INFO_METRIC)
Willy Tarreaude58d242022-05-03 18:05:23 +0200464 desc = ist(info_fields[ctx->field_num].desc);
William Dauchy69164222021-02-07 20:42:38 +0100465 else if (metric->flags & PROMEX_FL_STICKTABLE_METRIC)
Willy Tarreaude58d242022-05-03 18:05:23 +0200466 desc = promex_sticktable_metric_desc[ctx->field_num];
467 else if (!isttest(promex_st_metric_desc[ctx->field_num]))
468 desc = ist(stat_fields[ctx->field_num].desc);
William Dauchy82b2ce22021-02-01 13:11:55 +0100469 else
Willy Tarreaude58d242022-05-03 18:05:23 +0200470 desc = promex_st_metric_desc[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100471
William Dauchy82b2ce22021-02-01 13:11:55 +0100472 if (istcat(out, desc, max) == -1 ||
473 istcat(out, ist("\n# TYPE "), max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100474 istcat(out, name, max) == -1 ||
475 istcat(out, ist(" "), max) == -1 ||
Christopher Faulet37286a52021-01-20 15:20:53 +0100476 istcat(out, type, max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100477 istcat(out, ist("\n"), max) == -1)
478 goto full;
479
480 return 1;
481
482 full:
483 return 0;
484}
485
486/* Dump the line for <metric>. It starts by the metric name followed by its
487 * labels (proxy name, server name...) between braces and finally its value. If
488 * not already done, the header lines are dumped first. It returns 1 on
489 * success. Otherwise if <out> length exceeds <max>, it returns 0.
490 */
Christopher Faulet37286a52021-01-20 15:20:53 +0100491static int promex_dump_metric(struct appctx *appctx, struct htx *htx, struct ist prefix,
William Dauchyc6464592021-01-27 22:40:17 +0100492 const struct promex_metric *metric, struct field *val,
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100493 struct promex_label *labels, struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100494{
495 struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
Willy Tarreauae1747d2022-05-03 17:33:00 +0200496 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100497 size_t len = out->len;
498
499 if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
500 return 0;
501
Christopher Faulet37286a52021-01-20 15:20:53 +0100502 /* Fill the metric name */
503 istcat(&name, prefix, PROMEX_MAX_NAME_LEN);
504 istcat(&name, metric->n, PROMEX_MAX_NAME_LEN);
505
506
Willy Tarreauae1747d2022-05-03 17:33:00 +0200507 if ((ctx->flags & PROMEX_FL_METRIC_HDR) &&
Christopher Faulet37286a52021-01-20 15:20:53 +0100508 !promex_dump_metric_header(appctx, htx, metric, name, out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100509 goto full;
510
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100511 if (istcat(out, name, max) == -1)
512 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100513
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100514 if (isttest(labels[0].name)) {
515 int i;
516
517 if (istcat(out, ist("{"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100518 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100519
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100520 for (i = 0; isttest(labels[i].name); i++) {
521 if (!isttest(labels[i].value))
522 continue;
523
524 if ((i && istcat(out, ist(","), max) == -1) ||
525 istcat(out, labels[i].name, max) == -1 ||
526 istcat(out, ist("=\""), max) == -1 ||
527 istcat(out, labels[i].value, max) == -1 ||
528 istcat(out, ist("\""), max) == -1)
529 goto full;
530 }
531
532 if (istcat(out, ist("}"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100533 goto full;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100534
Christopher Fauletf959d082019-02-07 15:38:42 +0100535 }
536
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100537 if (istcat(out, ist(" "), max) == -1)
538 goto full;
539
Christopher Fauletf959d082019-02-07 15:38:42 +0100540 trash.data = out->len;
Christopher Faulet37286a52021-01-20 15:20:53 +0100541 if (!promex_metric_to_str(&trash, val, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100542 goto full;
543 out->len = trash.data;
544
Willy Tarreauae1747d2022-05-03 17:33:00 +0200545 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100546 return 1;
547 full:
548 // Restore previous length
549 out->len = len;
550 return 0;
551
552}
553
554
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500555/* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100556 * 0 if <htx> is full and -1 in case of any error. */
557static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
558{
559 static struct ist prefix = IST("haproxy_process_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200560 struct promex_ctx *ctx = appctx->svcctx;
Christopher Faulet37286a52021-01-20 15:20:53 +0100561 struct field val;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200562 struct channel *chn = sc_ic(appctx_sc(appctx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100563 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200564 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100565 int ret = 1;
566
Willy Tarreau0b26b382021-05-08 07:43:53 +0200567 if (!stats_fill_info(info, INF_TOTAL_FIELDS, 0))
William Dauchy5d9b8f32021-01-11 20:07:49 +0100568 return -1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100569
Willy Tarreaude58d242022-05-03 18:05:23 +0200570 for (; ctx->field_num < INF_TOTAL_FIELDS; ctx->field_num++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100571 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
572
Willy Tarreaude58d242022-05-03 18:05:23 +0200573 if (!(promex_global_metrics[ctx->field_num].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100574 continue;
575
Willy Tarreaude58d242022-05-03 18:05:23 +0200576 switch (ctx->field_num) {
William Dauchy5a982a72021-01-08 13:18:06 +0100577 case INF_BUILD_INFO:
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100578 labels[0].name = ist("version");
579 labels[0].value = ist(HAPROXY_VERSION);
Christopher Faulet37286a52021-01-20 15:20:53 +0100580 val = mkf_u32(FN_GAUGE, 1);
William Dauchy5a982a72021-01-08 13:18:06 +0100581 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100582
583 default:
Willy Tarreaude58d242022-05-03 18:05:23 +0200584 val = info[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100585 }
586
Willy Tarreaude58d242022-05-03 18:05:23 +0200587 if (!promex_dump_metric(appctx, htx, prefix, &promex_global_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100588 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100589 goto full;
590
Willy Tarreauae1747d2022-05-03 17:33:00 +0200591 ctx->flags |= PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100592 }
593
594 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200595 if (out.len) {
596 if (!htx_add_data_atonce(htx, out))
597 return -1; /* Unexpected and unrecoverable error */
598 channel_add_input(chn, out.len);
599 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100600 return ret;
601 full:
602 ret = 0;
603 goto end;
604}
605
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500606/* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100607 * 0 if <htx> is full and -1 in case of any error. */
608static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
609{
610 static struct ist prefix = IST("haproxy_frontend_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200611 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100612 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100613 struct field val;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200614 struct channel *chn = sc_ic(appctx_sc(appctx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100615 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200616 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchyb9577452021-01-17 18:27:46 +0100617 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100618 int ret = 1;
William Dauchyc6464592021-01-27 22:40:17 +0100619 enum promex_front_state state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100620
Willy Tarreaude58d242022-05-03 18:05:23 +0200621 for (;ctx->field_num < ST_F_TOTAL_FIELDS; ctx->field_num++) {
622 if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100623 continue;
624
Willy Tarreauae1747d2022-05-03 17:33:00 +0200625 while (ctx->px) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100626 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
627
Willy Tarreauae1747d2022-05-03 17:33:00 +0200628 px = ctx->px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100629
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100630 labels[0].name = ist("proxy");
631 labels[0].value = ist2(px->id, strlen(px->id));
632
Christopher Fauletf959d082019-02-07 15:38:42 +0100633 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200634 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100635 goto next_px;
636
Willy Tarreaude58d242022-05-03 18:05:23 +0200637 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(ctx->field_num)))
William Dauchyb9577452021-01-17 18:27:46 +0100638 return -1;
639
Willy Tarreaude58d242022-05-03 18:05:23 +0200640 switch (ctx->field_num) {
Christopher Fauletf959d082019-02-07 15:38:42 +0100641 case ST_F_STATUS:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200642 state = !(px->flags & PR_FL_STOPPED);
Willy Tarreauae1747d2022-05-03 17:33:00 +0200643 for (; ctx->obj_state < PROMEX_FRONT_STATE_COUNT; ctx->obj_state++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100644 labels[1].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200645 labels[1].value = promex_front_st[ctx->obj_state];
646 val = mkf_u32(FO_STATUS, state == ctx->obj_state);
Willy Tarreaude58d242022-05-03 18:05:23 +0200647 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100648 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100649 goto full;
650 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200651 ctx->obj_state = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100652 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100653 case ST_F_REQ_RATE_MAX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100654 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100655 case ST_F_INTERCEPTED:
Christopher Fauletf959d082019-02-07 15:38:42 +0100656 case ST_F_CACHE_LOOKUPS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100657 case ST_F_CACHE_HITS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100658 case ST_F_COMP_IN:
Christopher Fauletf959d082019-02-07 15:38:42 +0100659 case ST_F_COMP_OUT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100660 case ST_F_COMP_BYP:
William Dauchyb9577452021-01-17 18:27:46 +0100661 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100662 if (px->mode != PR_MODE_HTTP)
663 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +0200664 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100665 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100666 case ST_F_HRSP_1XX:
William Dauchyb9577452021-01-17 18:27:46 +0100667 case ST_F_HRSP_2XX:
668 case ST_F_HRSP_3XX:
669 case ST_F_HRSP_4XX:
670 case ST_F_HRSP_5XX:
671 case ST_F_HRSP_OTHER:
Christopher Fauletf959d082019-02-07 15:38:42 +0100672 if (px->mode != PR_MODE_HTTP)
673 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +0200674 if (ctx->field_num != ST_F_HRSP_1XX)
Willy Tarreauae1747d2022-05-03 17:33:00 +0200675 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100676 labels[1].name = ist("code");
Willy Tarreaude58d242022-05-03 18:05:23 +0200677 labels[1].value = promex_hrsp_code[ctx->field_num - ST_F_HRSP_1XX];
678 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100679 break;
680
681 default:
Willy Tarreaude58d242022-05-03 18:05:23 +0200682 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100683 }
684
Willy Tarreaude58d242022-05-03 18:05:23 +0200685 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100686 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100687 goto full;
688 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +0200689 ctx->px = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100690 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200691 ctx->flags |= PROMEX_FL_METRIC_HDR;
692 ctx->px = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100693 }
694
695 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200696 if (out.len) {
697 if (!htx_add_data_atonce(htx, out))
698 return -1; /* Unexpected and unrecoverable error */
699 channel_add_input(chn, out.len);
700 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100701 return ret;
702 full:
703 ret = 0;
704 goto end;
705}
706
William Dauchye3f7bd52021-02-14 23:22:56 +0100707/* Dump listener metrics (prefixed by "haproxy_listen_"). It returns 1 on
708 * success, 0 if <htx> is full and -1 in case of any error. */
709static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx)
710{
711 static struct ist prefix = IST("haproxy_listener_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200712 struct promex_ctx *ctx = appctx->svcctx;
William Dauchye3f7bd52021-02-14 23:22:56 +0100713 struct proxy *px;
714 struct field val;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200715 struct channel *chn = sc_ic(appctx_sc(appctx));
William Dauchye3f7bd52021-02-14 23:22:56 +0100716 struct ist out = ist2(trash.area, 0);
717 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
718 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
719 struct listener *li;
720 int ret = 1;
721 enum li_status status;
722
Willy Tarreaude58d242022-05-03 18:05:23 +0200723 for (;ctx->field_num < ST_F_TOTAL_FIELDS; ctx->field_num++) {
724 if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
William Dauchye3f7bd52021-02-14 23:22:56 +0100725 continue;
726
Willy Tarreauae1747d2022-05-03 17:33:00 +0200727 while (ctx->px) {
William Dauchye3f7bd52021-02-14 23:22:56 +0100728 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
729
Willy Tarreauae1747d2022-05-03 17:33:00 +0200730 px = ctx->px;
William Dauchye3f7bd52021-02-14 23:22:56 +0100731
732 labels[0].name = ist("proxy");
733 labels[0].value = ist2(px->id, strlen(px->id));
734
735 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200736 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
William Dauchye3f7bd52021-02-14 23:22:56 +0100737 goto next_px;
738
Willy Tarreauae1747d2022-05-03 17:33:00 +0200739 li = ctx->li;
William Dauchye3f7bd52021-02-14 23:22:56 +0100740 list_for_each_entry_from(li, &px->conf.listeners, by_fe) {
741
William Dauchye3f7bd52021-02-14 23:22:56 +0100742 if (!li->counters)
743 continue;
744
William Dauchybaf22732021-02-25 00:53:13 +0100745 labels[1].name = ist("listener");
746 labels[1].value = ist2(li->name, strlen(li->name));
747
William Dauchye3f7bd52021-02-14 23:22:56 +0100748 if (!stats_fill_li_stats(px, li, 0, stats,
Willy Tarreaude58d242022-05-03 18:05:23 +0200749 ST_F_TOTAL_FIELDS, &(ctx->field_num)))
William Dauchye3f7bd52021-02-14 23:22:56 +0100750 return -1;
751
Willy Tarreaude58d242022-05-03 18:05:23 +0200752 switch (ctx->field_num) {
William Dauchye3f7bd52021-02-14 23:22:56 +0100753 case ST_F_STATUS:
754 status = get_li_status(li);
Willy Tarreauae1747d2022-05-03 17:33:00 +0200755 for (; ctx->obj_state < LI_STATE_COUNT; ctx->obj_state++) {
756 val = mkf_u32(FO_STATUS, status == ctx->obj_state);
William Dauchye3f7bd52021-02-14 23:22:56 +0100757 labels[2].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200758 labels[2].value = ist(li_status_st[ctx->obj_state]);
Willy Tarreaude58d242022-05-03 18:05:23 +0200759 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
William Dauchye3f7bd52021-02-14 23:22:56 +0100760 &val, labels, &out, max))
761 goto full;
762 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200763 ctx->obj_state = 0;
William Dauchye3f7bd52021-02-14 23:22:56 +0100764 continue;
765 default:
Willy Tarreaude58d242022-05-03 18:05:23 +0200766 val = stats[ctx->field_num];
William Dauchye3f7bd52021-02-14 23:22:56 +0100767 }
768
769 if (!promex_dump_metric(appctx, htx, prefix,
Willy Tarreaude58d242022-05-03 18:05:23 +0200770 &promex_st_metrics[ctx->field_num],
William Dauchye3f7bd52021-02-14 23:22:56 +0100771 &val, labels, &out, max))
772 goto full;
773 }
774
775 next_px:
776 px = px->next;
Willy Tarreauae1747d2022-05-03 17:33:00 +0200777 ctx->px = px;
778 ctx->li = (px ? LIST_NEXT(&px->conf.listeners, struct listener *, by_fe) : NULL);
William Dauchye3f7bd52021-02-14 23:22:56 +0100779 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200780 ctx->flags |= PROMEX_FL_METRIC_HDR;
781 ctx->px = proxies_list;
782 ctx->li = LIST_NEXT(&proxies_list->conf.listeners, struct listener *, by_fe);
William Dauchye3f7bd52021-02-14 23:22:56 +0100783 }
784
785 end:
786 if (out.len) {
787 if (!htx_add_data_atonce(htx, out))
788 return -1; /* Unexpected and unrecoverable error */
789 channel_add_input(chn, out.len);
790 }
791 return ret;
792 full:
Willy Tarreauae1747d2022-05-03 17:33:00 +0200793 ctx->li = li;
William Dauchye3f7bd52021-02-14 23:22:56 +0100794 ret = 0;
795 goto end;
796}
797
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500798/* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100799 * 0 if <htx> is full and -1 in case of any error. */
800static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
801{
802 static struct ist prefix = IST("haproxy_backend_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200803 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100804 struct proxy *px;
William Dauchy42d7c402021-11-07 10:18:47 +0100805 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100806 struct field val;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200807 struct channel *chn = sc_ic(appctx_sc(appctx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100808 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200809 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchy3c6f0062021-01-25 17:29:02 +0100810 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100811 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200812 double secs;
William Dauchy42d7c402021-11-07 10:18:47 +0100813 enum promex_back_state bkd_state;
814 enum promex_srv_state srv_state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100815
Willy Tarreaude58d242022-05-03 18:05:23 +0200816 for (;ctx->field_num < ST_F_TOTAL_FIELDS; ctx->field_num++) {
817 if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100818 continue;
819
Willy Tarreauae1747d2022-05-03 17:33:00 +0200820 while (ctx->px) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100821 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
William Dauchy42d7c402021-11-07 10:18:47 +0100822 unsigned int srv_state_count[PROMEX_SRV_STATE_COUNT] = { 0 };
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100823
Willy Tarreauae1747d2022-05-03 17:33:00 +0200824 px = ctx->px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100825
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100826 labels[0].name = ist("proxy");
827 labels[0].value = ist2(px->id, strlen(px->id));
828
Christopher Fauletf959d082019-02-07 15:38:42 +0100829 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200830 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100831 goto next_px;
832
Willy Tarreaude58d242022-05-03 18:05:23 +0200833 if (!stats_fill_be_stats(px, 0, stats, ST_F_TOTAL_FIELDS, &(ctx->field_num)))
William Dauchy3c6f0062021-01-25 17:29:02 +0100834 return -1;
835
Willy Tarreaude58d242022-05-03 18:05:23 +0200836 switch (ctx->field_num) {
Cedric Paillet7d6644e2022-12-08 09:17:00 +0000837 case ST_F_AGG_SRV_CHECK_STATUS: // DEPRECATED
838 case ST_F_AGG_SRV_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +0100839 if (!px->srv)
840 goto next_px;
841 sv = px->srv;
842 while (sv) {
843 srv_state = promex_srv_status(sv);
844 srv_state_count[srv_state] += 1;
845 sv = sv->next;
846 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200847 for (; ctx->obj_state < PROMEX_SRV_STATE_COUNT; ctx->obj_state++) {
848 val = mkf_u32(FN_GAUGE, srv_state_count[ctx->obj_state]);
William Dauchy42d7c402021-11-07 10:18:47 +0100849 labels[1].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200850 labels[1].value = promex_srv_st[ctx->obj_state];
Willy Tarreaude58d242022-05-03 18:05:23 +0200851 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
William Dauchy42d7c402021-11-07 10:18:47 +0100852 &val, labels, &out, max))
853 goto full;
854 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200855 ctx->obj_state = 0;
William Dauchy42d7c402021-11-07 10:18:47 +0100856 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100857 case ST_F_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +0100858 bkd_state = ((px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0);
Willy Tarreauae1747d2022-05-03 17:33:00 +0200859 for (; ctx->obj_state < PROMEX_BACK_STATE_COUNT; ctx->obj_state++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100860 labels[1].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200861 labels[1].value = promex_back_st[ctx->obj_state];
862 val = mkf_u32(FO_STATUS, bkd_state == ctx->obj_state);
Willy Tarreaude58d242022-05-03 18:05:23 +0200863 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100864 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100865 goto full;
866 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200867 ctx->obj_state = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100868 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100869 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200870 secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100871 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100872 break;
873 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200874 secs = (double)swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100875 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100876 break;
877 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200878 secs = (double)swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100879 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100880 break;
881 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200882 secs = (double)swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100883 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100884 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100885 case ST_F_QT_MAX:
886 secs = (double)px->be_counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100887 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100888 break;
889 case ST_F_CT_MAX:
890 secs = (double)px->be_counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100891 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100892 break;
893 case ST_F_RT_MAX:
894 secs = (double)px->be_counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100895 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100896 break;
897 case ST_F_TT_MAX:
898 secs = (double)px->be_counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100899 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100900 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100901 case ST_F_REQ_TOT:
William Dauchy3c6f0062021-01-25 17:29:02 +0100902 case ST_F_CACHE_LOOKUPS:
903 case ST_F_CACHE_HITS:
904 case ST_F_COMP_IN:
905 case ST_F_COMP_OUT:
906 case ST_F_COMP_BYP:
907 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100908 if (px->mode != PR_MODE_HTTP)
909 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +0200910 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100911 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100912 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100913 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100914 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100915 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100916 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100917 case ST_F_HRSP_OTHER:
918 if (px->mode != PR_MODE_HTTP)
919 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +0200920 if (ctx->field_num != ST_F_HRSP_1XX)
Willy Tarreauae1747d2022-05-03 17:33:00 +0200921 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100922 labels[1].name = ist("code");
Willy Tarreaude58d242022-05-03 18:05:23 +0200923 labels[1].value = promex_hrsp_code[ctx->field_num - ST_F_HRSP_1XX];
924 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100925 break;
926
927 default:
Willy Tarreaude58d242022-05-03 18:05:23 +0200928 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +0100929 }
930
Willy Tarreaude58d242022-05-03 18:05:23 +0200931 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100932 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100933 goto full;
934 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +0200935 ctx->px = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100936 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200937 ctx->flags |= PROMEX_FL_METRIC_HDR;
938 ctx->px = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100939 }
940
941 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200942 if (out.len) {
943 if (!htx_add_data_atonce(htx, out))
944 return -1; /* Unexpected and unrecoverable error */
945 channel_add_input(chn, out.len);
946 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100947 return ret;
948 full:
949 ret = 0;
950 goto end;
951}
952
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500953/* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100954 * 0 if <htx> is full and -1 in case of any error. */
955static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
956{
957 static struct ist prefix = IST("haproxy_server_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200958 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100959 struct proxy *px;
960 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100961 struct field val;
Willy Tarreauc12b3212022-05-27 11:08:15 +0200962 struct channel *chn = sc_ic(appctx_sc(appctx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100963 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200964 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchybde2bf62021-01-25 17:29:04 +0100965 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100966 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200967 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100968 enum promex_srv_state state;
William Dauchyde3c3262021-02-01 13:11:51 +0100969 const char *check_state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100970
Willy Tarreaude58d242022-05-03 18:05:23 +0200971 for (;ctx->field_num < ST_F_TOTAL_FIELDS; ctx->field_num++) {
972 if (!(promex_st_metrics[ctx->field_num].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100973 continue;
974
Willy Tarreauae1747d2022-05-03 17:33:00 +0200975 while (ctx->px) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100976 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
977
Willy Tarreauae1747d2022-05-03 17:33:00 +0200978 px = ctx->px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100979
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100980 labels[0].name = ist("proxy");
981 labels[0].value = ist2(px->id, strlen(px->id));
982
Christopher Fauletf959d082019-02-07 15:38:42 +0100983 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200984 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100985 goto next_px;
986
Willy Tarreauae1747d2022-05-03 17:33:00 +0200987 while (ctx->sv) {
988 sv = ctx->sv;
Christopher Fauletf959d082019-02-07 15:38:42 +0100989
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100990 labels[1].name = ist("server");
991 labels[1].value = ist2(sv->id, strlen(sv->id));
992
Willy Tarreaude58d242022-05-03 18:05:23 +0200993 if (!stats_fill_sv_stats(px, sv, 0, stats, ST_F_TOTAL_FIELDS, &(ctx->field_num)))
William Dauchybde2bf62021-01-25 17:29:04 +0100994 return -1;
995
Willy Tarreauae1747d2022-05-03 17:33:00 +0200996 if ((ctx->flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
Christopher Fauleteba22942019-11-19 14:18:24 +0100997 goto next_sv;
998
Willy Tarreaude58d242022-05-03 18:05:23 +0200999 switch (ctx->field_num) {
Christopher Fauletf959d082019-02-07 15:38:42 +01001000 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +01001001 state = promex_srv_status(sv);
Willy Tarreauae1747d2022-05-03 17:33:00 +02001002 for (; ctx->obj_state < PROMEX_SRV_STATE_COUNT; ctx->obj_state++) {
1003 val = mkf_u32(FO_STATUS, state == ctx->obj_state);
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001004 labels[2].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +02001005 labels[2].value = promex_srv_st[ctx->obj_state];
Willy Tarreaude58d242022-05-03 18:05:23 +02001006 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001007 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +01001008 goto full;
1009 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001010 ctx->obj_state = 0;
William Dauchyc6464592021-01-27 22:40:17 +01001011 goto next_sv;
Christopher Fauletf959d082019-02-07 15:38:42 +01001012 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001013 secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001014 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001015 break;
1016 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001017 secs = (double)swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001018 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001019 break;
1020 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001021 secs = (double)swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001022 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001023 break;
1024 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001025 secs = (double)swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001026 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001027 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001028 case ST_F_QT_MAX:
1029 secs = (double)sv->counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001030 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001031 break;
1032 case ST_F_CT_MAX:
1033 secs = (double)sv->counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001034 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001035 break;
1036 case ST_F_RT_MAX:
1037 secs = (double)sv->counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001038 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001039 break;
1040 case ST_F_TT_MAX:
1041 secs = (double)sv->counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001042 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001043 break;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001044 case ST_F_CHECK_STATUS:
1045 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1046 goto next_sv;
William Dauchyde3c3262021-02-01 13:11:51 +01001047
Willy Tarreauae1747d2022-05-03 17:33:00 +02001048 for (; ctx->obj_state < HCHK_STATUS_SIZE; ctx->obj_state++) {
1049 if (get_check_status_result(ctx->obj_state) < CHK_RES_FAILED)
William Dauchyde3c3262021-02-01 13:11:51 +01001050 continue;
Willy Tarreauae1747d2022-05-03 17:33:00 +02001051 val = mkf_u32(FO_STATUS, sv->check.status == ctx->obj_state);
1052 check_state = get_check_status_info(ctx->obj_state);
William Dauchyde3c3262021-02-01 13:11:51 +01001053 labels[2].name = ist("state");
Tim Duesterhusb113b5c2021-09-15 13:58:44 +02001054 labels[2].value = ist(check_state);
Willy Tarreaude58d242022-05-03 18:05:23 +02001055 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
William Dauchyde3c3262021-02-01 13:11:51 +01001056 &val, labels, &out, max))
1057 goto full;
1058 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001059 ctx->obj_state = 0;
William Dauchyde3c3262021-02-01 13:11:51 +01001060 goto next_sv;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001061 case ST_F_CHECK_CODE:
1062 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1063 goto next_sv;
Christopher Faulet37286a52021-01-20 15:20:53 +01001064 val = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code);
Christopher Fauletcf403f32019-11-21 14:35:46 +01001065 break;
Christopher Faulet2711e512020-02-27 16:12:07 +01001066 case ST_F_CHECK_DURATION:
1067 if (sv->check.status < HCHK_STATUS_CHECKED)
1068 goto next_sv;
1069 secs = (double)sv->check.duration / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001070 val = mkf_flt(FN_DURATION, secs);
Christopher Faulet2711e512020-02-27 16:12:07 +01001071 break;
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001072 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +01001073 if (px->mode != PR_MODE_HTTP)
1074 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +02001075 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +01001076 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +01001077 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001078 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001079 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001080 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001081 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001082 case ST_F_HRSP_OTHER:
1083 if (px->mode != PR_MODE_HTTP)
1084 goto next_px;
Willy Tarreaude58d242022-05-03 18:05:23 +02001085 if (ctx->field_num != ST_F_HRSP_1XX)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001086 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001087 labels[2].name = ist("code");
Willy Tarreaude58d242022-05-03 18:05:23 +02001088 labels[2].value = promex_hrsp_code[ctx->field_num - ST_F_HRSP_1XX];
1089 val = stats[ctx->field_num];
Christopher Fauletc55a6262020-07-10 15:39:39 +02001090 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001091
1092 default:
Willy Tarreaude58d242022-05-03 18:05:23 +02001093 val = stats[ctx->field_num];
Christopher Fauletf959d082019-02-07 15:38:42 +01001094 }
1095
Willy Tarreaude58d242022-05-03 18:05:23 +02001096 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[ctx->field_num],
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001097 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +01001098 goto full;
Christopher Fauleteba22942019-11-19 14:18:24 +01001099 next_sv:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001100 ctx->sv = sv->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001101 }
1102
1103 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001104 ctx->px = px->next;
1105 ctx->sv = (ctx->px ? ctx->px->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001106 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001107 ctx->flags |= PROMEX_FL_METRIC_HDR;
1108 ctx->px = proxies_list;
1109 ctx->sv = (ctx->px ? ctx->px->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001110 }
1111
1112
1113 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001114 if (out.len) {
1115 if (!htx_add_data_atonce(htx, out))
1116 return -1; /* Unexpected and unrecoverable error */
1117 channel_add_input(chn, out.len);
1118 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001119 return ret;
1120 full:
1121 ret = 0;
1122 goto end;
1123}
1124
William Dauchy69164222021-02-07 20:42:38 +01001125/* Dump stick table metrics (prefixed by "haproxy_sticktable_"). It returns 1 on success,
1126 * 0 if <htx> is full and -1 in case of any error. */
1127static int promex_dump_sticktable_metrics(struct appctx *appctx, struct htx *htx)
1128{
1129 static struct ist prefix = IST("haproxy_sticktable_");
Willy Tarreauae1747d2022-05-03 17:33:00 +02001130 struct promex_ctx *ctx = appctx->svcctx;
William Dauchy69164222021-02-07 20:42:38 +01001131 struct field val;
Willy Tarreauc12b3212022-05-27 11:08:15 +02001132 struct channel *chn = sc_ic(appctx_sc(appctx));
William Dauchy69164222021-02-07 20:42:38 +01001133 struct ist out = ist2(trash.area, 0);
1134 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
1135 int ret = 1;
1136 struct stktable *t;
1137
Willy Tarreaude58d242022-05-03 18:05:23 +02001138 for (; ctx->field_num < STICKTABLE_TOTAL_FIELDS; ctx->field_num++) {
1139 if (!(promex_sticktable_metrics[ctx->field_num].flags & ctx->flags))
William Dauchy69164222021-02-07 20:42:38 +01001140 continue;
1141
Willy Tarreauae1747d2022-05-03 17:33:00 +02001142 while (ctx->st) {
William Dauchy69164222021-02-07 20:42:38 +01001143 struct promex_label labels[PROMEX_MAX_LABELS - 1] = {};
1144
Willy Tarreauae1747d2022-05-03 17:33:00 +02001145 t = ctx->st;
William Dauchy69164222021-02-07 20:42:38 +01001146 if (!t->size)
1147 goto next_px;
1148
1149 labels[0].name = ist("name");
1150 labels[0].value = ist2(t->id, strlen(t->id));
1151 labels[1].name = ist("type");
1152 labels[1].value = ist2(stktable_types[t->type].kw, strlen(stktable_types[t->type].kw));
Willy Tarreaude58d242022-05-03 18:05:23 +02001153 switch (ctx->field_num) {
William Dauchy69164222021-02-07 20:42:38 +01001154 case STICKTABLE_SIZE:
1155 val = mkf_u32(FN_GAUGE, t->size);
1156 break;
1157 case STICKTABLE_USED:
1158 val = mkf_u32(FN_GAUGE, t->current);
1159 break;
1160 default:
1161 goto next_px;
1162 }
1163
1164 if (!promex_dump_metric(appctx, htx, prefix,
Willy Tarreaude58d242022-05-03 18:05:23 +02001165 &promex_sticktable_metrics[ctx->field_num],
William Dauchy69164222021-02-07 20:42:38 +01001166 &val, labels, &out, max))
1167 goto full;
1168
1169 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001170 ctx->st = t->next;
William Dauchy69164222021-02-07 20:42:38 +01001171 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001172 ctx->flags |= PROMEX_FL_METRIC_HDR;
1173 ctx->st = stktables_list;
William Dauchy69164222021-02-07 20:42:38 +01001174 }
1175
1176 end:
1177 if (out.len) {
1178 if (!htx_add_data_atonce(htx, out))
1179 return -1; /* Unexpected and unrecoverable error */
1180 channel_add_input(chn, out.len);
1181 }
1182 return ret;
1183 full:
1184 ret = 0;
1185 goto end;
1186}
1187
Christopher Fauletf959d082019-02-07 15:38:42 +01001188/* Dump all metrics (global, frontends, backends and servers) depending on the
1189 * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001190 * -1 in case of any error.
Willy Tarreauae1747d2022-05-03 17:33:00 +02001191 * Uses <appctx.ctx.stats.px> as a pointer to the current proxy and <sv>/<li>
1192 * as pointers to the current server/listener respectively.
1193 */
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001194static int promex_dump_metrics(struct appctx *appctx, struct stconn *sc, struct htx *htx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001195{
Willy Tarreauae1747d2022-05-03 17:33:00 +02001196 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +01001197 int ret;
1198
1199 switch (appctx->st1) {
1200 case PROMEX_DUMPER_INIT:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001201 ctx->px = NULL;
1202 ctx->st = NULL;
1203 ctx->li = NULL;
1204 ctx->sv = NULL;
1205 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
1206 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001207 ctx->field_num = INF_NAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001208 appctx->st1 = PROMEX_DUMPER_GLOBAL;
Willy Tarreau913bea52022-11-14 07:37:45 +01001209 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001210
1211 case PROMEX_DUMPER_GLOBAL:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001212 if (ctx->flags & PROMEX_FL_SCOPE_GLOBAL) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001213 ret = promex_dump_global_metrics(appctx, htx);
1214 if (ret <= 0) {
1215 if (ret == -1)
1216 goto error;
1217 goto full;
1218 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001219 }
1220
Willy Tarreauae1747d2022-05-03 17:33:00 +02001221 ctx->px = proxies_list;
1222 ctx->st = NULL;
1223 ctx->li = NULL;
1224 ctx->sv = NULL;
1225 ctx->flags &= ~PROMEX_FL_INFO_METRIC;
1226 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC);
1227 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001228 ctx->field_num = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001229 appctx->st1 = PROMEX_DUMPER_FRONT;
Willy Tarreau913bea52022-11-14 07:37:45 +01001230 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001231
1232 case PROMEX_DUMPER_FRONT:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001233 if (ctx->flags & PROMEX_FL_SCOPE_FRONT) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001234 ret = promex_dump_front_metrics(appctx, htx);
1235 if (ret <= 0) {
1236 if (ret == -1)
1237 goto error;
1238 goto full;
1239 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001240 }
1241
Willy Tarreauae1747d2022-05-03 17:33:00 +02001242 ctx->px = proxies_list;
1243 ctx->st = NULL;
1244 ctx->li = LIST_NEXT(&proxies_list->conf.listeners, struct listener *, by_fe);
1245 ctx->sv = NULL;
1246 ctx->flags &= ~PROMEX_FL_FRONT_METRIC;
1247 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_LI_METRIC);
1248 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001249 ctx->field_num = ST_F_PXNAME;
William Dauchye3f7bd52021-02-14 23:22:56 +01001250 appctx->st1 = PROMEX_DUMPER_LI;
Willy Tarreau913bea52022-11-14 07:37:45 +01001251 __fallthrough;
William Dauchye3f7bd52021-02-14 23:22:56 +01001252
1253 case PROMEX_DUMPER_LI:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001254 if (ctx->flags & PROMEX_FL_SCOPE_LI) {
William Dauchye3f7bd52021-02-14 23:22:56 +01001255 ret = promex_dump_listener_metrics(appctx, htx);
1256 if (ret <= 0) {
1257 if (ret == -1)
1258 goto error;
1259 goto full;
1260 }
1261 }
1262
Willy Tarreauae1747d2022-05-03 17:33:00 +02001263 ctx->px = proxies_list;
1264 ctx->st = NULL;
1265 ctx->li = NULL;
1266 ctx->sv = NULL;
1267 ctx->flags &= ~PROMEX_FL_LI_METRIC;
1268 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC);
1269 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001270 ctx->field_num = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001271 appctx->st1 = PROMEX_DUMPER_BACK;
Willy Tarreau913bea52022-11-14 07:37:45 +01001272 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001273
1274 case PROMEX_DUMPER_BACK:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001275 if (ctx->flags & PROMEX_FL_SCOPE_BACK) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001276 ret = promex_dump_back_metrics(appctx, htx);
1277 if (ret <= 0) {
1278 if (ret == -1)
1279 goto error;
1280 goto full;
1281 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001282 }
1283
Willy Tarreauae1747d2022-05-03 17:33:00 +02001284 ctx->px = proxies_list;
1285 ctx->st = NULL;
1286 ctx->li = NULL;
1287 ctx->sv = ctx->px ? ctx->px->srv : NULL;
1288 ctx->flags &= ~PROMEX_FL_BACK_METRIC;
1289 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
1290 ctx->obj_state = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001291 ctx->field_num = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001292 appctx->st1 = PROMEX_DUMPER_SRV;
Willy Tarreau913bea52022-11-14 07:37:45 +01001293 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001294
1295 case PROMEX_DUMPER_SRV:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001296 if (ctx->flags & PROMEX_FL_SCOPE_SERVER) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001297 ret = promex_dump_srv_metrics(appctx, htx);
1298 if (ret <= 0) {
1299 if (ret == -1)
1300 goto error;
1301 goto full;
1302 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001303 }
1304
Willy Tarreauae1747d2022-05-03 17:33:00 +02001305 ctx->px = NULL;
1306 ctx->st = stktables_list;
1307 ctx->li = NULL;
1308 ctx->sv = NULL;
1309 ctx->flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
1310 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC);
Willy Tarreaude58d242022-05-03 18:05:23 +02001311 ctx->field_num = STICKTABLE_SIZE;
William Dauchy69164222021-02-07 20:42:38 +01001312 appctx->st1 = PROMEX_DUMPER_STICKTABLE;
Willy Tarreau913bea52022-11-14 07:37:45 +01001313 __fallthrough;
William Dauchy69164222021-02-07 20:42:38 +01001314
1315 case PROMEX_DUMPER_STICKTABLE:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001316 if (ctx->flags & PROMEX_FL_SCOPE_STICKTABLE) {
William Dauchy69164222021-02-07 20:42:38 +01001317 ret = promex_dump_sticktable_metrics(appctx, htx);
1318 if (ret <= 0) {
1319 if (ret == -1)
1320 goto error;
1321 goto full;
1322 }
1323 }
1324
Willy Tarreauae1747d2022-05-03 17:33:00 +02001325 ctx->px = NULL;
1326 ctx->st = NULL;
1327 ctx->li = NULL;
1328 ctx->sv = NULL;
1329 ctx->flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC);
Willy Tarreaude58d242022-05-03 18:05:23 +02001330 ctx->field_num = 0;
Christopher Fauletf959d082019-02-07 15:38:42 +01001331 appctx->st1 = PROMEX_DUMPER_DONE;
Willy Tarreau913bea52022-11-14 07:37:45 +01001332 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001333
1334 case PROMEX_DUMPER_DONE:
1335 default:
1336 break;
1337 }
1338
1339 return 1;
1340
1341 full:
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001342 sc_need_room(sc);
Christopher Fauletf959d082019-02-07 15:38:42 +01001343 return 0;
1344 error:
1345 /* unrecoverable error */
Willy Tarreauae1747d2022-05-03 17:33:00 +02001346 ctx->px = NULL;
1347 ctx->st = NULL;
1348 ctx->li = NULL;
1349 ctx->sv = NULL;
1350 ctx->flags = 0;
Willy Tarreaude58d242022-05-03 18:05:23 +02001351 ctx->field_num = 0;
Christopher Fauletf959d082019-02-07 15:38:42 +01001352 appctx->st1 = PROMEX_DUMPER_DONE;
1353 return -1;
1354}
1355
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001356/* Parse the query string of request URI to filter the metrics. It returns 1 on
Christopher Faulet78407ce2019-11-18 14:47:08 +01001357 * success and -1 on error. */
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001358static int promex_parse_uri(struct appctx *appctx, struct stconn *sc)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001359{
Willy Tarreauae1747d2022-05-03 17:33:00 +02001360 struct promex_ctx *ctx = appctx->svcctx;
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001361 struct channel *req = sc_oc(sc);
1362 struct channel *res = sc_ic(sc);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001363 struct htx *req_htx, *res_htx;
1364 struct htx_sl *sl;
William Dauchyc65f6562019-11-26 12:56:26 +01001365 char *p, *key, *value;
1366 const char *end;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001367 struct buffer *err;
1368 int default_scopes = PROMEX_FL_SCOPE_ALL;
1369 int len;
1370
1371 /* Get the query-string */
1372 req_htx = htxbuf(&req->buf);
1373 sl = http_get_stline(req_htx);
1374 if (!sl)
1375 goto error;
1376 p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?');
1377 if (!p)
1378 goto end;
1379 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001380
William Dauchyc65f6562019-11-26 12:56:26 +01001381 /* copy the query-string */
1382 len = end - p;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001383 chunk_reset(&trash);
1384 memcpy(trash.area, p, len);
1385 trash.area[len] = 0;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001386 p = trash.area;
William Dauchyc65f6562019-11-26 12:56:26 +01001387 end = trash.area + len;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001388
1389 /* Parse the query-string */
William Dauchyc65f6562019-11-26 12:56:26 +01001390 while (p < end && *p && *p != '#') {
1391 value = NULL;
1392
1393 /* decode parameter name */
1394 key = p;
1395 while (p < end && *p != '=' && *p != '&' && *p != '#')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001396 ++p;
William Dauchyc65f6562019-11-26 12:56:26 +01001397 /* found a value */
1398 if (*p == '=') {
1399 *(p++) = 0;
1400 value = p;
1401 }
1402 else if (*p == '&')
1403 *(p++) = 0;
1404 else if (*p == '#')
1405 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001406 len = url_decode(key, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001407 if (len == -1)
1408 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001409
William Dauchyc65f6562019-11-26 12:56:26 +01001410 /* decode value */
1411 if (value) {
1412 while (p < end && *p != '=' && *p != '&' && *p != '#')
1413 ++p;
1414 if (*p == '=')
1415 goto error;
1416 if (*p == '&')
1417 *(p++) = 0;
1418 else if (*p == '#')
1419 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001420 len = url_decode(value, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001421 if (len == -1)
1422 goto error;
1423 }
1424
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001425 if (strcmp(key, "scope") == 0) {
William Dauchyc65f6562019-11-26 12:56:26 +01001426 default_scopes = 0; /* at least a scope defined, unset default scopes */
1427 if (!value)
1428 goto error;
1429 else if (*value == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001430 ctx->flags &= ~PROMEX_FL_SCOPE_ALL;
William Dauchyc65f6562019-11-26 12:56:26 +01001431 else if (*value == '*')
Willy Tarreauae1747d2022-05-03 17:33:00 +02001432 ctx->flags |= PROMEX_FL_SCOPE_ALL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001433 else if (strcmp(value, "global") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001434 ctx->flags |= PROMEX_FL_SCOPE_GLOBAL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001435 else if (strcmp(value, "server") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001436 ctx->flags |= PROMEX_FL_SCOPE_SERVER;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001437 else if (strcmp(value, "backend") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001438 ctx->flags |= PROMEX_FL_SCOPE_BACK;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001439 else if (strcmp(value, "frontend") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001440 ctx->flags |= PROMEX_FL_SCOPE_FRONT;
William Dauchye3f7bd52021-02-14 23:22:56 +01001441 else if (strcmp(value, "listener") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001442 ctx->flags |= PROMEX_FL_SCOPE_LI;
William Dauchy69164222021-02-07 20:42:38 +01001443 else if (strcmp(value, "sticktable") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001444 ctx->flags |= PROMEX_FL_SCOPE_STICKTABLE;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001445 else
1446 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001447 }
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001448 else if (strcmp(key, "no-maint") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001449 ctx->flags |= PROMEX_FL_NO_MAINT_SRV;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001450 }
1451
1452 end:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001453 ctx->flags |= default_scopes;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001454 return 1;
1455
1456 error:
1457 err = &http_err_chunks[HTTP_ERR_400];
1458 channel_erase(res);
1459 res->buf.data = b_data(err);
1460 memcpy(res->buf.area, b_head(err), b_data(err));
1461 res_htx = htx_from_buf(&res->buf);
1462 channel_add_input(res, res_htx->data);
1463 appctx->st0 = PROMEX_ST_END;
1464 return -1;
1465}
1466
Christopher Fauletf959d082019-02-07 15:38:42 +01001467/* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is
1468 * full. */
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001469static int promex_send_headers(struct appctx *appctx, struct stconn *sc, struct htx *htx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001470{
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001471 struct channel *chn = sc_ic(sc);
Christopher Fauletf959d082019-02-07 15:38:42 +01001472 struct htx_sl *sl;
1473 unsigned int flags;
1474
1475 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);
1476 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
1477 if (!sl)
1478 goto full;
1479 sl->info.res.status = 200;
1480 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001481 !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) ||
1482 !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) ||
1483 !htx_add_endof(htx, HTX_BLK_EOH))
1484 goto full;
1485
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001486 channel_add_input(chn, htx->data);
Christopher Fauletf959d082019-02-07 15:38:42 +01001487 return 1;
1488 full:
1489 htx_reset(htx);
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001490 sc_need_room(sc);
Christopher Fauletf959d082019-02-07 15:38:42 +01001491 return 0;
1492}
1493
1494/* The function returns 1 if the initialisation is complete, 0 if
1495 * an errors occurs and -1 if more data are required for initializing
1496 * the applet.
1497 */
Christopher Faulet4aa1d282022-01-13 16:01:35 +01001498static int promex_appctx_init(struct appctx *appctx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001499{
Willy Tarreauae1747d2022-05-03 17:33:00 +02001500 applet_reserve_svcctx(appctx, sizeof(struct promex_ctx));
Christopher Fauletf959d082019-02-07 15:38:42 +01001501 appctx->st0 = PROMEX_ST_INIT;
Christopher Fauletc9929382022-05-12 11:52:27 +02001502 return 0;
Christopher Fauletf959d082019-02-07 15:38:42 +01001503}
1504
1505/* The main I/O handler for the promex applet. */
1506static void promex_appctx_handle_io(struct appctx *appctx)
1507{
Willy Tarreauc12b3212022-05-27 11:08:15 +02001508 struct stconn *sc = appctx_sc(appctx);
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001509 struct stream *s = __sc_strm(sc);
1510 struct channel *req = sc_oc(sc);
1511 struct channel *res = sc_ic(sc);
Christopher Fauletf959d082019-02-07 15:38:42 +01001512 struct htx *req_htx, *res_htx;
1513 int ret;
1514
1515 res_htx = htx_from_buf(&res->buf);
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001516 if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO))
Christopher Fauletf959d082019-02-07 15:38:42 +01001517 goto out;
1518
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001519 /* Check if the input buffer is available. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001520 if (!b_size(&res->buf)) {
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001521 sc_need_room(sc);
Christopher Fauletf959d082019-02-07 15:38:42 +01001522 goto out;
1523 }
1524
1525 switch (appctx->st0) {
1526 case PROMEX_ST_INIT:
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001527 ret = promex_parse_uri(appctx, sc);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001528 if (ret <= 0) {
1529 if (ret == -1)
1530 goto error;
1531 goto out;
1532 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001533 appctx->st0 = PROMEX_ST_HEAD;
1534 appctx->st1 = PROMEX_DUMPER_INIT;
Willy Tarreau913bea52022-11-14 07:37:45 +01001535 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001536
1537 case PROMEX_ST_HEAD:
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001538 if (!promex_send_headers(appctx, sc, res_htx))
Christopher Fauletf959d082019-02-07 15:38:42 +01001539 goto out;
1540 appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP);
Willy Tarreau913bea52022-11-14 07:37:45 +01001541 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001542
1543 case PROMEX_ST_DUMP:
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001544 ret = promex_dump_metrics(appctx, sc, res_htx);
Christopher Fauletf959d082019-02-07 15:38:42 +01001545 if (ret <= 0) {
1546 if (ret == -1)
1547 goto error;
1548 goto out;
1549 }
1550 appctx->st0 = PROMEX_ST_DONE;
Willy Tarreau913bea52022-11-14 07:37:45 +01001551 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001552
1553 case PROMEX_ST_DONE:
Christopher Fauletbe69cbd2022-04-07 10:19:46 +02001554 /* no more data are expected. If the response buffer is
1555 * empty, be sure to add something (EOT block in this
1556 * case) to have something to send. It is important to
1557 * be sure the EOM flags will be handled by the
1558 * endpoint.
1559 */
1560 if (htx_is_empty(res_htx)) {
1561 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001562 sc_need_room(sc);
Christopher Fauletbe69cbd2022-04-07 10:19:46 +02001563 goto out;
1564 }
1565 channel_add_input(res, 1);
1566 }
1567 res_htx->flags |= HTX_FL_EOM;
Christopher Fauletbef64b22022-03-07 15:56:20 +01001568 res->flags |= CF_EOI;
Willy Tarreaud869e132022-05-17 18:05:31 +02001569 se_fl_set(appctx->sedesc, SE_FL_EOI);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001570 appctx->st0 = PROMEX_ST_END;
Willy Tarreau913bea52022-11-14 07:37:45 +01001571 __fallthrough;
Christopher Fauletf959d082019-02-07 15:38:42 +01001572
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001573 case PROMEX_ST_END:
1574 if (!(res->flags & CF_SHUTR)) {
1575 res->flags |= CF_READ_NULL;
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001576 sc_shutr(sc);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001577 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001578 }
1579
Christopher Fauletf959d082019-02-07 15:38:42 +01001580 out:
1581 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001582
1583 /* eat the whole request */
1584 if (co_data(req)) {
1585 req_htx = htx_from_buf(&req->buf);
1586 co_htx_skip(req, req_htx, co_data(req));
1587 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001588 return;
1589
1590 error:
1591 res->flags |= CF_READ_NULL;
Willy Tarreau4c218fb2022-05-27 10:16:15 +02001592 sc_shutr(sc);
1593 sc_shutw(sc);
Christopher Fauletf959d082019-02-07 15:38:42 +01001594}
1595
1596struct applet promex_applet = {
1597 .obj_type = OBJ_TYPE_APPLET,
1598 .name = "<PROMEX>", /* used for logging */
1599 .init = promex_appctx_init,
1600 .fct = promex_appctx_handle_io,
1601};
1602
1603static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px,
1604 struct act_rule *rule, char **err)
1605{
1606 /* Prometheus exporter service is only available on "http-request" rulesets */
1607 if (rule->from != ACT_F_HTTP_REQ) {
1608 memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets");
1609 return ACT_RET_PRS_ERR;
1610 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001611
1612 /* Add applet pointer in the rule. */
1613 rule->applet = promex_applet;
1614
1615 return ACT_RET_PRS_OK;
1616}
1617static void promex_register_build_options(void)
1618{
1619 char *ptr = NULL;
1620
1621 memprintf(&ptr, "Built with the Prometheus exporter as a service");
1622 hap_register_build_opts(ptr, 1);
1623}
1624
1625
1626static struct action_kw_list service_actions = { ILH, {
1627 { "prometheus-exporter", service_parse_prometheus_exporter },
1628 { /* END */ }
1629}};
1630
1631INITCALL1(STG_REGISTER, service_keywords_register, &service_actions);
1632INITCALL0(STG_REGISTER, promex_register_build_options);