blob: e4cf8ea8efdfc0ce4c75bf187061af4a25481842 [file] [log] [blame]
Christopher Fauletf959d082019-02-07 15:38:42 +01001/*
2 * Promex is a Prometheus exporter for HAProxy
3 *
4 * It is highly inspired by the official Prometheus exporter.
5 * See: https://github.com/prometheus/haproxy_exporter
6 *
7 * Copyright 2019 Christopher Faulet <cfaulet@haproxy.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 */
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020018#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020019#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
William Dauchyde3c3262021-02-01 13:11:51 +010021#include <haproxy/check.h>
Christopher Faulet908628c2022-03-25 16:43:49 +010022#include <haproxy/conn_stream.h>
23#include <haproxy/cs_utils.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020024#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020025#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020026#include <haproxy/http.h>
Willy Tarreaub7fc4c42021-10-06 18:56:42 +020027#include <haproxy/http_ana.h>
Willy Tarreau87735332020-06-04 09:08:41 +020028#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020029#include <haproxy/htx.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020030#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020031#include <haproxy/listener.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020032#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020033#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020034#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020035#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020036#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020037#include <haproxy/stream.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020038#include <haproxy/task.h>
Willy Tarreau0fd04fd2021-05-08 12:58:12 +020039#include <haproxy/tools.h>
William Dauchy5a982a72021-01-08 13:18:06 +010040#include <haproxy/version.h>
Christopher Fauletf959d082019-02-07 15:38:42 +010041
42/* Prometheus exporter applet states (appctx->st0) */
43enum {
44 PROMEX_ST_INIT = 0, /* initialized */
45 PROMEX_ST_HEAD, /* send headers before dump */
46 PROMEX_ST_DUMP, /* dumping stats */
47 PROMEX_ST_DONE, /* finished */
Christopher Faulet9744f7c2019-03-27 15:48:53 +010048 PROMEX_ST_END, /* treatment terminated */
Christopher Fauletf959d082019-02-07 15:38:42 +010049};
50
51/* Prometheus exporter dumper states (appctx->st1) */
52enum {
William Dauchy69164222021-02-07 20:42:38 +010053 PROMEX_DUMPER_INIT = 0, /* initialized */
54 PROMEX_DUMPER_GLOBAL, /* dump metrics of globals */
55 PROMEX_DUMPER_FRONT, /* dump metrics of frontend proxies */
56 PROMEX_DUMPER_BACK, /* dump metrics of backend proxies */
57 PROMEX_DUMPER_LI, /* dump metrics of listeners */
58 PROMEX_DUMPER_SRV, /* dump metrics of servers */
59 PROMEX_DUMPER_STICKTABLE, /* dump metrics of stick tables */
60 PROMEX_DUMPER_DONE, /* finished */
Christopher Fauletf959d082019-02-07 15:38:42 +010061};
62
Willy Tarreauae1747d2022-05-03 17:33:00 +020063/* Prometheus exporter flags (ctx->flags) */
William Dauchy69164222021-02-07 20:42:38 +010064#define PROMEX_FL_METRIC_HDR 0x00000001
65#define PROMEX_FL_INFO_METRIC 0x00000002
66#define PROMEX_FL_FRONT_METRIC 0x00000004
67#define PROMEX_FL_BACK_METRIC 0x00000008
68#define PROMEX_FL_SRV_METRIC 0x00000010
William Dauchye3f7bd52021-02-14 23:22:56 +010069#define PROMEX_FL_LI_METRIC 0x00000020
70#define PROMEX_FL_STICKTABLE_METRIC 0x00000040
71#define PROMEX_FL_SCOPE_GLOBAL 0x00000080
72#define PROMEX_FL_SCOPE_FRONT 0x00000100
73#define PROMEX_FL_SCOPE_BACK 0x00000200
74#define PROMEX_FL_SCOPE_SERVER 0x00000400
75#define PROMEX_FL_SCOPE_LI 0x00000800
76#define PROMEX_FL_SCOPE_STICKTABLE 0x00001000
77#define PROMEX_FL_NO_MAINT_SRV 0x00002000
Christopher Faulet78407ce2019-11-18 14:47:08 +010078
William Dauchy69164222021-02-07 20:42:38 +010079#define PROMEX_FL_SCOPE_ALL (PROMEX_FL_SCOPE_GLOBAL | PROMEX_FL_SCOPE_FRONT | \
William Dauchye3f7bd52021-02-14 23:22:56 +010080 PROMEX_FL_SCOPE_LI | PROMEX_FL_SCOPE_BACK | \
81 PROMEX_FL_SCOPE_SERVER | PROMEX_FL_SCOPE_STICKTABLE)
Christopher Fauletf959d082019-02-07 15:38:42 +010082
Willy Tarreauae1747d2022-05-03 17:33:00 +020083/* the context of the applet */
84struct promex_ctx {
85 struct proxy *px; /* current proxy */
86 struct stktable *st; /* current table */
87 struct listener *li; /* current listener */
88 struct server *sv; /* current server */
89 unsigned int flags; /* PROMEX_FL_* */
90 int obj_state; /* current state among PROMEX_{FRONT|BACK|SRV|LI}_STATE_* */
91};
92
Christopher Faulet0312c0d2021-01-20 15:19:12 +010093/* Promtheus metric type (gauge or counter) */
94enum promex_mt_type {
95 PROMEX_MT_GAUGE = 1,
96 PROMEX_MT_COUNTER = 2,
97};
98
Christopher Fauletf959d082019-02-07 15:38:42 +010099/* The max length for metrics name. It is a hard limit but it should be
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500100 * enough.
Christopher Fauletf959d082019-02-07 15:38:42 +0100101 */
102#define PROMEX_MAX_NAME_LEN 128
103
104/* The expected max length for a metric dump, including its header lines. It is
105 * just a soft limit to avoid extra work. We don't try to dump a metric if less
106 * than this size is available in the HTX.
107 */
108#define PROMEX_MAX_METRIC_LENGTH 512
109
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100110/* The max number of labels per metric */
111#define PROMEX_MAX_LABELS 8
William Dauchy5a982a72021-01-08 13:18:06 +0100112
Christopher Faulet0312c0d2021-01-20 15:19:12 +0100113/* Describe a prometheus metric */
114struct promex_metric {
115 const struct ist n; /* The metric name */
116 enum promex_mt_type type; /* The metric type (gauge or counter) */
117 unsigned int flags; /* PROMEX_FL_* flags */
118};
119
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100120/* Describe a prometheus metric label. It is just a key/value pair */
121struct promex_label {
122 struct ist name;
123 struct ist value;
124};
125
Christopher Faulet37286a52021-01-20 15:20:53 +0100126/* Global metrics */
127const struct promex_metric promex_global_metrics[INF_TOTAL_FIELDS] = {
128 //[INF_NAME] ignored
129 //[INF_VERSION], ignored
130 //[INF_RELEASE_DATE] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100131 [INF_NBTHREAD] = { .n = IST("nbthread"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
132 [INF_NBPROC] = { .n = IST("nbproc"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
133 [INF_PROCESS_NUM] = { .n = IST("relative_process_id"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
134 //[INF_PID] ignored
135 //[INF_UPTIME] ignored
136 [INF_UPTIME_SEC] = { .n = IST("uptime_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
137 [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 +0100138 //[INF_MEMMAX_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100139 [INF_MEMMAX_BYTES] = { .n = IST("max_memory_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100140 //[INF_POOL_ALLOC_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100141 [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 +0100142 //[INF_POOL_USED_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100143 [INF_POOL_USED_BYTES] = { .n = IST("pool_used_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
144 [INF_POOL_FAILED] = { .n = IST("pool_failures_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
145 [INF_ULIMIT_N] = { .n = IST("max_fds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
146 [INF_MAXSOCK] = { .n = IST("max_sockets"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
147 [INF_MAXCONN] = { .n = IST("max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
148 [INF_HARD_MAXCONN] = { .n = IST("hard_max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
149 [INF_CURR_CONN] = { .n = IST("current_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
150 [INF_CUM_CONN] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
151 [INF_CUM_REQ] = { .n = IST("requests_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
152 [INF_MAX_SSL_CONNS] = { .n = IST("max_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
153 [INF_CURR_SSL_CONNS] = { .n = IST("current_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
154 [INF_CUM_SSL_CONNS] = { .n = IST("ssl_connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
155 [INF_MAXPIPES] = { .n = IST("max_pipes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
156 [INF_PIPES_USED] = { .n = IST("pipes_used_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
157 [INF_PIPES_FREE] = { .n = IST("pipes_free_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
158 [INF_CONN_RATE] = { .n = IST("current_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
159 [INF_CONN_RATE_LIMIT] = { .n = IST("limit_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
160 [INF_MAX_CONN_RATE] = { .n = IST("max_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
161 [INF_SESS_RATE] = { .n = IST("current_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
162 [INF_SESS_RATE_LIMIT] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
163 [INF_MAX_SESS_RATE] = { .n = IST("max_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
164 [INF_SSL_RATE] = { .n = IST("current_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
165 [INF_SSL_RATE_LIMIT] = { .n = IST("limit_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
166 [INF_MAX_SSL_RATE] = { .n = IST("max_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
167 [INF_SSL_FRONTEND_KEY_RATE] = { .n = IST("current_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
168 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .n = IST("max_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
169 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .n = IST("frontend_ssl_reuse"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
170 [INF_SSL_BACKEND_KEY_RATE] = { .n = IST("current_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
171 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .n = IST("max_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
172 [INF_SSL_CACHE_LOOKUPS] = { .n = IST("ssl_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
173 [INF_SSL_CACHE_MISSES] = { .n = IST("ssl_cache_misses_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
174 [INF_COMPRESS_BPS_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
175 [INF_COMPRESS_BPS_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
176 [INF_COMPRESS_BPS_RATE_LIM] = { .n = IST("limit_http_comp"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
177 [INF_ZLIB_MEM_USAGE] = { .n = IST("current_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
178 [INF_MAX_ZLIB_MEM_USAGE] = { .n = IST("max_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
179 [INF_TASKS] = { .n = IST("current_tasks"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
180 [INF_RUN_QUEUE] = { .n = IST("current_run_queue"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
181 [INF_IDLE_PCT] = { .n = IST("idle_time_percent"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
182 //[INF_NODE] ignored
183 //[INF_DESCRIPTION] ignored
184 [INF_STOPPING] = { .n = IST("stopping"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
185 [INF_JOBS] = { .n = IST("jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
186 [INF_UNSTOPPABLE_JOBS] = { .n = IST("unstoppable_jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
187 [INF_LISTENERS] = { .n = IST("listeners"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
188 [INF_ACTIVE_PEERS] = { .n = IST("active_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
189 [INF_CONNECTED_PEERS] = { .n = IST("connected_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
190 [INF_DROPPED_LOGS] = { .n = IST("dropped_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
191 [INF_BUSY_POLLING] = { .n = IST("busy_polling_enabled"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
192 [INF_FAILED_RESOLUTIONS] = { .n = IST("failed_resolutions"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
193 [INF_TOTAL_BYTES_OUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
194 [INF_TOTAL_SPLICED_BYTES_OUT] = { .n = IST("spliced_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
195 [INF_BYTES_OUT_RATE] = { .n = IST("bytes_out_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
196 //[INF_DEBUG_COMMANDS_ISSUED] ignored
William Dauchy7741c332021-02-01 13:11:57 +0100197 [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 +0100198 [INF_BUILD_INFO] = { .n = IST("build_info"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
Christopher Fauletf959d082019-02-07 15:38:42 +0100199};
200
Christopher Faulet37286a52021-01-20 15:20:53 +0100201/* frontend/backend/server fields */
202const struct promex_metric promex_st_metrics[ST_F_TOTAL_FIELDS] = {
William Dauchy42d7c402021-11-07 10:18:47 +0100203 //[ST_F_PXNAME] ignored
204 //[ST_F_SVNAME] ignored
205 [ST_F_QCUR] = { .n = IST("current_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
206 [ST_F_QMAX] = { .n = IST("max_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
207 [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) },
208 [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) },
209 [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) },
210 [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) },
211 [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) },
212 [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) },
213 [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 ) },
214 [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) },
215 [ST_F_EREQ] = { .n = IST("request_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
216 [ST_F_ECON] = { .n = IST("connection_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
217 [ST_F_ERESP] = { .n = IST("response_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
218 [ST_F_WRETR] = { .n = IST("retry_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
219 [ST_F_WREDIS] = { .n = IST("redispatch_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
220 [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) },
221 [ST_F_WEIGHT] = { .n = IST("weight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
222 [ST_F_ACT] = { .n = IST("active_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
223 [ST_F_BCK] = { .n = IST("backup_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
224 [ST_F_CHKFAIL] = { .n = IST("check_failures_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_SRV_METRIC) },
225 [ST_F_CHKDOWN] = { .n = IST("check_up_down_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
226 [ST_F_LASTCHG] = { .n = IST("check_last_change_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
227 [ST_F_DOWNTIME] = { .n = IST("downtime_seconds_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
228 [ST_F_QLIMIT] = { .n = IST("queue_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
229 //[ST_F_PID] ignored
230 //[ST_F_IID] ignored
231 //[ST_F_SID] ignored
232 [ST_F_THROTTLE] = { .n = IST("current_throttle"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
233 [ST_F_LBTOT] = { .n = IST("loadbalanced_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
234 //[ST_F_TRACKED] ignored
235 //[ST_F_TYPE] ignored
236 //[ST_F_RATE] ignored
237 [ST_F_RATE_LIM] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
238 [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) },
239 [ST_F_CHECK_STATUS] = { .n = IST("check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
240 [ST_F_CHECK_CODE] = { .n = IST("check_code"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
241 [ST_F_CHECK_DURATION] = { .n = IST("check_duration_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
242 [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) },
243 [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) },
244 [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) },
245 [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) },
246 [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) },
247 [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) },
248 //[ST_F_HANAFAIL] ignored
249 //[ST_F_REQ_RATE] ignored
250 [ST_F_REQ_RATE_MAX] = { .n = IST("http_requests_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
251 [ST_F_REQ_TOT] = { .n = IST("http_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
252 [ST_F_CLI_ABRT] = { .n = IST("client_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
253 [ST_F_SRV_ABRT] = { .n = IST("server_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
254 [ST_F_COMP_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
255 [ST_F_COMP_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
256 [ST_F_COMP_BYP] = { .n = IST("http_comp_bytes_bypassed_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
257 [ST_F_COMP_RSP] = { .n = IST("http_comp_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
258 [ST_F_LASTSESS] = { .n = IST("last_session_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
259 //[ST_F_LAST_CHK] ignored
260 //[ST_F_LAST_AGT] ignored
261 [ST_F_QTIME] = { .n = IST("queue_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
262 [ST_F_CTIME] = { .n = IST("connect_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
263 [ST_F_RTIME] = { .n = IST("response_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
264 [ST_F_TTIME] = { .n = IST("total_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
265 //[ST_F_AGENT_STATUS] ignored
266 //[ST_F_AGENT_CODE] ignored
267 //[ST_F_AGENT_DURATION] ignored
268 //[ST_F_CHECK_DESC] ignored
269 //[ST_F_AGENT_DESC] ignored
270 //[ST_F_CHECK_RISE] ignored
271 //[ST_F_CHECK_FALL] ignored
272 //[ST_F_CHECK_HEALTH] ignored
273 //[ST_F_AGENT_RISE] ignored
274 //[ST_F_AGENT_FALL] ignored
275 //[ST_F_AGENT_HEALTH] ignored
276 //[ST_F_ADDR] ignored
277 //[ST_F_COOKIE] ignored
278 //[ST_F_MODE] ignored
279 //[ST_F_ALGO] ignored
280 //[ST_F_CONN_RATE] ignored
281 [ST_F_CONN_RATE_MAX] = { .n = IST("connections_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
282 [ST_F_CONN_TOT] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
283 [ST_F_INTERCEPTED] = { .n = IST("intercepted_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
284 [ST_F_DCON] = { .n = IST("denied_connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
285 [ST_F_DSES] = { .n = IST("denied_sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
286 [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) },
287 [ST_F_CONNECT] = { .n = IST("connection_attempts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
288 [ST_F_REUSE] = { .n = IST("connection_reuses_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
289 [ST_F_CACHE_LOOKUPS] = { .n = IST("http_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
290 [ST_F_CACHE_HITS] = { .n = IST("http_cache_hits_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
291 [ST_F_SRV_ICUR] = { .n = IST("idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
292 [ST_F_SRV_ILIM] = { .n = IST("idle_connections_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
293 [ST_F_QT_MAX] = { .n = IST("max_queue_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
294 [ST_F_CT_MAX] = { .n = IST("max_connect_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
295 [ST_F_RT_MAX] = { .n = IST("max_response_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
296 [ST_F_TT_MAX] = { .n = IST("max_total_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
297 [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) },
298 [ST_F_IDLE_CONN_CUR] = { .n = IST("unsafe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
299 [ST_F_SAFE_CONN_CUR] = { .n = IST("safe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
300 [ST_F_USED_CONN_CUR] = { .n = IST("used_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
301 [ST_F_NEED_CONN_EST] = { .n = IST("need_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
302 [ST_F_UWEIGHT] = { .n = IST("uweight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
303 [ST_F_AGG_SRV_CHECK_STATUS] = { .n = IST("agg_server_check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
Christopher Fauletf959d082019-02-07 15:38:42 +0100304};
305
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500306/* Description of overridden stats fields */
Christopher Fauletf959d082019-02-07 15:38:42 +0100307const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
William Dauchya1da7ba2021-02-01 13:11:52 +0100308 [ST_F_STATUS] = IST("Current status of the service, per state label value."),
William Dauchyde3c3262021-02-01 13:11:51 +0100309 [ST_F_CHECK_STATUS] = IST("Status of last health check, per state label value."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100310 [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."),
Christopher Faulet2711e512020-02-27 16:12:07 +0100311 [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100312 [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."),
313 [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."),
314 [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."),
315 [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100316 [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"),
317 [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"),
318 [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"),
319 [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100320};
321
William Dauchy69164222021-02-07 20:42:38 +0100322/* stick table base fields */
323enum sticktable_field {
324 STICKTABLE_SIZE = 0,
325 STICKTABLE_USED,
326 /* must always be the last one */
327 STICKTABLE_TOTAL_FIELDS
328};
329
330const struct promex_metric promex_sticktable_metrics[STICKTABLE_TOTAL_FIELDS] = {
331 [STICKTABLE_SIZE] = { .n = IST("size"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC },
332 [STICKTABLE_USED] = { .n = IST("used"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC },
333};
334
335/* stick table base description */
336const struct ist promex_sticktable_metric_desc[STICKTABLE_TOTAL_FIELDS] = {
337 [STICKTABLE_SIZE] = IST("Stick table size."),
338 [STICKTABLE_USED] = IST("Number of entries used in this stick table."),
339};
340
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100341/* Specific labels for all ST_F_HRSP_* fields */
342const struct ist promex_hrsp_code[1 + ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = {
343 [ST_F_HRSP_1XX - ST_F_HRSP_1XX] = IST("1xx"),
344 [ST_F_HRSP_2XX - ST_F_HRSP_1XX] = IST("2xx"),
345 [ST_F_HRSP_3XX - ST_F_HRSP_1XX] = IST("3xx"),
346 [ST_F_HRSP_4XX - ST_F_HRSP_1XX] = IST("4xx"),
347 [ST_F_HRSP_5XX - ST_F_HRSP_1XX] = IST("5xx"),
348 [ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = IST("other"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100349};
350
William Dauchy54938212021-01-27 22:40:16 +0100351enum promex_front_state {
352 PROMEX_FRONT_STATE_DOWN = 0,
353 PROMEX_FRONT_STATE_UP,
354
355 PROMEX_FRONT_STATE_COUNT /* must be last */
356};
357
358const struct ist promex_front_st[PROMEX_FRONT_STATE_COUNT] = {
359 [PROMEX_FRONT_STATE_DOWN] = IST("DOWN"),
360 [PROMEX_FRONT_STATE_UP] = IST("UP"),
361};
362
363enum promex_back_state {
364 PROMEX_BACK_STATE_DOWN = 0,
365 PROMEX_BACK_STATE_UP,
366
367 PROMEX_BACK_STATE_COUNT /* must be last */
368};
369
370const struct ist promex_back_st[PROMEX_BACK_STATE_COUNT] = {
371 [PROMEX_BACK_STATE_DOWN] = IST("DOWN"),
372 [PROMEX_BACK_STATE_UP] = IST("UP"),
373};
374
375enum promex_srv_state {
376 PROMEX_SRV_STATE_DOWN = 0,
377 PROMEX_SRV_STATE_UP,
378 PROMEX_SRV_STATE_MAINT,
379 PROMEX_SRV_STATE_DRAIN,
380 PROMEX_SRV_STATE_NOLB,
381
382 PROMEX_SRV_STATE_COUNT /* must be last */
383};
384
385const struct ist promex_srv_st[PROMEX_SRV_STATE_COUNT] = {
386 [PROMEX_SRV_STATE_DOWN] = IST("DOWN"),
387 [PROMEX_SRV_STATE_UP] = IST("UP"),
388 [PROMEX_SRV_STATE_MAINT] = IST("MAINT"),
389 [PROMEX_SRV_STATE_DRAIN] = IST("DRAIN"),
390 [PROMEX_SRV_STATE_NOLB] = IST("NOLB"),
391};
392
393/* Return the server status. */
394enum promex_srv_state promex_srv_status(struct server *sv)
Christopher Fauletf959d082019-02-07 15:38:42 +0100395{
William Dauchy54938212021-01-27 22:40:16 +0100396 int state = PROMEX_SRV_STATE_DOWN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100397
Christopher Fauletf959d082019-02-07 15:38:42 +0100398 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Dauchy54938212021-01-27 22:40:16 +0100399 state = PROMEX_SRV_STATE_UP;
Christopher Fauletf959d082019-02-07 15:38:42 +0100400 if (sv->cur_admin & SRV_ADMF_DRAIN)
William Dauchy54938212021-01-27 22:40:16 +0100401 state = PROMEX_SRV_STATE_DRAIN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100402 }
Christopher Fauletd45d1052019-09-06 16:10:19 +0200403 else if (sv->cur_state == SRV_ST_STOPPING)
William Dauchy54938212021-01-27 22:40:16 +0100404 state = PROMEX_SRV_STATE_NOLB;
Christopher Fauletd45d1052019-09-06 16:10:19 +0200405
406 if (sv->cur_admin & SRV_ADMF_MAINT)
William Dauchy54938212021-01-27 22:40:16 +0100407 state = PROMEX_SRV_STATE_MAINT;
Christopher Fauletf959d082019-02-07 15:38:42 +0100408
409 return state;
410}
411
412/* Convert a field to its string representation and write it in <out>, followed
413 * by a newline, if there is enough space. non-numeric value are converted in
William Dauchy18a2c6e2021-01-22 21:09:47 +0100414 * "NaN" because Prometheus only support numerical values (but it is unexepceted
Christopher Fauletf959d082019-02-07 15:38:42 +0100415 * to process this kind of value). It returns 1 on success. Otherwise, it
416 * returns 0. The buffer's length must not exceed <max> value.
417 */
418static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
419{
420 int ret = 0;
421
422 switch (field_format(f, 0)) {
William Dauchy18a2c6e2021-01-22 21:09:47 +0100423 case FF_EMPTY: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100424 case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break;
425 case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break;
426 case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break;
427 case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200428 case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break;
William Dauchy18a2c6e2021-01-22 21:09:47 +0100429 case FF_STR: ret = chunk_strcat(out, "NaN\n"); break;
430 default: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100431 }
432 if (!ret || out->data > max)
433 return 0;
434 return 1;
435}
436
Christopher Fauletf959d082019-02-07 15:38:42 +0100437/* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
438 * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
439 */
440static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
Christopher Faulet37286a52021-01-20 15:20:53 +0100441 const struct promex_metric *metric, const struct ist name,
442 struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100443{
Christopher Faulet37286a52021-01-20 15:20:53 +0100444 struct ist type;
William Dauchy82b2ce22021-02-01 13:11:55 +0100445 struct ist desc;
Christopher Faulet37286a52021-01-20 15:20:53 +0100446
447 switch (metric->type) {
448 case PROMEX_MT_COUNTER:
449 type = ist("counter");
450 break;
451 default:
452 type = ist("gauge");
453 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100454
William Dauchya191b772021-01-15 22:41:39 +0100455 if (istcat(out, ist("# HELP "), max) == -1 ||
456 istcat(out, name, max) == -1 ||
457 istcat(out, ist(" "), max) == -1)
458 goto full;
459
William Dauchy82b2ce22021-02-01 13:11:55 +0100460 if (metric->flags & PROMEX_FL_INFO_METRIC)
461 desc = ist(info_fields[appctx->st2].desc);
William Dauchy69164222021-02-07 20:42:38 +0100462 else if (metric->flags & PROMEX_FL_STICKTABLE_METRIC)
463 desc = promex_sticktable_metric_desc[appctx->st2];
William Dauchy82b2ce22021-02-01 13:11:55 +0100464 else if (!isttest(promex_st_metric_desc[appctx->st2]))
465 desc = ist(stat_fields[appctx->st2].desc);
466 else
467 desc = promex_st_metric_desc[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100468
William Dauchy82b2ce22021-02-01 13:11:55 +0100469 if (istcat(out, desc, max) == -1 ||
470 istcat(out, ist("\n# TYPE "), max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100471 istcat(out, name, max) == -1 ||
472 istcat(out, ist(" "), max) == -1 ||
Christopher Faulet37286a52021-01-20 15:20:53 +0100473 istcat(out, type, max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100474 istcat(out, ist("\n"), max) == -1)
475 goto full;
476
477 return 1;
478
479 full:
480 return 0;
481}
482
483/* Dump the line for <metric>. It starts by the metric name followed by its
484 * labels (proxy name, server name...) between braces and finally its value. If
485 * not already done, the header lines are dumped first. It returns 1 on
486 * success. Otherwise if <out> length exceeds <max>, it returns 0.
487 */
Christopher Faulet37286a52021-01-20 15:20:53 +0100488static int promex_dump_metric(struct appctx *appctx, struct htx *htx, struct ist prefix,
William Dauchyc6464592021-01-27 22:40:17 +0100489 const struct promex_metric *metric, struct field *val,
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100490 struct promex_label *labels, struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100491{
492 struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
Willy Tarreauae1747d2022-05-03 17:33:00 +0200493 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100494 size_t len = out->len;
495
496 if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
497 return 0;
498
Christopher Faulet37286a52021-01-20 15:20:53 +0100499 /* Fill the metric name */
500 istcat(&name, prefix, PROMEX_MAX_NAME_LEN);
501 istcat(&name, metric->n, PROMEX_MAX_NAME_LEN);
502
503
Willy Tarreauae1747d2022-05-03 17:33:00 +0200504 if ((ctx->flags & PROMEX_FL_METRIC_HDR) &&
Christopher Faulet37286a52021-01-20 15:20:53 +0100505 !promex_dump_metric_header(appctx, htx, metric, name, out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100506 goto full;
507
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100508 if (istcat(out, name, max) == -1)
509 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100510
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100511 if (isttest(labels[0].name)) {
512 int i;
513
514 if (istcat(out, ist("{"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100515 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100516
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100517 for (i = 0; isttest(labels[i].name); i++) {
518 if (!isttest(labels[i].value))
519 continue;
520
521 if ((i && istcat(out, ist(","), max) == -1) ||
522 istcat(out, labels[i].name, max) == -1 ||
523 istcat(out, ist("=\""), max) == -1 ||
524 istcat(out, labels[i].value, max) == -1 ||
525 istcat(out, ist("\""), max) == -1)
526 goto full;
527 }
528
529 if (istcat(out, ist("}"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100530 goto full;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100531
Christopher Fauletf959d082019-02-07 15:38:42 +0100532 }
533
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100534 if (istcat(out, ist(" "), max) == -1)
535 goto full;
536
Christopher Fauletf959d082019-02-07 15:38:42 +0100537 trash.data = out->len;
Christopher Faulet37286a52021-01-20 15:20:53 +0100538 if (!promex_metric_to_str(&trash, val, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100539 goto full;
540 out->len = trash.data;
541
Willy Tarreauae1747d2022-05-03 17:33:00 +0200542 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100543 return 1;
544 full:
545 // Restore previous length
546 out->len = len;
547 return 0;
548
549}
550
551
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500552/* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100553 * 0 if <htx> is full and -1 in case of any error. */
554static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
555{
556 static struct ist prefix = IST("haproxy_process_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200557 struct promex_ctx *ctx = appctx->svcctx;
Christopher Faulet37286a52021-01-20 15:20:53 +0100558 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100559 struct channel *chn = cs_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +0100560 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200561 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100562 int ret = 1;
563
Willy Tarreau0b26b382021-05-08 07:43:53 +0200564 if (!stats_fill_info(info, INF_TOTAL_FIELDS, 0))
William Dauchy5d9b8f32021-01-11 20:07:49 +0100565 return -1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100566
Christopher Faulet37286a52021-01-20 15:20:53 +0100567 for (; appctx->st2 < INF_TOTAL_FIELDS; appctx->st2++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100568 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
569
Willy Tarreauae1747d2022-05-03 17:33:00 +0200570 if (!(promex_global_metrics[appctx->st2].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100571 continue;
572
Christopher Fauletf959d082019-02-07 15:38:42 +0100573 switch (appctx->st2) {
William Dauchy5a982a72021-01-08 13:18:06 +0100574 case INF_BUILD_INFO:
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100575 labels[0].name = ist("version");
576 labels[0].value = ist(HAPROXY_VERSION);
Christopher Faulet37286a52021-01-20 15:20:53 +0100577 val = mkf_u32(FN_GAUGE, 1);
William Dauchy5a982a72021-01-08 13:18:06 +0100578 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100579
580 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100581 val = info[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100582 }
583
William Dauchyc6464592021-01-27 22:40:17 +0100584 if (!promex_dump_metric(appctx, htx, prefix, &promex_global_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100585 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100586 goto full;
587
Willy Tarreauae1747d2022-05-03 17:33:00 +0200588 ctx->flags |= PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100589 }
590
591 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200592 if (out.len) {
593 if (!htx_add_data_atonce(htx, out))
594 return -1; /* Unexpected and unrecoverable error */
595 channel_add_input(chn, out.len);
596 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100597 return ret;
598 full:
599 ret = 0;
600 goto end;
601}
602
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500603/* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100604 * 0 if <htx> is full and -1 in case of any error. */
605static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
606{
607 static struct ist prefix = IST("haproxy_frontend_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200608 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100609 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100610 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100611 struct channel *chn = cs_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +0100612 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200613 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchyb9577452021-01-17 18:27:46 +0100614 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100615 int ret = 1;
William Dauchyc6464592021-01-27 22:40:17 +0100616 enum promex_front_state state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100617
Christopher Faulet37286a52021-01-20 15:20:53 +0100618 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
Willy Tarreauae1747d2022-05-03 17:33:00 +0200619 if (!(promex_st_metrics[appctx->st2].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100620 continue;
621
Willy Tarreauae1747d2022-05-03 17:33:00 +0200622 while (ctx->px) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100623 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
624
Willy Tarreauae1747d2022-05-03 17:33:00 +0200625 px = ctx->px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100626
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100627 labels[0].name = ist("proxy");
628 labels[0].value = ist2(px->id, strlen(px->id));
629
Christopher Fauletf959d082019-02-07 15:38:42 +0100630 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200631 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100632 goto next_px;
633
William Dauchyb9577452021-01-17 18:27:46 +0100634 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
635 return -1;
636
Christopher Fauletf959d082019-02-07 15:38:42 +0100637 switch (appctx->st2) {
638 case ST_F_STATUS:
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200639 state = !(px->flags & PR_FL_STOPPED);
Willy Tarreauae1747d2022-05-03 17:33:00 +0200640 for (; ctx->obj_state < PROMEX_FRONT_STATE_COUNT; ctx->obj_state++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100641 labels[1].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200642 labels[1].value = promex_front_st[ctx->obj_state];
643 val = mkf_u32(FO_STATUS, state == ctx->obj_state);
William Dauchyc6464592021-01-27 22:40:17 +0100644 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100645 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100646 goto full;
647 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200648 ctx->obj_state = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100649 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100650 case ST_F_REQ_RATE_MAX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100651 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100652 case ST_F_INTERCEPTED:
Christopher Fauletf959d082019-02-07 15:38:42 +0100653 case ST_F_CACHE_LOOKUPS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100654 case ST_F_CACHE_HITS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100655 case ST_F_COMP_IN:
Christopher Fauletf959d082019-02-07 15:38:42 +0100656 case ST_F_COMP_OUT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100657 case ST_F_COMP_BYP:
William Dauchyb9577452021-01-17 18:27:46 +0100658 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100659 if (px->mode != PR_MODE_HTTP)
660 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100661 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100662 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100663 case ST_F_HRSP_1XX:
William Dauchyb9577452021-01-17 18:27:46 +0100664 case ST_F_HRSP_2XX:
665 case ST_F_HRSP_3XX:
666 case ST_F_HRSP_4XX:
667 case ST_F_HRSP_5XX:
668 case ST_F_HRSP_OTHER:
Christopher Fauletf959d082019-02-07 15:38:42 +0100669 if (px->mode != PR_MODE_HTTP)
670 goto next_px;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100671 if (appctx->st2 != ST_F_HRSP_1XX)
Willy Tarreauae1747d2022-05-03 17:33:00 +0200672 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100673 labels[1].name = ist("code");
674 labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
675 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100676 break;
677
678 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100679 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100680 }
681
William Dauchyc6464592021-01-27 22:40:17 +0100682 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100683 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100684 goto full;
685 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +0200686 ctx->px = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100687 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200688 ctx->flags |= PROMEX_FL_METRIC_HDR;
689 ctx->px = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100690 }
691
692 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200693 if (out.len) {
694 if (!htx_add_data_atonce(htx, out))
695 return -1; /* Unexpected and unrecoverable error */
696 channel_add_input(chn, out.len);
697 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100698 return ret;
699 full:
700 ret = 0;
701 goto end;
702}
703
William Dauchye3f7bd52021-02-14 23:22:56 +0100704/* Dump listener metrics (prefixed by "haproxy_listen_"). It returns 1 on
705 * success, 0 if <htx> is full and -1 in case of any error. */
706static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx)
707{
708 static struct ist prefix = IST("haproxy_listener_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200709 struct promex_ctx *ctx = appctx->svcctx;
William Dauchye3f7bd52021-02-14 23:22:56 +0100710 struct proxy *px;
711 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100712 struct channel *chn = cs_ic(appctx->owner);
William Dauchye3f7bd52021-02-14 23:22:56 +0100713 struct ist out = ist2(trash.area, 0);
714 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
715 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
716 struct listener *li;
717 int ret = 1;
718 enum li_status status;
719
720 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
Willy Tarreauae1747d2022-05-03 17:33:00 +0200721 if (!(promex_st_metrics[appctx->st2].flags & ctx->flags))
William Dauchye3f7bd52021-02-14 23:22:56 +0100722 continue;
723
Willy Tarreauae1747d2022-05-03 17:33:00 +0200724 while (ctx->px) {
William Dauchye3f7bd52021-02-14 23:22:56 +0100725 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
726
Willy Tarreauae1747d2022-05-03 17:33:00 +0200727 px = ctx->px;
William Dauchye3f7bd52021-02-14 23:22:56 +0100728
729 labels[0].name = ist("proxy");
730 labels[0].value = ist2(px->id, strlen(px->id));
731
732 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200733 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
William Dauchye3f7bd52021-02-14 23:22:56 +0100734 goto next_px;
735
Willy Tarreauae1747d2022-05-03 17:33:00 +0200736 li = ctx->li;
William Dauchye3f7bd52021-02-14 23:22:56 +0100737 list_for_each_entry_from(li, &px->conf.listeners, by_fe) {
738
William Dauchye3f7bd52021-02-14 23:22:56 +0100739 if (!li->counters)
740 continue;
741
William Dauchybaf22732021-02-25 00:53:13 +0100742 labels[1].name = ist("listener");
743 labels[1].value = ist2(li->name, strlen(li->name));
744
William Dauchye3f7bd52021-02-14 23:22:56 +0100745 if (!stats_fill_li_stats(px, li, 0, stats,
746 ST_F_TOTAL_FIELDS, &(appctx->st2)))
747 return -1;
748
749 switch (appctx->st2) {
750 case ST_F_STATUS:
751 status = get_li_status(li);
Willy Tarreauae1747d2022-05-03 17:33:00 +0200752 for (; ctx->obj_state < LI_STATE_COUNT; ctx->obj_state++) {
753 val = mkf_u32(FO_STATUS, status == ctx->obj_state);
William Dauchye3f7bd52021-02-14 23:22:56 +0100754 labels[2].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200755 labels[2].value = ist(li_status_st[ctx->obj_state]);
William Dauchye3f7bd52021-02-14 23:22:56 +0100756 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
757 &val, labels, &out, max))
758 goto full;
759 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200760 ctx->obj_state = 0;
William Dauchye3f7bd52021-02-14 23:22:56 +0100761 continue;
762 default:
763 val = stats[appctx->st2];
764 }
765
766 if (!promex_dump_metric(appctx, htx, prefix,
767 &promex_st_metrics[appctx->st2],
768 &val, labels, &out, max))
769 goto full;
770 }
771
772 next_px:
773 px = px->next;
Willy Tarreauae1747d2022-05-03 17:33:00 +0200774 ctx->px = px;
775 ctx->li = (px ? LIST_NEXT(&px->conf.listeners, struct listener *, by_fe) : NULL);
William Dauchye3f7bd52021-02-14 23:22:56 +0100776 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200777 ctx->flags |= PROMEX_FL_METRIC_HDR;
778 ctx->px = proxies_list;
779 ctx->li = LIST_NEXT(&proxies_list->conf.listeners, struct listener *, by_fe);
William Dauchye3f7bd52021-02-14 23:22:56 +0100780 }
781
782 end:
783 if (out.len) {
784 if (!htx_add_data_atonce(htx, out))
785 return -1; /* Unexpected and unrecoverable error */
786 channel_add_input(chn, out.len);
787 }
788 return ret;
789 full:
Willy Tarreauae1747d2022-05-03 17:33:00 +0200790 ctx->li = li;
William Dauchye3f7bd52021-02-14 23:22:56 +0100791 ret = 0;
792 goto end;
793}
794
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500795/* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100796 * 0 if <htx> is full and -1 in case of any error. */
797static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
798{
799 static struct ist prefix = IST("haproxy_backend_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200800 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100801 struct proxy *px;
William Dauchy42d7c402021-11-07 10:18:47 +0100802 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100803 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100804 struct channel *chn = cs_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +0100805 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200806 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchy3c6f0062021-01-25 17:29:02 +0100807 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100808 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200809 double secs;
William Dauchy42d7c402021-11-07 10:18:47 +0100810 enum promex_back_state bkd_state;
811 enum promex_srv_state srv_state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100812
Christopher Faulet37286a52021-01-20 15:20:53 +0100813 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
Willy Tarreauae1747d2022-05-03 17:33:00 +0200814 if (!(promex_st_metrics[appctx->st2].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100815 continue;
816
Willy Tarreauae1747d2022-05-03 17:33:00 +0200817 while (ctx->px) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100818 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
William Dauchy42d7c402021-11-07 10:18:47 +0100819 unsigned int srv_state_count[PROMEX_SRV_STATE_COUNT] = { 0 };
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100820
Willy Tarreauae1747d2022-05-03 17:33:00 +0200821 px = ctx->px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100822
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100823 labels[0].name = ist("proxy");
824 labels[0].value = ist2(px->id, strlen(px->id));
825
Christopher Fauletf959d082019-02-07 15:38:42 +0100826 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200827 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100828 goto next_px;
829
William Dauchy3c6f0062021-01-25 17:29:02 +0100830 if (!stats_fill_be_stats(px, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
831 return -1;
832
Christopher Fauletf959d082019-02-07 15:38:42 +0100833 switch (appctx->st2) {
William Dauchy42d7c402021-11-07 10:18:47 +0100834 case ST_F_AGG_SRV_CHECK_STATUS:
835 if (!px->srv)
836 goto next_px;
837 sv = px->srv;
838 while (sv) {
839 srv_state = promex_srv_status(sv);
840 srv_state_count[srv_state] += 1;
841 sv = sv->next;
842 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200843 for (; ctx->obj_state < PROMEX_SRV_STATE_COUNT; ctx->obj_state++) {
844 val = mkf_u32(FN_GAUGE, srv_state_count[ctx->obj_state]);
William Dauchy42d7c402021-11-07 10:18:47 +0100845 labels[1].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200846 labels[1].value = promex_srv_st[ctx->obj_state];
William Dauchy42d7c402021-11-07 10:18:47 +0100847 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
848 &val, labels, &out, max))
849 goto full;
850 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200851 ctx->obj_state = 0;
William Dauchy42d7c402021-11-07 10:18:47 +0100852 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100853 case ST_F_STATUS:
William Dauchy42d7c402021-11-07 10:18:47 +0100854 bkd_state = ((px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0);
Willy Tarreauae1747d2022-05-03 17:33:00 +0200855 for (; ctx->obj_state < PROMEX_BACK_STATE_COUNT; ctx->obj_state++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100856 labels[1].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200857 labels[1].value = promex_back_st[ctx->obj_state];
858 val = mkf_u32(FO_STATUS, bkd_state == ctx->obj_state);
William Dauchyc6464592021-01-27 22:40:17 +0100859 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100860 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100861 goto full;
862 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200863 ctx->obj_state = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100864 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100865 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200866 secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100867 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100868 break;
869 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200870 secs = (double)swrate_avg(px->be_counters.c_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_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200874 secs = (double)swrate_avg(px->be_counters.d_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_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200878 secs = (double)swrate_avg(px->be_counters.t_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;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100881 case ST_F_QT_MAX:
882 secs = (double)px->be_counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100883 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100884 break;
885 case ST_F_CT_MAX:
886 secs = (double)px->be_counters.ctime_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_RT_MAX:
890 secs = (double)px->be_counters.dtime_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_TT_MAX:
894 secs = (double)px->be_counters.ttime_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;
Christopher Fauletf959d082019-02-07 15:38:42 +0100897 case ST_F_REQ_TOT:
William Dauchy3c6f0062021-01-25 17:29:02 +0100898 case ST_F_CACHE_LOOKUPS:
899 case ST_F_CACHE_HITS:
900 case ST_F_COMP_IN:
901 case ST_F_COMP_OUT:
902 case ST_F_COMP_BYP:
903 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100904 if (px->mode != PR_MODE_HTTP)
905 goto next_px;
William Dauchy3c6f0062021-01-25 17:29:02 +0100906 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100907 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100908 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100909 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100910 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100911 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100912 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100913 case ST_F_HRSP_OTHER:
914 if (px->mode != PR_MODE_HTTP)
915 goto next_px;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100916 if (appctx->st2 != ST_F_HRSP_1XX)
Willy Tarreauae1747d2022-05-03 17:33:00 +0200917 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100918 labels[1].name = ist("code");
919 labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
William Dauchy3c6f0062021-01-25 17:29:02 +0100920 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100921 break;
922
923 default:
William Dauchy3c6f0062021-01-25 17:29:02 +0100924 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100925 }
926
William Dauchy3c6f0062021-01-25 17:29:02 +0100927 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100928 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100929 goto full;
930 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +0200931 ctx->px = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100932 }
Willy Tarreauae1747d2022-05-03 17:33:00 +0200933 ctx->flags |= PROMEX_FL_METRIC_HDR;
934 ctx->px = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100935 }
936
937 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200938 if (out.len) {
939 if (!htx_add_data_atonce(htx, out))
940 return -1; /* Unexpected and unrecoverable error */
941 channel_add_input(chn, out.len);
942 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100943 return ret;
944 full:
945 ret = 0;
946 goto end;
947}
948
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500949/* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100950 * 0 if <htx> is full and -1 in case of any error. */
951static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
952{
953 static struct ist prefix = IST("haproxy_server_");
Willy Tarreauae1747d2022-05-03 17:33:00 +0200954 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +0100955 struct proxy *px;
956 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100957 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +0100958 struct channel *chn = cs_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +0100959 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200960 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchybde2bf62021-01-25 17:29:04 +0100961 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100962 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200963 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100964 enum promex_srv_state state;
William Dauchyde3c3262021-02-01 13:11:51 +0100965 const char *check_state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100966
Christopher Faulet37286a52021-01-20 15:20:53 +0100967 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
Willy Tarreauae1747d2022-05-03 17:33:00 +0200968 if (!(promex_st_metrics[appctx->st2].flags & ctx->flags))
Christopher Faulet37286a52021-01-20 15:20:53 +0100969 continue;
970
Willy Tarreauae1747d2022-05-03 17:33:00 +0200971 while (ctx->px) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100972 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
973
Willy Tarreauae1747d2022-05-03 17:33:00 +0200974 px = ctx->px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100975
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100976 labels[0].name = ist("proxy");
977 labels[0].value = ist2(px->id, strlen(px->id));
978
Christopher Fauletf959d082019-02-07 15:38:42 +0100979 /* skip the disabled proxies, global frontend and non-networked ones */
Christopher Fauletdfd10ab2021-10-06 14:24:19 +0200980 if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100981 goto next_px;
982
Willy Tarreauae1747d2022-05-03 17:33:00 +0200983 while (ctx->sv) {
984 sv = ctx->sv;
Christopher Fauletf959d082019-02-07 15:38:42 +0100985
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100986 labels[1].name = ist("server");
987 labels[1].value = ist2(sv->id, strlen(sv->id));
988
William Dauchybde2bf62021-01-25 17:29:04 +0100989 if (!stats_fill_sv_stats(px, sv, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
990 return -1;
991
Willy Tarreauae1747d2022-05-03 17:33:00 +0200992 if ((ctx->flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
Christopher Fauleteba22942019-11-19 14:18:24 +0100993 goto next_sv;
994
Christopher Fauletf959d082019-02-07 15:38:42 +0100995 switch (appctx->st2) {
996 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100997 state = promex_srv_status(sv);
Willy Tarreauae1747d2022-05-03 17:33:00 +0200998 for (; ctx->obj_state < PROMEX_SRV_STATE_COUNT; ctx->obj_state++) {
999 val = mkf_u32(FO_STATUS, state == ctx->obj_state);
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001000 labels[2].name = ist("state");
Willy Tarreauae1747d2022-05-03 17:33:00 +02001001 labels[2].value = promex_srv_st[ctx->obj_state];
William Dauchyc6464592021-01-27 22:40:17 +01001002 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001003 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +01001004 goto full;
1005 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001006 ctx->obj_state = 0;
William Dauchyc6464592021-01-27 22:40:17 +01001007 goto next_sv;
Christopher Fauletf959d082019-02-07 15:38:42 +01001008 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001009 secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001010 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001011 break;
1012 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001013 secs = (double)swrate_avg(sv->counters.c_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_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001017 secs = (double)swrate_avg(sv->counters.d_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_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001021 secs = (double)swrate_avg(sv->counters.t_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;
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001024 case ST_F_QT_MAX:
1025 secs = (double)sv->counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001026 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001027 break;
1028 case ST_F_CT_MAX:
1029 secs = (double)sv->counters.ctime_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_RT_MAX:
1033 secs = (double)sv->counters.dtime_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_TT_MAX:
1037 secs = (double)sv->counters.ttime_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;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001040 case ST_F_CHECK_STATUS:
1041 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1042 goto next_sv;
William Dauchyde3c3262021-02-01 13:11:51 +01001043
Willy Tarreauae1747d2022-05-03 17:33:00 +02001044 for (; ctx->obj_state < HCHK_STATUS_SIZE; ctx->obj_state++) {
1045 if (get_check_status_result(ctx->obj_state) < CHK_RES_FAILED)
William Dauchyde3c3262021-02-01 13:11:51 +01001046 continue;
Willy Tarreauae1747d2022-05-03 17:33:00 +02001047 val = mkf_u32(FO_STATUS, sv->check.status == ctx->obj_state);
1048 check_state = get_check_status_info(ctx->obj_state);
William Dauchyde3c3262021-02-01 13:11:51 +01001049 labels[2].name = ist("state");
Tim Duesterhusb113b5c2021-09-15 13:58:44 +02001050 labels[2].value = ist(check_state);
William Dauchyde3c3262021-02-01 13:11:51 +01001051 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
1052 &val, labels, &out, max))
1053 goto full;
1054 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001055 ctx->obj_state = 0;
William Dauchyde3c3262021-02-01 13:11:51 +01001056 goto next_sv;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001057 case ST_F_CHECK_CODE:
1058 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1059 goto next_sv;
Christopher Faulet37286a52021-01-20 15:20:53 +01001060 val = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code);
Christopher Fauletcf403f32019-11-21 14:35:46 +01001061 break;
Christopher Faulet2711e512020-02-27 16:12:07 +01001062 case ST_F_CHECK_DURATION:
1063 if (sv->check.status < HCHK_STATUS_CHECKED)
1064 goto next_sv;
1065 secs = (double)sv->check.duration / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001066 val = mkf_flt(FN_DURATION, secs);
Christopher Faulet2711e512020-02-27 16:12:07 +01001067 break;
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001068 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +01001069 if (px->mode != PR_MODE_HTTP)
1070 goto next_px;
William Dauchybde2bf62021-01-25 17:29:04 +01001071 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001072 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +01001073 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001074 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001075 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001076 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001077 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001078 case ST_F_HRSP_OTHER:
1079 if (px->mode != PR_MODE_HTTP)
1080 goto next_px;
Christopher Faulet32ef48e2021-02-01 14:55:37 +01001081 if (appctx->st2 != ST_F_HRSP_1XX)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001082 ctx->flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001083 labels[2].name = ist("code");
1084 labels[2].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
William Dauchybde2bf62021-01-25 17:29:04 +01001085 val = stats[appctx->st2];
Christopher Fauletc55a6262020-07-10 15:39:39 +02001086 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001087
1088 default:
William Dauchybde2bf62021-01-25 17:29:04 +01001089 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001090 }
1091
William Dauchyc6464592021-01-27 22:40:17 +01001092 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001093 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +01001094 goto full;
Christopher Fauleteba22942019-11-19 14:18:24 +01001095 next_sv:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001096 ctx->sv = sv->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001097 }
1098
1099 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001100 ctx->px = px->next;
1101 ctx->sv = (ctx->px ? ctx->px->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001102 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001103 ctx->flags |= PROMEX_FL_METRIC_HDR;
1104 ctx->px = proxies_list;
1105 ctx->sv = (ctx->px ? ctx->px->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001106 }
1107
1108
1109 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001110 if (out.len) {
1111 if (!htx_add_data_atonce(htx, out))
1112 return -1; /* Unexpected and unrecoverable error */
1113 channel_add_input(chn, out.len);
1114 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001115 return ret;
1116 full:
1117 ret = 0;
1118 goto end;
1119}
1120
William Dauchy69164222021-02-07 20:42:38 +01001121/* Dump stick table metrics (prefixed by "haproxy_sticktable_"). It returns 1 on success,
1122 * 0 if <htx> is full and -1 in case of any error. */
1123static int promex_dump_sticktable_metrics(struct appctx *appctx, struct htx *htx)
1124{
1125 static struct ist prefix = IST("haproxy_sticktable_");
Willy Tarreauae1747d2022-05-03 17:33:00 +02001126 struct promex_ctx *ctx = appctx->svcctx;
William Dauchy69164222021-02-07 20:42:38 +01001127 struct field val;
Christopher Faulet908628c2022-03-25 16:43:49 +01001128 struct channel *chn = cs_ic(appctx->owner);
William Dauchy69164222021-02-07 20:42:38 +01001129 struct ist out = ist2(trash.area, 0);
1130 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
1131 int ret = 1;
1132 struct stktable *t;
1133
1134 for (; appctx->st2 < STICKTABLE_TOTAL_FIELDS; appctx->st2++) {
Willy Tarreauae1747d2022-05-03 17:33:00 +02001135 if (!(promex_sticktable_metrics[appctx->st2].flags & ctx->flags))
William Dauchy69164222021-02-07 20:42:38 +01001136 continue;
1137
Willy Tarreauae1747d2022-05-03 17:33:00 +02001138 while (ctx->st) {
William Dauchy69164222021-02-07 20:42:38 +01001139 struct promex_label labels[PROMEX_MAX_LABELS - 1] = {};
1140
Willy Tarreauae1747d2022-05-03 17:33:00 +02001141 t = ctx->st;
William Dauchy69164222021-02-07 20:42:38 +01001142 if (!t->size)
1143 goto next_px;
1144
1145 labels[0].name = ist("name");
1146 labels[0].value = ist2(t->id, strlen(t->id));
1147 labels[1].name = ist("type");
1148 labels[1].value = ist2(stktable_types[t->type].kw, strlen(stktable_types[t->type].kw));
1149 switch (appctx->st2) {
1150 case STICKTABLE_SIZE:
1151 val = mkf_u32(FN_GAUGE, t->size);
1152 break;
1153 case STICKTABLE_USED:
1154 val = mkf_u32(FN_GAUGE, t->current);
1155 break;
1156 default:
1157 goto next_px;
1158 }
1159
1160 if (!promex_dump_metric(appctx, htx, prefix,
1161 &promex_sticktable_metrics[appctx->st2],
1162 &val, labels, &out, max))
1163 goto full;
1164
1165 next_px:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001166 ctx->st = t->next;
William Dauchy69164222021-02-07 20:42:38 +01001167 }
Willy Tarreauae1747d2022-05-03 17:33:00 +02001168 ctx->flags |= PROMEX_FL_METRIC_HDR;
1169 ctx->st = stktables_list;
William Dauchy69164222021-02-07 20:42:38 +01001170 }
1171
1172 end:
1173 if (out.len) {
1174 if (!htx_add_data_atonce(htx, out))
1175 return -1; /* Unexpected and unrecoverable error */
1176 channel_add_input(chn, out.len);
1177 }
1178 return ret;
1179 full:
1180 ret = 0;
1181 goto end;
1182}
1183
Christopher Fauletf959d082019-02-07 15:38:42 +01001184/* Dump all metrics (global, frontends, backends and servers) depending on the
1185 * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001186 * -1 in case of any error.
Willy Tarreauae1747d2022-05-03 17:33:00 +02001187 * Uses <appctx.ctx.stats.px> as a pointer to the current proxy and <sv>/<li>
1188 * as pointers to the current server/listener respectively.
1189 */
Christopher Faulet908628c2022-03-25 16:43:49 +01001190static int promex_dump_metrics(struct appctx *appctx, struct conn_stream *cs, struct htx *htx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001191{
Willy Tarreauae1747d2022-05-03 17:33:00 +02001192 struct promex_ctx *ctx = appctx->svcctx;
Christopher Fauletf959d082019-02-07 15:38:42 +01001193 int ret;
1194
1195 switch (appctx->st1) {
1196 case PROMEX_DUMPER_INIT:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001197 ctx->px = NULL;
1198 ctx->st = NULL;
1199 ctx->li = NULL;
1200 ctx->sv = NULL;
1201 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
1202 ctx->obj_state = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001203 appctx->st2 = INF_NAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001204 appctx->st1 = PROMEX_DUMPER_GLOBAL;
1205 /* fall through */
1206
1207 case PROMEX_DUMPER_GLOBAL:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001208 if (ctx->flags & PROMEX_FL_SCOPE_GLOBAL) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001209 ret = promex_dump_global_metrics(appctx, htx);
1210 if (ret <= 0) {
1211 if (ret == -1)
1212 goto error;
1213 goto full;
1214 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001215 }
1216
Willy Tarreauae1747d2022-05-03 17:33:00 +02001217 ctx->px = proxies_list;
1218 ctx->st = NULL;
1219 ctx->li = NULL;
1220 ctx->sv = NULL;
1221 ctx->flags &= ~PROMEX_FL_INFO_METRIC;
1222 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC);
1223 ctx->obj_state = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001224 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001225 appctx->st1 = PROMEX_DUMPER_FRONT;
1226 /* fall through */
1227
1228 case PROMEX_DUMPER_FRONT:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001229 if (ctx->flags & PROMEX_FL_SCOPE_FRONT) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001230 ret = promex_dump_front_metrics(appctx, htx);
1231 if (ret <= 0) {
1232 if (ret == -1)
1233 goto error;
1234 goto full;
1235 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001236 }
1237
Willy Tarreauae1747d2022-05-03 17:33:00 +02001238 ctx->px = proxies_list;
1239 ctx->st = NULL;
1240 ctx->li = LIST_NEXT(&proxies_list->conf.listeners, struct listener *, by_fe);
1241 ctx->sv = NULL;
1242 ctx->flags &= ~PROMEX_FL_FRONT_METRIC;
1243 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_LI_METRIC);
1244 ctx->obj_state = 0;
William Dauchye3f7bd52021-02-14 23:22:56 +01001245 appctx->st2 = ST_F_PXNAME;
1246 appctx->st1 = PROMEX_DUMPER_LI;
1247 /* fall through */
1248
1249 case PROMEX_DUMPER_LI:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001250 if (ctx->flags & PROMEX_FL_SCOPE_LI) {
William Dauchye3f7bd52021-02-14 23:22:56 +01001251 ret = promex_dump_listener_metrics(appctx, htx);
1252 if (ret <= 0) {
1253 if (ret == -1)
1254 goto error;
1255 goto full;
1256 }
1257 }
1258
Willy Tarreauae1747d2022-05-03 17:33:00 +02001259 ctx->px = proxies_list;
1260 ctx->st = NULL;
1261 ctx->li = NULL;
1262 ctx->sv = NULL;
1263 ctx->flags &= ~PROMEX_FL_LI_METRIC;
1264 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC);
1265 ctx->obj_state = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001266 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001267 appctx->st1 = PROMEX_DUMPER_BACK;
1268 /* fall through */
1269
1270 case PROMEX_DUMPER_BACK:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001271 if (ctx->flags & PROMEX_FL_SCOPE_BACK) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001272 ret = promex_dump_back_metrics(appctx, htx);
1273 if (ret <= 0) {
1274 if (ret == -1)
1275 goto error;
1276 goto full;
1277 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001278 }
1279
Willy Tarreauae1747d2022-05-03 17:33:00 +02001280 ctx->px = proxies_list;
1281 ctx->st = NULL;
1282 ctx->li = NULL;
1283 ctx->sv = ctx->px ? ctx->px->srv : NULL;
1284 ctx->flags &= ~PROMEX_FL_BACK_METRIC;
1285 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
1286 ctx->obj_state = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001287 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001288 appctx->st1 = PROMEX_DUMPER_SRV;
1289 /* fall through */
1290
1291 case PROMEX_DUMPER_SRV:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001292 if (ctx->flags & PROMEX_FL_SCOPE_SERVER) {
Christopher Faulet78407ce2019-11-18 14:47:08 +01001293 ret = promex_dump_srv_metrics(appctx, htx);
1294 if (ret <= 0) {
1295 if (ret == -1)
1296 goto error;
1297 goto full;
1298 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001299 }
1300
Willy Tarreauae1747d2022-05-03 17:33:00 +02001301 ctx->px = NULL;
1302 ctx->st = stktables_list;
1303 ctx->li = NULL;
1304 ctx->sv = NULL;
1305 ctx->flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
1306 ctx->flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC);
William Dauchy69164222021-02-07 20:42:38 +01001307 appctx->st2 = STICKTABLE_SIZE;
1308 appctx->st1 = PROMEX_DUMPER_STICKTABLE;
1309 /* fall through */
1310
1311 case PROMEX_DUMPER_STICKTABLE:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001312 if (ctx->flags & PROMEX_FL_SCOPE_STICKTABLE) {
William Dauchy69164222021-02-07 20:42:38 +01001313 ret = promex_dump_sticktable_metrics(appctx, htx);
1314 if (ret <= 0) {
1315 if (ret == -1)
1316 goto error;
1317 goto full;
1318 }
1319 }
1320
Willy Tarreauae1747d2022-05-03 17:33:00 +02001321 ctx->px = NULL;
1322 ctx->st = NULL;
1323 ctx->li = NULL;
1324 ctx->sv = NULL;
1325 ctx->flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001326 appctx->st2 = 0;
1327 appctx->st1 = PROMEX_DUMPER_DONE;
1328 /* fall through */
1329
1330 case PROMEX_DUMPER_DONE:
1331 default:
1332 break;
1333 }
1334
1335 return 1;
1336
1337 full:
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001338 cs_rx_room_blk(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001339 return 0;
1340 error:
1341 /* unrecoverable error */
Willy Tarreauae1747d2022-05-03 17:33:00 +02001342 ctx->px = NULL;
1343 ctx->st = NULL;
1344 ctx->li = NULL;
1345 ctx->sv = NULL;
1346 ctx->flags = 0;
Christopher Fauletf959d082019-02-07 15:38:42 +01001347 appctx->st2 = 0;
1348 appctx->st1 = PROMEX_DUMPER_DONE;
1349 return -1;
1350}
1351
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001352/* Parse the query string of request URI to filter the metrics. It returns 1 on
Christopher Faulet78407ce2019-11-18 14:47:08 +01001353 * success and -1 on error. */
Christopher Faulet908628c2022-03-25 16:43:49 +01001354static int promex_parse_uri(struct appctx *appctx, struct conn_stream *cs)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001355{
Willy Tarreauae1747d2022-05-03 17:33:00 +02001356 struct promex_ctx *ctx = appctx->svcctx;
Christopher Faulet908628c2022-03-25 16:43:49 +01001357 struct channel *req = cs_oc(cs);
1358 struct channel *res = cs_ic(cs);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001359 struct htx *req_htx, *res_htx;
1360 struct htx_sl *sl;
William Dauchyc65f6562019-11-26 12:56:26 +01001361 char *p, *key, *value;
1362 const char *end;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001363 struct buffer *err;
1364 int default_scopes = PROMEX_FL_SCOPE_ALL;
1365 int len;
1366
1367 /* Get the query-string */
1368 req_htx = htxbuf(&req->buf);
1369 sl = http_get_stline(req_htx);
1370 if (!sl)
1371 goto error;
1372 p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?');
1373 if (!p)
1374 goto end;
1375 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001376
William Dauchyc65f6562019-11-26 12:56:26 +01001377 /* copy the query-string */
1378 len = end - p;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001379 chunk_reset(&trash);
1380 memcpy(trash.area, p, len);
1381 trash.area[len] = 0;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001382 p = trash.area;
William Dauchyc65f6562019-11-26 12:56:26 +01001383 end = trash.area + len;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001384
1385 /* Parse the query-string */
William Dauchyc65f6562019-11-26 12:56:26 +01001386 while (p < end && *p && *p != '#') {
1387 value = NULL;
1388
1389 /* decode parameter name */
1390 key = p;
1391 while (p < end && *p != '=' && *p != '&' && *p != '#')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001392 ++p;
William Dauchyc65f6562019-11-26 12:56:26 +01001393 /* found a value */
1394 if (*p == '=') {
1395 *(p++) = 0;
1396 value = p;
1397 }
1398 else if (*p == '&')
1399 *(p++) = 0;
1400 else if (*p == '#')
1401 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001402 len = url_decode(key, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001403 if (len == -1)
1404 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001405
William Dauchyc65f6562019-11-26 12:56:26 +01001406 /* decode value */
1407 if (value) {
1408 while (p < end && *p != '=' && *p != '&' && *p != '#')
1409 ++p;
1410 if (*p == '=')
1411 goto error;
1412 if (*p == '&')
1413 *(p++) = 0;
1414 else if (*p == '#')
1415 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001416 len = url_decode(value, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001417 if (len == -1)
1418 goto error;
1419 }
1420
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001421 if (strcmp(key, "scope") == 0) {
William Dauchyc65f6562019-11-26 12:56:26 +01001422 default_scopes = 0; /* at least a scope defined, unset default scopes */
1423 if (!value)
1424 goto error;
1425 else if (*value == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001426 ctx->flags &= ~PROMEX_FL_SCOPE_ALL;
William Dauchyc65f6562019-11-26 12:56:26 +01001427 else if (*value == '*')
Willy Tarreauae1747d2022-05-03 17:33:00 +02001428 ctx->flags |= PROMEX_FL_SCOPE_ALL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001429 else if (strcmp(value, "global") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001430 ctx->flags |= PROMEX_FL_SCOPE_GLOBAL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001431 else if (strcmp(value, "server") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001432 ctx->flags |= PROMEX_FL_SCOPE_SERVER;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001433 else if (strcmp(value, "backend") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001434 ctx->flags |= PROMEX_FL_SCOPE_BACK;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001435 else if (strcmp(value, "frontend") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001436 ctx->flags |= PROMEX_FL_SCOPE_FRONT;
William Dauchye3f7bd52021-02-14 23:22:56 +01001437 else if (strcmp(value, "listener") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001438 ctx->flags |= PROMEX_FL_SCOPE_LI;
William Dauchy69164222021-02-07 20:42:38 +01001439 else if (strcmp(value, "sticktable") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001440 ctx->flags |= PROMEX_FL_SCOPE_STICKTABLE;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001441 else
1442 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001443 }
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001444 else if (strcmp(key, "no-maint") == 0)
Willy Tarreauae1747d2022-05-03 17:33:00 +02001445 ctx->flags |= PROMEX_FL_NO_MAINT_SRV;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001446 }
1447
1448 end:
Willy Tarreauae1747d2022-05-03 17:33:00 +02001449 ctx->flags |= default_scopes;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001450 return 1;
1451
1452 error:
1453 err = &http_err_chunks[HTTP_ERR_400];
1454 channel_erase(res);
1455 res->buf.data = b_data(err);
1456 memcpy(res->buf.area, b_head(err), b_data(err));
1457 res_htx = htx_from_buf(&res->buf);
1458 channel_add_input(res, res_htx->data);
1459 appctx->st0 = PROMEX_ST_END;
1460 return -1;
1461}
1462
Christopher Fauletf959d082019-02-07 15:38:42 +01001463/* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is
1464 * full. */
Christopher Faulet908628c2022-03-25 16:43:49 +01001465static int promex_send_headers(struct appctx *appctx, struct conn_stream *cs, struct htx *htx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001466{
Christopher Faulet908628c2022-03-25 16:43:49 +01001467 struct channel *chn = cs_ic(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001468 struct htx_sl *sl;
1469 unsigned int flags;
1470
1471 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);
1472 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
1473 if (!sl)
1474 goto full;
1475 sl->info.res.status = 200;
1476 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001477 !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) ||
1478 !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) ||
1479 !htx_add_endof(htx, HTX_BLK_EOH))
1480 goto full;
1481
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001482 channel_add_input(chn, htx->data);
Christopher Fauletf959d082019-02-07 15:38:42 +01001483 return 1;
1484 full:
1485 htx_reset(htx);
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001486 cs_rx_room_blk(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001487 return 0;
1488}
1489
1490/* The function returns 1 if the initialisation is complete, 0 if
1491 * an errors occurs and -1 if more data are required for initializing
1492 * the applet.
1493 */
Christopher Faulet4aa1d282022-01-13 16:01:35 +01001494static int promex_appctx_init(struct appctx *appctx)
Christopher Fauletf959d082019-02-07 15:38:42 +01001495{
Willy Tarreauae1747d2022-05-03 17:33:00 +02001496 applet_reserve_svcctx(appctx, sizeof(struct promex_ctx));
Christopher Fauletf959d082019-02-07 15:38:42 +01001497 appctx->st0 = PROMEX_ST_INIT;
1498 return 1;
1499}
1500
1501/* The main I/O handler for the promex applet. */
1502static void promex_appctx_handle_io(struct appctx *appctx)
1503{
Christopher Faulet908628c2022-03-25 16:43:49 +01001504 struct conn_stream *cs = appctx->owner;
1505 struct stream *s = __cs_strm(cs);
1506 struct channel *req = cs_oc(cs);
1507 struct channel *res = cs_ic(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001508 struct htx *req_htx, *res_htx;
1509 int ret;
1510
1511 res_htx = htx_from_buf(&res->buf);
Christopher Faulet62e75742022-03-31 09:16:34 +02001512 if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO))
Christopher Fauletf959d082019-02-07 15:38:42 +01001513 goto out;
1514
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001515 /* Check if the input buffer is available. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001516 if (!b_size(&res->buf)) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001517 cs_rx_room_blk(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001518 goto out;
1519 }
1520
1521 switch (appctx->st0) {
1522 case PROMEX_ST_INIT:
Christopher Faulet908628c2022-03-25 16:43:49 +01001523 ret = promex_parse_uri(appctx, cs);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001524 if (ret <= 0) {
1525 if (ret == -1)
1526 goto error;
1527 goto out;
1528 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001529 appctx->st0 = PROMEX_ST_HEAD;
1530 appctx->st1 = PROMEX_DUMPER_INIT;
1531 /* fall through */
1532
1533 case PROMEX_ST_HEAD:
Christopher Faulet908628c2022-03-25 16:43:49 +01001534 if (!promex_send_headers(appctx, cs, res_htx))
Christopher Fauletf959d082019-02-07 15:38:42 +01001535 goto out;
1536 appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP);
1537 /* fall through */
1538
1539 case PROMEX_ST_DUMP:
Christopher Faulet908628c2022-03-25 16:43:49 +01001540 ret = promex_dump_metrics(appctx, cs, res_htx);
Christopher Fauletf959d082019-02-07 15:38:42 +01001541 if (ret <= 0) {
1542 if (ret == -1)
1543 goto error;
1544 goto out;
1545 }
1546 appctx->st0 = PROMEX_ST_DONE;
1547 /* fall through */
1548
1549 case PROMEX_ST_DONE:
Christopher Fauletbe69cbd2022-04-07 10:19:46 +02001550 /* no more data are expected. If the response buffer is
1551 * empty, be sure to add something (EOT block in this
1552 * case) to have something to send. It is important to
1553 * be sure the EOM flags will be handled by the
1554 * endpoint.
1555 */
1556 if (htx_is_empty(res_htx)) {
1557 if (!htx_add_endof(res_htx, HTX_BLK_EOT)) {
Christopher Fauleta0bdec32022-04-04 07:51:21 +02001558 cs_rx_room_blk(cs);
Christopher Fauletbe69cbd2022-04-07 10:19:46 +02001559 goto out;
1560 }
1561 channel_add_input(res, 1);
1562 }
1563 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet908628c2022-03-25 16:43:49 +01001564 cs->endp->flags |= CS_EP_EOI;
Christopher Fauletbef64b22022-03-07 15:56:20 +01001565 res->flags |= CF_EOI;
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001566 appctx->st0 = PROMEX_ST_END;
1567 /* fall through */
Christopher Fauletf959d082019-02-07 15:38:42 +01001568
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001569 case PROMEX_ST_END:
1570 if (!(res->flags & CF_SHUTR)) {
1571 res->flags |= CF_READ_NULL;
Christopher Fauletda098e62022-03-31 17:44:45 +02001572 cs_shutr(cs);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001573 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001574 }
1575
Christopher Fauletf959d082019-02-07 15:38:42 +01001576 out:
1577 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001578
1579 /* eat the whole request */
1580 if (co_data(req)) {
1581 req_htx = htx_from_buf(&req->buf);
1582 co_htx_skip(req, req_htx, co_data(req));
1583 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001584 return;
1585
1586 error:
1587 res->flags |= CF_READ_NULL;
Christopher Fauletda098e62022-03-31 17:44:45 +02001588 cs_shutr(cs);
1589 cs_shutw(cs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001590}
1591
1592struct applet promex_applet = {
1593 .obj_type = OBJ_TYPE_APPLET,
1594 .name = "<PROMEX>", /* used for logging */
1595 .init = promex_appctx_init,
1596 .fct = promex_appctx_handle_io,
1597};
1598
1599static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px,
1600 struct act_rule *rule, char **err)
1601{
1602 /* Prometheus exporter service is only available on "http-request" rulesets */
1603 if (rule->from != ACT_F_HTTP_REQ) {
1604 memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets");
1605 return ACT_RET_PRS_ERR;
1606 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001607
1608 /* Add applet pointer in the rule. */
1609 rule->applet = promex_applet;
1610
1611 return ACT_RET_PRS_OK;
1612}
1613static void promex_register_build_options(void)
1614{
1615 char *ptr = NULL;
1616
1617 memprintf(&ptr, "Built with the Prometheus exporter as a service");
1618 hap_register_build_opts(ptr, 1);
1619}
1620
1621
1622static struct action_kw_list service_actions = { ILH, {
1623 { "prometheus-exporter", service_parse_prometheus_exporter },
1624 { /* END */ }
1625}};
1626
1627INITCALL1(STG_REGISTER, service_keywords_register, &service_actions);
1628INITCALL0(STG_REGISTER, promex_register_build_options);