blob: dbf4c7f396f5a139bb79cc5c30282dc41e8ef2d7 [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>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/compression.h>
Christopher Fauletc55a6262020-07-10 15:39:39 +020022#include <haproxy/dns.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020023#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020024#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020025#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020029#include <haproxy/listener.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020030#include <haproxy/log.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020031#include <haproxy/pipe.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/pool.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 Tarreau209108d2020-06-04 20:30:20 +020036#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020037#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020038#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020039#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020040#include <haproxy/task.h>
William Dauchy5a982a72021-01-08 13:18:06 +010041#include <haproxy/version.h>
Christopher Fauletf959d082019-02-07 15:38:42 +010042
43/* Prometheus exporter applet states (appctx->st0) */
44enum {
45 PROMEX_ST_INIT = 0, /* initialized */
46 PROMEX_ST_HEAD, /* send headers before dump */
47 PROMEX_ST_DUMP, /* dumping stats */
48 PROMEX_ST_DONE, /* finished */
Christopher Faulet9744f7c2019-03-27 15:48:53 +010049 PROMEX_ST_END, /* treatment terminated */
Christopher Fauletf959d082019-02-07 15:38:42 +010050};
51
52/* Prometheus exporter dumper states (appctx->st1) */
53enum {
54 PROMEX_DUMPER_INIT = 0, /* initialized */
55 PROMEX_DUMPER_GLOBAL, /* dump metrics of globals */
56 PROMEX_DUMPER_FRONT, /* dump metrics of frontend proxies */
57 PROMEX_DUMPER_BACK, /* dump metrics of backend proxies */
58 PROMEX_DUMPER_LI, /* dump metrics of listeners */
59 PROMEX_DUMPER_SRV, /* dump metrics of servers */
60 PROMEX_DUMPER_DONE, /* finished */
61};
62
63/* Prometheus exporter flags (appctx->ctx.stats.flags) */
64#define PROMEX_FL_METRIC_HDR 0x00000001
65#define PROMEX_FL_INFO_METRIC 0x00000002
Christopher Fauletb713c4f2021-01-20 15:02:50 +010066#define PROMEX_FL_FRONT_METRIC 0x00000004
67#define PROMEX_FL_BACK_METRIC 0x00000008
68#define PROMEX_FL_SRV_METRIC 0x00000010
69#define PROMEX_FL_SCOPE_GLOBAL 0x00000020
70#define PROMEX_FL_SCOPE_FRONT 0x00000040
71#define PROMEX_FL_SCOPE_BACK 0x00000080
72#define PROMEX_FL_SCOPE_SERVER 0x00000100
73#define PROMEX_FL_NO_MAINT_SRV 0x00000200
Christopher Faulet78407ce2019-11-18 14:47:08 +010074
75#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 +010076
Christopher Faulet0312c0d2021-01-20 15:19:12 +010077/* Promtheus metric type (gauge or counter) */
78enum promex_mt_type {
79 PROMEX_MT_GAUGE = 1,
80 PROMEX_MT_COUNTER = 2,
81};
82
Christopher Fauletf959d082019-02-07 15:38:42 +010083/* The max length for metrics name. It is a hard limit but it should be
Ilya Shipitsince7b00f2020-03-23 22:28:40 +050084 * enough.
Christopher Fauletf959d082019-02-07 15:38:42 +010085 */
86#define PROMEX_MAX_NAME_LEN 128
87
88/* The expected max length for a metric dump, including its header lines. It is
89 * just a soft limit to avoid extra work. We don't try to dump a metric if less
90 * than this size is available in the HTX.
91 */
92#define PROMEX_MAX_METRIC_LENGTH 512
93
Christopher Faulet5a2f9382021-01-28 11:24:17 +010094/* The max number of labels per metric */
95#define PROMEX_MAX_LABELS 8
William Dauchy5a982a72021-01-08 13:18:06 +010096
Christopher Faulet0312c0d2021-01-20 15:19:12 +010097/* Describe a prometheus metric */
98struct promex_metric {
99 const struct ist n; /* The metric name */
100 enum promex_mt_type type; /* The metric type (gauge or counter) */
101 unsigned int flags; /* PROMEX_FL_* flags */
102};
103
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100104/* Describe a prometheus metric label. It is just a key/value pair */
105struct promex_label {
106 struct ist name;
107 struct ist value;
108};
109
Christopher Faulet37286a52021-01-20 15:20:53 +0100110/* Global metrics */
111const struct promex_metric promex_global_metrics[INF_TOTAL_FIELDS] = {
112 //[INF_NAME] ignored
113 //[INF_VERSION], ignored
114 //[INF_RELEASE_DATE] ignored
115 [INF_BUILD_INFO] = { .n = IST("build_info"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
116 [INF_NBTHREAD] = { .n = IST("nbthread"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
117 [INF_NBPROC] = { .n = IST("nbproc"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
118 [INF_PROCESS_NUM] = { .n = IST("relative_process_id"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
119 //[INF_PID] ignored
120 //[INF_UPTIME] ignored
121 [INF_UPTIME_SEC] = { .n = IST("uptime_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
122 [INF_START_TIME_SEC] = { .n = IST("start_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
123 [INF_MEMMAX_BYTES] = { .n = IST("max_memory_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
124 [INF_POOL_ALLOC_BYTES] = { .n = IST("pool_allocated_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
125 [INF_POOL_USED_BYTES] = { .n = IST("pool_used_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
126 [INF_POOL_FAILED] = { .n = IST("pool_failures_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
127 [INF_ULIMIT_N] = { .n = IST("max_fds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
128 [INF_MAXSOCK] = { .n = IST("max_sockets"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
129 [INF_MAXCONN] = { .n = IST("max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
130 [INF_HARD_MAXCONN] = { .n = IST("hard_max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
131 [INF_CURR_CONN] = { .n = IST("current_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
132 [INF_CUM_CONN] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
133 [INF_CUM_REQ] = { .n = IST("requests_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
134 [INF_MAX_SSL_CONNS] = { .n = IST("max_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
135 [INF_CURR_SSL_CONNS] = { .n = IST("current_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
136 [INF_CUM_SSL_CONNS] = { .n = IST("ssl_connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
137 [INF_MAXPIPES] = { .n = IST("max_pipes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
138 [INF_PIPES_USED] = { .n = IST("pipes_used_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
139 [INF_PIPES_FREE] = { .n = IST("pipes_free_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
140 [INF_CONN_RATE] = { .n = IST("current_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
141 [INF_CONN_RATE_LIMIT] = { .n = IST("limit_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
142 [INF_MAX_CONN_RATE] = { .n = IST("max_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
143 [INF_SESS_RATE] = { .n = IST("current_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
144 [INF_SESS_RATE_LIMIT] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
145 [INF_MAX_SESS_RATE] = { .n = IST("max_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
146 [INF_SSL_RATE] = { .n = IST("current_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
147 [INF_SSL_RATE_LIMIT] = { .n = IST("limit_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
148 [INF_MAX_SSL_RATE] = { .n = IST("max_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
149 [INF_SSL_FRONTEND_KEY_RATE] = { .n = IST("current_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
150 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .n = IST("max_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
151 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .n = IST("frontend_ssl_reuse"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
152 [INF_SSL_BACKEND_KEY_RATE] = { .n = IST("current_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
153 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .n = IST("max_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
154 [INF_SSL_CACHE_LOOKUPS] = { .n = IST("ssl_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
155 [INF_SSL_CACHE_MISSES] = { .n = IST("ssl_cache_misses_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
156 [INF_COMPRESS_BPS_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
157 [INF_COMPRESS_BPS_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
158 [INF_COMPRESS_BPS_RATE_LIM] = { .n = IST("limit_http_comp"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
159 [INF_ZLIB_MEM_USAGE] = { .n = IST("current_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
160 [INF_MAX_ZLIB_MEM_USAGE] = { .n = IST("max_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
161 [INF_TASKS] = { .n = IST("current_tasks"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
162 [INF_RUN_QUEUE] = { .n = IST("current_run_queue"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
163 [INF_IDLE_PCT] = { .n = IST("idle_time_percent"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
164 //[INF_NODE] ignored
165 //[INF_DESCRIPTION] ignored
166 [INF_STOPPING] = { .n = IST("stopping"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
167 [INF_JOBS] = { .n = IST("jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
168 [INF_UNSTOPPABLE_JOBS] = { .n = IST("unstoppable_jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
169 [INF_LISTENERS] = { .n = IST("listeners"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
170 [INF_ACTIVE_PEERS] = { .n = IST("active_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
171 [INF_CONNECTED_PEERS] = { .n = IST("connected_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
172 [INF_DROPPED_LOGS] = { .n = IST("dropped_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
173 [INF_BUSY_POLLING] = { .n = IST("busy_polling_enabled"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
174 [INF_FAILED_RESOLUTIONS] = { .n = IST("failed_resolutions"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
175 [INF_TOTAL_BYTES_OUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
176 [INF_TOTAL_SPLICED_BYTES_OUT] = { .n = IST("spliced_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
177 [INF_BYTES_OUT_RATE] = { .n = IST("bytes_out_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
178 //[INF_DEBUG_COMMANDS_ISSUED] ignored
Christopher Fauletf959d082019-02-07 15:38:42 +0100179};
180
Christopher Faulet37286a52021-01-20 15:20:53 +0100181/* frontend/backend/server fields */
182const struct promex_metric promex_st_metrics[ST_F_TOTAL_FIELDS] = {
183 //[ST_F_PXNAME] ignored
184 //[ST_F_SVNAME] ignored
185 [ST_F_QCUR] = { .n = IST("current_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
186 [ST_F_QMAX] = { .n = IST("max_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
187 [ST_F_SCUR] = { .n = IST("current_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
188 [ST_F_SMAX] = { .n = IST("max_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
189 [ST_F_SLIM] = { .n = IST("limit_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
190 [ST_F_STOT] = { .n = IST("sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
191 [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) },
192 [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) },
193 [ST_F_DREQ] = { .n = IST("requests_denied_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
194 [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) },
195 [ST_F_EREQ] = { .n = IST("request_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
196 [ST_F_ECON] = { .n = IST("connection_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
197 [ST_F_ERESP] = { .n = IST("response_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
198 [ST_F_WRETR] = { .n = IST("retry_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
199 [ST_F_WREDIS] = { .n = IST("redispatch_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
200 [ST_F_STATUS] = { .n = IST("status"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
201 [ST_F_WEIGHT] = { .n = IST("weight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
202 [ST_F_ACT] = { .n = IST("active_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
203 [ST_F_BCK] = { .n = IST("backup_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
204 [ST_F_CHKFAIL] = { .n = IST("check_failures_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_SRV_METRIC) },
205 [ST_F_CHKDOWN] = { .n = IST("check_up_down_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
206 [ST_F_LASTCHG] = { .n = IST("check_last_change_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
207 [ST_F_DOWNTIME] = { .n = IST("downtime_seconds_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
208 [ST_F_QLIMIT] = { .n = IST("queue_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
209 //[ST_F_PID] ignored
210 //[ST_F_IID] ignored
211 //[ST_F_SID] ignored
212 [ST_F_THROTTLE] = { .n = IST("current_throttle"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
213 [ST_F_LBTOT] = { .n = IST("loadbalanced_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
214 //[ST_F_TRACKED] ignored
215 //[ST_F_TYPE] ignored
216 //[ST_F_RATE] ignored
217 [ST_F_RATE_LIM] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
218 [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) },
219 [ST_F_CHECK_STATUS] = { .n = IST("check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
220 [ST_F_CHECK_CODE] = { .n = IST("check_code"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
221 [ST_F_CHECK_DURATION] = { .n = IST("check_duration_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
222 [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) },
223 [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) },
224 [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) },
225 [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) },
226 [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) },
227 [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) },
228 //[ST_F_HANAFAIL] ignored
229 //[ST_F_REQ_RATE] ignored
230 [ST_F_REQ_RATE_MAX] = { .n = IST("http_requests_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
231 [ST_F_REQ_TOT] = { .n = IST("http_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
232 [ST_F_CLI_ABRT] = { .n = IST("client_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
233 [ST_F_SRV_ABRT] = { .n = IST("server_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
234 [ST_F_COMP_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
235 [ST_F_COMP_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
236 [ST_F_COMP_BYP] = { .n = IST("http_comp_bytes_bypassed_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
237 [ST_F_COMP_RSP] = { .n = IST("http_comp_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
238 [ST_F_LASTSESS] = { .n = IST("last_session_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
239 //[ST_F_LAST_CHK] ignroed
240 //[ST_F_LAST_AGT] ignored
241 [ST_F_QTIME] = { .n = IST("queue_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
242 [ST_F_CTIME] = { .n = IST("connect_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
243 [ST_F_RTIME] = { .n = IST("response_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
244 [ST_F_TTIME] = { .n = IST("total_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
245 //[ST_F_AGENT_STATUS] ignored
246 //[ST_F_AGENT_CODE] ignored
247 //[ST_F_AGENT_DURATION] ignored
248 //[ST_F_CHECK_DESC] ignored
249 //[ST_F_AGENT_DESC] ignored
250 //[ST_F_CHECK_RISE] ignored
251 //[ST_F_CHECK_FALL] ignored
252 //[ST_F_CHECK_HEALTH] ignored
253 //[ST_F_AGENT_RISE] ignored
254 //[ST_F_AGENT_FALL] ignored
255 //[ST_F_AGENT_HEALTH] ignored
256 //[ST_F_ADDR] ignored
257 //[ST_F_COOKIE] ignored
258 //[ST_F_MODE] ignored
259 //[ST_F_ALGO] ignored
260 //[ST_F_CONN_RATE] ignored
261 [ST_F_CONN_RATE_MAX] = { .n = IST("connections_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
262 [ST_F_CONN_TOT] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
263 [ST_F_INTERCEPTED] = { .n = IST("intercepted_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
264 [ST_F_DCON] = { .n = IST("denied_connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
265 [ST_F_DSES] = { .n = IST("denied_sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
266 [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) },
267 [ST_F_CONNECT] = { .n = IST("connection_attempts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
268 [ST_F_REUSE] = { .n = IST("connection_reuses_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
269 [ST_F_CACHE_LOOKUPS] = { .n = IST("http_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
270 [ST_F_CACHE_HITS] = { .n = IST("http_cache_hits_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
271 [ST_F_SRV_ICUR] = { .n = IST("idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
272 [ST_F_SRV_ILIM] = { .n = IST("idle_connections_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
273 [ST_F_QT_MAX] = { .n = IST("max_queue_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
274 [ST_F_CT_MAX] = { .n = IST("max_connect_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
275 [ST_F_RT_MAX] = { .n = IST("max_response_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
276 [ST_F_TT_MAX] = { .n = IST("max_total_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
277 [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) },
278 [ST_F_IDLE_CONN_CUR] = { .n = IST("unsafe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
279 [ST_F_SAFE_CONN_CUR]= { .n = IST("safe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
280 [ST_F_USED_CONN_CUR] = { .n = IST("used_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
281 [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 +0100282};
283
Christopher Fauletf959d082019-02-07 15:38:42 +0100284/* Description of all stats fields */
285const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
286 [ST_F_PXNAME] = IST("The proxy name."),
287 [ST_F_SVNAME] = IST("The service name (FRONTEND for frontend, BACKEND for backend, any name for server/listener)."),
288 [ST_F_QCUR] = IST("Current number of queued requests."),
289 [ST_F_QMAX] = IST("Maximum observed number of queued requests."),
290 [ST_F_SCUR] = IST("Current number of active sessions."),
291 [ST_F_SMAX] = IST("Maximum observed number of active sessions."),
292 [ST_F_SLIM] = IST("Configured session limit."),
293 [ST_F_STOT] = IST("Total number of sessions."),
294 [ST_F_BIN] = IST("Current total of incoming bytes."),
295 [ST_F_BOUT] = IST("Current total of outgoing bytes."),
296 [ST_F_DREQ] = IST("Total number of denied requests."),
297 [ST_F_DRESP] = IST("Total number of denied responses."),
298 [ST_F_EREQ] = IST("Total number of request errors."),
299 [ST_F_ECON] = IST("Total number of connection errors."),
300 [ST_F_ERESP] = IST("Total number of response errors."),
301 [ST_F_WRETR] = IST("Total number of retry warnings."),
302 [ST_F_WREDIS] = IST("Total number of redispatch warnings."),
William Dauchyc6464592021-01-27 22:40:17 +0100303 [ST_F_STATUS] = IST("Current status of the service."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100304 [ST_F_WEIGHT] = IST("Service weight."),
305 [ST_F_ACT] = IST("Current number of active servers."),
306 [ST_F_BCK] = IST("Current number of backup servers."),
307 [ST_F_CHKFAIL] = IST("Total number of failed check (Only counts checks failed when the server is up)."),
308 [ST_F_CHKDOWN] = IST("Total number of UP->DOWN transitions."),
309 [ST_F_LASTCHG] = IST("Number of seconds since the last UP<->DOWN transition."),
310 [ST_F_DOWNTIME] = IST("Total downtime (in seconds) for the service."),
311 [ST_F_QLIMIT] = IST("Configured maxqueue for the server (0 meaning no limit)."),
312 [ST_F_PID] = IST("Process id (0 for first instance, 1 for second, ...)"),
313 [ST_F_IID] = IST("Unique proxy id."),
314 [ST_F_SID] = IST("Server id (unique inside a proxy)."),
315 [ST_F_THROTTLE] = IST("Current throttle percentage for the server, when slowstart is active, or no value if not in slowstart."),
316 [ST_F_LBTOT] = IST("Total number of times a service was selected, either for new sessions, or when redispatching."),
317 [ST_F_TRACKED] = IST("Id of proxy/server if tracking is enabled."),
318 [ST_F_TYPE] = IST("Service type (0=frontend, 1=backend, 2=server, 3=socket/listener)."),
319 [ST_F_RATE] = IST("Current number of sessions per second over last elapsed second."),
320 [ST_F_RATE_LIM] = IST("Configured limit on new sessions per second."),
321 [ST_F_RATE_MAX] = IST("Maximum observed number of sessions per second."),
Christopher Fauletcf403f32019-11-21 14:35:46 +0100322 [ST_F_CHECK_STATUS] = IST("Status of last health check (HCHK_STATUS_* values)."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100323 [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."),
Christopher Faulet2711e512020-02-27 16:12:07 +0100324 [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100325 [ST_F_HRSP_1XX] = IST("Total number of HTTP responses."),
326 [ST_F_HRSP_2XX] = IST("Total number of HTTP responses."),
327 [ST_F_HRSP_3XX] = IST("Total number of HTTP responses."),
328 [ST_F_HRSP_4XX] = IST("Total number of HTTP responses."),
329 [ST_F_HRSP_5XX] = IST("Total number of HTTP responses."),
330 [ST_F_HRSP_OTHER] = IST("Total number of HTTP responses."),
331 [ST_F_HANAFAIL] = IST("Total number of failed health checks."),
332 [ST_F_REQ_RATE] = IST("Current number of HTTP requests per second over last elapsed second."),
333 [ST_F_REQ_RATE_MAX] = IST("Maximum observed number of HTTP requests per second."),
334 [ST_F_REQ_TOT] = IST("Total number of HTTP requests received."),
335 [ST_F_CLI_ABRT] = IST("Total number of data transfers aborted by the client."),
336 [ST_F_SRV_ABRT] = IST("Total number of data transfers aborted by the server."),
337 [ST_F_COMP_IN] = IST("Total number of HTTP response bytes fed to the compressor."),
338 [ST_F_COMP_OUT] = IST("Total number of HTTP response bytes emitted by the compressor."),
339 [ST_F_COMP_BYP] = IST("Total number of bytes that bypassed the HTTP compressor (CPU/BW limit)."),
340 [ST_F_COMP_RSP] = IST("Total number of HTTP responses that were compressed."),
341 [ST_F_LASTSESS] = IST("Number of seconds since last session assigned to server/backend."),
342 [ST_F_LAST_CHK] = IST("Last health check contents or textual error"),
343 [ST_F_LAST_AGT] = IST("Last agent check contents or textual error"),
344 [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."),
345 [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."),
346 [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."),
347 [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."),
348 [ST_F_AGENT_STATUS] = IST("Status of last agent check."),
349 [ST_F_AGENT_CODE] = IST("Numeric code reported by agent if any (unused for now)."),
350 [ST_F_AGENT_DURATION] = IST("Time in ms taken to finish last agent check."),
351 [ST_F_CHECK_DESC] = IST("Short human-readable description of the last health status."),
352 [ST_F_AGENT_DESC] = IST("Short human-readable description of the last agent status."),
353 [ST_F_CHECK_RISE] = IST("Server's \"rise\" parameter used by health checks"),
354 [ST_F_CHECK_FALL] = IST("Server's \"fall\" parameter used by health checks"),
355 [ST_F_CHECK_HEALTH] = IST("Server's health check value between 0 and rise+fall-1"),
356 [ST_F_AGENT_RISE] = IST("Agent's \"rise\" parameter, normally 1."),
357 [ST_F_AGENT_FALL] = IST("Agent's \"fall\" parameter, normally 1."),
358 [ST_F_AGENT_HEALTH] = IST("Agent's health parameter, between 0 and rise+fall-1"),
359 [ST_F_ADDR] = IST("address:port or \"unix\". IPv6 has brackets around the address."),
360 [ST_F_COOKIE] = IST("Server's cookie value or backend's cookie name."),
361 [ST_F_MODE] = IST("Proxy mode (tcp, http, health, unknown)."),
362 [ST_F_ALGO] = IST("Load balancing algorithm."),
363 [ST_F_CONN_RATE] = IST("Current number of connections per second over the last elapsed second."),
364 [ST_F_CONN_RATE_MAX] = IST("Maximum observed number of connections per second."),
365 [ST_F_CONN_TOT] = IST("Total number of connections."),
366 [ST_F_INTERCEPTED] = IST("Total number of intercepted HTTP requests."),
367 [ST_F_DCON] = IST("Total number of requests denied by \"tcp-request connection\" rules."),
368 [ST_F_DSES] = IST("Total number of requests denied by \"tcp-request session\" rules."),
369 [ST_F_WREW] = IST("Total number of failed header rewriting warnings."),
370 [ST_F_CONNECT] = IST("Total number of connection establishment attempts."),
371 [ST_F_REUSE] = IST("Total number of connection reuses."),
372 [ST_F_CACHE_LOOKUPS] = IST("Total number of HTTP cache lookups."),
373 [ST_F_CACHE_HITS] = IST("Total number of HTTP cache hits."),
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100374 [ST_F_SRV_ICUR] = IST("Current number of idle connections available for reuse"),
375 [ST_F_SRV_ILIM] = IST("Limit on the number of available idle connections"),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100376 [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"),
377 [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"),
378 [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"),
379 [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100380 [ST_F_EINT] = IST("Total number of internal errors."),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200381 [ST_F_IDLE_CONN_CUR] = IST("Current number of unsafe idle connections."),
382 [ST_F_SAFE_CONN_CUR] = IST("Current number of safe idle connections."),
383 [ST_F_USED_CONN_CUR] = IST("Current number of connections in use."),
384 [ST_F_NEED_CONN_EST] = IST("Estimated needed number of connections."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100385};
386
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100387/* Specific labels for all ST_F_HRSP_* fields */
388const struct ist promex_hrsp_code[1 + ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = {
389 [ST_F_HRSP_1XX - ST_F_HRSP_1XX] = IST("1xx"),
390 [ST_F_HRSP_2XX - ST_F_HRSP_1XX] = IST("2xx"),
391 [ST_F_HRSP_3XX - ST_F_HRSP_1XX] = IST("3xx"),
392 [ST_F_HRSP_4XX - ST_F_HRSP_1XX] = IST("4xx"),
393 [ST_F_HRSP_5XX - ST_F_HRSP_1XX] = IST("5xx"),
394 [ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = IST("other"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100395};
396
William Dauchy54938212021-01-27 22:40:16 +0100397enum promex_front_state {
398 PROMEX_FRONT_STATE_DOWN = 0,
399 PROMEX_FRONT_STATE_UP,
400
401 PROMEX_FRONT_STATE_COUNT /* must be last */
402};
403
404const struct ist promex_front_st[PROMEX_FRONT_STATE_COUNT] = {
405 [PROMEX_FRONT_STATE_DOWN] = IST("DOWN"),
406 [PROMEX_FRONT_STATE_UP] = IST("UP"),
407};
408
409enum promex_back_state {
410 PROMEX_BACK_STATE_DOWN = 0,
411 PROMEX_BACK_STATE_UP,
412
413 PROMEX_BACK_STATE_COUNT /* must be last */
414};
415
416const struct ist promex_back_st[PROMEX_BACK_STATE_COUNT] = {
417 [PROMEX_BACK_STATE_DOWN] = IST("DOWN"),
418 [PROMEX_BACK_STATE_UP] = IST("UP"),
419};
420
421enum promex_srv_state {
422 PROMEX_SRV_STATE_DOWN = 0,
423 PROMEX_SRV_STATE_UP,
424 PROMEX_SRV_STATE_MAINT,
425 PROMEX_SRV_STATE_DRAIN,
426 PROMEX_SRV_STATE_NOLB,
427
428 PROMEX_SRV_STATE_COUNT /* must be last */
429};
430
431const struct ist promex_srv_st[PROMEX_SRV_STATE_COUNT] = {
432 [PROMEX_SRV_STATE_DOWN] = IST("DOWN"),
433 [PROMEX_SRV_STATE_UP] = IST("UP"),
434 [PROMEX_SRV_STATE_MAINT] = IST("MAINT"),
435 [PROMEX_SRV_STATE_DRAIN] = IST("DRAIN"),
436 [PROMEX_SRV_STATE_NOLB] = IST("NOLB"),
437};
438
439/* Return the server status. */
440enum promex_srv_state promex_srv_status(struct server *sv)
Christopher Fauletf959d082019-02-07 15:38:42 +0100441{
William Dauchy54938212021-01-27 22:40:16 +0100442 int state = PROMEX_SRV_STATE_DOWN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100443
Christopher Fauletf959d082019-02-07 15:38:42 +0100444 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Dauchy54938212021-01-27 22:40:16 +0100445 state = PROMEX_SRV_STATE_UP;
Christopher Fauletf959d082019-02-07 15:38:42 +0100446 if (sv->cur_admin & SRV_ADMF_DRAIN)
William Dauchy54938212021-01-27 22:40:16 +0100447 state = PROMEX_SRV_STATE_DRAIN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100448 }
Christopher Fauletd45d1052019-09-06 16:10:19 +0200449 else if (sv->cur_state == SRV_ST_STOPPING)
William Dauchy54938212021-01-27 22:40:16 +0100450 state = PROMEX_SRV_STATE_NOLB;
Christopher Fauletd45d1052019-09-06 16:10:19 +0200451
452 if (sv->cur_admin & SRV_ADMF_MAINT)
William Dauchy54938212021-01-27 22:40:16 +0100453 state = PROMEX_SRV_STATE_MAINT;
Christopher Fauletf959d082019-02-07 15:38:42 +0100454
455 return state;
456}
457
458/* Convert a field to its string representation and write it in <out>, followed
459 * by a newline, if there is enough space. non-numeric value are converted in
William Dauchy18a2c6e2021-01-22 21:09:47 +0100460 * "NaN" because Prometheus only support numerical values (but it is unexepceted
Christopher Fauletf959d082019-02-07 15:38:42 +0100461 * to process this kind of value). It returns 1 on success. Otherwise, it
462 * returns 0. The buffer's length must not exceed <max> value.
463 */
464static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
465{
466 int ret = 0;
467
468 switch (field_format(f, 0)) {
William Dauchy18a2c6e2021-01-22 21:09:47 +0100469 case FF_EMPTY: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100470 case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break;
471 case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break;
472 case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break;
473 case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200474 case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break;
William Dauchy18a2c6e2021-01-22 21:09:47 +0100475 case FF_STR: ret = chunk_strcat(out, "NaN\n"); break;
476 default: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100477 }
478 if (!ret || out->data > max)
479 return 0;
480 return 1;
481}
482
Christopher Fauletf959d082019-02-07 15:38:42 +0100483/* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
484 * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
485 */
486static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
Christopher Faulet37286a52021-01-20 15:20:53 +0100487 const struct promex_metric *metric, const struct ist name,
488 struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100489{
Christopher Faulet37286a52021-01-20 15:20:53 +0100490 struct ist type;
491
492 switch (metric->type) {
493 case PROMEX_MT_COUNTER:
494 type = ist("counter");
495 break;
496 default:
497 type = ist("gauge");
498 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100499
William Dauchya191b772021-01-15 22:41:39 +0100500 if (istcat(out, ist("# HELP "), max) == -1 ||
501 istcat(out, name, max) == -1 ||
502 istcat(out, ist(" "), max) == -1)
503 goto full;
504
Christopher Faulet37286a52021-01-20 15:20:53 +0100505 if (metric->flags & PROMEX_FL_INFO_METRIC) {
William Dauchya191b772021-01-15 22:41:39 +0100506 if (istcat(out, ist(info_fields[appctx->st2].desc), max) == -1)
507 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100508 }
509 else {
Christopher Faulet37286a52021-01-20 15:20:53 +0100510 if (istcat(out, promex_st_metric_desc[appctx->st2], max) == -1)
William Dauchya191b772021-01-15 22:41:39 +0100511 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100512 }
513
William Dauchya191b772021-01-15 22:41:39 +0100514 if (istcat(out, ist("\n# TYPE "), max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100515 istcat(out, name, max) == -1 ||
516 istcat(out, ist(" "), max) == -1 ||
Christopher Faulet37286a52021-01-20 15:20:53 +0100517 istcat(out, type, max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100518 istcat(out, ist("\n"), max) == -1)
519 goto full;
520
521 return 1;
522
523 full:
524 return 0;
525}
526
527/* Dump the line for <metric>. It starts by the metric name followed by its
528 * labels (proxy name, server name...) between braces and finally its value. If
529 * not already done, the header lines are dumped first. It returns 1 on
530 * success. Otherwise if <out> length exceeds <max>, it returns 0.
531 */
Christopher Faulet37286a52021-01-20 15:20:53 +0100532static int promex_dump_metric(struct appctx *appctx, struct htx *htx, struct ist prefix,
William Dauchyc6464592021-01-27 22:40:17 +0100533 const struct promex_metric *metric, struct field *val,
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100534 struct promex_label *labels, struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100535{
536 struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
537 size_t len = out->len;
538
539 if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
540 return 0;
541
Christopher Faulet37286a52021-01-20 15:20:53 +0100542 /* Fill the metric name */
543 istcat(&name, prefix, PROMEX_MAX_NAME_LEN);
544 istcat(&name, metric->n, PROMEX_MAX_NAME_LEN);
545
546
Christopher Fauletf959d082019-02-07 15:38:42 +0100547 if ((appctx->ctx.stats.flags & PROMEX_FL_METRIC_HDR) &&
Christopher Faulet37286a52021-01-20 15:20:53 +0100548 !promex_dump_metric_header(appctx, htx, metric, name, out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100549 goto full;
550
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100551 if (istcat(out, name, max) == -1)
552 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100553
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100554 if (isttest(labels[0].name)) {
555 int i;
556
557 if (istcat(out, ist("{"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100558 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100559
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100560 for (i = 0; isttest(labels[i].name); i++) {
561 if (!isttest(labels[i].value))
562 continue;
563
564 if ((i && istcat(out, ist(","), max) == -1) ||
565 istcat(out, labels[i].name, max) == -1 ||
566 istcat(out, ist("=\""), max) == -1 ||
567 istcat(out, labels[i].value, max) == -1 ||
568 istcat(out, ist("\""), max) == -1)
569 goto full;
570 }
571
572 if (istcat(out, ist("}"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100573 goto full;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100574
Christopher Fauletf959d082019-02-07 15:38:42 +0100575 }
576
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100577 if (istcat(out, ist(" "), max) == -1)
578 goto full;
579
Christopher Fauletf959d082019-02-07 15:38:42 +0100580 trash.data = out->len;
Christopher Faulet37286a52021-01-20 15:20:53 +0100581 if (!promex_metric_to_str(&trash, val, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100582 goto full;
583 out->len = trash.data;
584
585 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
586 return 1;
587 full:
588 // Restore previous length
589 out->len = len;
590 return 0;
591
592}
593
594
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500595/* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100596 * 0 if <htx> is full and -1 in case of any error. */
597static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
598{
599 static struct ist prefix = IST("haproxy_process_");
Christopher Faulet37286a52021-01-20 15:20:53 +0100600 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100601 struct channel *chn = si_ic(appctx->owner);
602 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200603 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100604 int ret = 1;
605
William Dauchy5d9b8f32021-01-11 20:07:49 +0100606 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
607 return -1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100608
Christopher Faulet37286a52021-01-20 15:20:53 +0100609 for (; appctx->st2 < INF_TOTAL_FIELDS; appctx->st2++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100610 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
611
Christopher Faulet37286a52021-01-20 15:20:53 +0100612 if (!(promex_global_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
613 continue;
614
Christopher Fauletf959d082019-02-07 15:38:42 +0100615 switch (appctx->st2) {
William Dauchy5a982a72021-01-08 13:18:06 +0100616 case INF_BUILD_INFO:
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100617 labels[0].name = ist("version");
618 labels[0].value = ist(HAPROXY_VERSION);
Christopher Faulet37286a52021-01-20 15:20:53 +0100619 val = mkf_u32(FN_GAUGE, 1);
William Dauchy5a982a72021-01-08 13:18:06 +0100620 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100621
622 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100623 val = info[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100624 }
625
William Dauchyc6464592021-01-27 22:40:17 +0100626 if (!promex_dump_metric(appctx, htx, prefix, &promex_global_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100627 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100628 goto full;
629
Christopher Fauletf959d082019-02-07 15:38:42 +0100630 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100631 }
632
633 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200634 if (out.len) {
635 if (!htx_add_data_atonce(htx, out))
636 return -1; /* Unexpected and unrecoverable error */
637 channel_add_input(chn, out.len);
638 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100639 return ret;
640 full:
641 ret = 0;
642 goto end;
643}
644
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500645/* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100646 * 0 if <htx> is full and -1 in case of any error. */
647static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
648{
649 static struct ist prefix = IST("haproxy_frontend_");
650 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100651 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100652 struct channel *chn = si_ic(appctx->owner);
653 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200654 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchyb9577452021-01-17 18:27:46 +0100655 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100656 int ret = 1;
William Dauchyc6464592021-01-27 22:40:17 +0100657 enum promex_front_state state;
William Dauchyc6464592021-01-27 22:40:17 +0100658 int i;
Christopher Fauletf959d082019-02-07 15:38:42 +0100659
Christopher Faulet37286a52021-01-20 15:20:53 +0100660 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
661 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
662 continue;
663
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200664 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100665 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
666
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200667 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100668
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100669 labels[0].name = ist("proxy");
670 labels[0].value = ist2(px->id, strlen(px->id));
671
Christopher Fauletf959d082019-02-07 15:38:42 +0100672 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200673 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100674 goto next_px;
675
William Dauchyb9577452021-01-17 18:27:46 +0100676 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
677 return -1;
678
Christopher Fauletf959d082019-02-07 15:38:42 +0100679 switch (appctx->st2) {
680 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100681 state = !px->disabled;
682 for (i = 0; i < PROMEX_FRONT_STATE_COUNT; i++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100683 labels[1].name = ist("state");
684 labels[1].value = promex_front_st[i];
William Dauchyc6464592021-01-27 22:40:17 +0100685 val = mkf_u32(FO_STATUS, state == i);
William Dauchyc6464592021-01-27 22:40:17 +0100686 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100687 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100688 goto full;
689 }
690 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100691 case ST_F_REQ_RATE_MAX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100692 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100693 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100694 case ST_F_INTERCEPTED:
Christopher Fauletf959d082019-02-07 15:38:42 +0100695 case ST_F_CACHE_LOOKUPS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100696 case ST_F_CACHE_HITS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100697 case ST_F_COMP_IN:
Christopher Fauletf959d082019-02-07 15:38:42 +0100698 case ST_F_COMP_OUT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100699 case ST_F_COMP_BYP:
William Dauchyb9577452021-01-17 18:27:46 +0100700 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100701 if (px->mode != PR_MODE_HTTP)
702 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100703 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100704 break;
William Dauchyb9577452021-01-17 18:27:46 +0100705 case ST_F_HRSP_2XX:
706 case ST_F_HRSP_3XX:
707 case ST_F_HRSP_4XX:
708 case ST_F_HRSP_5XX:
709 case ST_F_HRSP_OTHER:
Christopher Fauletf959d082019-02-07 15:38:42 +0100710 if (px->mode != PR_MODE_HTTP)
711 goto next_px;
William Dauchyb9577452021-01-17 18:27:46 +0100712 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100713 labels[1].name = ist("code");
714 labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
715 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100716 break;
717
718 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100719 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100720 }
721
William Dauchyc6464592021-01-27 22:40:17 +0100722 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100723 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100724 goto full;
725 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200726 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100727 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100728 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200729 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100730 }
731
732 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200733 if (out.len) {
734 if (!htx_add_data_atonce(htx, out))
735 return -1; /* Unexpected and unrecoverable error */
736 channel_add_input(chn, out.len);
737 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100738 return ret;
739 full:
740 ret = 0;
741 goto end;
742}
743
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500744/* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100745 * 0 if <htx> is full and -1 in case of any error. */
746static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
747{
748 static struct ist prefix = IST("haproxy_backend_");
749 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100750 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100751 struct channel *chn = si_ic(appctx->owner);
752 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200753 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchy3c6f0062021-01-25 17:29:02 +0100754 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100755 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200756 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100757 enum promex_back_state state;
William Dauchyc6464592021-01-27 22:40:17 +0100758 int i;
Christopher Fauletf959d082019-02-07 15:38:42 +0100759
Christopher Faulet37286a52021-01-20 15:20:53 +0100760 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
761 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
762 continue;
763
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200764 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100765 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
766
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200767 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100768
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100769 labels[0].name = ist("proxy");
770 labels[0].value = ist2(px->id, strlen(px->id));
771
Christopher Fauletf959d082019-02-07 15:38:42 +0100772 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200773 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100774 goto next_px;
775
William Dauchy3c6f0062021-01-25 17:29:02 +0100776 if (!stats_fill_be_stats(px, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
777 return -1;
778
Christopher Fauletf959d082019-02-07 15:38:42 +0100779 switch (appctx->st2) {
780 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100781 state = ((px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0);
782 for (i = 0; i < PROMEX_BACK_STATE_COUNT; i++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100783 labels[1].name = ist("state");
784 labels[1].value = promex_back_st[i];
William Dauchyc6464592021-01-27 22:40:17 +0100785 val = mkf_u32(FO_STATUS, state == i);
William Dauchyc6464592021-01-27 22:40:17 +0100786 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100787 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100788 goto full;
789 }
790 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100791 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200792 secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100793 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100794 break;
795 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200796 secs = (double)swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100797 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100798 break;
799 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200800 secs = (double)swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100801 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100802 break;
803 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200804 secs = (double)swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100805 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100806 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100807 case ST_F_QT_MAX:
808 secs = (double)px->be_counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100809 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100810 break;
811 case ST_F_CT_MAX:
812 secs = (double)px->be_counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100813 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100814 break;
815 case ST_F_RT_MAX:
816 secs = (double)px->be_counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100817 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100818 break;
819 case ST_F_TT_MAX:
820 secs = (double)px->be_counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100821 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100822 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100823 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100824 case ST_F_HRSP_1XX:
William Dauchy3c6f0062021-01-25 17:29:02 +0100825 case ST_F_CACHE_LOOKUPS:
826 case ST_F_CACHE_HITS:
827 case ST_F_COMP_IN:
828 case ST_F_COMP_OUT:
829 case ST_F_COMP_BYP:
830 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100831 if (px->mode != PR_MODE_HTTP)
832 goto next_px;
William Dauchy3c6f0062021-01-25 17:29:02 +0100833 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100834 break;
835 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100836 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100837 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100838 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100839 case ST_F_HRSP_OTHER:
840 if (px->mode != PR_MODE_HTTP)
841 goto next_px;
842 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100843 labels[1].name = ist("code");
844 labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
William Dauchy3c6f0062021-01-25 17:29:02 +0100845 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100846 break;
847
848 default:
William Dauchy3c6f0062021-01-25 17:29:02 +0100849 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100850 }
851
William Dauchy3c6f0062021-01-25 17:29:02 +0100852 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100853 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100854 goto full;
855 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200856 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100857 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100858 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200859 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100860 }
861
862 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200863 if (out.len) {
864 if (!htx_add_data_atonce(htx, out))
865 return -1; /* Unexpected and unrecoverable error */
866 channel_add_input(chn, out.len);
867 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100868 return ret;
869 full:
870 ret = 0;
871 goto end;
872}
873
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500874/* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100875 * 0 if <htx> is full and -1 in case of any error. */
876static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
877{
878 static struct ist prefix = IST("haproxy_server_");
879 struct proxy *px;
880 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100881 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100882 struct channel *chn = si_ic(appctx->owner);
883 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200884 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchybde2bf62021-01-25 17:29:04 +0100885 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100886 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200887 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100888 enum promex_srv_state state;
William Dauchyc6464592021-01-27 22:40:17 +0100889 int i;
Christopher Fauletf959d082019-02-07 15:38:42 +0100890
Christopher Faulet37286a52021-01-20 15:20:53 +0100891 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
892 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
893 continue;
894
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200895 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100896 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
897
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200898 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100899
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100900 labels[0].name = ist("proxy");
901 labels[0].value = ist2(px->id, strlen(px->id));
902
Christopher Fauletf959d082019-02-07 15:38:42 +0100903 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200904 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100905 goto next_px;
906
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200907 while (appctx->ctx.stats.obj2) {
908 sv = appctx->ctx.stats.obj2;
Christopher Fauletf959d082019-02-07 15:38:42 +0100909
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100910 labels[1].name = ist("server");
911 labels[1].value = ist2(sv->id, strlen(sv->id));
912
William Dauchybde2bf62021-01-25 17:29:04 +0100913 if (!stats_fill_sv_stats(px, sv, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
914 return -1;
915
Christopher Fauleteba22942019-11-19 14:18:24 +0100916 if ((appctx->ctx.stats.flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
917 goto next_sv;
918
Christopher Fauletf959d082019-02-07 15:38:42 +0100919 switch (appctx->st2) {
920 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100921 state = promex_srv_status(sv);
922 for (i = 0; i < PROMEX_SRV_STATE_COUNT; i++) {
923 val = mkf_u32(FO_STATUS, state == i);
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100924 labels[2].name = ist("state");
925 labels[2].value = promex_srv_st[i];
William Dauchyc6464592021-01-27 22:40:17 +0100926 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100927 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100928 goto full;
929 }
930 goto next_sv;
Christopher Fauletf959d082019-02-07 15:38:42 +0100931 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200932 secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100933 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100934 break;
935 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200936 secs = (double)swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100937 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100938 break;
939 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200940 secs = (double)swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100941 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100942 break;
943 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200944 secs = (double)swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100945 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100946 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100947 case ST_F_QT_MAX:
948 secs = (double)sv->counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100949 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100950 break;
951 case ST_F_CT_MAX:
952 secs = (double)sv->counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100953 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100954 break;
955 case ST_F_RT_MAX:
956 secs = (double)sv->counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100957 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100958 break;
959 case ST_F_TT_MAX:
960 secs = (double)sv->counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100961 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100962 break;
Christopher Fauletcf403f32019-11-21 14:35:46 +0100963 case ST_F_CHECK_STATUS:
964 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
965 goto next_sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100966 val = mkf_u32(FN_OUTPUT, sv->check.status);
Christopher Fauletcf403f32019-11-21 14:35:46 +0100967 break;
968 case ST_F_CHECK_CODE:
969 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
970 goto next_sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100971 val = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code);
Christopher Fauletcf403f32019-11-21 14:35:46 +0100972 break;
Christopher Faulet2711e512020-02-27 16:12:07 +0100973 case ST_F_CHECK_DURATION:
974 if (sv->check.status < HCHK_STATUS_CHECKED)
975 goto next_sv;
976 secs = (double)sv->check.duration / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100977 val = mkf_flt(FN_DURATION, secs);
Christopher Faulet2711e512020-02-27 16:12:07 +0100978 break;
Marcin Deranek3c27dda2020-05-15 18:32:51 +0200979 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100980 case ST_F_HRSP_1XX:
981 if (px->mode != PR_MODE_HTTP)
982 goto next_px;
William Dauchybde2bf62021-01-25 17:29:04 +0100983 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100984 break;
985 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100986 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100987 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100988 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100989 case ST_F_HRSP_OTHER:
990 if (px->mode != PR_MODE_HTTP)
991 goto next_px;
992 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100993 labels[2].name = ist("code");
994 labels[2].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
William Dauchybde2bf62021-01-25 17:29:04 +0100995 val = stats[appctx->st2];
Christopher Fauletc55a6262020-07-10 15:39:39 +0200996 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100997
998 default:
William Dauchybde2bf62021-01-25 17:29:04 +0100999 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001000 }
1001
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))
Christopher Fauletf959d082019-02-07 15:38:42 +01001004 goto full;
Christopher Fauleteba22942019-11-19 14:18:24 +01001005 next_sv:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001006 appctx->ctx.stats.obj2 = sv->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001007 }
1008
1009 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001010 appctx->ctx.stats.obj1 = px->next;
1011 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001012 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001013 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001014 appctx->ctx.stats.obj1 = proxies_list;
1015 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001016 }
1017
1018
1019 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001020 if (out.len) {
1021 if (!htx_add_data_atonce(htx, out))
1022 return -1; /* Unexpected and unrecoverable error */
1023 channel_add_input(chn, out.len);
1024 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001025 return ret;
1026 full:
1027 ret = 0;
1028 goto end;
1029}
1030
1031/* Dump all metrics (global, frontends, backends and servers) depending on the
1032 * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001033 * -1 in case of any error.
1034 * Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
1035 * a pointer to the current server/listener. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001036static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1037{
1038 int ret;
1039
1040 switch (appctx->st1) {
1041 case PROMEX_DUMPER_INIT:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001042 appctx->ctx.stats.obj1 = NULL;
1043 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001044 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001045 appctx->st2 = INF_NAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001046 appctx->st1 = PROMEX_DUMPER_GLOBAL;
1047 /* fall through */
1048
1049 case PROMEX_DUMPER_GLOBAL:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001050 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_GLOBAL) {
1051 ret = promex_dump_global_metrics(appctx, htx);
1052 if (ret <= 0) {
1053 if (ret == -1)
1054 goto error;
1055 goto full;
1056 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001057 }
1058
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001059 appctx->ctx.stats.obj1 = proxies_list;
1060 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001061 appctx->ctx.stats.flags &= ~PROMEX_FL_INFO_METRIC;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001062 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001063 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001064 appctx->st1 = PROMEX_DUMPER_FRONT;
1065 /* fall through */
1066
1067 case PROMEX_DUMPER_FRONT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001068 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_FRONT) {
1069 ret = promex_dump_front_metrics(appctx, htx);
1070 if (ret <= 0) {
1071 if (ret == -1)
1072 goto error;
1073 goto full;
1074 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001075 }
1076
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001077 appctx->ctx.stats.obj1 = proxies_list;
1078 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001079 appctx->ctx.stats.flags &= ~PROMEX_FL_FRONT_METRIC;
1080 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001081 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001082 appctx->st1 = PROMEX_DUMPER_BACK;
1083 /* fall through */
1084
1085 case PROMEX_DUMPER_BACK:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001086 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_BACK) {
1087 ret = promex_dump_back_metrics(appctx, htx);
1088 if (ret <= 0) {
1089 if (ret == -1)
1090 goto error;
1091 goto full;
1092 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001093 }
1094
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001095 appctx->ctx.stats.obj1 = proxies_list;
1096 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001097 appctx->ctx.stats.flags &= ~PROMEX_FL_BACK_METRIC;
1098 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001099 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001100 appctx->st1 = PROMEX_DUMPER_SRV;
1101 /* fall through */
1102
1103 case PROMEX_DUMPER_SRV:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001104 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_SERVER) {
1105 ret = promex_dump_srv_metrics(appctx, htx);
1106 if (ret <= 0) {
1107 if (ret == -1)
1108 goto error;
1109 goto full;
1110 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001111 }
1112
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001113 appctx->ctx.stats.obj1 = NULL;
1114 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001115 appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001116 appctx->st2 = 0;
1117 appctx->st1 = PROMEX_DUMPER_DONE;
1118 /* fall through */
1119
1120 case PROMEX_DUMPER_DONE:
1121 default:
1122 break;
1123 }
1124
1125 return 1;
1126
1127 full:
1128 si_rx_room_blk(si);
1129 return 0;
1130 error:
1131 /* unrecoverable error */
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001132 appctx->ctx.stats.obj1 = NULL;
1133 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletf959d082019-02-07 15:38:42 +01001134 appctx->ctx.stats.flags = 0;
1135 appctx->st2 = 0;
1136 appctx->st1 = PROMEX_DUMPER_DONE;
1137 return -1;
1138}
1139
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001140/* Parse the query string of request URI to filter the metrics. It returns 1 on
Christopher Faulet78407ce2019-11-18 14:47:08 +01001141 * success and -1 on error. */
1142static int promex_parse_uri(struct appctx *appctx, struct stream_interface *si)
1143{
1144 struct channel *req = si_oc(si);
1145 struct channel *res = si_ic(si);
1146 struct htx *req_htx, *res_htx;
1147 struct htx_sl *sl;
William Dauchyc65f6562019-11-26 12:56:26 +01001148 char *p, *key, *value;
1149 const char *end;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001150 struct buffer *err;
1151 int default_scopes = PROMEX_FL_SCOPE_ALL;
1152 int len;
1153
1154 /* Get the query-string */
1155 req_htx = htxbuf(&req->buf);
1156 sl = http_get_stline(req_htx);
1157 if (!sl)
1158 goto error;
1159 p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?');
1160 if (!p)
1161 goto end;
1162 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001163
William Dauchyc65f6562019-11-26 12:56:26 +01001164 /* copy the query-string */
1165 len = end - p;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001166 chunk_reset(&trash);
1167 memcpy(trash.area, p, len);
1168 trash.area[len] = 0;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001169 p = trash.area;
William Dauchyc65f6562019-11-26 12:56:26 +01001170 end = trash.area + len;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001171
1172 /* Parse the query-string */
William Dauchyc65f6562019-11-26 12:56:26 +01001173 while (p < end && *p && *p != '#') {
1174 value = NULL;
1175
1176 /* decode parameter name */
1177 key = p;
1178 while (p < end && *p != '=' && *p != '&' && *p != '#')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001179 ++p;
William Dauchyc65f6562019-11-26 12:56:26 +01001180 /* found a value */
1181 if (*p == '=') {
1182 *(p++) = 0;
1183 value = p;
1184 }
1185 else if (*p == '&')
1186 *(p++) = 0;
1187 else if (*p == '#')
1188 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001189 len = url_decode(key, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001190 if (len == -1)
1191 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001192
William Dauchyc65f6562019-11-26 12:56:26 +01001193 /* decode value */
1194 if (value) {
1195 while (p < end && *p != '=' && *p != '&' && *p != '#')
1196 ++p;
1197 if (*p == '=')
1198 goto error;
1199 if (*p == '&')
1200 *(p++) = 0;
1201 else if (*p == '#')
1202 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001203 len = url_decode(value, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001204 if (len == -1)
1205 goto error;
1206 }
1207
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001208 if (strcmp(key, "scope") == 0) {
William Dauchyc65f6562019-11-26 12:56:26 +01001209 default_scopes = 0; /* at least a scope defined, unset default scopes */
1210 if (!value)
1211 goto error;
1212 else if (*value == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001213 appctx->ctx.stats.flags &= ~PROMEX_FL_SCOPE_ALL;
William Dauchyc65f6562019-11-26 12:56:26 +01001214 else if (*value == '*')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001215 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_ALL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001216 else if (strcmp(value, "global") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001217 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_GLOBAL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001218 else if (strcmp(value, "server") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001219 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_SERVER;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001220 else if (strcmp(value, "backend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001221 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_BACK;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001222 else if (strcmp(value, "frontend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001223 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_FRONT;
1224 else
1225 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001226 }
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001227 else if (strcmp(key, "no-maint") == 0)
Christopher Fauleteba22942019-11-19 14:18:24 +01001228 appctx->ctx.stats.flags |= PROMEX_FL_NO_MAINT_SRV;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001229 }
1230
1231 end:
1232 appctx->ctx.stats.flags |= default_scopes;
1233 return 1;
1234
1235 error:
1236 err = &http_err_chunks[HTTP_ERR_400];
1237 channel_erase(res);
1238 res->buf.data = b_data(err);
1239 memcpy(res->buf.area, b_head(err), b_data(err));
1240 res_htx = htx_from_buf(&res->buf);
1241 channel_add_input(res, res_htx->data);
1242 appctx->st0 = PROMEX_ST_END;
1243 return -1;
1244}
1245
Christopher Fauletf959d082019-02-07 15:38:42 +01001246/* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is
1247 * full. */
1248static int promex_send_headers(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1249{
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001250 struct channel *chn = si_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +01001251 struct htx_sl *sl;
1252 unsigned int flags;
1253
1254 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);
1255 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
1256 if (!sl)
1257 goto full;
1258 sl->info.res.status = 200;
1259 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001260 !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) ||
1261 !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) ||
1262 !htx_add_endof(htx, HTX_BLK_EOH))
1263 goto full;
1264
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001265 channel_add_input(chn, htx->data);
Christopher Fauletf959d082019-02-07 15:38:42 +01001266 return 1;
1267 full:
1268 htx_reset(htx);
1269 si_rx_room_blk(si);
1270 return 0;
1271}
1272
1273/* The function returns 1 if the initialisation is complete, 0 if
1274 * an errors occurs and -1 if more data are required for initializing
1275 * the applet.
1276 */
1277static int promex_appctx_init(struct appctx *appctx, struct proxy *px, struct stream *strm)
1278{
1279 appctx->st0 = PROMEX_ST_INIT;
1280 return 1;
1281}
1282
1283/* The main I/O handler for the promex applet. */
1284static void promex_appctx_handle_io(struct appctx *appctx)
1285{
1286 struct stream_interface *si = appctx->owner;
1287 struct stream *s = si_strm(si);
1288 struct channel *req = si_oc(si);
1289 struct channel *res = si_ic(si);
1290 struct htx *req_htx, *res_htx;
1291 int ret;
1292
1293 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf959d082019-02-07 15:38:42 +01001294 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1295 goto out;
1296
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001297 /* Check if the input buffer is available. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001298 if (!b_size(&res->buf)) {
1299 si_rx_room_blk(si);
1300 goto out;
1301 }
1302
1303 switch (appctx->st0) {
1304 case PROMEX_ST_INIT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001305 ret = promex_parse_uri(appctx, si);
1306 if (ret <= 0) {
1307 if (ret == -1)
1308 goto error;
1309 goto out;
1310 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001311 appctx->st0 = PROMEX_ST_HEAD;
1312 appctx->st1 = PROMEX_DUMPER_INIT;
1313 /* fall through */
1314
1315 case PROMEX_ST_HEAD:
1316 if (!promex_send_headers(appctx, si, res_htx))
1317 goto out;
1318 appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP);
1319 /* fall through */
1320
1321 case PROMEX_ST_DUMP:
1322 ret = promex_dump_metrics(appctx, si, res_htx);
1323 if (ret <= 0) {
1324 if (ret == -1)
1325 goto error;
1326 goto out;
1327 }
1328 appctx->st0 = PROMEX_ST_DONE;
1329 /* fall through */
1330
1331 case PROMEX_ST_DONE:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001332 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
1333 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001334 appctx->st0 = PROMEX_ST_END;
1335 /* fall through */
Christopher Fauletf959d082019-02-07 15:38:42 +01001336
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001337 case PROMEX_ST_END:
1338 if (!(res->flags & CF_SHUTR)) {
1339 res->flags |= CF_READ_NULL;
1340 si_shutr(si);
1341 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001342 }
1343
Christopher Fauletf959d082019-02-07 15:38:42 +01001344 out:
1345 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001346
1347 /* eat the whole request */
1348 if (co_data(req)) {
1349 req_htx = htx_from_buf(&req->buf);
1350 co_htx_skip(req, req_htx, co_data(req));
1351 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001352 return;
1353
1354 error:
1355 res->flags |= CF_READ_NULL;
1356 si_shutr(si);
1357 si_shutw(si);
1358}
1359
1360struct applet promex_applet = {
1361 .obj_type = OBJ_TYPE_APPLET,
1362 .name = "<PROMEX>", /* used for logging */
1363 .init = promex_appctx_init,
1364 .fct = promex_appctx_handle_io,
1365};
1366
1367static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px,
1368 struct act_rule *rule, char **err)
1369{
1370 /* Prometheus exporter service is only available on "http-request" rulesets */
1371 if (rule->from != ACT_F_HTTP_REQ) {
1372 memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets");
1373 return ACT_RET_PRS_ERR;
1374 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001375
1376 /* Add applet pointer in the rule. */
1377 rule->applet = promex_applet;
1378
1379 return ACT_RET_PRS_OK;
1380}
1381static void promex_register_build_options(void)
1382{
1383 char *ptr = NULL;
1384
1385 memprintf(&ptr, "Built with the Prometheus exporter as a service");
1386 hap_register_build_opts(ptr, 1);
1387}
1388
1389
1390static struct action_kw_list service_actions = { ILH, {
1391 { "prometheus-exporter", service_parse_prometheus_exporter },
1392 { /* END */ }
1393}};
1394
1395INITCALL1(STG_REGISTER, service_keywords_register, &service_actions);
1396INITCALL0(STG_REGISTER, promex_register_build_options);