blob: df9c7cfdf293c8e0e2dbf09c37b9d0ddb78c5e57 [file] [log] [blame]
Christopher Fauletf959d082019-02-07 15:38:42 +01001/*
2 * Promex is a Prometheus exporter for HAProxy
3 *
4 * It is highly inspired by the official Prometheus exporter.
5 * See: https://github.com/prometheus/haproxy_exporter
6 *
7 * Copyright 2019 Christopher Faulet <cfaulet@haproxy.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 */
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020018#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020019#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
William Dauchyde3c3262021-02-01 13:11:51 +010021#include <haproxy/check.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020022#include <haproxy/compression.h>
Christopher Fauletc55a6262020-07-10 15:39:39 +020023#include <haproxy/dns.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 Tarreau87735332020-06-04 09:08:41 +020027#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020028#include <haproxy/htx.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020029#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020030#include <haproxy/listener.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020031#include <haproxy/log.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020032#include <haproxy/pipe.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020033#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020034#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020035#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020036#include <haproxy/server.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020037#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020038#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020039#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020040#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020041#include <haproxy/task.h>
William Dauchy5a982a72021-01-08 13:18:06 +010042#include <haproxy/version.h>
Christopher Fauletf959d082019-02-07 15:38:42 +010043
44/* Prometheus exporter applet states (appctx->st0) */
45enum {
46 PROMEX_ST_INIT = 0, /* initialized */
47 PROMEX_ST_HEAD, /* send headers before dump */
48 PROMEX_ST_DUMP, /* dumping stats */
49 PROMEX_ST_DONE, /* finished */
Christopher Faulet9744f7c2019-03-27 15:48:53 +010050 PROMEX_ST_END, /* treatment terminated */
Christopher Fauletf959d082019-02-07 15:38:42 +010051};
52
53/* Prometheus exporter dumper states (appctx->st1) */
54enum {
55 PROMEX_DUMPER_INIT = 0, /* initialized */
56 PROMEX_DUMPER_GLOBAL, /* dump metrics of globals */
57 PROMEX_DUMPER_FRONT, /* dump metrics of frontend proxies */
58 PROMEX_DUMPER_BACK, /* dump metrics of backend proxies */
59 PROMEX_DUMPER_LI, /* dump metrics of listeners */
60 PROMEX_DUMPER_SRV, /* dump metrics of servers */
61 PROMEX_DUMPER_DONE, /* finished */
62};
63
64/* Prometheus exporter flags (appctx->ctx.stats.flags) */
65#define PROMEX_FL_METRIC_HDR 0x00000001
66#define PROMEX_FL_INFO_METRIC 0x00000002
Christopher Fauletb713c4f2021-01-20 15:02:50 +010067#define PROMEX_FL_FRONT_METRIC 0x00000004
68#define PROMEX_FL_BACK_METRIC 0x00000008
69#define PROMEX_FL_SRV_METRIC 0x00000010
70#define PROMEX_FL_SCOPE_GLOBAL 0x00000020
71#define PROMEX_FL_SCOPE_FRONT 0x00000040
72#define PROMEX_FL_SCOPE_BACK 0x00000080
73#define PROMEX_FL_SCOPE_SERVER 0x00000100
74#define PROMEX_FL_NO_MAINT_SRV 0x00000200
Christopher Faulet78407ce2019-11-18 14:47:08 +010075
76#define PROMEX_FL_SCOPE_ALL (PROMEX_FL_SCOPE_GLOBAL|PROMEX_FL_SCOPE_FRONT|PROMEX_FL_SCOPE_BACK|PROMEX_FL_SCOPE_SERVER)
Christopher Fauletf959d082019-02-07 15:38:42 +010077
Christopher Faulet0312c0d2021-01-20 15:19:12 +010078/* Promtheus metric type (gauge or counter) */
79enum promex_mt_type {
80 PROMEX_MT_GAUGE = 1,
81 PROMEX_MT_COUNTER = 2,
82};
83
Christopher Fauletf959d082019-02-07 15:38:42 +010084/* The max length for metrics name. It is a hard limit but it should be
Ilya Shipitsince7b00f2020-03-23 22:28:40 +050085 * enough.
Christopher Fauletf959d082019-02-07 15:38:42 +010086 */
87#define PROMEX_MAX_NAME_LEN 128
88
89/* The expected max length for a metric dump, including its header lines. It is
90 * just a soft limit to avoid extra work. We don't try to dump a metric if less
91 * than this size is available in the HTX.
92 */
93#define PROMEX_MAX_METRIC_LENGTH 512
94
Christopher Faulet5a2f9382021-01-28 11:24:17 +010095/* The max number of labels per metric */
96#define PROMEX_MAX_LABELS 8
William Dauchy5a982a72021-01-08 13:18:06 +010097
Christopher Faulet0312c0d2021-01-20 15:19:12 +010098/* Describe a prometheus metric */
99struct promex_metric {
100 const struct ist n; /* The metric name */
101 enum promex_mt_type type; /* The metric type (gauge or counter) */
102 unsigned int flags; /* PROMEX_FL_* flags */
103};
104
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100105/* Describe a prometheus metric label. It is just a key/value pair */
106struct promex_label {
107 struct ist name;
108 struct ist value;
109};
110
Christopher Faulet37286a52021-01-20 15:20:53 +0100111/* Global metrics */
112const struct promex_metric promex_global_metrics[INF_TOTAL_FIELDS] = {
113 //[INF_NAME] ignored
114 //[INF_VERSION], ignored
115 //[INF_RELEASE_DATE] ignored
116 [INF_BUILD_INFO] = { .n = IST("build_info"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
117 [INF_NBTHREAD] = { .n = IST("nbthread"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
118 [INF_NBPROC] = { .n = IST("nbproc"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
119 [INF_PROCESS_NUM] = { .n = IST("relative_process_id"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
120 //[INF_PID] ignored
121 //[INF_UPTIME] ignored
122 [INF_UPTIME_SEC] = { .n = IST("uptime_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
123 [INF_START_TIME_SEC] = { .n = IST("start_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
124 [INF_MEMMAX_BYTES] = { .n = IST("max_memory_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
125 [INF_POOL_ALLOC_BYTES] = { .n = IST("pool_allocated_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
126 [INF_POOL_USED_BYTES] = { .n = IST("pool_used_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
127 [INF_POOL_FAILED] = { .n = IST("pool_failures_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
128 [INF_ULIMIT_N] = { .n = IST("max_fds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
129 [INF_MAXSOCK] = { .n = IST("max_sockets"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
130 [INF_MAXCONN] = { .n = IST("max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
131 [INF_HARD_MAXCONN] = { .n = IST("hard_max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
132 [INF_CURR_CONN] = { .n = IST("current_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
133 [INF_CUM_CONN] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
134 [INF_CUM_REQ] = { .n = IST("requests_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
135 [INF_MAX_SSL_CONNS] = { .n = IST("max_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
136 [INF_CURR_SSL_CONNS] = { .n = IST("current_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
137 [INF_CUM_SSL_CONNS] = { .n = IST("ssl_connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
138 [INF_MAXPIPES] = { .n = IST("max_pipes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
139 [INF_PIPES_USED] = { .n = IST("pipes_used_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
140 [INF_PIPES_FREE] = { .n = IST("pipes_free_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
141 [INF_CONN_RATE] = { .n = IST("current_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
142 [INF_CONN_RATE_LIMIT] = { .n = IST("limit_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
143 [INF_MAX_CONN_RATE] = { .n = IST("max_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
144 [INF_SESS_RATE] = { .n = IST("current_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
145 [INF_SESS_RATE_LIMIT] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
146 [INF_MAX_SESS_RATE] = { .n = IST("max_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
147 [INF_SSL_RATE] = { .n = IST("current_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
148 [INF_SSL_RATE_LIMIT] = { .n = IST("limit_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
149 [INF_MAX_SSL_RATE] = { .n = IST("max_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
150 [INF_SSL_FRONTEND_KEY_RATE] = { .n = IST("current_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
151 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .n = IST("max_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
152 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .n = IST("frontend_ssl_reuse"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
153 [INF_SSL_BACKEND_KEY_RATE] = { .n = IST("current_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
154 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .n = IST("max_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
155 [INF_SSL_CACHE_LOOKUPS] = { .n = IST("ssl_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
156 [INF_SSL_CACHE_MISSES] = { .n = IST("ssl_cache_misses_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
157 [INF_COMPRESS_BPS_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
158 [INF_COMPRESS_BPS_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
159 [INF_COMPRESS_BPS_RATE_LIM] = { .n = IST("limit_http_comp"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
160 [INF_ZLIB_MEM_USAGE] = { .n = IST("current_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
161 [INF_MAX_ZLIB_MEM_USAGE] = { .n = IST("max_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
162 [INF_TASKS] = { .n = IST("current_tasks"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
163 [INF_RUN_QUEUE] = { .n = IST("current_run_queue"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
164 [INF_IDLE_PCT] = { .n = IST("idle_time_percent"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
165 //[INF_NODE] ignored
166 //[INF_DESCRIPTION] ignored
167 [INF_STOPPING] = { .n = IST("stopping"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
168 [INF_JOBS] = { .n = IST("jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
169 [INF_UNSTOPPABLE_JOBS] = { .n = IST("unstoppable_jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
170 [INF_LISTENERS] = { .n = IST("listeners"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
171 [INF_ACTIVE_PEERS] = { .n = IST("active_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
172 [INF_CONNECTED_PEERS] = { .n = IST("connected_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
173 [INF_DROPPED_LOGS] = { .n = IST("dropped_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
174 [INF_BUSY_POLLING] = { .n = IST("busy_polling_enabled"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
175 [INF_FAILED_RESOLUTIONS] = { .n = IST("failed_resolutions"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
176 [INF_TOTAL_BYTES_OUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
177 [INF_TOTAL_SPLICED_BYTES_OUT] = { .n = IST("spliced_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
178 [INF_BYTES_OUT_RATE] = { .n = IST("bytes_out_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
179 //[INF_DEBUG_COMMANDS_ISSUED] ignored
Christopher Fauletf959d082019-02-07 15:38:42 +0100180};
181
Christopher Faulet37286a52021-01-20 15:20:53 +0100182/* frontend/backend/server fields */
183const struct promex_metric promex_st_metrics[ST_F_TOTAL_FIELDS] = {
184 //[ST_F_PXNAME] ignored
185 //[ST_F_SVNAME] ignored
186 [ST_F_QCUR] = { .n = IST("current_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
187 [ST_F_QMAX] = { .n = IST("max_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
188 [ST_F_SCUR] = { .n = IST("current_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
189 [ST_F_SMAX] = { .n = IST("max_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
190 [ST_F_SLIM] = { .n = IST("limit_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
191 [ST_F_STOT] = { .n = IST("sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
192 [ST_F_BIN] = { .n = IST("bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
193 [ST_F_BOUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
194 [ST_F_DREQ] = { .n = IST("requests_denied_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
195 [ST_F_DRESP] = { .n = IST("responses_denied_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
196 [ST_F_EREQ] = { .n = IST("request_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
197 [ST_F_ECON] = { .n = IST("connection_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
198 [ST_F_ERESP] = { .n = IST("response_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
199 [ST_F_WRETR] = { .n = IST("retry_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
200 [ST_F_WREDIS] = { .n = IST("redispatch_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
201 [ST_F_STATUS] = { .n = IST("status"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
202 [ST_F_WEIGHT] = { .n = IST("weight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
203 [ST_F_ACT] = { .n = IST("active_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
204 [ST_F_BCK] = { .n = IST("backup_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
205 [ST_F_CHKFAIL] = { .n = IST("check_failures_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_SRV_METRIC) },
206 [ST_F_CHKDOWN] = { .n = IST("check_up_down_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
207 [ST_F_LASTCHG] = { .n = IST("check_last_change_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
208 [ST_F_DOWNTIME] = { .n = IST("downtime_seconds_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
209 [ST_F_QLIMIT] = { .n = IST("queue_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
210 //[ST_F_PID] ignored
211 //[ST_F_IID] ignored
212 //[ST_F_SID] ignored
213 [ST_F_THROTTLE] = { .n = IST("current_throttle"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
214 [ST_F_LBTOT] = { .n = IST("loadbalanced_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
215 //[ST_F_TRACKED] ignored
216 //[ST_F_TYPE] ignored
217 //[ST_F_RATE] ignored
218 [ST_F_RATE_LIM] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
219 [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) },
220 [ST_F_CHECK_STATUS] = { .n = IST("check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
221 [ST_F_CHECK_CODE] = { .n = IST("check_code"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
222 [ST_F_CHECK_DURATION] = { .n = IST("check_duration_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
223 [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) },
224 [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) },
225 [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) },
226 [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) },
227 [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) },
228 [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) },
229 //[ST_F_HANAFAIL] ignored
230 //[ST_F_REQ_RATE] ignored
231 [ST_F_REQ_RATE_MAX] = { .n = IST("http_requests_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
232 [ST_F_REQ_TOT] = { .n = IST("http_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
233 [ST_F_CLI_ABRT] = { .n = IST("client_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
234 [ST_F_SRV_ABRT] = { .n = IST("server_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
235 [ST_F_COMP_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
236 [ST_F_COMP_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
237 [ST_F_COMP_BYP] = { .n = IST("http_comp_bytes_bypassed_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
238 [ST_F_COMP_RSP] = { .n = IST("http_comp_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
239 [ST_F_LASTSESS] = { .n = IST("last_session_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
240 //[ST_F_LAST_CHK] ignroed
241 //[ST_F_LAST_AGT] ignored
242 [ST_F_QTIME] = { .n = IST("queue_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
243 [ST_F_CTIME] = { .n = IST("connect_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
244 [ST_F_RTIME] = { .n = IST("response_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
245 [ST_F_TTIME] = { .n = IST("total_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
246 //[ST_F_AGENT_STATUS] ignored
247 //[ST_F_AGENT_CODE] ignored
248 //[ST_F_AGENT_DURATION] ignored
249 //[ST_F_CHECK_DESC] ignored
250 //[ST_F_AGENT_DESC] ignored
251 //[ST_F_CHECK_RISE] ignored
252 //[ST_F_CHECK_FALL] ignored
253 //[ST_F_CHECK_HEALTH] ignored
254 //[ST_F_AGENT_RISE] ignored
255 //[ST_F_AGENT_FALL] ignored
256 //[ST_F_AGENT_HEALTH] ignored
257 //[ST_F_ADDR] ignored
258 //[ST_F_COOKIE] ignored
259 //[ST_F_MODE] ignored
260 //[ST_F_ALGO] ignored
261 //[ST_F_CONN_RATE] ignored
262 [ST_F_CONN_RATE_MAX] = { .n = IST("connections_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
263 [ST_F_CONN_TOT] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
264 [ST_F_INTERCEPTED] = { .n = IST("intercepted_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
265 [ST_F_DCON] = { .n = IST("denied_connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
266 [ST_F_DSES] = { .n = IST("denied_sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
267 [ST_F_WREW] = { .n = IST("failed_header_rewriting_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
268 [ST_F_CONNECT] = { .n = IST("connection_attempts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
269 [ST_F_REUSE] = { .n = IST("connection_reuses_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
270 [ST_F_CACHE_LOOKUPS] = { .n = IST("http_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
271 [ST_F_CACHE_HITS] = { .n = IST("http_cache_hits_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
272 [ST_F_SRV_ICUR] = { .n = IST("idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
273 [ST_F_SRV_ILIM] = { .n = IST("idle_connections_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
274 [ST_F_QT_MAX] = { .n = IST("max_queue_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
275 [ST_F_CT_MAX] = { .n = IST("max_connect_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
276 [ST_F_RT_MAX] = { .n = IST("max_response_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
277 [ST_F_TT_MAX] = { .n = IST("max_total_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
278 [ST_F_EINT] = { .n = IST("internal_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
279 [ST_F_IDLE_CONN_CUR] = { .n = IST("unsafe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
280 [ST_F_SAFE_CONN_CUR]= { .n = IST("safe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
281 [ST_F_USED_CONN_CUR] = { .n = IST("used_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
282 [ST_F_NEED_CONN_EST] = { .n = IST("need_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
Christopher Fauletf959d082019-02-07 15:38:42 +0100283};
284
Christopher Fauletf959d082019-02-07 15:38:42 +0100285/* Description of all stats fields */
286const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
287 [ST_F_PXNAME] = IST("The proxy name."),
288 [ST_F_SVNAME] = IST("The service name (FRONTEND for frontend, BACKEND for backend, any name for server/listener)."),
289 [ST_F_QCUR] = IST("Current number of queued requests."),
290 [ST_F_QMAX] = IST("Maximum observed number of queued requests."),
291 [ST_F_SCUR] = IST("Current number of active sessions."),
292 [ST_F_SMAX] = IST("Maximum observed number of active sessions."),
293 [ST_F_SLIM] = IST("Configured session limit."),
294 [ST_F_STOT] = IST("Total number of sessions."),
295 [ST_F_BIN] = IST("Current total of incoming bytes."),
296 [ST_F_BOUT] = IST("Current total of outgoing bytes."),
297 [ST_F_DREQ] = IST("Total number of denied requests."),
298 [ST_F_DRESP] = IST("Total number of denied responses."),
299 [ST_F_EREQ] = IST("Total number of request errors."),
300 [ST_F_ECON] = IST("Total number of connection errors."),
301 [ST_F_ERESP] = IST("Total number of response errors."),
302 [ST_F_WRETR] = IST("Total number of retry warnings."),
303 [ST_F_WREDIS] = IST("Total number of redispatch warnings."),
William Dauchyc6464592021-01-27 22:40:17 +0100304 [ST_F_STATUS] = IST("Current status of the service."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100305 [ST_F_WEIGHT] = IST("Service weight."),
306 [ST_F_ACT] = IST("Current number of active servers."),
307 [ST_F_BCK] = IST("Current number of backup servers."),
308 [ST_F_CHKFAIL] = IST("Total number of failed check (Only counts checks failed when the server is up)."),
309 [ST_F_CHKDOWN] = IST("Total number of UP->DOWN transitions."),
310 [ST_F_LASTCHG] = IST("Number of seconds since the last UP<->DOWN transition."),
311 [ST_F_DOWNTIME] = IST("Total downtime (in seconds) for the service."),
312 [ST_F_QLIMIT] = IST("Configured maxqueue for the server (0 meaning no limit)."),
313 [ST_F_PID] = IST("Process id (0 for first instance, 1 for second, ...)"),
314 [ST_F_IID] = IST("Unique proxy id."),
315 [ST_F_SID] = IST("Server id (unique inside a proxy)."),
316 [ST_F_THROTTLE] = IST("Current throttle percentage for the server, when slowstart is active, or no value if not in slowstart."),
317 [ST_F_LBTOT] = IST("Total number of times a service was selected, either for new sessions, or when redispatching."),
318 [ST_F_TRACKED] = IST("Id of proxy/server if tracking is enabled."),
319 [ST_F_TYPE] = IST("Service type (0=frontend, 1=backend, 2=server, 3=socket/listener)."),
320 [ST_F_RATE] = IST("Current number of sessions per second over last elapsed second."),
321 [ST_F_RATE_LIM] = IST("Configured limit on new sessions per second."),
322 [ST_F_RATE_MAX] = IST("Maximum observed number of sessions per second."),
William Dauchyde3c3262021-02-01 13:11:51 +0100323 [ST_F_CHECK_STATUS] = IST("Status of last health check, per state label value."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100324 [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."),
Christopher Faulet2711e512020-02-27 16:12:07 +0100325 [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100326 [ST_F_HRSP_1XX] = IST("Total number of HTTP responses."),
327 [ST_F_HRSP_2XX] = IST("Total number of HTTP responses."),
328 [ST_F_HRSP_3XX] = IST("Total number of HTTP responses."),
329 [ST_F_HRSP_4XX] = IST("Total number of HTTP responses."),
330 [ST_F_HRSP_5XX] = IST("Total number of HTTP responses."),
331 [ST_F_HRSP_OTHER] = IST("Total number of HTTP responses."),
332 [ST_F_HANAFAIL] = IST("Total number of failed health checks."),
333 [ST_F_REQ_RATE] = IST("Current number of HTTP requests per second over last elapsed second."),
334 [ST_F_REQ_RATE_MAX] = IST("Maximum observed number of HTTP requests per second."),
335 [ST_F_REQ_TOT] = IST("Total number of HTTP requests received."),
336 [ST_F_CLI_ABRT] = IST("Total number of data transfers aborted by the client."),
337 [ST_F_SRV_ABRT] = IST("Total number of data transfers aborted by the server."),
338 [ST_F_COMP_IN] = IST("Total number of HTTP response bytes fed to the compressor."),
339 [ST_F_COMP_OUT] = IST("Total number of HTTP response bytes emitted by the compressor."),
340 [ST_F_COMP_BYP] = IST("Total number of bytes that bypassed the HTTP compressor (CPU/BW limit)."),
341 [ST_F_COMP_RSP] = IST("Total number of HTTP responses that were compressed."),
342 [ST_F_LASTSESS] = IST("Number of seconds since last session assigned to server/backend."),
343 [ST_F_LAST_CHK] = IST("Last health check contents or textual error"),
344 [ST_F_LAST_AGT] = IST("Last agent check contents or textual error"),
345 [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."),
346 [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."),
347 [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."),
348 [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."),
349 [ST_F_AGENT_STATUS] = IST("Status of last agent check."),
350 [ST_F_AGENT_CODE] = IST("Numeric code reported by agent if any (unused for now)."),
351 [ST_F_AGENT_DURATION] = IST("Time in ms taken to finish last agent check."),
352 [ST_F_CHECK_DESC] = IST("Short human-readable description of the last health status."),
353 [ST_F_AGENT_DESC] = IST("Short human-readable description of the last agent status."),
354 [ST_F_CHECK_RISE] = IST("Server's \"rise\" parameter used by health checks"),
355 [ST_F_CHECK_FALL] = IST("Server's \"fall\" parameter used by health checks"),
356 [ST_F_CHECK_HEALTH] = IST("Server's health check value between 0 and rise+fall-1"),
357 [ST_F_AGENT_RISE] = IST("Agent's \"rise\" parameter, normally 1."),
358 [ST_F_AGENT_FALL] = IST("Agent's \"fall\" parameter, normally 1."),
359 [ST_F_AGENT_HEALTH] = IST("Agent's health parameter, between 0 and rise+fall-1"),
360 [ST_F_ADDR] = IST("address:port or \"unix\". IPv6 has brackets around the address."),
361 [ST_F_COOKIE] = IST("Server's cookie value or backend's cookie name."),
362 [ST_F_MODE] = IST("Proxy mode (tcp, http, health, unknown)."),
363 [ST_F_ALGO] = IST("Load balancing algorithm."),
364 [ST_F_CONN_RATE] = IST("Current number of connections per second over the last elapsed second."),
365 [ST_F_CONN_RATE_MAX] = IST("Maximum observed number of connections per second."),
366 [ST_F_CONN_TOT] = IST("Total number of connections."),
367 [ST_F_INTERCEPTED] = IST("Total number of intercepted HTTP requests."),
368 [ST_F_DCON] = IST("Total number of requests denied by \"tcp-request connection\" rules."),
369 [ST_F_DSES] = IST("Total number of requests denied by \"tcp-request session\" rules."),
370 [ST_F_WREW] = IST("Total number of failed header rewriting warnings."),
371 [ST_F_CONNECT] = IST("Total number of connection establishment attempts."),
372 [ST_F_REUSE] = IST("Total number of connection reuses."),
373 [ST_F_CACHE_LOOKUPS] = IST("Total number of HTTP cache lookups."),
374 [ST_F_CACHE_HITS] = IST("Total number of HTTP cache hits."),
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100375 [ST_F_SRV_ICUR] = IST("Current number of idle connections available for reuse"),
376 [ST_F_SRV_ILIM] = IST("Limit on the number of available idle connections"),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100377 [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"),
378 [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"),
379 [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"),
380 [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100381 [ST_F_EINT] = IST("Total number of internal errors."),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200382 [ST_F_IDLE_CONN_CUR] = IST("Current number of unsafe idle connections."),
383 [ST_F_SAFE_CONN_CUR] = IST("Current number of safe idle connections."),
384 [ST_F_USED_CONN_CUR] = IST("Current number of connections in use."),
385 [ST_F_NEED_CONN_EST] = IST("Estimated needed number of connections."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100386};
387
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100388/* Specific labels for all ST_F_HRSP_* fields */
389const struct ist promex_hrsp_code[1 + ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = {
390 [ST_F_HRSP_1XX - ST_F_HRSP_1XX] = IST("1xx"),
391 [ST_F_HRSP_2XX - ST_F_HRSP_1XX] = IST("2xx"),
392 [ST_F_HRSP_3XX - ST_F_HRSP_1XX] = IST("3xx"),
393 [ST_F_HRSP_4XX - ST_F_HRSP_1XX] = IST("4xx"),
394 [ST_F_HRSP_5XX - ST_F_HRSP_1XX] = IST("5xx"),
395 [ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = IST("other"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100396};
397
William Dauchy54938212021-01-27 22:40:16 +0100398enum promex_front_state {
399 PROMEX_FRONT_STATE_DOWN = 0,
400 PROMEX_FRONT_STATE_UP,
401
402 PROMEX_FRONT_STATE_COUNT /* must be last */
403};
404
405const struct ist promex_front_st[PROMEX_FRONT_STATE_COUNT] = {
406 [PROMEX_FRONT_STATE_DOWN] = IST("DOWN"),
407 [PROMEX_FRONT_STATE_UP] = IST("UP"),
408};
409
410enum promex_back_state {
411 PROMEX_BACK_STATE_DOWN = 0,
412 PROMEX_BACK_STATE_UP,
413
414 PROMEX_BACK_STATE_COUNT /* must be last */
415};
416
417const struct ist promex_back_st[PROMEX_BACK_STATE_COUNT] = {
418 [PROMEX_BACK_STATE_DOWN] = IST("DOWN"),
419 [PROMEX_BACK_STATE_UP] = IST("UP"),
420};
421
422enum promex_srv_state {
423 PROMEX_SRV_STATE_DOWN = 0,
424 PROMEX_SRV_STATE_UP,
425 PROMEX_SRV_STATE_MAINT,
426 PROMEX_SRV_STATE_DRAIN,
427 PROMEX_SRV_STATE_NOLB,
428
429 PROMEX_SRV_STATE_COUNT /* must be last */
430};
431
432const struct ist promex_srv_st[PROMEX_SRV_STATE_COUNT] = {
433 [PROMEX_SRV_STATE_DOWN] = IST("DOWN"),
434 [PROMEX_SRV_STATE_UP] = IST("UP"),
435 [PROMEX_SRV_STATE_MAINT] = IST("MAINT"),
436 [PROMEX_SRV_STATE_DRAIN] = IST("DRAIN"),
437 [PROMEX_SRV_STATE_NOLB] = IST("NOLB"),
438};
439
440/* Return the server status. */
441enum promex_srv_state promex_srv_status(struct server *sv)
Christopher Fauletf959d082019-02-07 15:38:42 +0100442{
William Dauchy54938212021-01-27 22:40:16 +0100443 int state = PROMEX_SRV_STATE_DOWN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100444
Christopher Fauletf959d082019-02-07 15:38:42 +0100445 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Dauchy54938212021-01-27 22:40:16 +0100446 state = PROMEX_SRV_STATE_UP;
Christopher Fauletf959d082019-02-07 15:38:42 +0100447 if (sv->cur_admin & SRV_ADMF_DRAIN)
William Dauchy54938212021-01-27 22:40:16 +0100448 state = PROMEX_SRV_STATE_DRAIN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100449 }
Christopher Fauletd45d1052019-09-06 16:10:19 +0200450 else if (sv->cur_state == SRV_ST_STOPPING)
William Dauchy54938212021-01-27 22:40:16 +0100451 state = PROMEX_SRV_STATE_NOLB;
Christopher Fauletd45d1052019-09-06 16:10:19 +0200452
453 if (sv->cur_admin & SRV_ADMF_MAINT)
William Dauchy54938212021-01-27 22:40:16 +0100454 state = PROMEX_SRV_STATE_MAINT;
Christopher Fauletf959d082019-02-07 15:38:42 +0100455
456 return state;
457}
458
459/* Convert a field to its string representation and write it in <out>, followed
460 * by a newline, if there is enough space. non-numeric value are converted in
William Dauchy18a2c6e2021-01-22 21:09:47 +0100461 * "NaN" because Prometheus only support numerical values (but it is unexepceted
Christopher Fauletf959d082019-02-07 15:38:42 +0100462 * to process this kind of value). It returns 1 on success. Otherwise, it
463 * returns 0. The buffer's length must not exceed <max> value.
464 */
465static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
466{
467 int ret = 0;
468
469 switch (field_format(f, 0)) {
William Dauchy18a2c6e2021-01-22 21:09:47 +0100470 case FF_EMPTY: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100471 case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break;
472 case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break;
473 case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break;
474 case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200475 case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break;
William Dauchy18a2c6e2021-01-22 21:09:47 +0100476 case FF_STR: ret = chunk_strcat(out, "NaN\n"); break;
477 default: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100478 }
479 if (!ret || out->data > max)
480 return 0;
481 return 1;
482}
483
Christopher Fauletf959d082019-02-07 15:38:42 +0100484/* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
485 * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
486 */
487static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
Christopher Faulet37286a52021-01-20 15:20:53 +0100488 const struct promex_metric *metric, const struct ist name,
489 struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100490{
Christopher Faulet37286a52021-01-20 15:20:53 +0100491 struct ist type;
492
493 switch (metric->type) {
494 case PROMEX_MT_COUNTER:
495 type = ist("counter");
496 break;
497 default:
498 type = ist("gauge");
499 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100500
William Dauchya191b772021-01-15 22:41:39 +0100501 if (istcat(out, ist("# HELP "), max) == -1 ||
502 istcat(out, name, max) == -1 ||
503 istcat(out, ist(" "), max) == -1)
504 goto full;
505
Christopher Faulet37286a52021-01-20 15:20:53 +0100506 if (metric->flags & PROMEX_FL_INFO_METRIC) {
William Dauchya191b772021-01-15 22:41:39 +0100507 if (istcat(out, ist(info_fields[appctx->st2].desc), max) == -1)
508 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100509 }
510 else {
Christopher Faulet37286a52021-01-20 15:20:53 +0100511 if (istcat(out, promex_st_metric_desc[appctx->st2], max) == -1)
William Dauchya191b772021-01-15 22:41:39 +0100512 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100513 }
514
William Dauchya191b772021-01-15 22:41:39 +0100515 if (istcat(out, ist("\n# TYPE "), max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100516 istcat(out, name, max) == -1 ||
517 istcat(out, ist(" "), max) == -1 ||
Christopher Faulet37286a52021-01-20 15:20:53 +0100518 istcat(out, type, max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100519 istcat(out, ist("\n"), max) == -1)
520 goto full;
521
522 return 1;
523
524 full:
525 return 0;
526}
527
528/* Dump the line for <metric>. It starts by the metric name followed by its
529 * labels (proxy name, server name...) between braces and finally its value. If
530 * not already done, the header lines are dumped first. It returns 1 on
531 * success. Otherwise if <out> length exceeds <max>, it returns 0.
532 */
Christopher Faulet37286a52021-01-20 15:20:53 +0100533static int promex_dump_metric(struct appctx *appctx, struct htx *htx, struct ist prefix,
William Dauchyc6464592021-01-27 22:40:17 +0100534 const struct promex_metric *metric, struct field *val,
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100535 struct promex_label *labels, struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100536{
537 struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
538 size_t len = out->len;
539
540 if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
541 return 0;
542
Christopher Faulet37286a52021-01-20 15:20:53 +0100543 /* Fill the metric name */
544 istcat(&name, prefix, PROMEX_MAX_NAME_LEN);
545 istcat(&name, metric->n, PROMEX_MAX_NAME_LEN);
546
547
Christopher Fauletf959d082019-02-07 15:38:42 +0100548 if ((appctx->ctx.stats.flags & PROMEX_FL_METRIC_HDR) &&
Christopher Faulet37286a52021-01-20 15:20:53 +0100549 !promex_dump_metric_header(appctx, htx, metric, name, out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100550 goto full;
551
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100552 if (istcat(out, name, max) == -1)
553 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100554
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100555 if (isttest(labels[0].name)) {
556 int i;
557
558 if (istcat(out, ist("{"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100559 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100560
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100561 for (i = 0; isttest(labels[i].name); i++) {
562 if (!isttest(labels[i].value))
563 continue;
564
565 if ((i && istcat(out, ist(","), max) == -1) ||
566 istcat(out, labels[i].name, max) == -1 ||
567 istcat(out, ist("=\""), max) == -1 ||
568 istcat(out, labels[i].value, max) == -1 ||
569 istcat(out, ist("\""), max) == -1)
570 goto full;
571 }
572
573 if (istcat(out, ist("}"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100574 goto full;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100575
Christopher Fauletf959d082019-02-07 15:38:42 +0100576 }
577
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100578 if (istcat(out, ist(" "), max) == -1)
579 goto full;
580
Christopher Fauletf959d082019-02-07 15:38:42 +0100581 trash.data = out->len;
Christopher Faulet37286a52021-01-20 15:20:53 +0100582 if (!promex_metric_to_str(&trash, val, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100583 goto full;
584 out->len = trash.data;
585
586 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
587 return 1;
588 full:
589 // Restore previous length
590 out->len = len;
591 return 0;
592
593}
594
595
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500596/* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100597 * 0 if <htx> is full and -1 in case of any error. */
598static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
599{
600 static struct ist prefix = IST("haproxy_process_");
Christopher Faulet37286a52021-01-20 15:20:53 +0100601 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100602 struct channel *chn = si_ic(appctx->owner);
603 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200604 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100605 int ret = 1;
606
William Dauchy5d9b8f32021-01-11 20:07:49 +0100607 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
608 return -1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100609
Christopher Faulet37286a52021-01-20 15:20:53 +0100610 for (; appctx->st2 < INF_TOTAL_FIELDS; appctx->st2++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100611 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
612
Christopher Faulet37286a52021-01-20 15:20:53 +0100613 if (!(promex_global_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
614 continue;
615
Christopher Fauletf959d082019-02-07 15:38:42 +0100616 switch (appctx->st2) {
William Dauchy5a982a72021-01-08 13:18:06 +0100617 case INF_BUILD_INFO:
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100618 labels[0].name = ist("version");
619 labels[0].value = ist(HAPROXY_VERSION);
Christopher Faulet37286a52021-01-20 15:20:53 +0100620 val = mkf_u32(FN_GAUGE, 1);
William Dauchy5a982a72021-01-08 13:18:06 +0100621 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100622
623 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100624 val = info[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100625 }
626
William Dauchyc6464592021-01-27 22:40:17 +0100627 if (!promex_dump_metric(appctx, htx, prefix, &promex_global_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100628 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100629 goto full;
630
Christopher Fauletf959d082019-02-07 15:38:42 +0100631 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100632 }
633
634 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200635 if (out.len) {
636 if (!htx_add_data_atonce(htx, out))
637 return -1; /* Unexpected and unrecoverable error */
638 channel_add_input(chn, out.len);
639 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100640 return ret;
641 full:
642 ret = 0;
643 goto end;
644}
645
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500646/* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100647 * 0 if <htx> is full and -1 in case of any error. */
648static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
649{
650 static struct ist prefix = IST("haproxy_frontend_");
651 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100652 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100653 struct channel *chn = si_ic(appctx->owner);
654 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200655 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchyb9577452021-01-17 18:27:46 +0100656 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100657 int ret = 1;
William Dauchyc6464592021-01-27 22:40:17 +0100658 enum promex_front_state state;
William Dauchyc6464592021-01-27 22:40:17 +0100659 int i;
Christopher Fauletf959d082019-02-07 15:38:42 +0100660
Christopher Faulet37286a52021-01-20 15:20:53 +0100661 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
662 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
663 continue;
664
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200665 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100666 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
667
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200668 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100669
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100670 labels[0].name = ist("proxy");
671 labels[0].value = ist2(px->id, strlen(px->id));
672
Christopher Fauletf959d082019-02-07 15:38:42 +0100673 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200674 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100675 goto next_px;
676
William Dauchyb9577452021-01-17 18:27:46 +0100677 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
678 return -1;
679
Christopher Fauletf959d082019-02-07 15:38:42 +0100680 switch (appctx->st2) {
681 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100682 state = !px->disabled;
683 for (i = 0; i < PROMEX_FRONT_STATE_COUNT; i++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100684 labels[1].name = ist("state");
685 labels[1].value = promex_front_st[i];
William Dauchyc6464592021-01-27 22:40:17 +0100686 val = mkf_u32(FO_STATUS, state == i);
William Dauchyc6464592021-01-27 22:40:17 +0100687 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100688 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100689 goto full;
690 }
691 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100692 case ST_F_REQ_RATE_MAX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100693 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100694 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100695 case ST_F_INTERCEPTED:
Christopher Fauletf959d082019-02-07 15:38:42 +0100696 case ST_F_CACHE_LOOKUPS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100697 case ST_F_CACHE_HITS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100698 case ST_F_COMP_IN:
Christopher Fauletf959d082019-02-07 15:38:42 +0100699 case ST_F_COMP_OUT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100700 case ST_F_COMP_BYP:
William Dauchyb9577452021-01-17 18:27:46 +0100701 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100702 if (px->mode != PR_MODE_HTTP)
703 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100704 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100705 break;
William Dauchyb9577452021-01-17 18:27:46 +0100706 case ST_F_HRSP_2XX:
707 case ST_F_HRSP_3XX:
708 case ST_F_HRSP_4XX:
709 case ST_F_HRSP_5XX:
710 case ST_F_HRSP_OTHER:
Christopher Fauletf959d082019-02-07 15:38:42 +0100711 if (px->mode != PR_MODE_HTTP)
712 goto next_px;
William Dauchyb9577452021-01-17 18:27:46 +0100713 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100714 labels[1].name = ist("code");
715 labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
716 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100717 break;
718
719 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100720 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100721 }
722
William Dauchyc6464592021-01-27 22:40:17 +0100723 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100724 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100725 goto full;
726 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200727 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100728 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100729 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200730 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100731 }
732
733 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200734 if (out.len) {
735 if (!htx_add_data_atonce(htx, out))
736 return -1; /* Unexpected and unrecoverable error */
737 channel_add_input(chn, out.len);
738 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100739 return ret;
740 full:
741 ret = 0;
742 goto end;
743}
744
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500745/* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100746 * 0 if <htx> is full and -1 in case of any error. */
747static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
748{
749 static struct ist prefix = IST("haproxy_backend_");
750 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100751 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100752 struct channel *chn = si_ic(appctx->owner);
753 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200754 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchy3c6f0062021-01-25 17:29:02 +0100755 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100756 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200757 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100758 enum promex_back_state state;
William Dauchyc6464592021-01-27 22:40:17 +0100759 int i;
Christopher Fauletf959d082019-02-07 15:38:42 +0100760
Christopher Faulet37286a52021-01-20 15:20:53 +0100761 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
762 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
763 continue;
764
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200765 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100766 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
767
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200768 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100769
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100770 labels[0].name = ist("proxy");
771 labels[0].value = ist2(px->id, strlen(px->id));
772
Christopher Fauletf959d082019-02-07 15:38:42 +0100773 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200774 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100775 goto next_px;
776
William Dauchy3c6f0062021-01-25 17:29:02 +0100777 if (!stats_fill_be_stats(px, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
778 return -1;
779
Christopher Fauletf959d082019-02-07 15:38:42 +0100780 switch (appctx->st2) {
781 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100782 state = ((px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0);
783 for (i = 0; i < PROMEX_BACK_STATE_COUNT; i++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100784 labels[1].name = ist("state");
785 labels[1].value = promex_back_st[i];
William Dauchyc6464592021-01-27 22:40:17 +0100786 val = mkf_u32(FO_STATUS, state == i);
William Dauchyc6464592021-01-27 22:40:17 +0100787 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100788 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100789 goto full;
790 }
791 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100792 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200793 secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100794 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100795 break;
796 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200797 secs = (double)swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100798 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100799 break;
800 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200801 secs = (double)swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100802 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100803 break;
804 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200805 secs = (double)swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100806 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100807 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100808 case ST_F_QT_MAX:
809 secs = (double)px->be_counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100810 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100811 break;
812 case ST_F_CT_MAX:
813 secs = (double)px->be_counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100814 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100815 break;
816 case ST_F_RT_MAX:
817 secs = (double)px->be_counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100818 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100819 break;
820 case ST_F_TT_MAX:
821 secs = (double)px->be_counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100822 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100823 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100824 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100825 case ST_F_HRSP_1XX:
William Dauchy3c6f0062021-01-25 17:29:02 +0100826 case ST_F_CACHE_LOOKUPS:
827 case ST_F_CACHE_HITS:
828 case ST_F_COMP_IN:
829 case ST_F_COMP_OUT:
830 case ST_F_COMP_BYP:
831 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100832 if (px->mode != PR_MODE_HTTP)
833 goto next_px;
William Dauchy3c6f0062021-01-25 17:29:02 +0100834 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100835 break;
836 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100837 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100838 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100839 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100840 case ST_F_HRSP_OTHER:
841 if (px->mode != PR_MODE_HTTP)
842 goto next_px;
843 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100844 labels[1].name = ist("code");
845 labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
William Dauchy3c6f0062021-01-25 17:29:02 +0100846 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100847 break;
848
849 default:
William Dauchy3c6f0062021-01-25 17:29:02 +0100850 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100851 }
852
William Dauchy3c6f0062021-01-25 17:29:02 +0100853 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100854 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100855 goto full;
856 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200857 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100858 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100859 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200860 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100861 }
862
863 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200864 if (out.len) {
865 if (!htx_add_data_atonce(htx, out))
866 return -1; /* Unexpected and unrecoverable error */
867 channel_add_input(chn, out.len);
868 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100869 return ret;
870 full:
871 ret = 0;
872 goto end;
873}
874
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500875/* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100876 * 0 if <htx> is full and -1 in case of any error. */
877static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
878{
879 static struct ist prefix = IST("haproxy_server_");
880 struct proxy *px;
881 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100882 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100883 struct channel *chn = si_ic(appctx->owner);
884 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200885 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchybde2bf62021-01-25 17:29:04 +0100886 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100887 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200888 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100889 enum promex_srv_state state;
William Dauchyde3c3262021-02-01 13:11:51 +0100890 const char *check_state;
William Dauchyc6464592021-01-27 22:40:17 +0100891 int i;
Christopher Fauletf959d082019-02-07 15:38:42 +0100892
Christopher Faulet37286a52021-01-20 15:20:53 +0100893 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
894 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
895 continue;
896
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200897 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100898 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
899
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200900 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100901
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100902 labels[0].name = ist("proxy");
903 labels[0].value = ist2(px->id, strlen(px->id));
904
Christopher Fauletf959d082019-02-07 15:38:42 +0100905 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200906 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100907 goto next_px;
908
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200909 while (appctx->ctx.stats.obj2) {
910 sv = appctx->ctx.stats.obj2;
Christopher Fauletf959d082019-02-07 15:38:42 +0100911
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100912 labels[1].name = ist("server");
913 labels[1].value = ist2(sv->id, strlen(sv->id));
914
William Dauchybde2bf62021-01-25 17:29:04 +0100915 if (!stats_fill_sv_stats(px, sv, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
916 return -1;
917
Christopher Fauleteba22942019-11-19 14:18:24 +0100918 if ((appctx->ctx.stats.flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
919 goto next_sv;
920
Christopher Fauletf959d082019-02-07 15:38:42 +0100921 switch (appctx->st2) {
922 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100923 state = promex_srv_status(sv);
924 for (i = 0; i < PROMEX_SRV_STATE_COUNT; i++) {
925 val = mkf_u32(FO_STATUS, state == i);
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100926 labels[2].name = ist("state");
927 labels[2].value = promex_srv_st[i];
William Dauchyc6464592021-01-27 22:40:17 +0100928 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100929 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100930 goto full;
931 }
932 goto next_sv;
Christopher Fauletf959d082019-02-07 15:38:42 +0100933 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200934 secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100935 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100936 break;
937 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200938 secs = (double)swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100939 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100940 break;
941 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200942 secs = (double)swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100943 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100944 break;
945 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200946 secs = (double)swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100947 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100948 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100949 case ST_F_QT_MAX:
950 secs = (double)sv->counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100951 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100952 break;
953 case ST_F_CT_MAX:
954 secs = (double)sv->counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100955 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100956 break;
957 case ST_F_RT_MAX:
958 secs = (double)sv->counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100959 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100960 break;
961 case ST_F_TT_MAX:
962 secs = (double)sv->counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100963 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100964 break;
Christopher Fauletcf403f32019-11-21 14:35:46 +0100965 case ST_F_CHECK_STATUS:
966 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
967 goto next_sv;
William Dauchyde3c3262021-02-01 13:11:51 +0100968
969 for (i = 0; i < HCHK_STATUS_SIZE; i++) {
970 if (get_check_status_result(i) < CHK_RES_FAILED)
971 continue;
972 val = mkf_u32(FO_STATUS, sv->check.status == i);
973 check_state = get_check_status_info(i);
974 labels[2].name = ist("state");
975 labels[2].value = ist2(check_state, strlen(check_state));
976 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
977 &val, labels, &out, max))
978 goto full;
979 }
980 goto next_sv;
Christopher Fauletcf403f32019-11-21 14:35:46 +0100981 case ST_F_CHECK_CODE:
982 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
983 goto next_sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100984 val = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code);
Christopher Fauletcf403f32019-11-21 14:35:46 +0100985 break;
Christopher Faulet2711e512020-02-27 16:12:07 +0100986 case ST_F_CHECK_DURATION:
987 if (sv->check.status < HCHK_STATUS_CHECKED)
988 goto next_sv;
989 secs = (double)sv->check.duration / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100990 val = mkf_flt(FN_DURATION, secs);
Christopher Faulet2711e512020-02-27 16:12:07 +0100991 break;
Marcin Deranek3c27dda2020-05-15 18:32:51 +0200992 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100993 case ST_F_HRSP_1XX:
994 if (px->mode != PR_MODE_HTTP)
995 goto next_px;
William Dauchybde2bf62021-01-25 17:29:04 +0100996 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100997 break;
998 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100999 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001000 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001001 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001002 case ST_F_HRSP_OTHER:
1003 if (px->mode != PR_MODE_HTTP)
1004 goto next_px;
1005 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001006 labels[2].name = ist("code");
1007 labels[2].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
William Dauchybde2bf62021-01-25 17:29:04 +01001008 val = stats[appctx->st2];
Christopher Fauletc55a6262020-07-10 15:39:39 +02001009 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001010
1011 default:
William Dauchybde2bf62021-01-25 17:29:04 +01001012 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001013 }
1014
William Dauchyc6464592021-01-27 22:40:17 +01001015 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001016 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +01001017 goto full;
Christopher Fauleteba22942019-11-19 14:18:24 +01001018 next_sv:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001019 appctx->ctx.stats.obj2 = sv->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001020 }
1021
1022 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001023 appctx->ctx.stats.obj1 = px->next;
1024 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001025 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001026 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001027 appctx->ctx.stats.obj1 = proxies_list;
1028 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001029 }
1030
1031
1032 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001033 if (out.len) {
1034 if (!htx_add_data_atonce(htx, out))
1035 return -1; /* Unexpected and unrecoverable error */
1036 channel_add_input(chn, out.len);
1037 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001038 return ret;
1039 full:
1040 ret = 0;
1041 goto end;
1042}
1043
1044/* Dump all metrics (global, frontends, backends and servers) depending on the
1045 * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001046 * -1 in case of any error.
1047 * Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
1048 * a pointer to the current server/listener. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001049static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1050{
1051 int ret;
1052
1053 switch (appctx->st1) {
1054 case PROMEX_DUMPER_INIT:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001055 appctx->ctx.stats.obj1 = NULL;
1056 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001057 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001058 appctx->st2 = INF_NAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001059 appctx->st1 = PROMEX_DUMPER_GLOBAL;
1060 /* fall through */
1061
1062 case PROMEX_DUMPER_GLOBAL:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001063 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_GLOBAL) {
1064 ret = promex_dump_global_metrics(appctx, htx);
1065 if (ret <= 0) {
1066 if (ret == -1)
1067 goto error;
1068 goto full;
1069 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001070 }
1071
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001072 appctx->ctx.stats.obj1 = proxies_list;
1073 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001074 appctx->ctx.stats.flags &= ~PROMEX_FL_INFO_METRIC;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001075 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001076 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001077 appctx->st1 = PROMEX_DUMPER_FRONT;
1078 /* fall through */
1079
1080 case PROMEX_DUMPER_FRONT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001081 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_FRONT) {
1082 ret = promex_dump_front_metrics(appctx, htx);
1083 if (ret <= 0) {
1084 if (ret == -1)
1085 goto error;
1086 goto full;
1087 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001088 }
1089
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001090 appctx->ctx.stats.obj1 = proxies_list;
1091 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001092 appctx->ctx.stats.flags &= ~PROMEX_FL_FRONT_METRIC;
1093 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001094 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001095 appctx->st1 = PROMEX_DUMPER_BACK;
1096 /* fall through */
1097
1098 case PROMEX_DUMPER_BACK:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001099 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_BACK) {
1100 ret = promex_dump_back_metrics(appctx, htx);
1101 if (ret <= 0) {
1102 if (ret == -1)
1103 goto error;
1104 goto full;
1105 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001106 }
1107
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001108 appctx->ctx.stats.obj1 = proxies_list;
1109 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001110 appctx->ctx.stats.flags &= ~PROMEX_FL_BACK_METRIC;
1111 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001112 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001113 appctx->st1 = PROMEX_DUMPER_SRV;
1114 /* fall through */
1115
1116 case PROMEX_DUMPER_SRV:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001117 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_SERVER) {
1118 ret = promex_dump_srv_metrics(appctx, htx);
1119 if (ret <= 0) {
1120 if (ret == -1)
1121 goto error;
1122 goto full;
1123 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001124 }
1125
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001126 appctx->ctx.stats.obj1 = NULL;
1127 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001128 appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001129 appctx->st2 = 0;
1130 appctx->st1 = PROMEX_DUMPER_DONE;
1131 /* fall through */
1132
1133 case PROMEX_DUMPER_DONE:
1134 default:
1135 break;
1136 }
1137
1138 return 1;
1139
1140 full:
1141 si_rx_room_blk(si);
1142 return 0;
1143 error:
1144 /* unrecoverable error */
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001145 appctx->ctx.stats.obj1 = NULL;
1146 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletf959d082019-02-07 15:38:42 +01001147 appctx->ctx.stats.flags = 0;
1148 appctx->st2 = 0;
1149 appctx->st1 = PROMEX_DUMPER_DONE;
1150 return -1;
1151}
1152
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001153/* Parse the query string of request URI to filter the metrics. It returns 1 on
Christopher Faulet78407ce2019-11-18 14:47:08 +01001154 * success and -1 on error. */
1155static int promex_parse_uri(struct appctx *appctx, struct stream_interface *si)
1156{
1157 struct channel *req = si_oc(si);
1158 struct channel *res = si_ic(si);
1159 struct htx *req_htx, *res_htx;
1160 struct htx_sl *sl;
William Dauchyc65f6562019-11-26 12:56:26 +01001161 char *p, *key, *value;
1162 const char *end;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001163 struct buffer *err;
1164 int default_scopes = PROMEX_FL_SCOPE_ALL;
1165 int len;
1166
1167 /* Get the query-string */
1168 req_htx = htxbuf(&req->buf);
1169 sl = http_get_stline(req_htx);
1170 if (!sl)
1171 goto error;
1172 p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?');
1173 if (!p)
1174 goto end;
1175 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001176
William Dauchyc65f6562019-11-26 12:56:26 +01001177 /* copy the query-string */
1178 len = end - p;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001179 chunk_reset(&trash);
1180 memcpy(trash.area, p, len);
1181 trash.area[len] = 0;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001182 p = trash.area;
William Dauchyc65f6562019-11-26 12:56:26 +01001183 end = trash.area + len;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001184
1185 /* Parse the query-string */
William Dauchyc65f6562019-11-26 12:56:26 +01001186 while (p < end && *p && *p != '#') {
1187 value = NULL;
1188
1189 /* decode parameter name */
1190 key = p;
1191 while (p < end && *p != '=' && *p != '&' && *p != '#')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001192 ++p;
William Dauchyc65f6562019-11-26 12:56:26 +01001193 /* found a value */
1194 if (*p == '=') {
1195 *(p++) = 0;
1196 value = p;
1197 }
1198 else if (*p == '&')
1199 *(p++) = 0;
1200 else if (*p == '#')
1201 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001202 len = url_decode(key, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001203 if (len == -1)
1204 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001205
William Dauchyc65f6562019-11-26 12:56:26 +01001206 /* decode value */
1207 if (value) {
1208 while (p < end && *p != '=' && *p != '&' && *p != '#')
1209 ++p;
1210 if (*p == '=')
1211 goto error;
1212 if (*p == '&')
1213 *(p++) = 0;
1214 else if (*p == '#')
1215 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001216 len = url_decode(value, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001217 if (len == -1)
1218 goto error;
1219 }
1220
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001221 if (strcmp(key, "scope") == 0) {
William Dauchyc65f6562019-11-26 12:56:26 +01001222 default_scopes = 0; /* at least a scope defined, unset default scopes */
1223 if (!value)
1224 goto error;
1225 else if (*value == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001226 appctx->ctx.stats.flags &= ~PROMEX_FL_SCOPE_ALL;
William Dauchyc65f6562019-11-26 12:56:26 +01001227 else if (*value == '*')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001228 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_ALL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001229 else if (strcmp(value, "global") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001230 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_GLOBAL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001231 else if (strcmp(value, "server") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001232 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_SERVER;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001233 else if (strcmp(value, "backend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001234 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_BACK;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001235 else if (strcmp(value, "frontend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001236 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_FRONT;
1237 else
1238 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001239 }
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001240 else if (strcmp(key, "no-maint") == 0)
Christopher Fauleteba22942019-11-19 14:18:24 +01001241 appctx->ctx.stats.flags |= PROMEX_FL_NO_MAINT_SRV;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001242 }
1243
1244 end:
1245 appctx->ctx.stats.flags |= default_scopes;
1246 return 1;
1247
1248 error:
1249 err = &http_err_chunks[HTTP_ERR_400];
1250 channel_erase(res);
1251 res->buf.data = b_data(err);
1252 memcpy(res->buf.area, b_head(err), b_data(err));
1253 res_htx = htx_from_buf(&res->buf);
1254 channel_add_input(res, res_htx->data);
1255 appctx->st0 = PROMEX_ST_END;
1256 return -1;
1257}
1258
Christopher Fauletf959d082019-02-07 15:38:42 +01001259/* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is
1260 * full. */
1261static int promex_send_headers(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1262{
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001263 struct channel *chn = si_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +01001264 struct htx_sl *sl;
1265 unsigned int flags;
1266
1267 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);
1268 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
1269 if (!sl)
1270 goto full;
1271 sl->info.res.status = 200;
1272 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001273 !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) ||
1274 !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) ||
1275 !htx_add_endof(htx, HTX_BLK_EOH))
1276 goto full;
1277
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001278 channel_add_input(chn, htx->data);
Christopher Fauletf959d082019-02-07 15:38:42 +01001279 return 1;
1280 full:
1281 htx_reset(htx);
1282 si_rx_room_blk(si);
1283 return 0;
1284}
1285
1286/* The function returns 1 if the initialisation is complete, 0 if
1287 * an errors occurs and -1 if more data are required for initializing
1288 * the applet.
1289 */
1290static int promex_appctx_init(struct appctx *appctx, struct proxy *px, struct stream *strm)
1291{
1292 appctx->st0 = PROMEX_ST_INIT;
1293 return 1;
1294}
1295
1296/* The main I/O handler for the promex applet. */
1297static void promex_appctx_handle_io(struct appctx *appctx)
1298{
1299 struct stream_interface *si = appctx->owner;
1300 struct stream *s = si_strm(si);
1301 struct channel *req = si_oc(si);
1302 struct channel *res = si_ic(si);
1303 struct htx *req_htx, *res_htx;
1304 int ret;
1305
1306 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf959d082019-02-07 15:38:42 +01001307 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1308 goto out;
1309
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001310 /* Check if the input buffer is available. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001311 if (!b_size(&res->buf)) {
1312 si_rx_room_blk(si);
1313 goto out;
1314 }
1315
1316 switch (appctx->st0) {
1317 case PROMEX_ST_INIT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001318 ret = promex_parse_uri(appctx, si);
1319 if (ret <= 0) {
1320 if (ret == -1)
1321 goto error;
1322 goto out;
1323 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001324 appctx->st0 = PROMEX_ST_HEAD;
1325 appctx->st1 = PROMEX_DUMPER_INIT;
1326 /* fall through */
1327
1328 case PROMEX_ST_HEAD:
1329 if (!promex_send_headers(appctx, si, res_htx))
1330 goto out;
1331 appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP);
1332 /* fall through */
1333
1334 case PROMEX_ST_DUMP:
1335 ret = promex_dump_metrics(appctx, si, res_htx);
1336 if (ret <= 0) {
1337 if (ret == -1)
1338 goto error;
1339 goto out;
1340 }
1341 appctx->st0 = PROMEX_ST_DONE;
1342 /* fall through */
1343
1344 case PROMEX_ST_DONE:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001345 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
1346 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001347 appctx->st0 = PROMEX_ST_END;
1348 /* fall through */
Christopher Fauletf959d082019-02-07 15:38:42 +01001349
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001350 case PROMEX_ST_END:
1351 if (!(res->flags & CF_SHUTR)) {
1352 res->flags |= CF_READ_NULL;
1353 si_shutr(si);
1354 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001355 }
1356
Christopher Fauletf959d082019-02-07 15:38:42 +01001357 out:
1358 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001359
1360 /* eat the whole request */
1361 if (co_data(req)) {
1362 req_htx = htx_from_buf(&req->buf);
1363 co_htx_skip(req, req_htx, co_data(req));
1364 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001365 return;
1366
1367 error:
1368 res->flags |= CF_READ_NULL;
1369 si_shutr(si);
1370 si_shutw(si);
1371}
1372
1373struct applet promex_applet = {
1374 .obj_type = OBJ_TYPE_APPLET,
1375 .name = "<PROMEX>", /* used for logging */
1376 .init = promex_appctx_init,
1377 .fct = promex_appctx_handle_io,
1378};
1379
1380static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px,
1381 struct act_rule *rule, char **err)
1382{
1383 /* Prometheus exporter service is only available on "http-request" rulesets */
1384 if (rule->from != ACT_F_HTTP_REQ) {
1385 memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets");
1386 return ACT_RET_PRS_ERR;
1387 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001388
1389 /* Add applet pointer in the rule. */
1390 rule->applet = promex_applet;
1391
1392 return ACT_RET_PRS_OK;
1393}
1394static void promex_register_build_options(void)
1395{
1396 char *ptr = NULL;
1397
1398 memprintf(&ptr, "Built with the Prometheus exporter as a service");
1399 hap_register_build_opts(ptr, 1);
1400}
1401
1402
1403static struct action_kw_list service_actions = { ILH, {
1404 { "prometheus-exporter", service_parse_prometheus_exporter },
1405 { /* END */ }
1406}};
1407
1408INITCALL1(STG_REGISTER, service_keywords_register, &service_actions);
1409INITCALL0(STG_REGISTER, promex_register_build_options);