blob: bfcc126ec886598ef10a87cb2191fe1f7ed0e401 [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
111 [INF_BUILD_INFO] = { .n = IST("build_info"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
112 [INF_NBTHREAD] = { .n = IST("nbthread"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
113 [INF_NBPROC] = { .n = IST("nbproc"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
114 [INF_PROCESS_NUM] = { .n = IST("relative_process_id"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
115 //[INF_PID] ignored
116 //[INF_UPTIME] ignored
117 [INF_UPTIME_SEC] = { .n = IST("uptime_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
118 [INF_START_TIME_SEC] = { .n = IST("start_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
119 [INF_MEMMAX_BYTES] = { .n = IST("max_memory_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
120 [INF_POOL_ALLOC_BYTES] = { .n = IST("pool_allocated_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
121 [INF_POOL_USED_BYTES] = { .n = IST("pool_used_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
122 [INF_POOL_FAILED] = { .n = IST("pool_failures_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
123 [INF_ULIMIT_N] = { .n = IST("max_fds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
124 [INF_MAXSOCK] = { .n = IST("max_sockets"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
125 [INF_MAXCONN] = { .n = IST("max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
126 [INF_HARD_MAXCONN] = { .n = IST("hard_max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
127 [INF_CURR_CONN] = { .n = IST("current_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
128 [INF_CUM_CONN] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
129 [INF_CUM_REQ] = { .n = IST("requests_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
130 [INF_MAX_SSL_CONNS] = { .n = IST("max_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
131 [INF_CURR_SSL_CONNS] = { .n = IST("current_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
132 [INF_CUM_SSL_CONNS] = { .n = IST("ssl_connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
133 [INF_MAXPIPES] = { .n = IST("max_pipes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
134 [INF_PIPES_USED] = { .n = IST("pipes_used_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
135 [INF_PIPES_FREE] = { .n = IST("pipes_free_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
136 [INF_CONN_RATE] = { .n = IST("current_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
137 [INF_CONN_RATE_LIMIT] = { .n = IST("limit_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
138 [INF_MAX_CONN_RATE] = { .n = IST("max_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
139 [INF_SESS_RATE] = { .n = IST("current_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
140 [INF_SESS_RATE_LIMIT] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
141 [INF_MAX_SESS_RATE] = { .n = IST("max_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
142 [INF_SSL_RATE] = { .n = IST("current_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
143 [INF_SSL_RATE_LIMIT] = { .n = IST("limit_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
144 [INF_MAX_SSL_RATE] = { .n = IST("max_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
145 [INF_SSL_FRONTEND_KEY_RATE] = { .n = IST("current_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
146 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .n = IST("max_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
147 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .n = IST("frontend_ssl_reuse"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
148 [INF_SSL_BACKEND_KEY_RATE] = { .n = IST("current_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
149 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .n = IST("max_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
150 [INF_SSL_CACHE_LOOKUPS] = { .n = IST("ssl_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
151 [INF_SSL_CACHE_MISSES] = { .n = IST("ssl_cache_misses_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
152 [INF_COMPRESS_BPS_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
153 [INF_COMPRESS_BPS_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
154 [INF_COMPRESS_BPS_RATE_LIM] = { .n = IST("limit_http_comp"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
155 [INF_ZLIB_MEM_USAGE] = { .n = IST("current_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
156 [INF_MAX_ZLIB_MEM_USAGE] = { .n = IST("max_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
157 [INF_TASKS] = { .n = IST("current_tasks"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
158 [INF_RUN_QUEUE] = { .n = IST("current_run_queue"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
159 [INF_IDLE_PCT] = { .n = IST("idle_time_percent"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
160 //[INF_NODE] ignored
161 //[INF_DESCRIPTION] ignored
162 [INF_STOPPING] = { .n = IST("stopping"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
163 [INF_JOBS] = { .n = IST("jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
164 [INF_UNSTOPPABLE_JOBS] = { .n = IST("unstoppable_jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
165 [INF_LISTENERS] = { .n = IST("listeners"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
166 [INF_ACTIVE_PEERS] = { .n = IST("active_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
167 [INF_CONNECTED_PEERS] = { .n = IST("connected_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
168 [INF_DROPPED_LOGS] = { .n = IST("dropped_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
169 [INF_BUSY_POLLING] = { .n = IST("busy_polling_enabled"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
170 [INF_FAILED_RESOLUTIONS] = { .n = IST("failed_resolutions"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
171 [INF_TOTAL_BYTES_OUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
172 [INF_TOTAL_SPLICED_BYTES_OUT] = { .n = IST("spliced_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
173 [INF_BYTES_OUT_RATE] = { .n = IST("bytes_out_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
174 //[INF_DEBUG_COMMANDS_ISSUED] ignored
William Dauchy7741c332021-02-01 13:11:57 +0100175 [INF_CUM_LOG_MSGS] = { .n = IST("recv_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
Christopher Fauletf959d082019-02-07 15:38:42 +0100176};
177
Christopher Faulet37286a52021-01-20 15:20:53 +0100178/* frontend/backend/server fields */
179const struct promex_metric promex_st_metrics[ST_F_TOTAL_FIELDS] = {
180 //[ST_F_PXNAME] ignored
181 //[ST_F_SVNAME] ignored
182 [ST_F_QCUR] = { .n = IST("current_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
183 [ST_F_QMAX] = { .n = IST("max_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
184 [ST_F_SCUR] = { .n = IST("current_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
185 [ST_F_SMAX] = { .n = IST("max_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
186 [ST_F_SLIM] = { .n = IST("limit_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
187 [ST_F_STOT] = { .n = IST("sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
188 [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) },
189 [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) },
190 [ST_F_DREQ] = { .n = IST("requests_denied_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
191 [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) },
192 [ST_F_EREQ] = { .n = IST("request_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
193 [ST_F_ECON] = { .n = IST("connection_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
194 [ST_F_ERESP] = { .n = IST("response_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
195 [ST_F_WRETR] = { .n = IST("retry_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
196 [ST_F_WREDIS] = { .n = IST("redispatch_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
197 [ST_F_STATUS] = { .n = IST("status"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
198 [ST_F_WEIGHT] = { .n = IST("weight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
199 [ST_F_ACT] = { .n = IST("active_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
200 [ST_F_BCK] = { .n = IST("backup_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
201 [ST_F_CHKFAIL] = { .n = IST("check_failures_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_SRV_METRIC) },
202 [ST_F_CHKDOWN] = { .n = IST("check_up_down_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
203 [ST_F_LASTCHG] = { .n = IST("check_last_change_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
204 [ST_F_DOWNTIME] = { .n = IST("downtime_seconds_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
205 [ST_F_QLIMIT] = { .n = IST("queue_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
206 //[ST_F_PID] ignored
207 //[ST_F_IID] ignored
208 //[ST_F_SID] ignored
209 [ST_F_THROTTLE] = { .n = IST("current_throttle"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
210 [ST_F_LBTOT] = { .n = IST("loadbalanced_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
211 //[ST_F_TRACKED] ignored
212 //[ST_F_TYPE] ignored
213 //[ST_F_RATE] ignored
214 [ST_F_RATE_LIM] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
215 [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) },
216 [ST_F_CHECK_STATUS] = { .n = IST("check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
217 [ST_F_CHECK_CODE] = { .n = IST("check_code"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
218 [ST_F_CHECK_DURATION] = { .n = IST("check_duration_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
219 [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) },
220 [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) },
221 [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) },
222 [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) },
223 [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) },
224 [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) },
225 //[ST_F_HANAFAIL] ignored
226 //[ST_F_REQ_RATE] ignored
227 [ST_F_REQ_RATE_MAX] = { .n = IST("http_requests_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
228 [ST_F_REQ_TOT] = { .n = IST("http_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
229 [ST_F_CLI_ABRT] = { .n = IST("client_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
230 [ST_F_SRV_ABRT] = { .n = IST("server_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
231 [ST_F_COMP_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
232 [ST_F_COMP_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
233 [ST_F_COMP_BYP] = { .n = IST("http_comp_bytes_bypassed_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
234 [ST_F_COMP_RSP] = { .n = IST("http_comp_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
235 [ST_F_LASTSESS] = { .n = IST("last_session_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
236 //[ST_F_LAST_CHK] ignroed
237 //[ST_F_LAST_AGT] ignored
238 [ST_F_QTIME] = { .n = IST("queue_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
239 [ST_F_CTIME] = { .n = IST("connect_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
240 [ST_F_RTIME] = { .n = IST("response_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
241 [ST_F_TTIME] = { .n = IST("total_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
242 //[ST_F_AGENT_STATUS] ignored
243 //[ST_F_AGENT_CODE] ignored
244 //[ST_F_AGENT_DURATION] ignored
245 //[ST_F_CHECK_DESC] ignored
246 //[ST_F_AGENT_DESC] ignored
247 //[ST_F_CHECK_RISE] ignored
248 //[ST_F_CHECK_FALL] ignored
249 //[ST_F_CHECK_HEALTH] ignored
250 //[ST_F_AGENT_RISE] ignored
251 //[ST_F_AGENT_FALL] ignored
252 //[ST_F_AGENT_HEALTH] ignored
253 //[ST_F_ADDR] ignored
254 //[ST_F_COOKIE] ignored
255 //[ST_F_MODE] ignored
256 //[ST_F_ALGO] ignored
257 //[ST_F_CONN_RATE] ignored
258 [ST_F_CONN_RATE_MAX] = { .n = IST("connections_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
259 [ST_F_CONN_TOT] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
260 [ST_F_INTERCEPTED] = { .n = IST("intercepted_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
261 [ST_F_DCON] = { .n = IST("denied_connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
262 [ST_F_DSES] = { .n = IST("denied_sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
263 [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) },
264 [ST_F_CONNECT] = { .n = IST("connection_attempts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
265 [ST_F_REUSE] = { .n = IST("connection_reuses_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
266 [ST_F_CACHE_LOOKUPS] = { .n = IST("http_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
267 [ST_F_CACHE_HITS] = { .n = IST("http_cache_hits_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
268 [ST_F_SRV_ICUR] = { .n = IST("idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
269 [ST_F_SRV_ILIM] = { .n = IST("idle_connections_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
270 [ST_F_QT_MAX] = { .n = IST("max_queue_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
271 [ST_F_CT_MAX] = { .n = IST("max_connect_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
272 [ST_F_RT_MAX] = { .n = IST("max_response_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
273 [ST_F_TT_MAX] = { .n = IST("max_total_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
274 [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) },
275 [ST_F_IDLE_CONN_CUR] = { .n = IST("unsafe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
276 [ST_F_SAFE_CONN_CUR]= { .n = IST("safe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
277 [ST_F_USED_CONN_CUR] = { .n = IST("used_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
278 [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 +0100279 [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 +0100280};
281
William Dauchy82b2ce22021-02-01 13:11:55 +0100282/* Description of overriden stats fields */
Christopher Fauletf959d082019-02-07 15:38:42 +0100283const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
284 [ST_F_PXNAME] = IST("The proxy name."),
285 [ST_F_SVNAME] = IST("The service name (FRONTEND for frontend, BACKEND for backend, any name for server/listener)."),
286 [ST_F_QCUR] = IST("Current number of queued requests."),
287 [ST_F_QMAX] = IST("Maximum observed number of queued requests."),
288 [ST_F_SCUR] = IST("Current number of active sessions."),
289 [ST_F_SMAX] = IST("Maximum observed number of active sessions."),
290 [ST_F_SLIM] = IST("Configured session limit."),
291 [ST_F_STOT] = IST("Total number of sessions."),
292 [ST_F_BIN] = IST("Current total of incoming bytes."),
293 [ST_F_BOUT] = IST("Current total of outgoing bytes."),
294 [ST_F_DREQ] = IST("Total number of denied requests."),
295 [ST_F_DRESP] = IST("Total number of denied responses."),
296 [ST_F_EREQ] = IST("Total number of request errors."),
297 [ST_F_ECON] = IST("Total number of connection errors."),
298 [ST_F_ERESP] = IST("Total number of response errors."),
299 [ST_F_WRETR] = IST("Total number of retry warnings."),
300 [ST_F_WREDIS] = IST("Total number of redispatch warnings."),
William Dauchya1da7ba2021-02-01 13:11:52 +0100301 [ST_F_STATUS] = IST("Current status of the service, per state label value."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100302 [ST_F_WEIGHT] = IST("Service weight."),
303 [ST_F_ACT] = IST("Current number of active servers."),
304 [ST_F_BCK] = IST("Current number of backup servers."),
305 [ST_F_CHKFAIL] = IST("Total number of failed check (Only counts checks failed when the server is up)."),
306 [ST_F_CHKDOWN] = IST("Total number of UP->DOWN transitions."),
307 [ST_F_LASTCHG] = IST("Number of seconds since the last UP<->DOWN transition."),
308 [ST_F_DOWNTIME] = IST("Total downtime (in seconds) for the service."),
309 [ST_F_QLIMIT] = IST("Configured maxqueue for the server (0 meaning no limit)."),
310 [ST_F_PID] = IST("Process id (0 for first instance, 1 for second, ...)"),
311 [ST_F_IID] = IST("Unique proxy id."),
312 [ST_F_SID] = IST("Server id (unique inside a proxy)."),
313 [ST_F_THROTTLE] = IST("Current throttle percentage for the server, when slowstart is active, or no value if not in slowstart."),
314 [ST_F_LBTOT] = IST("Total number of times a service was selected, either for new sessions, or when redispatching."),
315 [ST_F_TRACKED] = IST("Id of proxy/server if tracking is enabled."),
316 [ST_F_TYPE] = IST("Service type (0=frontend, 1=backend, 2=server, 3=socket/listener)."),
317 [ST_F_RATE] = IST("Current number of sessions per second over last elapsed second."),
318 [ST_F_RATE_LIM] = IST("Configured limit on new sessions per second."),
319 [ST_F_RATE_MAX] = IST("Maximum observed number of sessions per second."),
William Dauchyde3c3262021-02-01 13:11:51 +0100320 [ST_F_CHECK_STATUS] = IST("Status of last health check, per state label value."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100321 [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."),
Christopher Faulet2711e512020-02-27 16:12:07 +0100322 [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100323 [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."),
324 [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."),
325 [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."),
326 [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100327 [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"),
328 [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"),
329 [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"),
330 [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100331};
332
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100333/* Specific labels for all ST_F_HRSP_* fields */
334const struct ist promex_hrsp_code[1 + ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = {
335 [ST_F_HRSP_1XX - ST_F_HRSP_1XX] = IST("1xx"),
336 [ST_F_HRSP_2XX - ST_F_HRSP_1XX] = IST("2xx"),
337 [ST_F_HRSP_3XX - ST_F_HRSP_1XX] = IST("3xx"),
338 [ST_F_HRSP_4XX - ST_F_HRSP_1XX] = IST("4xx"),
339 [ST_F_HRSP_5XX - ST_F_HRSP_1XX] = IST("5xx"),
340 [ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = IST("other"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100341};
342
William Dauchy54938212021-01-27 22:40:16 +0100343enum promex_front_state {
344 PROMEX_FRONT_STATE_DOWN = 0,
345 PROMEX_FRONT_STATE_UP,
346
347 PROMEX_FRONT_STATE_COUNT /* must be last */
348};
349
350const struct ist promex_front_st[PROMEX_FRONT_STATE_COUNT] = {
351 [PROMEX_FRONT_STATE_DOWN] = IST("DOWN"),
352 [PROMEX_FRONT_STATE_UP] = IST("UP"),
353};
354
355enum promex_back_state {
356 PROMEX_BACK_STATE_DOWN = 0,
357 PROMEX_BACK_STATE_UP,
358
359 PROMEX_BACK_STATE_COUNT /* must be last */
360};
361
362const struct ist promex_back_st[PROMEX_BACK_STATE_COUNT] = {
363 [PROMEX_BACK_STATE_DOWN] = IST("DOWN"),
364 [PROMEX_BACK_STATE_UP] = IST("UP"),
365};
366
367enum promex_srv_state {
368 PROMEX_SRV_STATE_DOWN = 0,
369 PROMEX_SRV_STATE_UP,
370 PROMEX_SRV_STATE_MAINT,
371 PROMEX_SRV_STATE_DRAIN,
372 PROMEX_SRV_STATE_NOLB,
373
374 PROMEX_SRV_STATE_COUNT /* must be last */
375};
376
377const struct ist promex_srv_st[PROMEX_SRV_STATE_COUNT] = {
378 [PROMEX_SRV_STATE_DOWN] = IST("DOWN"),
379 [PROMEX_SRV_STATE_UP] = IST("UP"),
380 [PROMEX_SRV_STATE_MAINT] = IST("MAINT"),
381 [PROMEX_SRV_STATE_DRAIN] = IST("DRAIN"),
382 [PROMEX_SRV_STATE_NOLB] = IST("NOLB"),
383};
384
385/* Return the server status. */
386enum promex_srv_state promex_srv_status(struct server *sv)
Christopher Fauletf959d082019-02-07 15:38:42 +0100387{
William Dauchy54938212021-01-27 22:40:16 +0100388 int state = PROMEX_SRV_STATE_DOWN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100389
Christopher Fauletf959d082019-02-07 15:38:42 +0100390 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Dauchy54938212021-01-27 22:40:16 +0100391 state = PROMEX_SRV_STATE_UP;
Christopher Fauletf959d082019-02-07 15:38:42 +0100392 if (sv->cur_admin & SRV_ADMF_DRAIN)
William Dauchy54938212021-01-27 22:40:16 +0100393 state = PROMEX_SRV_STATE_DRAIN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100394 }
Christopher Fauletd45d1052019-09-06 16:10:19 +0200395 else if (sv->cur_state == SRV_ST_STOPPING)
William Dauchy54938212021-01-27 22:40:16 +0100396 state = PROMEX_SRV_STATE_NOLB;
Christopher Fauletd45d1052019-09-06 16:10:19 +0200397
398 if (sv->cur_admin & SRV_ADMF_MAINT)
William Dauchy54938212021-01-27 22:40:16 +0100399 state = PROMEX_SRV_STATE_MAINT;
Christopher Fauletf959d082019-02-07 15:38:42 +0100400
401 return state;
402}
403
404/* Convert a field to its string representation and write it in <out>, followed
405 * by a newline, if there is enough space. non-numeric value are converted in
William Dauchy18a2c6e2021-01-22 21:09:47 +0100406 * "NaN" because Prometheus only support numerical values (but it is unexepceted
Christopher Fauletf959d082019-02-07 15:38:42 +0100407 * to process this kind of value). It returns 1 on success. Otherwise, it
408 * returns 0. The buffer's length must not exceed <max> value.
409 */
410static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
411{
412 int ret = 0;
413
414 switch (field_format(f, 0)) {
William Dauchy18a2c6e2021-01-22 21:09:47 +0100415 case FF_EMPTY: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100416 case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break;
417 case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break;
418 case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break;
419 case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200420 case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break;
William Dauchy18a2c6e2021-01-22 21:09:47 +0100421 case FF_STR: ret = chunk_strcat(out, "NaN\n"); break;
422 default: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100423 }
424 if (!ret || out->data > max)
425 return 0;
426 return 1;
427}
428
Christopher Fauletf959d082019-02-07 15:38:42 +0100429/* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
430 * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
431 */
432static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
Christopher Faulet37286a52021-01-20 15:20:53 +0100433 const struct promex_metric *metric, const struct ist name,
434 struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100435{
Christopher Faulet37286a52021-01-20 15:20:53 +0100436 struct ist type;
William Dauchy82b2ce22021-02-01 13:11:55 +0100437 struct ist desc;
Christopher Faulet37286a52021-01-20 15:20:53 +0100438
439 switch (metric->type) {
440 case PROMEX_MT_COUNTER:
441 type = ist("counter");
442 break;
443 default:
444 type = ist("gauge");
445 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100446
William Dauchya191b772021-01-15 22:41:39 +0100447 if (istcat(out, ist("# HELP "), max) == -1 ||
448 istcat(out, name, max) == -1 ||
449 istcat(out, ist(" "), max) == -1)
450 goto full;
451
William Dauchy82b2ce22021-02-01 13:11:55 +0100452 if (metric->flags & PROMEX_FL_INFO_METRIC)
453 desc = ist(info_fields[appctx->st2].desc);
454 else if (!isttest(promex_st_metric_desc[appctx->st2]))
455 desc = ist(stat_fields[appctx->st2].desc);
456 else
457 desc = promex_st_metric_desc[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100458
William Dauchy82b2ce22021-02-01 13:11:55 +0100459 if (istcat(out, desc, max) == -1 ||
460 istcat(out, ist("\n# TYPE "), max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100461 istcat(out, name, max) == -1 ||
462 istcat(out, ist(" "), max) == -1 ||
Christopher Faulet37286a52021-01-20 15:20:53 +0100463 istcat(out, type, max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100464 istcat(out, ist("\n"), max) == -1)
465 goto full;
466
467 return 1;
468
469 full:
470 return 0;
471}
472
473/* Dump the line for <metric>. It starts by the metric name followed by its
474 * labels (proxy name, server name...) between braces and finally its value. If
475 * not already done, the header lines are dumped first. It returns 1 on
476 * success. Otherwise if <out> length exceeds <max>, it returns 0.
477 */
Christopher Faulet37286a52021-01-20 15:20:53 +0100478static int promex_dump_metric(struct appctx *appctx, struct htx *htx, struct ist prefix,
William Dauchyc6464592021-01-27 22:40:17 +0100479 const struct promex_metric *metric, struct field *val,
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100480 struct promex_label *labels, struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100481{
482 struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
483 size_t len = out->len;
484
485 if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
486 return 0;
487
Christopher Faulet37286a52021-01-20 15:20:53 +0100488 /* Fill the metric name */
489 istcat(&name, prefix, PROMEX_MAX_NAME_LEN);
490 istcat(&name, metric->n, PROMEX_MAX_NAME_LEN);
491
492
Christopher Fauletf959d082019-02-07 15:38:42 +0100493 if ((appctx->ctx.stats.flags & PROMEX_FL_METRIC_HDR) &&
Christopher Faulet37286a52021-01-20 15:20:53 +0100494 !promex_dump_metric_header(appctx, htx, metric, name, out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100495 goto full;
496
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100497 if (istcat(out, name, max) == -1)
498 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100499
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100500 if (isttest(labels[0].name)) {
501 int i;
502
503 if (istcat(out, ist("{"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100504 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100505
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100506 for (i = 0; isttest(labels[i].name); i++) {
507 if (!isttest(labels[i].value))
508 continue;
509
510 if ((i && istcat(out, ist(","), max) == -1) ||
511 istcat(out, labels[i].name, max) == -1 ||
512 istcat(out, ist("=\""), max) == -1 ||
513 istcat(out, labels[i].value, max) == -1 ||
514 istcat(out, ist("\""), max) == -1)
515 goto full;
516 }
517
518 if (istcat(out, ist("}"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100519 goto full;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100520
Christopher Fauletf959d082019-02-07 15:38:42 +0100521 }
522
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100523 if (istcat(out, ist(" "), max) == -1)
524 goto full;
525
Christopher Fauletf959d082019-02-07 15:38:42 +0100526 trash.data = out->len;
Christopher Faulet37286a52021-01-20 15:20:53 +0100527 if (!promex_metric_to_str(&trash, val, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100528 goto full;
529 out->len = trash.data;
530
531 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
532 return 1;
533 full:
534 // Restore previous length
535 out->len = len;
536 return 0;
537
538}
539
540
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500541/* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100542 * 0 if <htx> is full and -1 in case of any error. */
543static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
544{
545 static struct ist prefix = IST("haproxy_process_");
Christopher Faulet37286a52021-01-20 15:20:53 +0100546 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100547 struct channel *chn = si_ic(appctx->owner);
548 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200549 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100550 int ret = 1;
551
William Dauchy5d9b8f32021-01-11 20:07:49 +0100552 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
553 return -1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100554
Christopher Faulet37286a52021-01-20 15:20:53 +0100555 for (; appctx->st2 < INF_TOTAL_FIELDS; appctx->st2++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100556 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
557
Christopher Faulet37286a52021-01-20 15:20:53 +0100558 if (!(promex_global_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
559 continue;
560
Christopher Fauletf959d082019-02-07 15:38:42 +0100561 switch (appctx->st2) {
William Dauchy5a982a72021-01-08 13:18:06 +0100562 case INF_BUILD_INFO:
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100563 labels[0].name = ist("version");
564 labels[0].value = ist(HAPROXY_VERSION);
Christopher Faulet37286a52021-01-20 15:20:53 +0100565 val = mkf_u32(FN_GAUGE, 1);
William Dauchy5a982a72021-01-08 13:18:06 +0100566 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100567
568 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100569 val = info[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100570 }
571
William Dauchyc6464592021-01-27 22:40:17 +0100572 if (!promex_dump_metric(appctx, htx, prefix, &promex_global_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100573 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100574 goto full;
575
Christopher Fauletf959d082019-02-07 15:38:42 +0100576 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100577 }
578
579 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200580 if (out.len) {
581 if (!htx_add_data_atonce(htx, out))
582 return -1; /* Unexpected and unrecoverable error */
583 channel_add_input(chn, out.len);
584 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100585 return ret;
586 full:
587 ret = 0;
588 goto end;
589}
590
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500591/* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100592 * 0 if <htx> is full and -1 in case of any error. */
593static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
594{
595 static struct ist prefix = IST("haproxy_frontend_");
596 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100597 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100598 struct channel *chn = si_ic(appctx->owner);
599 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200600 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchyb9577452021-01-17 18:27:46 +0100601 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100602 int ret = 1;
William Dauchyc6464592021-01-27 22:40:17 +0100603 enum promex_front_state state;
William Dauchyc6464592021-01-27 22:40:17 +0100604 int i;
Christopher Fauletf959d082019-02-07 15:38:42 +0100605
Christopher Faulet37286a52021-01-20 15:20:53 +0100606 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
607 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
608 continue;
609
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200610 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100611 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
612
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200613 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100614
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100615 labels[0].name = ist("proxy");
616 labels[0].value = ist2(px->id, strlen(px->id));
617
Christopher Fauletf959d082019-02-07 15:38:42 +0100618 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200619 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100620 goto next_px;
621
William Dauchyb9577452021-01-17 18:27:46 +0100622 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
623 return -1;
624
Christopher Fauletf959d082019-02-07 15:38:42 +0100625 switch (appctx->st2) {
626 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100627 state = !px->disabled;
628 for (i = 0; i < PROMEX_FRONT_STATE_COUNT; i++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100629 labels[1].name = ist("state");
630 labels[1].value = promex_front_st[i];
William Dauchyc6464592021-01-27 22:40:17 +0100631 val = mkf_u32(FO_STATUS, state == i);
William Dauchyc6464592021-01-27 22:40:17 +0100632 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100633 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100634 goto full;
635 }
636 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100637 case ST_F_REQ_RATE_MAX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100638 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100639 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100640 case ST_F_INTERCEPTED:
Christopher Fauletf959d082019-02-07 15:38:42 +0100641 case ST_F_CACHE_LOOKUPS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100642 case ST_F_CACHE_HITS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100643 case ST_F_COMP_IN:
Christopher Fauletf959d082019-02-07 15:38:42 +0100644 case ST_F_COMP_OUT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100645 case ST_F_COMP_BYP:
William Dauchyb9577452021-01-17 18:27:46 +0100646 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100647 if (px->mode != PR_MODE_HTTP)
648 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100649 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100650 break;
William Dauchyb9577452021-01-17 18:27:46 +0100651 case ST_F_HRSP_2XX:
652 case ST_F_HRSP_3XX:
653 case ST_F_HRSP_4XX:
654 case ST_F_HRSP_5XX:
655 case ST_F_HRSP_OTHER:
Christopher Fauletf959d082019-02-07 15:38:42 +0100656 if (px->mode != PR_MODE_HTTP)
657 goto next_px;
William Dauchyb9577452021-01-17 18:27:46 +0100658 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100659 labels[1].name = ist("code");
660 labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
661 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100662 break;
663
664 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100665 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100666 }
667
William Dauchyc6464592021-01-27 22:40:17 +0100668 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100669 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100670 goto full;
671 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200672 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100673 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100674 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200675 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100676 }
677
678 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200679 if (out.len) {
680 if (!htx_add_data_atonce(htx, out))
681 return -1; /* Unexpected and unrecoverable error */
682 channel_add_input(chn, out.len);
683 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100684 return ret;
685 full:
686 ret = 0;
687 goto end;
688}
689
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500690/* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100691 * 0 if <htx> is full and -1 in case of any error. */
692static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
693{
694 static struct ist prefix = IST("haproxy_backend_");
695 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100696 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100697 struct channel *chn = si_ic(appctx->owner);
698 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200699 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchy3c6f0062021-01-25 17:29:02 +0100700 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100701 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200702 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100703 enum promex_back_state state;
William Dauchyc6464592021-01-27 22:40:17 +0100704 int i;
Christopher Fauletf959d082019-02-07 15:38:42 +0100705
Christopher Faulet37286a52021-01-20 15:20:53 +0100706 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
707 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
708 continue;
709
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200710 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100711 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
712
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200713 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100714
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100715 labels[0].name = ist("proxy");
716 labels[0].value = ist2(px->id, strlen(px->id));
717
Christopher Fauletf959d082019-02-07 15:38:42 +0100718 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200719 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100720 goto next_px;
721
William Dauchy3c6f0062021-01-25 17:29:02 +0100722 if (!stats_fill_be_stats(px, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
723 return -1;
724
Christopher Fauletf959d082019-02-07 15:38:42 +0100725 switch (appctx->st2) {
726 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100727 state = ((px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0);
728 for (i = 0; i < PROMEX_BACK_STATE_COUNT; i++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100729 labels[1].name = ist("state");
730 labels[1].value = promex_back_st[i];
William Dauchyc6464592021-01-27 22:40:17 +0100731 val = mkf_u32(FO_STATUS, state == i);
William Dauchyc6464592021-01-27 22:40:17 +0100732 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100733 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100734 goto full;
735 }
736 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100737 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200738 secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100739 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100740 break;
741 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200742 secs = (double)swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100743 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100744 break;
745 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200746 secs = (double)swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100747 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100748 break;
749 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200750 secs = (double)swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100751 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100752 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100753 case ST_F_QT_MAX:
754 secs = (double)px->be_counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100755 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100756 break;
757 case ST_F_CT_MAX:
758 secs = (double)px->be_counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100759 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100760 break;
761 case ST_F_RT_MAX:
762 secs = (double)px->be_counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100763 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100764 break;
765 case ST_F_TT_MAX:
766 secs = (double)px->be_counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100767 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100768 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100769 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100770 case ST_F_HRSP_1XX:
William Dauchy3c6f0062021-01-25 17:29:02 +0100771 case ST_F_CACHE_LOOKUPS:
772 case ST_F_CACHE_HITS:
773 case ST_F_COMP_IN:
774 case ST_F_COMP_OUT:
775 case ST_F_COMP_BYP:
776 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100777 if (px->mode != PR_MODE_HTTP)
778 goto next_px;
William Dauchy3c6f0062021-01-25 17:29:02 +0100779 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100780 break;
781 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100782 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100783 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100784 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100785 case ST_F_HRSP_OTHER:
786 if (px->mode != PR_MODE_HTTP)
787 goto next_px;
788 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100789 labels[1].name = ist("code");
790 labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
William Dauchy3c6f0062021-01-25 17:29:02 +0100791 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100792 break;
793
794 default:
William Dauchy3c6f0062021-01-25 17:29:02 +0100795 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100796 }
797
William Dauchy3c6f0062021-01-25 17:29:02 +0100798 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100799 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100800 goto full;
801 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200802 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100803 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100804 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200805 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100806 }
807
808 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200809 if (out.len) {
810 if (!htx_add_data_atonce(htx, out))
811 return -1; /* Unexpected and unrecoverable error */
812 channel_add_input(chn, out.len);
813 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100814 return ret;
815 full:
816 ret = 0;
817 goto end;
818}
819
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500820/* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100821 * 0 if <htx> is full and -1 in case of any error. */
822static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
823{
824 static struct ist prefix = IST("haproxy_server_");
825 struct proxy *px;
826 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100827 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100828 struct channel *chn = si_ic(appctx->owner);
829 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200830 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchybde2bf62021-01-25 17:29:04 +0100831 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100832 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200833 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100834 enum promex_srv_state state;
William Dauchyde3c3262021-02-01 13:11:51 +0100835 const char *check_state;
William Dauchyc6464592021-01-27 22:40:17 +0100836 int i;
Christopher Fauletf959d082019-02-07 15:38:42 +0100837
Christopher Faulet37286a52021-01-20 15:20:53 +0100838 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
839 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
840 continue;
841
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200842 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100843 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
844
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200845 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100846
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100847 labels[0].name = ist("proxy");
848 labels[0].value = ist2(px->id, strlen(px->id));
849
Christopher Fauletf959d082019-02-07 15:38:42 +0100850 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200851 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100852 goto next_px;
853
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200854 while (appctx->ctx.stats.obj2) {
855 sv = appctx->ctx.stats.obj2;
Christopher Fauletf959d082019-02-07 15:38:42 +0100856
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100857 labels[1].name = ist("server");
858 labels[1].value = ist2(sv->id, strlen(sv->id));
859
William Dauchybde2bf62021-01-25 17:29:04 +0100860 if (!stats_fill_sv_stats(px, sv, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
861 return -1;
862
Christopher Fauleteba22942019-11-19 14:18:24 +0100863 if ((appctx->ctx.stats.flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
864 goto next_sv;
865
Christopher Fauletf959d082019-02-07 15:38:42 +0100866 switch (appctx->st2) {
867 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100868 state = promex_srv_status(sv);
869 for (i = 0; i < PROMEX_SRV_STATE_COUNT; i++) {
870 val = mkf_u32(FO_STATUS, state == i);
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100871 labels[2].name = ist("state");
872 labels[2].value = promex_srv_st[i];
William Dauchyc6464592021-01-27 22:40:17 +0100873 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100874 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100875 goto full;
876 }
877 goto next_sv;
Christopher Fauletf959d082019-02-07 15:38:42 +0100878 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200879 secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100880 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100881 break;
882 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200883 secs = (double)swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100884 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100885 break;
886 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200887 secs = (double)swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100888 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100889 break;
890 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200891 secs = (double)swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100892 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100893 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100894 case ST_F_QT_MAX:
895 secs = (double)sv->counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100896 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100897 break;
898 case ST_F_CT_MAX:
899 secs = (double)sv->counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100900 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100901 break;
902 case ST_F_RT_MAX:
903 secs = (double)sv->counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100904 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100905 break;
906 case ST_F_TT_MAX:
907 secs = (double)sv->counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100908 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100909 break;
Christopher Fauletcf403f32019-11-21 14:35:46 +0100910 case ST_F_CHECK_STATUS:
911 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
912 goto next_sv;
William Dauchyde3c3262021-02-01 13:11:51 +0100913
914 for (i = 0; i < HCHK_STATUS_SIZE; i++) {
915 if (get_check_status_result(i) < CHK_RES_FAILED)
916 continue;
917 val = mkf_u32(FO_STATUS, sv->check.status == i);
918 check_state = get_check_status_info(i);
919 labels[2].name = ist("state");
920 labels[2].value = ist2(check_state, strlen(check_state));
921 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
922 &val, labels, &out, max))
923 goto full;
924 }
925 goto next_sv;
Christopher Fauletcf403f32019-11-21 14:35:46 +0100926 case ST_F_CHECK_CODE:
927 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
928 goto next_sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100929 val = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code);
Christopher Fauletcf403f32019-11-21 14:35:46 +0100930 break;
Christopher Faulet2711e512020-02-27 16:12:07 +0100931 case ST_F_CHECK_DURATION:
932 if (sv->check.status < HCHK_STATUS_CHECKED)
933 goto next_sv;
934 secs = (double)sv->check.duration / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100935 val = mkf_flt(FN_DURATION, secs);
Christopher Faulet2711e512020-02-27 16:12:07 +0100936 break;
Marcin Deranek3c27dda2020-05-15 18:32:51 +0200937 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100938 case ST_F_HRSP_1XX:
939 if (px->mode != PR_MODE_HTTP)
940 goto next_px;
William Dauchybde2bf62021-01-25 17:29:04 +0100941 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100942 break;
943 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100944 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100945 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100946 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100947 case ST_F_HRSP_OTHER:
948 if (px->mode != PR_MODE_HTTP)
949 goto next_px;
950 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100951 labels[2].name = ist("code");
952 labels[2].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
William Dauchybde2bf62021-01-25 17:29:04 +0100953 val = stats[appctx->st2];
Christopher Fauletc55a6262020-07-10 15:39:39 +0200954 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100955
956 default:
William Dauchybde2bf62021-01-25 17:29:04 +0100957 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100958 }
959
William Dauchyc6464592021-01-27 22:40:17 +0100960 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100961 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100962 goto full;
Christopher Fauleteba22942019-11-19 14:18:24 +0100963 next_sv:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200964 appctx->ctx.stats.obj2 = sv->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100965 }
966
967 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200968 appctx->ctx.stats.obj1 = px->next;
969 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +0100970 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100971 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200972 appctx->ctx.stats.obj1 = proxies_list;
973 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +0100974 }
975
976
977 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200978 if (out.len) {
979 if (!htx_add_data_atonce(htx, out))
980 return -1; /* Unexpected and unrecoverable error */
981 channel_add_input(chn, out.len);
982 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100983 return ret;
984 full:
985 ret = 0;
986 goto end;
987}
988
989/* Dump all metrics (global, frontends, backends and servers) depending on the
990 * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200991 * -1 in case of any error.
992 * Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
993 * a pointer to the current server/listener. */
Christopher Fauletf959d082019-02-07 15:38:42 +0100994static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
995{
996 int ret;
997
998 switch (appctx->st1) {
999 case PROMEX_DUMPER_INIT:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001000 appctx->ctx.stats.obj1 = NULL;
1001 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001002 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001003 appctx->st2 = INF_NAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001004 appctx->st1 = PROMEX_DUMPER_GLOBAL;
1005 /* fall through */
1006
1007 case PROMEX_DUMPER_GLOBAL:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001008 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_GLOBAL) {
1009 ret = promex_dump_global_metrics(appctx, htx);
1010 if (ret <= 0) {
1011 if (ret == -1)
1012 goto error;
1013 goto full;
1014 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001015 }
1016
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001017 appctx->ctx.stats.obj1 = proxies_list;
1018 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001019 appctx->ctx.stats.flags &= ~PROMEX_FL_INFO_METRIC;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001020 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001021 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001022 appctx->st1 = PROMEX_DUMPER_FRONT;
1023 /* fall through */
1024
1025 case PROMEX_DUMPER_FRONT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001026 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_FRONT) {
1027 ret = promex_dump_front_metrics(appctx, htx);
1028 if (ret <= 0) {
1029 if (ret == -1)
1030 goto error;
1031 goto full;
1032 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001033 }
1034
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001035 appctx->ctx.stats.obj1 = proxies_list;
1036 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001037 appctx->ctx.stats.flags &= ~PROMEX_FL_FRONT_METRIC;
1038 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001039 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001040 appctx->st1 = PROMEX_DUMPER_BACK;
1041 /* fall through */
1042
1043 case PROMEX_DUMPER_BACK:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001044 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_BACK) {
1045 ret = promex_dump_back_metrics(appctx, htx);
1046 if (ret <= 0) {
1047 if (ret == -1)
1048 goto error;
1049 goto full;
1050 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001051 }
1052
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001053 appctx->ctx.stats.obj1 = proxies_list;
1054 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001055 appctx->ctx.stats.flags &= ~PROMEX_FL_BACK_METRIC;
1056 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001057 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001058 appctx->st1 = PROMEX_DUMPER_SRV;
1059 /* fall through */
1060
1061 case PROMEX_DUMPER_SRV:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001062 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_SERVER) {
1063 ret = promex_dump_srv_metrics(appctx, htx);
1064 if (ret <= 0) {
1065 if (ret == -1)
1066 goto error;
1067 goto full;
1068 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001069 }
1070
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001071 appctx->ctx.stats.obj1 = NULL;
1072 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001073 appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001074 appctx->st2 = 0;
1075 appctx->st1 = PROMEX_DUMPER_DONE;
1076 /* fall through */
1077
1078 case PROMEX_DUMPER_DONE:
1079 default:
1080 break;
1081 }
1082
1083 return 1;
1084
1085 full:
1086 si_rx_room_blk(si);
1087 return 0;
1088 error:
1089 /* unrecoverable error */
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001090 appctx->ctx.stats.obj1 = NULL;
1091 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletf959d082019-02-07 15:38:42 +01001092 appctx->ctx.stats.flags = 0;
1093 appctx->st2 = 0;
1094 appctx->st1 = PROMEX_DUMPER_DONE;
1095 return -1;
1096}
1097
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001098/* Parse the query string of request URI to filter the metrics. It returns 1 on
Christopher Faulet78407ce2019-11-18 14:47:08 +01001099 * success and -1 on error. */
1100static int promex_parse_uri(struct appctx *appctx, struct stream_interface *si)
1101{
1102 struct channel *req = si_oc(si);
1103 struct channel *res = si_ic(si);
1104 struct htx *req_htx, *res_htx;
1105 struct htx_sl *sl;
William Dauchyc65f6562019-11-26 12:56:26 +01001106 char *p, *key, *value;
1107 const char *end;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001108 struct buffer *err;
1109 int default_scopes = PROMEX_FL_SCOPE_ALL;
1110 int len;
1111
1112 /* Get the query-string */
1113 req_htx = htxbuf(&req->buf);
1114 sl = http_get_stline(req_htx);
1115 if (!sl)
1116 goto error;
1117 p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?');
1118 if (!p)
1119 goto end;
1120 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001121
William Dauchyc65f6562019-11-26 12:56:26 +01001122 /* copy the query-string */
1123 len = end - p;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001124 chunk_reset(&trash);
1125 memcpy(trash.area, p, len);
1126 trash.area[len] = 0;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001127 p = trash.area;
William Dauchyc65f6562019-11-26 12:56:26 +01001128 end = trash.area + len;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001129
1130 /* Parse the query-string */
William Dauchyc65f6562019-11-26 12:56:26 +01001131 while (p < end && *p && *p != '#') {
1132 value = NULL;
1133
1134 /* decode parameter name */
1135 key = p;
1136 while (p < end && *p != '=' && *p != '&' && *p != '#')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001137 ++p;
William Dauchyc65f6562019-11-26 12:56:26 +01001138 /* found a value */
1139 if (*p == '=') {
1140 *(p++) = 0;
1141 value = p;
1142 }
1143 else if (*p == '&')
1144 *(p++) = 0;
1145 else if (*p == '#')
1146 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001147 len = url_decode(key, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001148 if (len == -1)
1149 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001150
William Dauchyc65f6562019-11-26 12:56:26 +01001151 /* decode value */
1152 if (value) {
1153 while (p < end && *p != '=' && *p != '&' && *p != '#')
1154 ++p;
1155 if (*p == '=')
1156 goto error;
1157 if (*p == '&')
1158 *(p++) = 0;
1159 else if (*p == '#')
1160 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001161 len = url_decode(value, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001162 if (len == -1)
1163 goto error;
1164 }
1165
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001166 if (strcmp(key, "scope") == 0) {
William Dauchyc65f6562019-11-26 12:56:26 +01001167 default_scopes = 0; /* at least a scope defined, unset default scopes */
1168 if (!value)
1169 goto error;
1170 else if (*value == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001171 appctx->ctx.stats.flags &= ~PROMEX_FL_SCOPE_ALL;
William Dauchyc65f6562019-11-26 12:56:26 +01001172 else if (*value == '*')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001173 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_ALL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001174 else if (strcmp(value, "global") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001175 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_GLOBAL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001176 else if (strcmp(value, "server") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001177 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_SERVER;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001178 else if (strcmp(value, "backend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001179 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_BACK;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001180 else if (strcmp(value, "frontend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001181 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_FRONT;
1182 else
1183 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001184 }
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001185 else if (strcmp(key, "no-maint") == 0)
Christopher Fauleteba22942019-11-19 14:18:24 +01001186 appctx->ctx.stats.flags |= PROMEX_FL_NO_MAINT_SRV;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001187 }
1188
1189 end:
1190 appctx->ctx.stats.flags |= default_scopes;
1191 return 1;
1192
1193 error:
1194 err = &http_err_chunks[HTTP_ERR_400];
1195 channel_erase(res);
1196 res->buf.data = b_data(err);
1197 memcpy(res->buf.area, b_head(err), b_data(err));
1198 res_htx = htx_from_buf(&res->buf);
1199 channel_add_input(res, res_htx->data);
1200 appctx->st0 = PROMEX_ST_END;
1201 return -1;
1202}
1203
Christopher Fauletf959d082019-02-07 15:38:42 +01001204/* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is
1205 * full. */
1206static int promex_send_headers(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1207{
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001208 struct channel *chn = si_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +01001209 struct htx_sl *sl;
1210 unsigned int flags;
1211
1212 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);
1213 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
1214 if (!sl)
1215 goto full;
1216 sl->info.res.status = 200;
1217 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001218 !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) ||
1219 !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) ||
1220 !htx_add_endof(htx, HTX_BLK_EOH))
1221 goto full;
1222
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001223 channel_add_input(chn, htx->data);
Christopher Fauletf959d082019-02-07 15:38:42 +01001224 return 1;
1225 full:
1226 htx_reset(htx);
1227 si_rx_room_blk(si);
1228 return 0;
1229}
1230
1231/* The function returns 1 if the initialisation is complete, 0 if
1232 * an errors occurs and -1 if more data are required for initializing
1233 * the applet.
1234 */
1235static int promex_appctx_init(struct appctx *appctx, struct proxy *px, struct stream *strm)
1236{
1237 appctx->st0 = PROMEX_ST_INIT;
1238 return 1;
1239}
1240
1241/* The main I/O handler for the promex applet. */
1242static void promex_appctx_handle_io(struct appctx *appctx)
1243{
1244 struct stream_interface *si = appctx->owner;
1245 struct stream *s = si_strm(si);
1246 struct channel *req = si_oc(si);
1247 struct channel *res = si_ic(si);
1248 struct htx *req_htx, *res_htx;
1249 int ret;
1250
1251 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf959d082019-02-07 15:38:42 +01001252 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1253 goto out;
1254
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001255 /* Check if the input buffer is available. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001256 if (!b_size(&res->buf)) {
1257 si_rx_room_blk(si);
1258 goto out;
1259 }
1260
1261 switch (appctx->st0) {
1262 case PROMEX_ST_INIT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001263 ret = promex_parse_uri(appctx, si);
1264 if (ret <= 0) {
1265 if (ret == -1)
1266 goto error;
1267 goto out;
1268 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001269 appctx->st0 = PROMEX_ST_HEAD;
1270 appctx->st1 = PROMEX_DUMPER_INIT;
1271 /* fall through */
1272
1273 case PROMEX_ST_HEAD:
1274 if (!promex_send_headers(appctx, si, res_htx))
1275 goto out;
1276 appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP);
1277 /* fall through */
1278
1279 case PROMEX_ST_DUMP:
1280 ret = promex_dump_metrics(appctx, si, res_htx);
1281 if (ret <= 0) {
1282 if (ret == -1)
1283 goto error;
1284 goto out;
1285 }
1286 appctx->st0 = PROMEX_ST_DONE;
1287 /* fall through */
1288
1289 case PROMEX_ST_DONE:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001290 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
1291 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001292 appctx->st0 = PROMEX_ST_END;
1293 /* fall through */
Christopher Fauletf959d082019-02-07 15:38:42 +01001294
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001295 case PROMEX_ST_END:
1296 if (!(res->flags & CF_SHUTR)) {
1297 res->flags |= CF_READ_NULL;
1298 si_shutr(si);
1299 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001300 }
1301
Christopher Fauletf959d082019-02-07 15:38:42 +01001302 out:
1303 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001304
1305 /* eat the whole request */
1306 if (co_data(req)) {
1307 req_htx = htx_from_buf(&req->buf);
1308 co_htx_skip(req, req_htx, co_data(req));
1309 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001310 return;
1311
1312 error:
1313 res->flags |= CF_READ_NULL;
1314 si_shutr(si);
1315 si_shutw(si);
1316}
1317
1318struct applet promex_applet = {
1319 .obj_type = OBJ_TYPE_APPLET,
1320 .name = "<PROMEX>", /* used for logging */
1321 .init = promex_appctx_init,
1322 .fct = promex_appctx_handle_io,
1323};
1324
1325static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px,
1326 struct act_rule *rule, char **err)
1327{
1328 /* Prometheus exporter service is only available on "http-request" rulesets */
1329 if (rule->from != ACT_F_HTTP_REQ) {
1330 memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets");
1331 return ACT_RET_PRS_ERR;
1332 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001333
1334 /* Add applet pointer in the rule. */
1335 rule->applet = promex_applet;
1336
1337 return ACT_RET_PRS_OK;
1338}
1339static void promex_register_build_options(void)
1340{
1341 char *ptr = NULL;
1342
1343 memprintf(&ptr, "Built with the Prometheus exporter as a service");
1344 hap_register_build_opts(ptr, 1);
1345}
1346
1347
1348static struct action_kw_list service_actions = { ILH, {
1349 { "prometheus-exporter", service_parse_prometheus_exporter },
1350 { /* END */ }
1351}};
1352
1353INITCALL1(STG_REGISTER, service_keywords_register, &service_actions);
1354INITCALL0(STG_REGISTER, promex_register_build_options);