blob: 769389735ac632e658dfa4e195cc15c3cce4461e [file] [log] [blame]
Christopher Fauletf959d082019-02-07 15:38:42 +01001/*
2 * Promex is a Prometheus exporter for HAProxy
3 *
4 * It is highly inspired by the official Prometheus exporter.
5 * See: https://github.com/prometheus/haproxy_exporter
6 *
7 * Copyright 2019 Christopher Faulet <cfaulet@haproxy.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 */
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020018#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020019#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
William Dauchyde3c3262021-02-01 13:11:51 +010021#include <haproxy/check.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020022#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020023#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020024#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020025#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020026#include <haproxy/htx.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020027#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020028#include <haproxy/listener.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020029#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020031#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020032#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020033#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020034#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020035#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020036#include <haproxy/task.h>
William Dauchy5a982a72021-01-08 13:18:06 +010037#include <haproxy/version.h>
Christopher Fauletf959d082019-02-07 15:38:42 +010038
39/* Prometheus exporter applet states (appctx->st0) */
40enum {
41 PROMEX_ST_INIT = 0, /* initialized */
42 PROMEX_ST_HEAD, /* send headers before dump */
43 PROMEX_ST_DUMP, /* dumping stats */
44 PROMEX_ST_DONE, /* finished */
Christopher Faulet9744f7c2019-03-27 15:48:53 +010045 PROMEX_ST_END, /* treatment terminated */
Christopher Fauletf959d082019-02-07 15:38:42 +010046};
47
48/* Prometheus exporter dumper states (appctx->st1) */
49enum {
50 PROMEX_DUMPER_INIT = 0, /* initialized */
51 PROMEX_DUMPER_GLOBAL, /* dump metrics of globals */
52 PROMEX_DUMPER_FRONT, /* dump metrics of frontend proxies */
53 PROMEX_DUMPER_BACK, /* dump metrics of backend proxies */
54 PROMEX_DUMPER_LI, /* dump metrics of listeners */
55 PROMEX_DUMPER_SRV, /* dump metrics of servers */
56 PROMEX_DUMPER_DONE, /* finished */
57};
58
59/* Prometheus exporter flags (appctx->ctx.stats.flags) */
60#define PROMEX_FL_METRIC_HDR 0x00000001
61#define PROMEX_FL_INFO_METRIC 0x00000002
Christopher Fauletb713c4f2021-01-20 15:02:50 +010062#define PROMEX_FL_FRONT_METRIC 0x00000004
63#define PROMEX_FL_BACK_METRIC 0x00000008
64#define PROMEX_FL_SRV_METRIC 0x00000010
65#define PROMEX_FL_SCOPE_GLOBAL 0x00000020
66#define PROMEX_FL_SCOPE_FRONT 0x00000040
67#define PROMEX_FL_SCOPE_BACK 0x00000080
68#define PROMEX_FL_SCOPE_SERVER 0x00000100
69#define PROMEX_FL_NO_MAINT_SRV 0x00000200
Christopher Faulet78407ce2019-11-18 14:47:08 +010070
71#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 +010072
Christopher Faulet0312c0d2021-01-20 15:19:12 +010073/* Promtheus metric type (gauge or counter) */
74enum promex_mt_type {
75 PROMEX_MT_GAUGE = 1,
76 PROMEX_MT_COUNTER = 2,
77};
78
Christopher Fauletf959d082019-02-07 15:38:42 +010079/* The max length for metrics name. It is a hard limit but it should be
Ilya Shipitsince7b00f2020-03-23 22:28:40 +050080 * enough.
Christopher Fauletf959d082019-02-07 15:38:42 +010081 */
82#define PROMEX_MAX_NAME_LEN 128
83
84/* The expected max length for a metric dump, including its header lines. It is
85 * just a soft limit to avoid extra work. We don't try to dump a metric if less
86 * than this size is available in the HTX.
87 */
88#define PROMEX_MAX_METRIC_LENGTH 512
89
Christopher Faulet5a2f9382021-01-28 11:24:17 +010090/* The max number of labels per metric */
91#define PROMEX_MAX_LABELS 8
William Dauchy5a982a72021-01-08 13:18:06 +010092
Christopher Faulet0312c0d2021-01-20 15:19:12 +010093/* Describe a prometheus metric */
94struct promex_metric {
95 const struct ist n; /* The metric name */
96 enum promex_mt_type type; /* The metric type (gauge or counter) */
97 unsigned int flags; /* PROMEX_FL_* flags */
98};
99
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100100/* Describe a prometheus metric label. It is just a key/value pair */
101struct promex_label {
102 struct ist name;
103 struct ist value;
104};
105
Christopher Faulet37286a52021-01-20 15:20:53 +0100106/* Global metrics */
107const struct promex_metric promex_global_metrics[INF_TOTAL_FIELDS] = {
108 //[INF_NAME] ignored
109 //[INF_VERSION], ignored
110 //[INF_RELEASE_DATE] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100111 [INF_NBTHREAD] = { .n = IST("nbthread"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
112 [INF_NBPROC] = { .n = IST("nbproc"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
113 [INF_PROCESS_NUM] = { .n = IST("relative_process_id"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
114 //[INF_PID] ignored
115 //[INF_UPTIME] ignored
116 [INF_UPTIME_SEC] = { .n = IST("uptime_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
117 [INF_START_TIME_SEC] = { .n = IST("start_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100118 //[INF_MEMMAX_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100119 [INF_MEMMAX_BYTES] = { .n = IST("max_memory_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100120 //[INF_POOL_ALLOC_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100121 [INF_POOL_ALLOC_BYTES] = { .n = IST("pool_allocated_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100122 //[INF_POOL_USED_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100123 [INF_POOL_USED_BYTES] = { .n = IST("pool_used_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
124 [INF_POOL_FAILED] = { .n = IST("pool_failures_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
125 [INF_ULIMIT_N] = { .n = IST("max_fds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
126 [INF_MAXSOCK] = { .n = IST("max_sockets"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
127 [INF_MAXCONN] = { .n = IST("max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
128 [INF_HARD_MAXCONN] = { .n = IST("hard_max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
129 [INF_CURR_CONN] = { .n = IST("current_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
130 [INF_CUM_CONN] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
131 [INF_CUM_REQ] = { .n = IST("requests_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
132 [INF_MAX_SSL_CONNS] = { .n = IST("max_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
133 [INF_CURR_SSL_CONNS] = { .n = IST("current_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
134 [INF_CUM_SSL_CONNS] = { .n = IST("ssl_connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
135 [INF_MAXPIPES] = { .n = IST("max_pipes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
136 [INF_PIPES_USED] = { .n = IST("pipes_used_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
137 [INF_PIPES_FREE] = { .n = IST("pipes_free_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
138 [INF_CONN_RATE] = { .n = IST("current_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
139 [INF_CONN_RATE_LIMIT] = { .n = IST("limit_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
140 [INF_MAX_CONN_RATE] = { .n = IST("max_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
141 [INF_SESS_RATE] = { .n = IST("current_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
142 [INF_SESS_RATE_LIMIT] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
143 [INF_MAX_SESS_RATE] = { .n = IST("max_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
144 [INF_SSL_RATE] = { .n = IST("current_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
145 [INF_SSL_RATE_LIMIT] = { .n = IST("limit_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
146 [INF_MAX_SSL_RATE] = { .n = IST("max_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
147 [INF_SSL_FRONTEND_KEY_RATE] = { .n = IST("current_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
148 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .n = IST("max_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
149 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .n = IST("frontend_ssl_reuse"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
150 [INF_SSL_BACKEND_KEY_RATE] = { .n = IST("current_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
151 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .n = IST("max_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
152 [INF_SSL_CACHE_LOOKUPS] = { .n = IST("ssl_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
153 [INF_SSL_CACHE_MISSES] = { .n = IST("ssl_cache_misses_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
154 [INF_COMPRESS_BPS_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
155 [INF_COMPRESS_BPS_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
156 [INF_COMPRESS_BPS_RATE_LIM] = { .n = IST("limit_http_comp"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
157 [INF_ZLIB_MEM_USAGE] = { .n = IST("current_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
158 [INF_MAX_ZLIB_MEM_USAGE] = { .n = IST("max_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
159 [INF_TASKS] = { .n = IST("current_tasks"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
160 [INF_RUN_QUEUE] = { .n = IST("current_run_queue"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
161 [INF_IDLE_PCT] = { .n = IST("idle_time_percent"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
162 //[INF_NODE] ignored
163 //[INF_DESCRIPTION] ignored
164 [INF_STOPPING] = { .n = IST("stopping"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
165 [INF_JOBS] = { .n = IST("jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
166 [INF_UNSTOPPABLE_JOBS] = { .n = IST("unstoppable_jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
167 [INF_LISTENERS] = { .n = IST("listeners"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
168 [INF_ACTIVE_PEERS] = { .n = IST("active_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
169 [INF_CONNECTED_PEERS] = { .n = IST("connected_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
170 [INF_DROPPED_LOGS] = { .n = IST("dropped_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
171 [INF_BUSY_POLLING] = { .n = IST("busy_polling_enabled"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
172 [INF_FAILED_RESOLUTIONS] = { .n = IST("failed_resolutions"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
173 [INF_TOTAL_BYTES_OUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
174 [INF_TOTAL_SPLICED_BYTES_OUT] = { .n = IST("spliced_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
175 [INF_BYTES_OUT_RATE] = { .n = IST("bytes_out_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
176 //[INF_DEBUG_COMMANDS_ISSUED] ignored
William Dauchy7741c332021-02-01 13:11:57 +0100177 [INF_CUM_LOG_MSGS] = { .n = IST("recv_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100178 [INF_BUILD_INFO] = { .n = IST("build_info"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
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) },
William Dauchydf9a05d2021-02-01 13:11:59 +0100279 [ST_F_SAFE_CONN_CUR] = { .n = IST("safe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
Christopher Faulet37286a52021-01-20 15:20:53 +0100280 [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) },
William Dauchye5a26a22021-02-01 13:11:56 +0100282 [ST_F_UWEIGHT] = { .n = IST("uweight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
Christopher Fauletf959d082019-02-07 15:38:42 +0100283};
284
William Dauchy82b2ce22021-02-01 13:11:55 +0100285/* Description of overriden stats fields */
Christopher Fauletf959d082019-02-07 15:38:42 +0100286const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
William Dauchya1da7ba2021-02-01 13:11:52 +0100287 [ST_F_STATUS] = IST("Current status of the service, per state label value."),
William Dauchyde3c3262021-02-01 13:11:51 +0100288 [ST_F_CHECK_STATUS] = IST("Status of last health check, per state label value."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100289 [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."),
Christopher Faulet2711e512020-02-27 16:12:07 +0100290 [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100291 [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."),
292 [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."),
293 [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."),
294 [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100295 [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"),
296 [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"),
297 [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"),
298 [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100299};
300
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100301/* Specific labels for all ST_F_HRSP_* fields */
302const struct ist promex_hrsp_code[1 + ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = {
303 [ST_F_HRSP_1XX - ST_F_HRSP_1XX] = IST("1xx"),
304 [ST_F_HRSP_2XX - ST_F_HRSP_1XX] = IST("2xx"),
305 [ST_F_HRSP_3XX - ST_F_HRSP_1XX] = IST("3xx"),
306 [ST_F_HRSP_4XX - ST_F_HRSP_1XX] = IST("4xx"),
307 [ST_F_HRSP_5XX - ST_F_HRSP_1XX] = IST("5xx"),
308 [ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = IST("other"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100309};
310
William Dauchy54938212021-01-27 22:40:16 +0100311enum promex_front_state {
312 PROMEX_FRONT_STATE_DOWN = 0,
313 PROMEX_FRONT_STATE_UP,
314
315 PROMEX_FRONT_STATE_COUNT /* must be last */
316};
317
318const struct ist promex_front_st[PROMEX_FRONT_STATE_COUNT] = {
319 [PROMEX_FRONT_STATE_DOWN] = IST("DOWN"),
320 [PROMEX_FRONT_STATE_UP] = IST("UP"),
321};
322
323enum promex_back_state {
324 PROMEX_BACK_STATE_DOWN = 0,
325 PROMEX_BACK_STATE_UP,
326
327 PROMEX_BACK_STATE_COUNT /* must be last */
328};
329
330const struct ist promex_back_st[PROMEX_BACK_STATE_COUNT] = {
331 [PROMEX_BACK_STATE_DOWN] = IST("DOWN"),
332 [PROMEX_BACK_STATE_UP] = IST("UP"),
333};
334
335enum promex_srv_state {
336 PROMEX_SRV_STATE_DOWN = 0,
337 PROMEX_SRV_STATE_UP,
338 PROMEX_SRV_STATE_MAINT,
339 PROMEX_SRV_STATE_DRAIN,
340 PROMEX_SRV_STATE_NOLB,
341
342 PROMEX_SRV_STATE_COUNT /* must be last */
343};
344
345const struct ist promex_srv_st[PROMEX_SRV_STATE_COUNT] = {
346 [PROMEX_SRV_STATE_DOWN] = IST("DOWN"),
347 [PROMEX_SRV_STATE_UP] = IST("UP"),
348 [PROMEX_SRV_STATE_MAINT] = IST("MAINT"),
349 [PROMEX_SRV_STATE_DRAIN] = IST("DRAIN"),
350 [PROMEX_SRV_STATE_NOLB] = IST("NOLB"),
351};
352
353/* Return the server status. */
354enum promex_srv_state promex_srv_status(struct server *sv)
Christopher Fauletf959d082019-02-07 15:38:42 +0100355{
William Dauchy54938212021-01-27 22:40:16 +0100356 int state = PROMEX_SRV_STATE_DOWN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100357
Christopher Fauletf959d082019-02-07 15:38:42 +0100358 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Dauchy54938212021-01-27 22:40:16 +0100359 state = PROMEX_SRV_STATE_UP;
Christopher Fauletf959d082019-02-07 15:38:42 +0100360 if (sv->cur_admin & SRV_ADMF_DRAIN)
William Dauchy54938212021-01-27 22:40:16 +0100361 state = PROMEX_SRV_STATE_DRAIN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100362 }
Christopher Fauletd45d1052019-09-06 16:10:19 +0200363 else if (sv->cur_state == SRV_ST_STOPPING)
William Dauchy54938212021-01-27 22:40:16 +0100364 state = PROMEX_SRV_STATE_NOLB;
Christopher Fauletd45d1052019-09-06 16:10:19 +0200365
366 if (sv->cur_admin & SRV_ADMF_MAINT)
William Dauchy54938212021-01-27 22:40:16 +0100367 state = PROMEX_SRV_STATE_MAINT;
Christopher Fauletf959d082019-02-07 15:38:42 +0100368
369 return state;
370}
371
372/* Convert a field to its string representation and write it in <out>, followed
373 * by a newline, if there is enough space. non-numeric value are converted in
William Dauchy18a2c6e2021-01-22 21:09:47 +0100374 * "NaN" because Prometheus only support numerical values (but it is unexepceted
Christopher Fauletf959d082019-02-07 15:38:42 +0100375 * to process this kind of value). It returns 1 on success. Otherwise, it
376 * returns 0. The buffer's length must not exceed <max> value.
377 */
378static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
379{
380 int ret = 0;
381
382 switch (field_format(f, 0)) {
William Dauchy18a2c6e2021-01-22 21:09:47 +0100383 case FF_EMPTY: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100384 case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break;
385 case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break;
386 case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break;
387 case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200388 case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break;
William Dauchy18a2c6e2021-01-22 21:09:47 +0100389 case FF_STR: ret = chunk_strcat(out, "NaN\n"); break;
390 default: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100391 }
392 if (!ret || out->data > max)
393 return 0;
394 return 1;
395}
396
Christopher Fauletf959d082019-02-07 15:38:42 +0100397/* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
398 * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
399 */
400static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
Christopher Faulet37286a52021-01-20 15:20:53 +0100401 const struct promex_metric *metric, const struct ist name,
402 struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100403{
Christopher Faulet37286a52021-01-20 15:20:53 +0100404 struct ist type;
William Dauchy82b2ce22021-02-01 13:11:55 +0100405 struct ist desc;
Christopher Faulet37286a52021-01-20 15:20:53 +0100406
407 switch (metric->type) {
408 case PROMEX_MT_COUNTER:
409 type = ist("counter");
410 break;
411 default:
412 type = ist("gauge");
413 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100414
William Dauchya191b772021-01-15 22:41:39 +0100415 if (istcat(out, ist("# HELP "), max) == -1 ||
416 istcat(out, name, max) == -1 ||
417 istcat(out, ist(" "), max) == -1)
418 goto full;
419
William Dauchy82b2ce22021-02-01 13:11:55 +0100420 if (metric->flags & PROMEX_FL_INFO_METRIC)
421 desc = ist(info_fields[appctx->st2].desc);
422 else if (!isttest(promex_st_metric_desc[appctx->st2]))
423 desc = ist(stat_fields[appctx->st2].desc);
424 else
425 desc = promex_st_metric_desc[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100426
William Dauchy82b2ce22021-02-01 13:11:55 +0100427 if (istcat(out, desc, max) == -1 ||
428 istcat(out, ist("\n# TYPE "), max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100429 istcat(out, name, max) == -1 ||
430 istcat(out, ist(" "), max) == -1 ||
Christopher Faulet37286a52021-01-20 15:20:53 +0100431 istcat(out, type, max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100432 istcat(out, ist("\n"), max) == -1)
433 goto full;
434
435 return 1;
436
437 full:
438 return 0;
439}
440
441/* Dump the line for <metric>. It starts by the metric name followed by its
442 * labels (proxy name, server name...) between braces and finally its value. If
443 * not already done, the header lines are dumped first. It returns 1 on
444 * success. Otherwise if <out> length exceeds <max>, it returns 0.
445 */
Christopher Faulet37286a52021-01-20 15:20:53 +0100446static int promex_dump_metric(struct appctx *appctx, struct htx *htx, struct ist prefix,
William Dauchyc6464592021-01-27 22:40:17 +0100447 const struct promex_metric *metric, struct field *val,
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100448 struct promex_label *labels, struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100449{
450 struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
451 size_t len = out->len;
452
453 if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
454 return 0;
455
Christopher Faulet37286a52021-01-20 15:20:53 +0100456 /* Fill the metric name */
457 istcat(&name, prefix, PROMEX_MAX_NAME_LEN);
458 istcat(&name, metric->n, PROMEX_MAX_NAME_LEN);
459
460
Christopher Fauletf959d082019-02-07 15:38:42 +0100461 if ((appctx->ctx.stats.flags & PROMEX_FL_METRIC_HDR) &&
Christopher Faulet37286a52021-01-20 15:20:53 +0100462 !promex_dump_metric_header(appctx, htx, metric, name, out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100463 goto full;
464
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100465 if (istcat(out, name, max) == -1)
466 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100467
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100468 if (isttest(labels[0].name)) {
469 int i;
470
471 if (istcat(out, ist("{"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100472 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100473
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100474 for (i = 0; isttest(labels[i].name); i++) {
475 if (!isttest(labels[i].value))
476 continue;
477
478 if ((i && istcat(out, ist(","), max) == -1) ||
479 istcat(out, labels[i].name, max) == -1 ||
480 istcat(out, ist("=\""), max) == -1 ||
481 istcat(out, labels[i].value, max) == -1 ||
482 istcat(out, ist("\""), max) == -1)
483 goto full;
484 }
485
486 if (istcat(out, ist("}"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100487 goto full;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100488
Christopher Fauletf959d082019-02-07 15:38:42 +0100489 }
490
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100491 if (istcat(out, ist(" "), max) == -1)
492 goto full;
493
Christopher Fauletf959d082019-02-07 15:38:42 +0100494 trash.data = out->len;
Christopher Faulet37286a52021-01-20 15:20:53 +0100495 if (!promex_metric_to_str(&trash, val, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100496 goto full;
497 out->len = trash.data;
498
499 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
500 return 1;
501 full:
502 // Restore previous length
503 out->len = len;
504 return 0;
505
506}
507
508
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500509/* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100510 * 0 if <htx> is full and -1 in case of any error. */
511static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
512{
513 static struct ist prefix = IST("haproxy_process_");
Christopher Faulet37286a52021-01-20 15:20:53 +0100514 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100515 struct channel *chn = si_ic(appctx->owner);
516 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200517 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100518 int ret = 1;
519
William Dauchy5d9b8f32021-01-11 20:07:49 +0100520 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
521 return -1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100522
Christopher Faulet37286a52021-01-20 15:20:53 +0100523 for (; appctx->st2 < INF_TOTAL_FIELDS; appctx->st2++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100524 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
525
Christopher Faulet37286a52021-01-20 15:20:53 +0100526 if (!(promex_global_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
527 continue;
528
Christopher Fauletf959d082019-02-07 15:38:42 +0100529 switch (appctx->st2) {
William Dauchy5a982a72021-01-08 13:18:06 +0100530 case INF_BUILD_INFO:
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100531 labels[0].name = ist("version");
532 labels[0].value = ist(HAPROXY_VERSION);
Christopher Faulet37286a52021-01-20 15:20:53 +0100533 val = mkf_u32(FN_GAUGE, 1);
William Dauchy5a982a72021-01-08 13:18:06 +0100534 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100535
536 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100537 val = info[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100538 }
539
William Dauchyc6464592021-01-27 22:40:17 +0100540 if (!promex_dump_metric(appctx, htx, prefix, &promex_global_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100541 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100542 goto full;
543
Christopher Fauletf959d082019-02-07 15:38:42 +0100544 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100545 }
546
547 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200548 if (out.len) {
549 if (!htx_add_data_atonce(htx, out))
550 return -1; /* Unexpected and unrecoverable error */
551 channel_add_input(chn, out.len);
552 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100553 return ret;
554 full:
555 ret = 0;
556 goto end;
557}
558
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500559/* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100560 * 0 if <htx> is full and -1 in case of any error. */
561static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
562{
563 static struct ist prefix = IST("haproxy_frontend_");
564 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100565 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100566 struct channel *chn = si_ic(appctx->owner);
567 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200568 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchyb9577452021-01-17 18:27:46 +0100569 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100570 int ret = 1;
William Dauchyc6464592021-01-27 22:40:17 +0100571 enum promex_front_state state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100572
Christopher Faulet37286a52021-01-20 15:20:53 +0100573 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
574 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
575 continue;
576
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200577 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100578 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
579
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200580 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100581
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100582 labels[0].name = ist("proxy");
583 labels[0].value = ist2(px->id, strlen(px->id));
584
Christopher Fauletf959d082019-02-07 15:38:42 +0100585 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200586 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100587 goto next_px;
588
William Dauchyb9577452021-01-17 18:27:46 +0100589 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
590 return -1;
591
Christopher Fauletf959d082019-02-07 15:38:42 +0100592 switch (appctx->st2) {
593 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100594 state = !px->disabled;
Christopher Faulet040b1192021-02-01 15:05:21 +0100595 for (; appctx->ctx.stats.st_code < PROMEX_FRONT_STATE_COUNT; appctx->ctx.stats.st_code++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100596 labels[1].name = ist("state");
Christopher Faulet040b1192021-02-01 15:05:21 +0100597 labels[1].value = promex_front_st[appctx->ctx.stats.st_code];
598 val = mkf_u32(FO_STATUS, state == appctx->ctx.stats.st_code);
William Dauchyc6464592021-01-27 22:40:17 +0100599 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100600 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100601 goto full;
602 }
Christopher Faulet040b1192021-02-01 15:05:21 +0100603 appctx->ctx.stats.st_code = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100604 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100605 case ST_F_REQ_RATE_MAX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100606 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100607 case ST_F_INTERCEPTED:
Christopher Fauletf959d082019-02-07 15:38:42 +0100608 case ST_F_CACHE_LOOKUPS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100609 case ST_F_CACHE_HITS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100610 case ST_F_COMP_IN:
Christopher Fauletf959d082019-02-07 15:38:42 +0100611 case ST_F_COMP_OUT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100612 case ST_F_COMP_BYP:
William Dauchyb9577452021-01-17 18:27:46 +0100613 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100614 if (px->mode != PR_MODE_HTTP)
615 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100616 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100617 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100618 case ST_F_HRSP_1XX:
William Dauchyb9577452021-01-17 18:27:46 +0100619 case ST_F_HRSP_2XX:
620 case ST_F_HRSP_3XX:
621 case ST_F_HRSP_4XX:
622 case ST_F_HRSP_5XX:
623 case ST_F_HRSP_OTHER:
Christopher Fauletf959d082019-02-07 15:38:42 +0100624 if (px->mode != PR_MODE_HTTP)
625 goto next_px;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100626 if (appctx->st2 != ST_F_HRSP_1XX)
627 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100628 labels[1].name = ist("code");
629 labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
630 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100631 break;
632
633 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100634 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100635 }
636
William Dauchyc6464592021-01-27 22:40:17 +0100637 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100638 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100639 goto full;
640 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200641 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100642 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100643 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200644 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100645 }
646
647 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200648 if (out.len) {
649 if (!htx_add_data_atonce(htx, out))
650 return -1; /* Unexpected and unrecoverable error */
651 channel_add_input(chn, out.len);
652 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100653 return ret;
654 full:
655 ret = 0;
656 goto end;
657}
658
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500659/* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100660 * 0 if <htx> is full and -1 in case of any error. */
661static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
662{
663 static struct ist prefix = IST("haproxy_backend_");
664 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100665 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100666 struct channel *chn = si_ic(appctx->owner);
667 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200668 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchy3c6f0062021-01-25 17:29:02 +0100669 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100670 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200671 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100672 enum promex_back_state state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100673
Christopher Faulet37286a52021-01-20 15:20:53 +0100674 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
675 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
676 continue;
677
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200678 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100679 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
680
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200681 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100682
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100683 labels[0].name = ist("proxy");
684 labels[0].value = ist2(px->id, strlen(px->id));
685
Christopher Fauletf959d082019-02-07 15:38:42 +0100686 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200687 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100688 goto next_px;
689
William Dauchy3c6f0062021-01-25 17:29:02 +0100690 if (!stats_fill_be_stats(px, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
691 return -1;
692
Christopher Fauletf959d082019-02-07 15:38:42 +0100693 switch (appctx->st2) {
694 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100695 state = ((px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0);
Christopher Faulet040b1192021-02-01 15:05:21 +0100696 for (; appctx->ctx.stats.st_code < PROMEX_BACK_STATE_COUNT; appctx->ctx.stats.st_code++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100697 labels[1].name = ist("state");
Christopher Faulet040b1192021-02-01 15:05:21 +0100698 labels[1].value = promex_back_st[appctx->ctx.stats.st_code];
699 val = mkf_u32(FO_STATUS, state == appctx->ctx.stats.st_code);
William Dauchyc6464592021-01-27 22:40:17 +0100700 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100701 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100702 goto full;
703 }
Christopher Faulet040b1192021-02-01 15:05:21 +0100704 appctx->ctx.stats.st_code = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100705 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100706 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200707 secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100708 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100709 break;
710 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200711 secs = (double)swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100712 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100713 break;
714 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200715 secs = (double)swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100716 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100717 break;
718 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200719 secs = (double)swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100720 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100721 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100722 case ST_F_QT_MAX:
723 secs = (double)px->be_counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100724 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100725 break;
726 case ST_F_CT_MAX:
727 secs = (double)px->be_counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100728 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100729 break;
730 case ST_F_RT_MAX:
731 secs = (double)px->be_counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100732 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100733 break;
734 case ST_F_TT_MAX:
735 secs = (double)px->be_counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100736 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100737 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100738 case ST_F_REQ_TOT:
William Dauchy3c6f0062021-01-25 17:29:02 +0100739 case ST_F_CACHE_LOOKUPS:
740 case ST_F_CACHE_HITS:
741 case ST_F_COMP_IN:
742 case ST_F_COMP_OUT:
743 case ST_F_COMP_BYP:
744 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100745 if (px->mode != PR_MODE_HTTP)
746 goto next_px;
William Dauchy3c6f0062021-01-25 17:29:02 +0100747 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100748 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100749 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100750 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100751 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100752 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100753 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100754 case ST_F_HRSP_OTHER:
755 if (px->mode != PR_MODE_HTTP)
756 goto next_px;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100757 if (appctx->st2 != ST_F_HRSP_1XX)
758 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100759 labels[1].name = ist("code");
760 labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
William Dauchy3c6f0062021-01-25 17:29:02 +0100761 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100762 break;
763
764 default:
William Dauchy3c6f0062021-01-25 17:29:02 +0100765 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100766 }
767
William Dauchy3c6f0062021-01-25 17:29:02 +0100768 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100769 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100770 goto full;
771 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200772 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100773 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100774 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200775 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100776 }
777
778 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200779 if (out.len) {
780 if (!htx_add_data_atonce(htx, out))
781 return -1; /* Unexpected and unrecoverable error */
782 channel_add_input(chn, out.len);
783 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100784 return ret;
785 full:
786 ret = 0;
787 goto end;
788}
789
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500790/* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100791 * 0 if <htx> is full and -1 in case of any error. */
792static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
793{
794 static struct ist prefix = IST("haproxy_server_");
795 struct proxy *px;
796 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100797 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100798 struct channel *chn = si_ic(appctx->owner);
799 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200800 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchybde2bf62021-01-25 17:29:04 +0100801 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100802 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200803 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100804 enum promex_srv_state state;
William Dauchyde3c3262021-02-01 13:11:51 +0100805 const char *check_state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100806
Christopher Faulet37286a52021-01-20 15:20:53 +0100807 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
808 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
809 continue;
810
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200811 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100812 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
813
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200814 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100815
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100816 labels[0].name = ist("proxy");
817 labels[0].value = ist2(px->id, strlen(px->id));
818
Christopher Fauletf959d082019-02-07 15:38:42 +0100819 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200820 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100821 goto next_px;
822
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200823 while (appctx->ctx.stats.obj2) {
824 sv = appctx->ctx.stats.obj2;
Christopher Fauletf959d082019-02-07 15:38:42 +0100825
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100826 labels[1].name = ist("server");
827 labels[1].value = ist2(sv->id, strlen(sv->id));
828
William Dauchybde2bf62021-01-25 17:29:04 +0100829 if (!stats_fill_sv_stats(px, sv, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
830 return -1;
831
Christopher Fauleteba22942019-11-19 14:18:24 +0100832 if ((appctx->ctx.stats.flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
833 goto next_sv;
834
Christopher Fauletf959d082019-02-07 15:38:42 +0100835 switch (appctx->st2) {
836 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100837 state = promex_srv_status(sv);
Christopher Faulet040b1192021-02-01 15:05:21 +0100838 for (; appctx->ctx.stats.st_code < PROMEX_SRV_STATE_COUNT; appctx->ctx.stats.st_code++) {
839 val = mkf_u32(FO_STATUS, state == appctx->ctx.stats.st_code);
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100840 labels[2].name = ist("state");
Christopher Faulet040b1192021-02-01 15:05:21 +0100841 labels[2].value = promex_srv_st[appctx->ctx.stats.st_code];
William Dauchyc6464592021-01-27 22:40:17 +0100842 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100843 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100844 goto full;
845 }
Christopher Faulet040b1192021-02-01 15:05:21 +0100846 appctx->ctx.stats.st_code = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100847 goto next_sv;
Christopher Fauletf959d082019-02-07 15:38:42 +0100848 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200849 secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100850 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100851 break;
852 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200853 secs = (double)swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100854 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100855 break;
856 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200857 secs = (double)swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100858 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100859 break;
860 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200861 secs = (double)swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100862 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100863 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100864 case ST_F_QT_MAX:
865 secs = (double)sv->counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100866 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100867 break;
868 case ST_F_CT_MAX:
869 secs = (double)sv->counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100870 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100871 break;
872 case ST_F_RT_MAX:
873 secs = (double)sv->counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100874 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100875 break;
876 case ST_F_TT_MAX:
877 secs = (double)sv->counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100878 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100879 break;
Christopher Fauletcf403f32019-11-21 14:35:46 +0100880 case ST_F_CHECK_STATUS:
881 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
882 goto next_sv;
William Dauchyde3c3262021-02-01 13:11:51 +0100883
Christopher Faulet040b1192021-02-01 15:05:21 +0100884 for (; appctx->ctx.stats.st_code < HCHK_STATUS_SIZE; appctx->ctx.stats.st_code++) {
885 if (get_check_status_result(appctx->ctx.stats.st_code) < CHK_RES_FAILED)
William Dauchyde3c3262021-02-01 13:11:51 +0100886 continue;
Christopher Faulet040b1192021-02-01 15:05:21 +0100887 val = mkf_u32(FO_STATUS, sv->check.status == appctx->ctx.stats.st_code);
888 check_state = get_check_status_info(appctx->ctx.stats.st_code);
William Dauchyde3c3262021-02-01 13:11:51 +0100889 labels[2].name = ist("state");
890 labels[2].value = ist2(check_state, strlen(check_state));
891 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
892 &val, labels, &out, max))
893 goto full;
894 }
Christopher Faulet040b1192021-02-01 15:05:21 +0100895 appctx->ctx.stats.st_code = 0;
William Dauchyde3c3262021-02-01 13:11:51 +0100896 goto next_sv;
Christopher Fauletcf403f32019-11-21 14:35:46 +0100897 case ST_F_CHECK_CODE:
898 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
899 goto next_sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100900 val = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code);
Christopher Fauletcf403f32019-11-21 14:35:46 +0100901 break;
Christopher Faulet2711e512020-02-27 16:12:07 +0100902 case ST_F_CHECK_DURATION:
903 if (sv->check.status < HCHK_STATUS_CHECKED)
904 goto next_sv;
905 secs = (double)sv->check.duration / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100906 val = mkf_flt(FN_DURATION, secs);
Christopher Faulet2711e512020-02-27 16:12:07 +0100907 break;
Marcin Deranek3c27dda2020-05-15 18:32:51 +0200908 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100909 if (px->mode != PR_MODE_HTTP)
910 goto next_px;
William Dauchybde2bf62021-01-25 17:29:04 +0100911 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100912 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100913 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100914 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100915 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100916 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100917 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100918 case ST_F_HRSP_OTHER:
919 if (px->mode != PR_MODE_HTTP)
920 goto next_px;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100921 if (appctx->st2 != ST_F_HRSP_1XX)
922 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100923 labels[2].name = ist("code");
924 labels[2].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
William Dauchybde2bf62021-01-25 17:29:04 +0100925 val = stats[appctx->st2];
Christopher Fauletc55a6262020-07-10 15:39:39 +0200926 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100927
928 default:
William Dauchybde2bf62021-01-25 17:29:04 +0100929 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100930 }
931
William Dauchyc6464592021-01-27 22:40:17 +0100932 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100933 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100934 goto full;
Christopher Fauleteba22942019-11-19 14:18:24 +0100935 next_sv:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200936 appctx->ctx.stats.obj2 = sv->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100937 }
938
939 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200940 appctx->ctx.stats.obj1 = px->next;
941 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +0100942 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100943 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200944 appctx->ctx.stats.obj1 = proxies_list;
945 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +0100946 }
947
948
949 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200950 if (out.len) {
951 if (!htx_add_data_atonce(htx, out))
952 return -1; /* Unexpected and unrecoverable error */
953 channel_add_input(chn, out.len);
954 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100955 return ret;
956 full:
957 ret = 0;
958 goto end;
959}
960
961/* Dump all metrics (global, frontends, backends and servers) depending on the
962 * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200963 * -1 in case of any error.
964 * Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
965 * a pointer to the current server/listener. */
Christopher Fauletf959d082019-02-07 15:38:42 +0100966static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
967{
968 int ret;
969
970 switch (appctx->st1) {
971 case PROMEX_DUMPER_INIT:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200972 appctx->ctx.stats.obj1 = NULL;
973 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +0200974 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
Christopher Faulet040b1192021-02-01 15:05:21 +0100975 appctx->ctx.stats.st_code = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100976 appctx->st2 = INF_NAME;
Christopher Fauletf959d082019-02-07 15:38:42 +0100977 appctx->st1 = PROMEX_DUMPER_GLOBAL;
978 /* fall through */
979
980 case PROMEX_DUMPER_GLOBAL:
Christopher Faulet78407ce2019-11-18 14:47:08 +0100981 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_GLOBAL) {
982 ret = promex_dump_global_metrics(appctx, htx);
983 if (ret <= 0) {
984 if (ret == -1)
985 goto error;
986 goto full;
987 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100988 }
989
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200990 appctx->ctx.stats.obj1 = proxies_list;
991 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +0200992 appctx->ctx.stats.flags &= ~PROMEX_FL_INFO_METRIC;
Christopher Fauletb713c4f2021-01-20 15:02:50 +0100993 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC);
Christopher Faulet040b1192021-02-01 15:05:21 +0100994 appctx->ctx.stats.st_code = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100995 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +0100996 appctx->st1 = PROMEX_DUMPER_FRONT;
997 /* fall through */
998
999 case PROMEX_DUMPER_FRONT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001000 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_FRONT) {
1001 ret = promex_dump_front_metrics(appctx, htx);
1002 if (ret <= 0) {
1003 if (ret == -1)
1004 goto error;
1005 goto full;
1006 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001007 }
1008
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001009 appctx->ctx.stats.obj1 = proxies_list;
1010 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001011 appctx->ctx.stats.flags &= ~PROMEX_FL_FRONT_METRIC;
1012 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC);
Christopher Faulet040b1192021-02-01 15:05:21 +01001013 appctx->ctx.stats.st_code = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001014 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001015 appctx->st1 = PROMEX_DUMPER_BACK;
1016 /* fall through */
1017
1018 case PROMEX_DUMPER_BACK:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001019 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_BACK) {
1020 ret = promex_dump_back_metrics(appctx, htx);
1021 if (ret <= 0) {
1022 if (ret == -1)
1023 goto error;
1024 goto full;
1025 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001026 }
1027
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001028 appctx->ctx.stats.obj1 = proxies_list;
1029 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001030 appctx->ctx.stats.flags &= ~PROMEX_FL_BACK_METRIC;
1031 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Faulet040b1192021-02-01 15:05:21 +01001032 appctx->ctx.stats.st_code = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001033 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001034 appctx->st1 = PROMEX_DUMPER_SRV;
1035 /* fall through */
1036
1037 case PROMEX_DUMPER_SRV:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001038 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_SERVER) {
1039 ret = promex_dump_srv_metrics(appctx, htx);
1040 if (ret <= 0) {
1041 if (ret == -1)
1042 goto error;
1043 goto full;
1044 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001045 }
1046
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001047 appctx->ctx.stats.obj1 = NULL;
1048 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001049 appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001050 appctx->st2 = 0;
1051 appctx->st1 = PROMEX_DUMPER_DONE;
1052 /* fall through */
1053
1054 case PROMEX_DUMPER_DONE:
1055 default:
1056 break;
1057 }
1058
1059 return 1;
1060
1061 full:
1062 si_rx_room_blk(si);
1063 return 0;
1064 error:
1065 /* unrecoverable error */
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001066 appctx->ctx.stats.obj1 = NULL;
1067 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletf959d082019-02-07 15:38:42 +01001068 appctx->ctx.stats.flags = 0;
1069 appctx->st2 = 0;
1070 appctx->st1 = PROMEX_DUMPER_DONE;
1071 return -1;
1072}
1073
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001074/* Parse the query string of request URI to filter the metrics. It returns 1 on
Christopher Faulet78407ce2019-11-18 14:47:08 +01001075 * success and -1 on error. */
1076static int promex_parse_uri(struct appctx *appctx, struct stream_interface *si)
1077{
1078 struct channel *req = si_oc(si);
1079 struct channel *res = si_ic(si);
1080 struct htx *req_htx, *res_htx;
1081 struct htx_sl *sl;
William Dauchyc65f6562019-11-26 12:56:26 +01001082 char *p, *key, *value;
1083 const char *end;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001084 struct buffer *err;
1085 int default_scopes = PROMEX_FL_SCOPE_ALL;
1086 int len;
1087
1088 /* Get the query-string */
1089 req_htx = htxbuf(&req->buf);
1090 sl = http_get_stline(req_htx);
1091 if (!sl)
1092 goto error;
1093 p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?');
1094 if (!p)
1095 goto end;
1096 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001097
William Dauchyc65f6562019-11-26 12:56:26 +01001098 /* copy the query-string */
1099 len = end - p;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001100 chunk_reset(&trash);
1101 memcpy(trash.area, p, len);
1102 trash.area[len] = 0;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001103 p = trash.area;
William Dauchyc65f6562019-11-26 12:56:26 +01001104 end = trash.area + len;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001105
1106 /* Parse the query-string */
William Dauchyc65f6562019-11-26 12:56:26 +01001107 while (p < end && *p && *p != '#') {
1108 value = NULL;
1109
1110 /* decode parameter name */
1111 key = p;
1112 while (p < end && *p != '=' && *p != '&' && *p != '#')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001113 ++p;
William Dauchyc65f6562019-11-26 12:56:26 +01001114 /* found a value */
1115 if (*p == '=') {
1116 *(p++) = 0;
1117 value = p;
1118 }
1119 else if (*p == '&')
1120 *(p++) = 0;
1121 else if (*p == '#')
1122 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001123 len = url_decode(key, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001124 if (len == -1)
1125 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001126
William Dauchyc65f6562019-11-26 12:56:26 +01001127 /* decode value */
1128 if (value) {
1129 while (p < end && *p != '=' && *p != '&' && *p != '#')
1130 ++p;
1131 if (*p == '=')
1132 goto error;
1133 if (*p == '&')
1134 *(p++) = 0;
1135 else if (*p == '#')
1136 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001137 len = url_decode(value, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001138 if (len == -1)
1139 goto error;
1140 }
1141
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001142 if (strcmp(key, "scope") == 0) {
William Dauchyc65f6562019-11-26 12:56:26 +01001143 default_scopes = 0; /* at least a scope defined, unset default scopes */
1144 if (!value)
1145 goto error;
1146 else if (*value == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001147 appctx->ctx.stats.flags &= ~PROMEX_FL_SCOPE_ALL;
William Dauchyc65f6562019-11-26 12:56:26 +01001148 else if (*value == '*')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001149 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_ALL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001150 else if (strcmp(value, "global") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001151 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_GLOBAL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001152 else if (strcmp(value, "server") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001153 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_SERVER;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001154 else if (strcmp(value, "backend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001155 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_BACK;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001156 else if (strcmp(value, "frontend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001157 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_FRONT;
1158 else
1159 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001160 }
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001161 else if (strcmp(key, "no-maint") == 0)
Christopher Fauleteba22942019-11-19 14:18:24 +01001162 appctx->ctx.stats.flags |= PROMEX_FL_NO_MAINT_SRV;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001163 }
1164
1165 end:
1166 appctx->ctx.stats.flags |= default_scopes;
1167 return 1;
1168
1169 error:
1170 err = &http_err_chunks[HTTP_ERR_400];
1171 channel_erase(res);
1172 res->buf.data = b_data(err);
1173 memcpy(res->buf.area, b_head(err), b_data(err));
1174 res_htx = htx_from_buf(&res->buf);
1175 channel_add_input(res, res_htx->data);
1176 appctx->st0 = PROMEX_ST_END;
1177 return -1;
1178}
1179
Christopher Fauletf959d082019-02-07 15:38:42 +01001180/* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is
1181 * full. */
1182static int promex_send_headers(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1183{
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001184 struct channel *chn = si_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +01001185 struct htx_sl *sl;
1186 unsigned int flags;
1187
1188 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);
1189 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
1190 if (!sl)
1191 goto full;
1192 sl->info.res.status = 200;
1193 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001194 !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) ||
1195 !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) ||
1196 !htx_add_endof(htx, HTX_BLK_EOH))
1197 goto full;
1198
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001199 channel_add_input(chn, htx->data);
Christopher Fauletf959d082019-02-07 15:38:42 +01001200 return 1;
1201 full:
1202 htx_reset(htx);
1203 si_rx_room_blk(si);
1204 return 0;
1205}
1206
1207/* The function returns 1 if the initialisation is complete, 0 if
1208 * an errors occurs and -1 if more data are required for initializing
1209 * the applet.
1210 */
1211static int promex_appctx_init(struct appctx *appctx, struct proxy *px, struct stream *strm)
1212{
1213 appctx->st0 = PROMEX_ST_INIT;
1214 return 1;
1215}
1216
1217/* The main I/O handler for the promex applet. */
1218static void promex_appctx_handle_io(struct appctx *appctx)
1219{
1220 struct stream_interface *si = appctx->owner;
1221 struct stream *s = si_strm(si);
1222 struct channel *req = si_oc(si);
1223 struct channel *res = si_ic(si);
1224 struct htx *req_htx, *res_htx;
1225 int ret;
1226
1227 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf959d082019-02-07 15:38:42 +01001228 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1229 goto out;
1230
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001231 /* Check if the input buffer is available. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001232 if (!b_size(&res->buf)) {
1233 si_rx_room_blk(si);
1234 goto out;
1235 }
1236
1237 switch (appctx->st0) {
1238 case PROMEX_ST_INIT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001239 ret = promex_parse_uri(appctx, si);
1240 if (ret <= 0) {
1241 if (ret == -1)
1242 goto error;
1243 goto out;
1244 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001245 appctx->st0 = PROMEX_ST_HEAD;
1246 appctx->st1 = PROMEX_DUMPER_INIT;
1247 /* fall through */
1248
1249 case PROMEX_ST_HEAD:
1250 if (!promex_send_headers(appctx, si, res_htx))
1251 goto out;
1252 appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP);
1253 /* fall through */
1254
1255 case PROMEX_ST_DUMP:
1256 ret = promex_dump_metrics(appctx, si, res_htx);
1257 if (ret <= 0) {
1258 if (ret == -1)
1259 goto error;
1260 goto out;
1261 }
1262 appctx->st0 = PROMEX_ST_DONE;
1263 /* fall through */
1264
1265 case PROMEX_ST_DONE:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001266 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
1267 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001268 appctx->st0 = PROMEX_ST_END;
1269 /* fall through */
Christopher Fauletf959d082019-02-07 15:38:42 +01001270
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001271 case PROMEX_ST_END:
1272 if (!(res->flags & CF_SHUTR)) {
1273 res->flags |= CF_READ_NULL;
1274 si_shutr(si);
1275 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001276 }
1277
Christopher Fauletf959d082019-02-07 15:38:42 +01001278 out:
1279 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001280
1281 /* eat the whole request */
1282 if (co_data(req)) {
1283 req_htx = htx_from_buf(&req->buf);
1284 co_htx_skip(req, req_htx, co_data(req));
1285 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001286 return;
1287
1288 error:
1289 res->flags |= CF_READ_NULL;
1290 si_shutr(si);
1291 si_shutw(si);
1292}
1293
1294struct applet promex_applet = {
1295 .obj_type = OBJ_TYPE_APPLET,
1296 .name = "<PROMEX>", /* used for logging */
1297 .init = promex_appctx_init,
1298 .fct = promex_appctx_handle_io,
1299};
1300
1301static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px,
1302 struct act_rule *rule, char **err)
1303{
1304 /* Prometheus exporter service is only available on "http-request" rulesets */
1305 if (rule->from != ACT_F_HTTP_REQ) {
1306 memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets");
1307 return ACT_RET_PRS_ERR;
1308 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001309
1310 /* Add applet pointer in the rule. */
1311 rule->applet = promex_applet;
1312
1313 return ACT_RET_PRS_OK;
1314}
1315static void promex_register_build_options(void)
1316{
1317 char *ptr = NULL;
1318
1319 memprintf(&ptr, "Built with the Prometheus exporter as a service");
1320 hap_register_build_opts(ptr, 1);
1321}
1322
1323
1324static struct action_kw_list service_actions = { ILH, {
1325 { "prometheus-exporter", service_parse_prometheus_exporter },
1326 { /* END */ }
1327}};
1328
1329INITCALL1(STG_REGISTER, service_keywords_register, &service_actions);
1330INITCALL0(STG_REGISTER, promex_register_build_options);