blob: aa72382d94d2c22885fe187e018792cbc76e75c6 [file] [log] [blame]
Christopher Fauletf959d082019-02-07 15:38:42 +01001/*
2 * Promex is a Prometheus exporter for HAProxy
3 *
4 * It is highly inspired by the official Prometheus exporter.
5 * See: https://github.com/prometheus/haproxy_exporter
6 *
7 * Copyright 2019 Christopher Faulet <cfaulet@haproxy.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 */
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020018#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020019#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020021#include <haproxy/compression.h>
Christopher Fauletc55a6262020-07-10 15:39:39 +020022#include <haproxy/dns.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020023#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020024#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020025#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020026#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020027#include <haproxy/htx.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020028#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020029#include <haproxy/listener.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020030#include <haproxy/log.h>
Willy Tarreau551271d2020-06-04 08:32:23 +020031#include <haproxy/pipe.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020032#include <haproxy/pool.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020033#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020034#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020035#include <haproxy/server.h>
Willy Tarreau209108d2020-06-04 20:30:20 +020036#include <haproxy/ssl_sock.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020037#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020038#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020039#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020040#include <haproxy/task.h>
William Dauchy5a982a72021-01-08 13:18:06 +010041#include <haproxy/version.h>
Christopher Fauletf959d082019-02-07 15:38:42 +010042
43/* Prometheus exporter applet states (appctx->st0) */
44enum {
45 PROMEX_ST_INIT = 0, /* initialized */
46 PROMEX_ST_HEAD, /* send headers before dump */
47 PROMEX_ST_DUMP, /* dumping stats */
48 PROMEX_ST_DONE, /* finished */
Christopher Faulet9744f7c2019-03-27 15:48:53 +010049 PROMEX_ST_END, /* treatment terminated */
Christopher Fauletf959d082019-02-07 15:38:42 +010050};
51
52/* Prometheus exporter dumper states (appctx->st1) */
53enum {
54 PROMEX_DUMPER_INIT = 0, /* initialized */
55 PROMEX_DUMPER_GLOBAL, /* dump metrics of globals */
56 PROMEX_DUMPER_FRONT, /* dump metrics of frontend proxies */
57 PROMEX_DUMPER_BACK, /* dump metrics of backend proxies */
58 PROMEX_DUMPER_LI, /* dump metrics of listeners */
59 PROMEX_DUMPER_SRV, /* dump metrics of servers */
60 PROMEX_DUMPER_DONE, /* finished */
61};
62
63/* Prometheus exporter flags (appctx->ctx.stats.flags) */
64#define PROMEX_FL_METRIC_HDR 0x00000001
65#define PROMEX_FL_INFO_METRIC 0x00000002
Christopher Fauletb713c4f2021-01-20 15:02:50 +010066#define PROMEX_FL_FRONT_METRIC 0x00000004
67#define PROMEX_FL_BACK_METRIC 0x00000008
68#define PROMEX_FL_SRV_METRIC 0x00000010
69#define PROMEX_FL_SCOPE_GLOBAL 0x00000020
70#define PROMEX_FL_SCOPE_FRONT 0x00000040
71#define PROMEX_FL_SCOPE_BACK 0x00000080
72#define PROMEX_FL_SCOPE_SERVER 0x00000100
73#define PROMEX_FL_NO_MAINT_SRV 0x00000200
Christopher Faulet78407ce2019-11-18 14:47:08 +010074
75#define PROMEX_FL_SCOPE_ALL (PROMEX_FL_SCOPE_GLOBAL|PROMEX_FL_SCOPE_FRONT|PROMEX_FL_SCOPE_BACK|PROMEX_FL_SCOPE_SERVER)
Christopher Fauletf959d082019-02-07 15:38:42 +010076
Christopher Faulet0312c0d2021-01-20 15:19:12 +010077/* Promtheus metric type (gauge or counter) */
78enum promex_mt_type {
79 PROMEX_MT_GAUGE = 1,
80 PROMEX_MT_COUNTER = 2,
81};
82
Christopher Fauletf959d082019-02-07 15:38:42 +010083/* The max length for metrics name. It is a hard limit but it should be
Ilya Shipitsince7b00f2020-03-23 22:28:40 +050084 * enough.
Christopher Fauletf959d082019-02-07 15:38:42 +010085 */
86#define PROMEX_MAX_NAME_LEN 128
87
88/* The expected max length for a metric dump, including its header lines. It is
89 * just a soft limit to avoid extra work. We don't try to dump a metric if less
90 * than this size is available in the HTX.
91 */
92#define PROMEX_MAX_METRIC_LENGTH 512
93
William Dauchy5a982a72021-01-08 13:18:06 +010094/* Some labels for build_info */
95#define PROMEX_VERSION_LABEL "version=\"" HAPROXY_VERSION "\""
96#define PROMEX_BUILDINFO_LABEL PROMEX_VERSION_LABEL
97
Christopher Faulet0312c0d2021-01-20 15:19:12 +010098/* Describe a prometheus metric */
99struct promex_metric {
100 const struct ist n; /* The metric name */
101 enum promex_mt_type type; /* The metric type (gauge or counter) */
102 unsigned int flags; /* PROMEX_FL_* flags */
103};
104
Christopher Faulet37286a52021-01-20 15:20:53 +0100105/* Global metrics */
106const struct promex_metric promex_global_metrics[INF_TOTAL_FIELDS] = {
107 //[INF_NAME] ignored
108 //[INF_VERSION], ignored
109 //[INF_RELEASE_DATE] ignored
110 [INF_BUILD_INFO] = { .n = IST("build_info"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
111 [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 },
118 [INF_MEMMAX_BYTES] = { .n = IST("max_memory_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
119 [INF_POOL_ALLOC_BYTES] = { .n = IST("pool_allocated_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
120 [INF_POOL_USED_BYTES] = { .n = IST("pool_used_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
121 [INF_POOL_FAILED] = { .n = IST("pool_failures_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
122 [INF_ULIMIT_N] = { .n = IST("max_fds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
123 [INF_MAXSOCK] = { .n = IST("max_sockets"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
124 [INF_MAXCONN] = { .n = IST("max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
125 [INF_HARD_MAXCONN] = { .n = IST("hard_max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
126 [INF_CURR_CONN] = { .n = IST("current_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
127 [INF_CUM_CONN] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
128 [INF_CUM_REQ] = { .n = IST("requests_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
129 [INF_MAX_SSL_CONNS] = { .n = IST("max_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
130 [INF_CURR_SSL_CONNS] = { .n = IST("current_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
131 [INF_CUM_SSL_CONNS] = { .n = IST("ssl_connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
132 [INF_MAXPIPES] = { .n = IST("max_pipes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
133 [INF_PIPES_USED] = { .n = IST("pipes_used_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
134 [INF_PIPES_FREE] = { .n = IST("pipes_free_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
135 [INF_CONN_RATE] = { .n = IST("current_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
136 [INF_CONN_RATE_LIMIT] = { .n = IST("limit_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
137 [INF_MAX_CONN_RATE] = { .n = IST("max_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
138 [INF_SESS_RATE] = { .n = IST("current_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
139 [INF_SESS_RATE_LIMIT] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
140 [INF_MAX_SESS_RATE] = { .n = IST("max_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
141 [INF_SSL_RATE] = { .n = IST("current_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
142 [INF_SSL_RATE_LIMIT] = { .n = IST("limit_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
143 [INF_MAX_SSL_RATE] = { .n = IST("max_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
144 [INF_SSL_FRONTEND_KEY_RATE] = { .n = IST("current_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
145 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .n = IST("max_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
146 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .n = IST("frontend_ssl_reuse"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
147 [INF_SSL_BACKEND_KEY_RATE] = { .n = IST("current_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
148 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .n = IST("max_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
149 [INF_SSL_CACHE_LOOKUPS] = { .n = IST("ssl_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
150 [INF_SSL_CACHE_MISSES] = { .n = IST("ssl_cache_misses_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
151 [INF_COMPRESS_BPS_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
152 [INF_COMPRESS_BPS_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
153 [INF_COMPRESS_BPS_RATE_LIM] = { .n = IST("limit_http_comp"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
154 [INF_ZLIB_MEM_USAGE] = { .n = IST("current_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
155 [INF_MAX_ZLIB_MEM_USAGE] = { .n = IST("max_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
156 [INF_TASKS] = { .n = IST("current_tasks"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
157 [INF_RUN_QUEUE] = { .n = IST("current_run_queue"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
158 [INF_IDLE_PCT] = { .n = IST("idle_time_percent"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
159 //[INF_NODE] ignored
160 //[INF_DESCRIPTION] ignored
161 [INF_STOPPING] = { .n = IST("stopping"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
162 [INF_JOBS] = { .n = IST("jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
163 [INF_UNSTOPPABLE_JOBS] = { .n = IST("unstoppable_jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
164 [INF_LISTENERS] = { .n = IST("listeners"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
165 [INF_ACTIVE_PEERS] = { .n = IST("active_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
166 [INF_CONNECTED_PEERS] = { .n = IST("connected_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
167 [INF_DROPPED_LOGS] = { .n = IST("dropped_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
168 [INF_BUSY_POLLING] = { .n = IST("busy_polling_enabled"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
169 [INF_FAILED_RESOLUTIONS] = { .n = IST("failed_resolutions"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
170 [INF_TOTAL_BYTES_OUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
171 [INF_TOTAL_SPLICED_BYTES_OUT] = { .n = IST("spliced_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
172 [INF_BYTES_OUT_RATE] = { .n = IST("bytes_out_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
173 //[INF_DEBUG_COMMANDS_ISSUED] ignored
Christopher Fauletf959d082019-02-07 15:38:42 +0100174};
175
Christopher Faulet37286a52021-01-20 15:20:53 +0100176/* frontend/backend/server fields */
177const struct promex_metric promex_st_metrics[ST_F_TOTAL_FIELDS] = {
178 //[ST_F_PXNAME] ignored
179 //[ST_F_SVNAME] ignored
180 [ST_F_QCUR] = { .n = IST("current_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
181 [ST_F_QMAX] = { .n = IST("max_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
182 [ST_F_SCUR] = { .n = IST("current_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
183 [ST_F_SMAX] = { .n = IST("max_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
184 [ST_F_SLIM] = { .n = IST("limit_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
185 [ST_F_STOT] = { .n = IST("sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
186 [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) },
187 [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) },
188 [ST_F_DREQ] = { .n = IST("requests_denied_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
189 [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) },
190 [ST_F_EREQ] = { .n = IST("request_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
191 [ST_F_ECON] = { .n = IST("connection_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
192 [ST_F_ERESP] = { .n = IST("response_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
193 [ST_F_WRETR] = { .n = IST("retry_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
194 [ST_F_WREDIS] = { .n = IST("redispatch_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
195 [ST_F_STATUS] = { .n = IST("status"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
196 [ST_F_WEIGHT] = { .n = IST("weight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
197 [ST_F_ACT] = { .n = IST("active_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
198 [ST_F_BCK] = { .n = IST("backup_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
199 [ST_F_CHKFAIL] = { .n = IST("check_failures_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_SRV_METRIC) },
200 [ST_F_CHKDOWN] = { .n = IST("check_up_down_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
201 [ST_F_LASTCHG] = { .n = IST("check_last_change_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
202 [ST_F_DOWNTIME] = { .n = IST("downtime_seconds_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
203 [ST_F_QLIMIT] = { .n = IST("queue_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
204 //[ST_F_PID] ignored
205 //[ST_F_IID] ignored
206 //[ST_F_SID] ignored
207 [ST_F_THROTTLE] = { .n = IST("current_throttle"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
208 [ST_F_LBTOT] = { .n = IST("loadbalanced_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
209 //[ST_F_TRACKED] ignored
210 //[ST_F_TYPE] ignored
211 //[ST_F_RATE] ignored
212 [ST_F_RATE_LIM] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
213 [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) },
214 [ST_F_CHECK_STATUS] = { .n = IST("check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
215 [ST_F_CHECK_CODE] = { .n = IST("check_code"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
216 [ST_F_CHECK_DURATION] = { .n = IST("check_duration_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
217 [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) },
218 [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) },
219 [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) },
220 [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) },
221 [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) },
222 [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) },
223 //[ST_F_HANAFAIL] ignored
224 //[ST_F_REQ_RATE] ignored
225 [ST_F_REQ_RATE_MAX] = { .n = IST("http_requests_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
226 [ST_F_REQ_TOT] = { .n = IST("http_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
227 [ST_F_CLI_ABRT] = { .n = IST("client_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
228 [ST_F_SRV_ABRT] = { .n = IST("server_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
229 [ST_F_COMP_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
230 [ST_F_COMP_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
231 [ST_F_COMP_BYP] = { .n = IST("http_comp_bytes_bypassed_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
232 [ST_F_COMP_RSP] = { .n = IST("http_comp_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
233 [ST_F_LASTSESS] = { .n = IST("last_session_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
234 //[ST_F_LAST_CHK] ignroed
235 //[ST_F_LAST_AGT] ignored
236 [ST_F_QTIME] = { .n = IST("queue_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
237 [ST_F_CTIME] = { .n = IST("connect_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
238 [ST_F_RTIME] = { .n = IST("response_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
239 [ST_F_TTIME] = { .n = IST("total_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
240 //[ST_F_AGENT_STATUS] ignored
241 //[ST_F_AGENT_CODE] ignored
242 //[ST_F_AGENT_DURATION] ignored
243 //[ST_F_CHECK_DESC] ignored
244 //[ST_F_AGENT_DESC] ignored
245 //[ST_F_CHECK_RISE] ignored
246 //[ST_F_CHECK_FALL] ignored
247 //[ST_F_CHECK_HEALTH] ignored
248 //[ST_F_AGENT_RISE] ignored
249 //[ST_F_AGENT_FALL] ignored
250 //[ST_F_AGENT_HEALTH] ignored
251 //[ST_F_ADDR] ignored
252 //[ST_F_COOKIE] ignored
253 //[ST_F_MODE] ignored
254 //[ST_F_ALGO] ignored
255 //[ST_F_CONN_RATE] ignored
256 [ST_F_CONN_RATE_MAX] = { .n = IST("connections_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
257 [ST_F_CONN_TOT] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
258 [ST_F_INTERCEPTED] = { .n = IST("intercepted_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
259 [ST_F_DCON] = { .n = IST("denied_connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
260 [ST_F_DSES] = { .n = IST("denied_sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
261 [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) },
262 [ST_F_CONNECT] = { .n = IST("connection_attempts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
263 [ST_F_REUSE] = { .n = IST("connection_reuses_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
264 [ST_F_CACHE_LOOKUPS] = { .n = IST("http_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
265 [ST_F_CACHE_HITS] = { .n = IST("http_cache_hits_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
266 [ST_F_SRV_ICUR] = { .n = IST("idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
267 [ST_F_SRV_ILIM] = { .n = IST("idle_connections_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
268 [ST_F_QT_MAX] = { .n = IST("max_queue_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
269 [ST_F_CT_MAX] = { .n = IST("max_connect_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
270 [ST_F_RT_MAX] = { .n = IST("max_response_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
271 [ST_F_TT_MAX] = { .n = IST("max_total_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
272 [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) },
273 [ST_F_IDLE_CONN_CUR] = { .n = IST("unsafe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
274 [ST_F_SAFE_CONN_CUR]= { .n = IST("safe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
275 [ST_F_USED_CONN_CUR] = { .n = IST("used_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
276 [ST_F_NEED_CONN_EST] = { .n = IST("need_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
Christopher Fauletf959d082019-02-07 15:38:42 +0100277};
278
Christopher Fauletf959d082019-02-07 15:38:42 +0100279/* Description of all stats fields */
280const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
281 [ST_F_PXNAME] = IST("The proxy name."),
282 [ST_F_SVNAME] = IST("The service name (FRONTEND for frontend, BACKEND for backend, any name for server/listener)."),
283 [ST_F_QCUR] = IST("Current number of queued requests."),
284 [ST_F_QMAX] = IST("Maximum observed number of queued requests."),
285 [ST_F_SCUR] = IST("Current number of active sessions."),
286 [ST_F_SMAX] = IST("Maximum observed number of active sessions."),
287 [ST_F_SLIM] = IST("Configured session limit."),
288 [ST_F_STOT] = IST("Total number of sessions."),
289 [ST_F_BIN] = IST("Current total of incoming bytes."),
290 [ST_F_BOUT] = IST("Current total of outgoing bytes."),
291 [ST_F_DREQ] = IST("Total number of denied requests."),
292 [ST_F_DRESP] = IST("Total number of denied responses."),
293 [ST_F_EREQ] = IST("Total number of request errors."),
294 [ST_F_ECON] = IST("Total number of connection errors."),
295 [ST_F_ERESP] = IST("Total number of response errors."),
296 [ST_F_WRETR] = IST("Total number of retry warnings."),
297 [ST_F_WREDIS] = IST("Total number of redispatch warnings."),
Willy Tarreau6b3bf732020-09-24 07:35:46 +0200298 [ST_F_STATUS] = IST("Current status of the service (frontend: 0=STOP, 1=UP - backend: 0=DOWN, 1=UP - server: 0=DOWN, 1=UP, 2=MAINT, 3=DRAIN, 4=NOLB)."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100299 [ST_F_WEIGHT] = IST("Service weight."),
300 [ST_F_ACT] = IST("Current number of active servers."),
301 [ST_F_BCK] = IST("Current number of backup servers."),
302 [ST_F_CHKFAIL] = IST("Total number of failed check (Only counts checks failed when the server is up)."),
303 [ST_F_CHKDOWN] = IST("Total number of UP->DOWN transitions."),
304 [ST_F_LASTCHG] = IST("Number of seconds since the last UP<->DOWN transition."),
305 [ST_F_DOWNTIME] = IST("Total downtime (in seconds) for the service."),
306 [ST_F_QLIMIT] = IST("Configured maxqueue for the server (0 meaning no limit)."),
307 [ST_F_PID] = IST("Process id (0 for first instance, 1 for second, ...)"),
308 [ST_F_IID] = IST("Unique proxy id."),
309 [ST_F_SID] = IST("Server id (unique inside a proxy)."),
310 [ST_F_THROTTLE] = IST("Current throttle percentage for the server, when slowstart is active, or no value if not in slowstart."),
311 [ST_F_LBTOT] = IST("Total number of times a service was selected, either for new sessions, or when redispatching."),
312 [ST_F_TRACKED] = IST("Id of proxy/server if tracking is enabled."),
313 [ST_F_TYPE] = IST("Service type (0=frontend, 1=backend, 2=server, 3=socket/listener)."),
314 [ST_F_RATE] = IST("Current number of sessions per second over last elapsed second."),
315 [ST_F_RATE_LIM] = IST("Configured limit on new sessions per second."),
316 [ST_F_RATE_MAX] = IST("Maximum observed number of sessions per second."),
Christopher Fauletcf403f32019-11-21 14:35:46 +0100317 [ST_F_CHECK_STATUS] = IST("Status of last health check (HCHK_STATUS_* values)."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100318 [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."),
Christopher Faulet2711e512020-02-27 16:12:07 +0100319 [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100320 [ST_F_HRSP_1XX] = IST("Total number of HTTP responses."),
321 [ST_F_HRSP_2XX] = IST("Total number of HTTP responses."),
322 [ST_F_HRSP_3XX] = IST("Total number of HTTP responses."),
323 [ST_F_HRSP_4XX] = IST("Total number of HTTP responses."),
324 [ST_F_HRSP_5XX] = IST("Total number of HTTP responses."),
325 [ST_F_HRSP_OTHER] = IST("Total number of HTTP responses."),
326 [ST_F_HANAFAIL] = IST("Total number of failed health checks."),
327 [ST_F_REQ_RATE] = IST("Current number of HTTP requests per second over last elapsed second."),
328 [ST_F_REQ_RATE_MAX] = IST("Maximum observed number of HTTP requests per second."),
329 [ST_F_REQ_TOT] = IST("Total number of HTTP requests received."),
330 [ST_F_CLI_ABRT] = IST("Total number of data transfers aborted by the client."),
331 [ST_F_SRV_ABRT] = IST("Total number of data transfers aborted by the server."),
332 [ST_F_COMP_IN] = IST("Total number of HTTP response bytes fed to the compressor."),
333 [ST_F_COMP_OUT] = IST("Total number of HTTP response bytes emitted by the compressor."),
334 [ST_F_COMP_BYP] = IST("Total number of bytes that bypassed the HTTP compressor (CPU/BW limit)."),
335 [ST_F_COMP_RSP] = IST("Total number of HTTP responses that were compressed."),
336 [ST_F_LASTSESS] = IST("Number of seconds since last session assigned to server/backend."),
337 [ST_F_LAST_CHK] = IST("Last health check contents or textual error"),
338 [ST_F_LAST_AGT] = IST("Last agent check contents or textual error"),
339 [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."),
340 [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."),
341 [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."),
342 [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."),
343 [ST_F_AGENT_STATUS] = IST("Status of last agent check."),
344 [ST_F_AGENT_CODE] = IST("Numeric code reported by agent if any (unused for now)."),
345 [ST_F_AGENT_DURATION] = IST("Time in ms taken to finish last agent check."),
346 [ST_F_CHECK_DESC] = IST("Short human-readable description of the last health status."),
347 [ST_F_AGENT_DESC] = IST("Short human-readable description of the last agent status."),
348 [ST_F_CHECK_RISE] = IST("Server's \"rise\" parameter used by health checks"),
349 [ST_F_CHECK_FALL] = IST("Server's \"fall\" parameter used by health checks"),
350 [ST_F_CHECK_HEALTH] = IST("Server's health check value between 0 and rise+fall-1"),
351 [ST_F_AGENT_RISE] = IST("Agent's \"rise\" parameter, normally 1."),
352 [ST_F_AGENT_FALL] = IST("Agent's \"fall\" parameter, normally 1."),
353 [ST_F_AGENT_HEALTH] = IST("Agent's health parameter, between 0 and rise+fall-1"),
354 [ST_F_ADDR] = IST("address:port or \"unix\". IPv6 has brackets around the address."),
355 [ST_F_COOKIE] = IST("Server's cookie value or backend's cookie name."),
356 [ST_F_MODE] = IST("Proxy mode (tcp, http, health, unknown)."),
357 [ST_F_ALGO] = IST("Load balancing algorithm."),
358 [ST_F_CONN_RATE] = IST("Current number of connections per second over the last elapsed second."),
359 [ST_F_CONN_RATE_MAX] = IST("Maximum observed number of connections per second."),
360 [ST_F_CONN_TOT] = IST("Total number of connections."),
361 [ST_F_INTERCEPTED] = IST("Total number of intercepted HTTP requests."),
362 [ST_F_DCON] = IST("Total number of requests denied by \"tcp-request connection\" rules."),
363 [ST_F_DSES] = IST("Total number of requests denied by \"tcp-request session\" rules."),
364 [ST_F_WREW] = IST("Total number of failed header rewriting warnings."),
365 [ST_F_CONNECT] = IST("Total number of connection establishment attempts."),
366 [ST_F_REUSE] = IST("Total number of connection reuses."),
367 [ST_F_CACHE_LOOKUPS] = IST("Total number of HTTP cache lookups."),
368 [ST_F_CACHE_HITS] = IST("Total number of HTTP cache hits."),
Christopher Faulet20ab80c2019-11-08 15:24:32 +0100369 [ST_F_SRV_ICUR] = IST("Current number of idle connections available for reuse"),
370 [ST_F_SRV_ILIM] = IST("Limit on the number of available idle connections"),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100371 [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"),
372 [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"),
373 [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"),
374 [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100375 [ST_F_EINT] = IST("Total number of internal errors."),
Christopher Fauletc55a6262020-07-10 15:39:39 +0200376 [ST_F_IDLE_CONN_CUR] = IST("Current number of unsafe idle connections."),
377 [ST_F_SAFE_CONN_CUR] = IST("Current number of safe idle connections."),
378 [ST_F_USED_CONN_CUR] = IST("Current number of connections in use."),
379 [ST_F_NEED_CONN_EST] = IST("Estimated needed number of connections."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100380};
381
382/* Specific labels for all info fields. Empty by default. */
383const struct ist promex_inf_metric_labels[INF_TOTAL_FIELDS] = {
Christopher Faulet0175b1f2021-01-20 15:28:22 +0100384 [INF_BUILD_INFO] = IST(PROMEX_BUILDINFO_LABEL),
Christopher Fauletf959d082019-02-07 15:38:42 +0100385};
386
387/* Specific labels for all stats fields. Empty by default. */
388const struct ist promex_st_metric_labels[ST_F_TOTAL_FIELDS] = {
Christopher Faulet0175b1f2021-01-20 15:28:22 +0100389 [ST_F_HRSP_1XX] = IST("code=\"1xx\""),
390 [ST_F_HRSP_2XX] = IST("code=\"2xx\""),
391 [ST_F_HRSP_3XX] = IST("code=\"3xx\""),
392 [ST_F_HRSP_4XX] = IST("code=\"4xx\""),
393 [ST_F_HRSP_5XX] = IST("code=\"5xx\""),
394 [ST_F_HRSP_OTHER] = IST("code=\"other\""),
Christopher Fauletf959d082019-02-07 15:38:42 +0100395};
396
Christopher Fauletd45d1052019-09-06 16:10:19 +0200397/* Return the server status: 0=DOWN, 1=UP, 2=MAINT, 3=DRAIN, 4=NOLB. */
Christopher Fauletf959d082019-02-07 15:38:42 +0100398static int promex_srv_status(struct server *sv)
399{
Christopher Fauletf959d082019-02-07 15:38:42 +0100400 int state = 0;
401
Christopher Fauletf959d082019-02-07 15:38:42 +0100402 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
403 state = 1;
404 if (sv->cur_admin & SRV_ADMF_DRAIN)
Christopher Fauletd45d1052019-09-06 16:10:19 +0200405 state = 3;
Christopher Fauletf959d082019-02-07 15:38:42 +0100406 }
Christopher Fauletd45d1052019-09-06 16:10:19 +0200407 else if (sv->cur_state == SRV_ST_STOPPING)
408 state = 4;
409
410 if (sv->cur_admin & SRV_ADMF_MAINT)
411 state = 2;
Christopher Fauletf959d082019-02-07 15:38:42 +0100412
413 return state;
414}
415
416/* Convert a field to its string representation and write it in <out>, followed
417 * by a newline, if there is enough space. non-numeric value are converted in
418 * "Nan" because Prometheus only support numerical values (but it is unexepceted
419 * to process this kind of value). It returns 1 on success. Otherwise, it
420 * returns 0. The buffer's length must not exceed <max> value.
421 */
422static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
423{
424 int ret = 0;
425
426 switch (field_format(f, 0)) {
427 case FF_EMPTY: ret = chunk_strcat(out, "Nan\n"); break;
428 case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break;
429 case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break;
430 case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break;
431 case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200432 case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100433 case FF_STR: ret = chunk_strcat(out, "Nan\n"); break;
434 default: ret = chunk_strcat(out, "Nan\n"); break;
435 }
436 if (!ret || out->data > max)
437 return 0;
438 return 1;
439}
440
Christopher Fauletf959d082019-02-07 15:38:42 +0100441/* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
442 * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
443 */
444static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
Christopher Faulet37286a52021-01-20 15:20:53 +0100445 const struct promex_metric *metric, const struct ist name,
446 struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100447{
Christopher Faulet37286a52021-01-20 15:20:53 +0100448 struct ist type;
449
450 switch (metric->type) {
451 case PROMEX_MT_COUNTER:
452 type = ist("counter");
453 break;
454 default:
455 type = ist("gauge");
456 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100457
William Dauchya191b772021-01-15 22:41:39 +0100458 if (istcat(out, ist("# HELP "), max) == -1 ||
459 istcat(out, name, max) == -1 ||
460 istcat(out, ist(" "), max) == -1)
461 goto full;
462
Christopher Faulet37286a52021-01-20 15:20:53 +0100463 if (metric->flags & PROMEX_FL_INFO_METRIC) {
William Dauchya191b772021-01-15 22:41:39 +0100464 if (istcat(out, ist(info_fields[appctx->st2].desc), max) == -1)
465 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100466 }
467 else {
Christopher Faulet37286a52021-01-20 15:20:53 +0100468 if (istcat(out, promex_st_metric_desc[appctx->st2], max) == -1)
William Dauchya191b772021-01-15 22:41:39 +0100469 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100470 }
471
William Dauchya191b772021-01-15 22:41:39 +0100472 if (istcat(out, ist("\n# TYPE "), max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100473 istcat(out, name, max) == -1 ||
474 istcat(out, ist(" "), max) == -1 ||
Christopher Faulet37286a52021-01-20 15:20:53 +0100475 istcat(out, type, max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100476 istcat(out, ist("\n"), max) == -1)
477 goto full;
478
479 return 1;
480
481 full:
482 return 0;
483}
484
485/* Dump the line for <metric>. It starts by the metric name followed by its
486 * labels (proxy name, server name...) between braces and finally its value. If
487 * not already done, the header lines are dumped first. It returns 1 on
488 * success. Otherwise if <out> length exceeds <max>, it returns 0.
489 */
Christopher Faulet37286a52021-01-20 15:20:53 +0100490static int promex_dump_metric(struct appctx *appctx, struct htx *htx, struct ist prefix,
491 const struct promex_metric *metric, struct field *val,
Christopher Fauletf959d082019-02-07 15:38:42 +0100492 struct ist *out, size_t max)
493{
494 struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
495 size_t len = out->len;
496
497 if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
498 return 0;
499
Christopher Faulet37286a52021-01-20 15:20:53 +0100500 /* Fill the metric name */
501 istcat(&name, prefix, PROMEX_MAX_NAME_LEN);
502 istcat(&name, metric->n, PROMEX_MAX_NAME_LEN);
503
504
Christopher Fauletf959d082019-02-07 15:38:42 +0100505 if ((appctx->ctx.stats.flags & PROMEX_FL_METRIC_HDR) &&
Christopher Faulet37286a52021-01-20 15:20:53 +0100506 !promex_dump_metric_header(appctx, htx, metric, name, out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100507 goto full;
508
509 if (appctx->ctx.stats.flags & PROMEX_FL_INFO_METRIC) {
510 const struct ist label = promex_inf_metric_labels[appctx->st2];
511
512 if (istcat(out, name, max) == -1 ||
513 (label.len && istcat(out, ist("{"), max) == -1) ||
514 (label.len && istcat(out, label, max) == -1) ||
515 (label.len && istcat(out, ist("}"), max) == -1) ||
516 istcat(out, ist(" "), max) == -1)
517 goto full;
518 }
519 else {
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200520 struct proxy *px = appctx->ctx.stats.obj1;
521 struct server *srv = appctx->ctx.stats.obj2;
Christopher Fauletf959d082019-02-07 15:38:42 +0100522 const struct ist label = promex_st_metric_labels[appctx->st2];
523
524 if (istcat(out, name, max) == -1 ||
525 istcat(out, ist("{proxy=\""), max) == -1 ||
526 istcat(out, ist2(px->id, strlen(px->id)), max) == -1 ||
527 istcat(out, ist("\""), max) == -1 ||
528 (srv && istcat(out, ist(",server=\""), max) == -1) ||
529 (srv && istcat(out, ist2(srv->id, strlen(srv->id)), max) == -1) ||
530 (srv && istcat(out, ist("\""), max) == -1) ||
531 (label.len && istcat(out, ist(","), max) == -1) ||
532 (label.len && istcat(out, label, max) == -1) ||
533 istcat(out, ist("} "), max) == -1)
534 goto full;
535 }
536
537 trash.data = out->len;
Christopher Faulet37286a52021-01-20 15:20:53 +0100538 if (!promex_metric_to_str(&trash, val, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100539 goto full;
540 out->len = trash.data;
541
542 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
543 return 1;
544 full:
545 // Restore previous length
546 out->len = len;
547 return 0;
548
549}
550
551
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500552/* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100553 * 0 if <htx> is full and -1 in case of any error. */
554static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
555{
556 static struct ist prefix = IST("haproxy_process_");
Christopher Faulet37286a52021-01-20 15:20:53 +0100557 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100558 struct channel *chn = si_ic(appctx->owner);
559 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200560 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100561 int ret = 1;
562
William Dauchy5d9b8f32021-01-11 20:07:49 +0100563 if (!stats_fill_info(info, INF_TOTAL_FIELDS))
564 return -1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100565
Christopher Faulet37286a52021-01-20 15:20:53 +0100566 for (; appctx->st2 < INF_TOTAL_FIELDS; appctx->st2++) {
567 if (!(promex_global_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
568 continue;
569
Christopher Fauletf959d082019-02-07 15:38:42 +0100570 switch (appctx->st2) {
William Dauchy5a982a72021-01-08 13:18:06 +0100571 case INF_BUILD_INFO:
Christopher Faulet37286a52021-01-20 15:20:53 +0100572 val = mkf_u32(FN_GAUGE, 1);
William Dauchy5a982a72021-01-08 13:18:06 +0100573 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100574
575 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100576 val = info[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100577 }
578
Christopher Faulet37286a52021-01-20 15:20:53 +0100579 if (!promex_dump_metric(appctx, htx, prefix, &promex_global_metrics[appctx->st2], &val, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100580 goto full;
581
Christopher Fauletf959d082019-02-07 15:38:42 +0100582 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100583 }
584
585 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200586 if (out.len) {
587 if (!htx_add_data_atonce(htx, out))
588 return -1; /* Unexpected and unrecoverable error */
589 channel_add_input(chn, out.len);
590 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100591 return ret;
592 full:
593 ret = 0;
594 goto end;
595}
596
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500597/* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100598 * 0 if <htx> is full and -1 in case of any error. */
599static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
600{
601 static struct ist prefix = IST("haproxy_frontend_");
602 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100603 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100604 struct channel *chn = si_ic(appctx->owner);
605 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200606 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchyb9577452021-01-17 18:27:46 +0100607 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100608 int ret = 1;
609
Christopher Faulet37286a52021-01-20 15:20:53 +0100610 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
611 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
612 continue;
613
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200614 while (appctx->ctx.stats.obj1) {
615 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100616
617 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200618 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100619 goto next_px;
620
William Dauchyb9577452021-01-17 18:27:46 +0100621 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
622 return -1;
623
Christopher Fauletf959d082019-02-07 15:38:42 +0100624 switch (appctx->st2) {
625 case ST_F_STATUS:
Christopher Faulet37286a52021-01-20 15:20:53 +0100626 val = mkf_u32(FO_STATUS, !px->disabled);
Christopher Fauletf959d082019-02-07 15:38:42 +0100627 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100628 case ST_F_REQ_RATE_MAX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100629 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100630 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100631 case ST_F_INTERCEPTED:
Christopher Fauletf959d082019-02-07 15:38:42 +0100632 case ST_F_CACHE_LOOKUPS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100633 case ST_F_CACHE_HITS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100634 case ST_F_COMP_IN:
Christopher Fauletf959d082019-02-07 15:38:42 +0100635 case ST_F_COMP_OUT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100636 case ST_F_COMP_BYP:
William Dauchyb9577452021-01-17 18:27:46 +0100637 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100638 if (px->mode != PR_MODE_HTTP)
639 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100640 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100641 break;
William Dauchyb9577452021-01-17 18:27:46 +0100642 case ST_F_HRSP_2XX:
643 case ST_F_HRSP_3XX:
644 case ST_F_HRSP_4XX:
645 case ST_F_HRSP_5XX:
646 case ST_F_HRSP_OTHER:
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];
William Dauchyb9577452021-01-17 18:27:46 +0100650 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100651 break;
652
653 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100654 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100655 }
656
Christopher Faulet37286a52021-01-20 15:20:53 +0100657 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], &val, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100658 goto full;
659 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200660 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100661 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100662 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200663 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100664 }
665
666 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200667 if (out.len) {
668 if (!htx_add_data_atonce(htx, out))
669 return -1; /* Unexpected and unrecoverable error */
670 channel_add_input(chn, out.len);
671 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100672 return ret;
673 full:
674 ret = 0;
675 goto end;
676}
677
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500678/* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100679 * 0 if <htx> is full and -1 in case of any error. */
680static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
681{
682 static struct ist prefix = IST("haproxy_backend_");
683 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100684 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100685 struct channel *chn = si_ic(appctx->owner);
686 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200687 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100688 int ret = 1;
689 uint32_t weight;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200690 double secs;
Christopher Fauletf959d082019-02-07 15:38:42 +0100691
Christopher Faulet37286a52021-01-20 15:20:53 +0100692 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
693 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
694 continue;
695
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200696 while (appctx->ctx.stats.obj1) {
697 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100698
699 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200700 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100701 goto next_px;
702
703 switch (appctx->st2) {
704 case ST_F_STATUS:
Christopher Faulet37286a52021-01-20 15:20:53 +0100705 val = mkf_u32(FO_STATUS, (px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0);
Christopher Fauletf959d082019-02-07 15:38:42 +0100706 break;
707 case ST_F_SCUR:
Christopher Faulet37286a52021-01-20 15:20:53 +0100708 val = mkf_u32(0, px->beconn);
Christopher Fauletf959d082019-02-07 15:38:42 +0100709 break;
710 case ST_F_SMAX:
Christopher Faulet37286a52021-01-20 15:20:53 +0100711 val = mkf_u32(FN_MAX, px->be_counters.conn_max);
Christopher Fauletf959d082019-02-07 15:38:42 +0100712 break;
713 case ST_F_SLIM:
Christopher Faulet37286a52021-01-20 15:20:53 +0100714 val = mkf_u32(FO_CONFIG|FN_LIMIT, px->fullconn);
Christopher Fauletf959d082019-02-07 15:38:42 +0100715 break;
716 case ST_F_STOT:
Christopher Faulet37286a52021-01-20 15:20:53 +0100717 val = mkf_u64(FN_COUNTER, px->be_counters.cum_conn);
Christopher Fauletf959d082019-02-07 15:38:42 +0100718 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100719 case ST_F_RATE_MAX:
Christopher Faulet37286a52021-01-20 15:20:53 +0100720 val = mkf_u32(0, px->be_counters.sps_max);
Christopher Fauletf959d082019-02-07 15:38:42 +0100721 break;
722 case ST_F_LASTSESS:
Christopher Faulet37286a52021-01-20 15:20:53 +0100723 val = mkf_s32(FN_AGE, be_lastsession(px));
Christopher Fauletf959d082019-02-07 15:38:42 +0100724 break;
725 case ST_F_QCUR:
Christopher Faulet37286a52021-01-20 15:20:53 +0100726 val = mkf_u32(0, px->nbpend);
Christopher Fauletf959d082019-02-07 15:38:42 +0100727 break;
728 case ST_F_QMAX:
Christopher Faulet37286a52021-01-20 15:20:53 +0100729 val = mkf_u32(FN_MAX, px->be_counters.nbpend_max);
Christopher Fauletf959d082019-02-07 15:38:42 +0100730 break;
731 case ST_F_CONNECT:
Christopher Faulet37286a52021-01-20 15:20:53 +0100732 val = mkf_u64(FN_COUNTER, px->be_counters.connect);
Christopher Fauletf959d082019-02-07 15:38:42 +0100733 break;
734 case ST_F_REUSE:
Christopher Faulet37286a52021-01-20 15:20:53 +0100735 val = mkf_u64(FN_COUNTER, px->be_counters.reuse);
Christopher Fauletf959d082019-02-07 15:38:42 +0100736 break;
737 case ST_F_BIN:
Christopher Faulet37286a52021-01-20 15:20:53 +0100738 val = mkf_u64(FN_COUNTER, px->be_counters.bytes_in);
Christopher Fauletf959d082019-02-07 15:38:42 +0100739 break;
740 case ST_F_BOUT:
Christopher Faulet37286a52021-01-20 15:20:53 +0100741 val = mkf_u64(FN_COUNTER, px->be_counters.bytes_out);
Christopher Fauletf959d082019-02-07 15:38:42 +0100742 break;
743 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200744 secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100745 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100746 break;
747 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200748 secs = (double)swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100749 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100750 break;
751 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200752 secs = (double)swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100753 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100754 break;
755 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200756 secs = (double)swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100757 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100758 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100759 case ST_F_QT_MAX:
760 secs = (double)px->be_counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100761 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100762 break;
763 case ST_F_CT_MAX:
764 secs = (double)px->be_counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100765 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100766 break;
767 case ST_F_RT_MAX:
768 secs = (double)px->be_counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100769 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100770 break;
771 case ST_F_TT_MAX:
772 secs = (double)px->be_counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100773 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100774 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100775 case ST_F_DREQ:
Christopher Faulet37286a52021-01-20 15:20:53 +0100776 val = mkf_u64(FN_COUNTER, px->be_counters.denied_req);
Christopher Fauletf959d082019-02-07 15:38:42 +0100777 break;
778 case ST_F_DRESP:
Christopher Faulet37286a52021-01-20 15:20:53 +0100779 val = mkf_u64(FN_COUNTER, px->be_counters.denied_resp);
Christopher Fauletf959d082019-02-07 15:38:42 +0100780 break;
781 case ST_F_ECON:
Christopher Faulet37286a52021-01-20 15:20:53 +0100782 val = mkf_u64(FN_COUNTER, px->be_counters.failed_conns);
Christopher Fauletf959d082019-02-07 15:38:42 +0100783 break;
784 case ST_F_ERESP:
Christopher Faulet37286a52021-01-20 15:20:53 +0100785 val = mkf_u64(FN_COUNTER, px->be_counters.failed_resp);
Christopher Fauletf959d082019-02-07 15:38:42 +0100786 break;
787 case ST_F_WRETR:
Christopher Faulet37286a52021-01-20 15:20:53 +0100788 val = mkf_u64(FN_COUNTER, px->be_counters.retries);
Christopher Fauletf959d082019-02-07 15:38:42 +0100789 break;
790 case ST_F_WREDIS:
Christopher Faulet37286a52021-01-20 15:20:53 +0100791 val = mkf_u64(FN_COUNTER, px->be_counters.redispatches);
Christopher Fauletf959d082019-02-07 15:38:42 +0100792 break;
793 case ST_F_WREW:
Christopher Faulet37286a52021-01-20 15:20:53 +0100794 val = mkf_u64(FN_COUNTER, px->be_counters.failed_rewrites);
Christopher Fauletf959d082019-02-07 15:38:42 +0100795 break;
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100796 case ST_F_EINT:
Christopher Faulet37286a52021-01-20 15:20:53 +0100797 val = mkf_u64(FN_COUNTER, px->be_counters.internal_errors);
Christopher Faulete4a2c8d2019-12-16 14:44:01 +0100798 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100799 case ST_F_CLI_ABRT:
Christopher Faulet37286a52021-01-20 15:20:53 +0100800 val = mkf_u64(FN_COUNTER, px->be_counters.cli_aborts);
Christopher Fauletf959d082019-02-07 15:38:42 +0100801 break;
802 case ST_F_SRV_ABRT:
Christopher Faulet37286a52021-01-20 15:20:53 +0100803 val = mkf_u64(FN_COUNTER, px->be_counters.srv_aborts);
Christopher Fauletf959d082019-02-07 15:38:42 +0100804 break;
805 case ST_F_WEIGHT:
806 weight = (px->lbprm.tot_weight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100807 val = mkf_u32(FN_AVG, weight);
Christopher Fauletf959d082019-02-07 15:38:42 +0100808 break;
809 case ST_F_ACT:
Christopher Faulet37286a52021-01-20 15:20:53 +0100810 val = mkf_u32(0, px->srv_act);
Christopher Fauletf959d082019-02-07 15:38:42 +0100811 break;
812 case ST_F_BCK:
Christopher Faulet37286a52021-01-20 15:20:53 +0100813 val = mkf_u32(0, px->srv_bck);
Christopher Fauletf959d082019-02-07 15:38:42 +0100814 break;
815 case ST_F_CHKDOWN:
Christopher Faulet37286a52021-01-20 15:20:53 +0100816 val = mkf_u64(FN_COUNTER, px->down_trans);
Christopher Fauletf959d082019-02-07 15:38:42 +0100817 break;
818 case ST_F_LASTCHG:
Christopher Faulet37286a52021-01-20 15:20:53 +0100819 val = mkf_u32(FN_AGE, now.tv_sec - px->last_change);
Christopher Fauletf959d082019-02-07 15:38:42 +0100820 break;
821 case ST_F_DOWNTIME:
Christopher Faulet37286a52021-01-20 15:20:53 +0100822 val = mkf_u32(FN_COUNTER, be_downtime(px));
Christopher Fauletf959d082019-02-07 15:38:42 +0100823 break;
824 case ST_F_LBTOT:
Christopher Faulet37286a52021-01-20 15:20:53 +0100825 val = mkf_u64(FN_COUNTER, px->be_counters.cum_lbconn);
Christopher Fauletf959d082019-02-07 15:38:42 +0100826 break;
827 case ST_F_REQ_TOT:
828 if (px->mode != PR_MODE_HTTP)
829 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100830 val = mkf_u64(FN_COUNTER, px->be_counters.p.http.cum_req);
Christopher Fauletf959d082019-02-07 15:38:42 +0100831 break;
832 case ST_F_HRSP_1XX:
833 if (px->mode != PR_MODE_HTTP)
834 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100835 val = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[1]);
Christopher Fauletf959d082019-02-07 15:38:42 +0100836 break;
837 case ST_F_HRSP_2XX:
838 if (px->mode != PR_MODE_HTTP)
839 goto next_px;
840 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet37286a52021-01-20 15:20:53 +0100841 val = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[2]);
Christopher Fauletf959d082019-02-07 15:38:42 +0100842 break;
843 case ST_F_HRSP_3XX:
844 if (px->mode != PR_MODE_HTTP)
845 goto next_px;
846 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet37286a52021-01-20 15:20:53 +0100847 val = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[3]);
Christopher Fauletf959d082019-02-07 15:38:42 +0100848 break;
849 case ST_F_HRSP_4XX:
850 if (px->mode != PR_MODE_HTTP)
851 goto next_px;
852 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet37286a52021-01-20 15:20:53 +0100853 val = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[4]);
Christopher Fauletf959d082019-02-07 15:38:42 +0100854 break;
855 case ST_F_HRSP_5XX:
856 if (px->mode != PR_MODE_HTTP)
857 goto next_px;
858 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet37286a52021-01-20 15:20:53 +0100859 val = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[5]);
Christopher Fauletf959d082019-02-07 15:38:42 +0100860 break;
861 case ST_F_HRSP_OTHER:
862 if (px->mode != PR_MODE_HTTP)
863 goto next_px;
864 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet37286a52021-01-20 15:20:53 +0100865 val = mkf_u64(FN_COUNTER, px->be_counters.p.http.rsp[0]);
Christopher Fauletf959d082019-02-07 15:38:42 +0100866 break;
867 case ST_F_CACHE_LOOKUPS:
868 if (px->mode != PR_MODE_HTTP)
869 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100870 val = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_lookups);
Christopher Fauletf959d082019-02-07 15:38:42 +0100871 break;
872 case ST_F_CACHE_HITS:
873 if (px->mode != PR_MODE_HTTP)
874 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100875 val = mkf_u64(FN_COUNTER, px->be_counters.p.http.cache_hits);
Christopher Fauletf959d082019-02-07 15:38:42 +0100876 break;
877 case ST_F_COMP_IN:
878 if (px->mode != PR_MODE_HTTP)
879 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100880 val = mkf_u64(FN_COUNTER, px->be_counters.comp_in);
Christopher Fauletf959d082019-02-07 15:38:42 +0100881 break;
882 case ST_F_COMP_OUT:
883 if (px->mode != PR_MODE_HTTP)
884 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100885 val = mkf_u64(FN_COUNTER, px->be_counters.comp_out);
Christopher Fauletf959d082019-02-07 15:38:42 +0100886 break;
887 case ST_F_COMP_BYP:
888 if (px->mode != PR_MODE_HTTP)
889 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100890 val = mkf_u64(FN_COUNTER, px->be_counters.comp_byp);
Christopher Fauletf959d082019-02-07 15:38:42 +0100891 break;
892 case ST_F_COMP_RSP:
893 if (px->mode != PR_MODE_HTTP)
894 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100895 val = mkf_u64(FN_COUNTER, px->be_counters.p.http.comp_rsp);
Christopher Fauletf959d082019-02-07 15:38:42 +0100896 break;
897
898 default:
899 goto next_metric;
900 }
901
Christopher Faulet37286a52021-01-20 15:20:53 +0100902 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], &val, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100903 goto full;
904 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200905 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100906 }
907 next_metric:
908 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200909 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100910 }
911
912 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200913 if (out.len) {
914 if (!htx_add_data_atonce(htx, out))
915 return -1; /* Unexpected and unrecoverable error */
916 channel_add_input(chn, out.len);
917 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100918 return ret;
919 full:
920 ret = 0;
921 goto end;
922}
923
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500924/* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100925 * 0 if <htx> is full and -1 in case of any error. */
926static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
927{
928 static struct ist prefix = IST("haproxy_server_");
929 struct proxy *px;
930 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100931 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100932 struct channel *chn = si_ic(appctx->owner);
933 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200934 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100935 int ret = 1;
936 uint32_t weight;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200937 double secs;
Christopher Fauletf959d082019-02-07 15:38:42 +0100938
Christopher Faulet37286a52021-01-20 15:20:53 +0100939 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
940 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
941 continue;
942
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200943 while (appctx->ctx.stats.obj1) {
944 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100945
946 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200947 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100948 goto next_px;
949
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200950 while (appctx->ctx.stats.obj2) {
951 sv = appctx->ctx.stats.obj2;
Christopher Fauletf959d082019-02-07 15:38:42 +0100952
Christopher Fauleteba22942019-11-19 14:18:24 +0100953 if ((appctx->ctx.stats.flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
954 goto next_sv;
955
Christopher Fauletf959d082019-02-07 15:38:42 +0100956 switch (appctx->st2) {
957 case ST_F_STATUS:
Christopher Faulet37286a52021-01-20 15:20:53 +0100958 val = mkf_u32(FO_STATUS, promex_srv_status(sv));
Christopher Fauletf959d082019-02-07 15:38:42 +0100959 break;
960 case ST_F_SCUR:
Christopher Faulet37286a52021-01-20 15:20:53 +0100961 val = mkf_u32(0, sv->cur_sess);
Christopher Fauletf959d082019-02-07 15:38:42 +0100962 break;
963 case ST_F_SMAX:
Christopher Faulet37286a52021-01-20 15:20:53 +0100964 val = mkf_u32(FN_MAX, sv->counters.cur_sess_max);
Christopher Fauletf959d082019-02-07 15:38:42 +0100965 break;
966 case ST_F_SLIM:
Christopher Faulet37286a52021-01-20 15:20:53 +0100967 val = mkf_u32(FO_CONFIG|FN_LIMIT, sv->maxconn);
Christopher Fauletf959d082019-02-07 15:38:42 +0100968 break;
969 case ST_F_STOT:
Christopher Faulet37286a52021-01-20 15:20:53 +0100970 val = mkf_u64(FN_COUNTER, sv->counters.cum_sess);
Christopher Fauletf959d082019-02-07 15:38:42 +0100971 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100972 case ST_F_RATE_MAX:
Christopher Faulet37286a52021-01-20 15:20:53 +0100973 val = mkf_u32(FN_MAX, sv->counters.sps_max);
Christopher Fauletf959d082019-02-07 15:38:42 +0100974 break;
975 case ST_F_LASTSESS:
Christopher Faulet37286a52021-01-20 15:20:53 +0100976 val = mkf_s32(FN_AGE, srv_lastsession(sv));
Christopher Fauletf959d082019-02-07 15:38:42 +0100977 break;
978 case ST_F_QCUR:
Christopher Faulet37286a52021-01-20 15:20:53 +0100979 val = mkf_u32(0, sv->nbpend);
Christopher Fauletf959d082019-02-07 15:38:42 +0100980 break;
981 case ST_F_QMAX:
Christopher Faulet37286a52021-01-20 15:20:53 +0100982 val = mkf_u32(FN_MAX, sv->counters.nbpend_max);
Christopher Fauletf959d082019-02-07 15:38:42 +0100983 break;
984 case ST_F_QLIMIT:
Christopher Faulet37286a52021-01-20 15:20:53 +0100985 val = mkf_u32(FO_CONFIG|FS_SERVICE, sv->maxqueue);
Christopher Fauletf959d082019-02-07 15:38:42 +0100986 break;
987 case ST_F_BIN:
Christopher Faulet37286a52021-01-20 15:20:53 +0100988 val = mkf_u64(FN_COUNTER, sv->counters.bytes_in);
Christopher Fauletf959d082019-02-07 15:38:42 +0100989 break;
990 case ST_F_BOUT:
Christopher Faulet37286a52021-01-20 15:20:53 +0100991 val = mkf_u64(FN_COUNTER, sv->counters.bytes_out);
Christopher Fauletf959d082019-02-07 15:38:42 +0100992 break;
993 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200994 secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100995 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100996 break;
997 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200998 secs = (double)swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100999 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001000 break;
1001 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001002 secs = (double)swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001003 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001004 break;
1005 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +02001006 secs = (double)swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001007 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +01001008 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001009 case ST_F_QT_MAX:
1010 secs = (double)sv->counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001011 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001012 break;
1013 case ST_F_CT_MAX:
1014 secs = (double)sv->counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001015 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001016 break;
1017 case ST_F_RT_MAX:
1018 secs = (double)sv->counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001019 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001020 break;
1021 case ST_F_TT_MAX:
1022 secs = (double)sv->counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001023 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +01001024 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001025 case ST_F_CONNECT:
Christopher Faulet37286a52021-01-20 15:20:53 +01001026 val = mkf_u64(FN_COUNTER, sv->counters.connect);
Christopher Fauletf959d082019-02-07 15:38:42 +01001027 break;
1028 case ST_F_REUSE:
Christopher Faulet37286a52021-01-20 15:20:53 +01001029 val = mkf_u64(FN_COUNTER, sv->counters.reuse);
Christopher Fauletf959d082019-02-07 15:38:42 +01001030 break;
1031 case ST_F_DRESP:
Christopher Faulet37286a52021-01-20 15:20:53 +01001032 val = mkf_u64(FN_COUNTER, sv->counters.denied_resp);
Christopher Fauletf959d082019-02-07 15:38:42 +01001033 break;
1034 case ST_F_ECON:
Christopher Faulet37286a52021-01-20 15:20:53 +01001035 val = mkf_u64(FN_COUNTER, sv->counters.failed_conns);
Christopher Fauletf959d082019-02-07 15:38:42 +01001036 break;
1037 case ST_F_ERESP:
Christopher Faulet37286a52021-01-20 15:20:53 +01001038 val = mkf_u64(FN_COUNTER, sv->counters.failed_resp);
Christopher Fauletf959d082019-02-07 15:38:42 +01001039 break;
1040 case ST_F_WRETR:
Christopher Faulet37286a52021-01-20 15:20:53 +01001041 val = mkf_u64(FN_COUNTER, sv->counters.retries);
Christopher Fauletf959d082019-02-07 15:38:42 +01001042 break;
1043 case ST_F_WREDIS:
Christopher Faulet37286a52021-01-20 15:20:53 +01001044 val = mkf_u64(FN_COUNTER, sv->counters.redispatches);
Christopher Fauletf959d082019-02-07 15:38:42 +01001045 break;
1046 case ST_F_WREW:
Christopher Faulet37286a52021-01-20 15:20:53 +01001047 val = mkf_u64(FN_COUNTER, sv->counters.failed_rewrites);
Christopher Fauletf959d082019-02-07 15:38:42 +01001048 break;
Christopher Faulete4a2c8d2019-12-16 14:44:01 +01001049 case ST_F_EINT:
Christopher Faulet37286a52021-01-20 15:20:53 +01001050 val = mkf_u64(FN_COUNTER, sv->counters.internal_errors);
Christopher Faulete4a2c8d2019-12-16 14:44:01 +01001051 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001052 case ST_F_CLI_ABRT:
Christopher Faulet37286a52021-01-20 15:20:53 +01001053 val = mkf_u64(FN_COUNTER, sv->counters.cli_aborts);
Christopher Fauletf959d082019-02-07 15:38:42 +01001054 break;
1055 case ST_F_SRV_ABRT:
Christopher Faulet37286a52021-01-20 15:20:53 +01001056 val = mkf_u64(FN_COUNTER, sv->counters.srv_aborts);
Christopher Fauletf959d082019-02-07 15:38:42 +01001057 break;
1058 case ST_F_WEIGHT:
1059 weight = (sv->cur_eweight * px->lbprm.wmult + px->lbprm.wdiv - 1) / px->lbprm.wdiv;
Christopher Faulet37286a52021-01-20 15:20:53 +01001060 val = mkf_u32(FN_AVG, weight);
Christopher Fauletf959d082019-02-07 15:38:42 +01001061 break;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001062 case ST_F_CHECK_STATUS:
1063 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1064 goto next_sv;
Christopher Faulet37286a52021-01-20 15:20:53 +01001065 val = mkf_u32(FN_OUTPUT, sv->check.status);
Christopher Fauletcf403f32019-11-21 14:35:46 +01001066 break;
1067 case ST_F_CHECK_CODE:
1068 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1069 goto next_sv;
Christopher Faulet37286a52021-01-20 15:20:53 +01001070 val = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code);
Christopher Fauletcf403f32019-11-21 14:35:46 +01001071 break;
Christopher Faulet2711e512020-02-27 16:12:07 +01001072 case ST_F_CHECK_DURATION:
1073 if (sv->check.status < HCHK_STATUS_CHECKED)
1074 goto next_sv;
1075 secs = (double)sv->check.duration / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001076 val = mkf_flt(FN_DURATION, secs);
Christopher Faulet2711e512020-02-27 16:12:07 +01001077 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001078 case ST_F_CHKFAIL:
Christopher Faulet37286a52021-01-20 15:20:53 +01001079 val = mkf_u64(FN_COUNTER, sv->counters.failed_checks);
Christopher Fauletf959d082019-02-07 15:38:42 +01001080 break;
1081 case ST_F_CHKDOWN:
Christopher Faulet37286a52021-01-20 15:20:53 +01001082 val = mkf_u64(FN_COUNTER, sv->counters.down_trans);
Christopher Fauletf959d082019-02-07 15:38:42 +01001083 break;
1084 case ST_F_DOWNTIME:
Christopher Faulet37286a52021-01-20 15:20:53 +01001085 val = mkf_u32(FN_COUNTER, srv_downtime(sv));
Christopher Fauletf959d082019-02-07 15:38:42 +01001086 break;
1087 case ST_F_LASTCHG:
Christopher Faulet37286a52021-01-20 15:20:53 +01001088 val = mkf_u32(FN_AGE, now.tv_sec - sv->last_change);
Christopher Fauletf959d082019-02-07 15:38:42 +01001089 break;
1090 case ST_F_THROTTLE:
Christopher Faulet37286a52021-01-20 15:20:53 +01001091 val = mkf_u32(FN_AVG, server_throttle_rate(sv));
Christopher Fauletf959d082019-02-07 15:38:42 +01001092 break;
1093 case ST_F_LBTOT:
Christopher Faulet37286a52021-01-20 15:20:53 +01001094 val = mkf_u64(FN_COUNTER, sv->counters.cum_lbconn);
Christopher Fauletf959d082019-02-07 15:38:42 +01001095 break;
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001096 case ST_F_REQ_TOT:
1097 if (px->mode != PR_MODE_HTTP)
1098 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +01001099 val = mkf_u64(FN_COUNTER, sv->counters.p.http.cum_req);
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001100 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001101 case ST_F_HRSP_1XX:
1102 if (px->mode != PR_MODE_HTTP)
1103 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +01001104 val = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[1]);
Christopher Fauletf959d082019-02-07 15:38:42 +01001105 break;
1106 case ST_F_HRSP_2XX:
1107 if (px->mode != PR_MODE_HTTP)
1108 goto next_px;
1109 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet37286a52021-01-20 15:20:53 +01001110 val = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[2]);
Christopher Fauletf959d082019-02-07 15:38:42 +01001111 break;
1112 case ST_F_HRSP_3XX:
1113 if (px->mode != PR_MODE_HTTP)
1114 goto next_px;
1115 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet37286a52021-01-20 15:20:53 +01001116 val = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[3]);
Christopher Fauletf959d082019-02-07 15:38:42 +01001117 break;
1118 case ST_F_HRSP_4XX:
1119 if (px->mode != PR_MODE_HTTP)
1120 goto next_px;
1121 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet37286a52021-01-20 15:20:53 +01001122 val = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[4]);
Christopher Fauletf959d082019-02-07 15:38:42 +01001123 break;
1124 case ST_F_HRSP_5XX:
1125 if (px->mode != PR_MODE_HTTP)
1126 goto next_px;
1127 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet37286a52021-01-20 15:20:53 +01001128 val = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[5]);
Christopher Fauletf959d082019-02-07 15:38:42 +01001129 break;
1130 case ST_F_HRSP_OTHER:
1131 if (px->mode != PR_MODE_HTTP)
1132 goto next_px;
1133 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet37286a52021-01-20 15:20:53 +01001134 val = mkf_u64(FN_COUNTER, sv->counters.p.http.rsp[0]);
Christopher Fauletf959d082019-02-07 15:38:42 +01001135 break;
Christopher Faulet20ab80c2019-11-08 15:24:32 +01001136 case ST_F_SRV_ICUR:
Christopher Faulet37286a52021-01-20 15:20:53 +01001137 val = mkf_u32(0, sv->curr_idle_conns);
Christopher Faulet20ab80c2019-11-08 15:24:32 +01001138 break;
1139 case ST_F_SRV_ILIM:
Christopher Faulet37286a52021-01-20 15:20:53 +01001140 val = mkf_u32(FO_CONFIG|FN_LIMIT, (sv->max_idle_conns == -1) ? 0 : sv->max_idle_conns);
Christopher Faulet20ab80c2019-11-08 15:24:32 +01001141 break;
Christopher Fauletc55a6262020-07-10 15:39:39 +02001142 case ST_F_IDLE_CONN_CUR:
Christopher Faulet37286a52021-01-20 15:20:53 +01001143 val = mkf_u32(0, sv->curr_idle_nb);
Christopher Fauletc55a6262020-07-10 15:39:39 +02001144 break;
1145 case ST_F_SAFE_CONN_CUR:
Christopher Faulet37286a52021-01-20 15:20:53 +01001146 val = mkf_u32(0, sv->curr_safe_nb);
Christopher Fauletc55a6262020-07-10 15:39:39 +02001147 break;
1148 case ST_F_USED_CONN_CUR:
Christopher Faulet37286a52021-01-20 15:20:53 +01001149 val = mkf_u32(0, sv->curr_used_conns);
Christopher Fauletc55a6262020-07-10 15:39:39 +02001150 break;
1151 case ST_F_NEED_CONN_EST:
Christopher Faulet37286a52021-01-20 15:20:53 +01001152 val = mkf_u32(0, sv->est_need_conns);
Christopher Fauletc55a6262020-07-10 15:39:39 +02001153 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001154
1155 default:
1156 goto next_metric;
1157 }
1158
Christopher Faulet37286a52021-01-20 15:20:53 +01001159 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], &val, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +01001160 goto full;
1161
Christopher Fauleteba22942019-11-19 14:18:24 +01001162 next_sv:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001163 appctx->ctx.stats.obj2 = sv->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001164 }
1165
1166 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001167 appctx->ctx.stats.obj1 = px->next;
1168 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001169 }
1170 next_metric:
1171 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001172 appctx->ctx.stats.obj1 = proxies_list;
1173 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001174 }
1175
1176
1177 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001178 if (out.len) {
1179 if (!htx_add_data_atonce(htx, out))
1180 return -1; /* Unexpected and unrecoverable error */
1181 channel_add_input(chn, out.len);
1182 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001183 return ret;
1184 full:
1185 ret = 0;
1186 goto end;
1187}
1188
1189/* Dump all metrics (global, frontends, backends and servers) depending on the
1190 * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001191 * -1 in case of any error.
1192 * Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
1193 * a pointer to the current server/listener. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001194static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1195{
1196 int ret;
1197
1198 switch (appctx->st1) {
1199 case PROMEX_DUMPER_INIT:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001200 appctx->ctx.stats.obj1 = NULL;
1201 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001202 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001203 appctx->st2 = INF_NAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001204 appctx->st1 = PROMEX_DUMPER_GLOBAL;
1205 /* fall through */
1206
1207 case PROMEX_DUMPER_GLOBAL:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001208 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_GLOBAL) {
1209 ret = promex_dump_global_metrics(appctx, htx);
1210 if (ret <= 0) {
1211 if (ret == -1)
1212 goto error;
1213 goto full;
1214 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001215 }
1216
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001217 appctx->ctx.stats.obj1 = proxies_list;
1218 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001219 appctx->ctx.stats.flags &= ~PROMEX_FL_INFO_METRIC;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001220 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001221 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001222 appctx->st1 = PROMEX_DUMPER_FRONT;
1223 /* fall through */
1224
1225 case PROMEX_DUMPER_FRONT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001226 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_FRONT) {
1227 ret = promex_dump_front_metrics(appctx, htx);
1228 if (ret <= 0) {
1229 if (ret == -1)
1230 goto error;
1231 goto full;
1232 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001233 }
1234
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001235 appctx->ctx.stats.obj1 = proxies_list;
1236 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001237 appctx->ctx.stats.flags &= ~PROMEX_FL_FRONT_METRIC;
1238 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001239 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001240 appctx->st1 = PROMEX_DUMPER_BACK;
1241 /* fall through */
1242
1243 case PROMEX_DUMPER_BACK:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001244 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_BACK) {
1245 ret = promex_dump_back_metrics(appctx, htx);
1246 if (ret <= 0) {
1247 if (ret == -1)
1248 goto error;
1249 goto full;
1250 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001251 }
1252
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001253 appctx->ctx.stats.obj1 = proxies_list;
1254 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001255 appctx->ctx.stats.flags &= ~PROMEX_FL_BACK_METRIC;
1256 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Faulet37286a52021-01-20 15:20:53 +01001257 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001258 appctx->st1 = PROMEX_DUMPER_SRV;
1259 /* fall through */
1260
1261 case PROMEX_DUMPER_SRV:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001262 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_SERVER) {
1263 ret = promex_dump_srv_metrics(appctx, htx);
1264 if (ret <= 0) {
1265 if (ret == -1)
1266 goto error;
1267 goto full;
1268 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001269 }
1270
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001271 appctx->ctx.stats.obj1 = NULL;
1272 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001273 appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001274 appctx->st2 = 0;
1275 appctx->st1 = PROMEX_DUMPER_DONE;
1276 /* fall through */
1277
1278 case PROMEX_DUMPER_DONE:
1279 default:
1280 break;
1281 }
1282
1283 return 1;
1284
1285 full:
1286 si_rx_room_blk(si);
1287 return 0;
1288 error:
1289 /* unrecoverable error */
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001290 appctx->ctx.stats.obj1 = NULL;
1291 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletf959d082019-02-07 15:38:42 +01001292 appctx->ctx.stats.flags = 0;
1293 appctx->st2 = 0;
1294 appctx->st1 = PROMEX_DUMPER_DONE;
1295 return -1;
1296}
1297
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001298/* Parse the query string of request URI to filter the metrics. It returns 1 on
Christopher Faulet78407ce2019-11-18 14:47:08 +01001299 * success and -1 on error. */
1300static int promex_parse_uri(struct appctx *appctx, struct stream_interface *si)
1301{
1302 struct channel *req = si_oc(si);
1303 struct channel *res = si_ic(si);
1304 struct htx *req_htx, *res_htx;
1305 struct htx_sl *sl;
William Dauchyc65f6562019-11-26 12:56:26 +01001306 char *p, *key, *value;
1307 const char *end;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001308 struct buffer *err;
1309 int default_scopes = PROMEX_FL_SCOPE_ALL;
1310 int len;
1311
1312 /* Get the query-string */
1313 req_htx = htxbuf(&req->buf);
1314 sl = http_get_stline(req_htx);
1315 if (!sl)
1316 goto error;
1317 p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?');
1318 if (!p)
1319 goto end;
1320 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001321
William Dauchyc65f6562019-11-26 12:56:26 +01001322 /* copy the query-string */
1323 len = end - p;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001324 chunk_reset(&trash);
1325 memcpy(trash.area, p, len);
1326 trash.area[len] = 0;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001327 p = trash.area;
William Dauchyc65f6562019-11-26 12:56:26 +01001328 end = trash.area + len;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001329
1330 /* Parse the query-string */
William Dauchyc65f6562019-11-26 12:56:26 +01001331 while (p < end && *p && *p != '#') {
1332 value = NULL;
1333
1334 /* decode parameter name */
1335 key = p;
1336 while (p < end && *p != '=' && *p != '&' && *p != '#')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001337 ++p;
William Dauchyc65f6562019-11-26 12:56:26 +01001338 /* found a value */
1339 if (*p == '=') {
1340 *(p++) = 0;
1341 value = p;
1342 }
1343 else if (*p == '&')
1344 *(p++) = 0;
1345 else if (*p == '#')
1346 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001347 len = url_decode(key, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001348 if (len == -1)
1349 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001350
William Dauchyc65f6562019-11-26 12:56:26 +01001351 /* decode value */
1352 if (value) {
1353 while (p < end && *p != '=' && *p != '&' && *p != '#')
1354 ++p;
1355 if (*p == '=')
1356 goto error;
1357 if (*p == '&')
1358 *(p++) = 0;
1359 else if (*p == '#')
1360 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001361 len = url_decode(value, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001362 if (len == -1)
1363 goto error;
1364 }
1365
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001366 if (strcmp(key, "scope") == 0) {
William Dauchyc65f6562019-11-26 12:56:26 +01001367 default_scopes = 0; /* at least a scope defined, unset default scopes */
1368 if (!value)
1369 goto error;
1370 else if (*value == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001371 appctx->ctx.stats.flags &= ~PROMEX_FL_SCOPE_ALL;
William Dauchyc65f6562019-11-26 12:56:26 +01001372 else if (*value == '*')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001373 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_ALL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001374 else if (strcmp(value, "global") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001375 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_GLOBAL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001376 else if (strcmp(value, "server") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001377 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_SERVER;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001378 else if (strcmp(value, "backend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001379 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_BACK;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001380 else if (strcmp(value, "frontend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001381 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_FRONT;
1382 else
1383 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001384 }
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001385 else if (strcmp(key, "no-maint") == 0)
Christopher Fauleteba22942019-11-19 14:18:24 +01001386 appctx->ctx.stats.flags |= PROMEX_FL_NO_MAINT_SRV;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001387 }
1388
1389 end:
1390 appctx->ctx.stats.flags |= default_scopes;
1391 return 1;
1392
1393 error:
1394 err = &http_err_chunks[HTTP_ERR_400];
1395 channel_erase(res);
1396 res->buf.data = b_data(err);
1397 memcpy(res->buf.area, b_head(err), b_data(err));
1398 res_htx = htx_from_buf(&res->buf);
1399 channel_add_input(res, res_htx->data);
1400 appctx->st0 = PROMEX_ST_END;
1401 return -1;
1402}
1403
Christopher Fauletf959d082019-02-07 15:38:42 +01001404/* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is
1405 * full. */
1406static int promex_send_headers(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1407{
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001408 struct channel *chn = si_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +01001409 struct htx_sl *sl;
1410 unsigned int flags;
1411
1412 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);
1413 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
1414 if (!sl)
1415 goto full;
1416 sl->info.res.status = 200;
1417 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001418 !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) ||
1419 !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) ||
1420 !htx_add_endof(htx, HTX_BLK_EOH))
1421 goto full;
1422
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001423 channel_add_input(chn, htx->data);
Christopher Fauletf959d082019-02-07 15:38:42 +01001424 return 1;
1425 full:
1426 htx_reset(htx);
1427 si_rx_room_blk(si);
1428 return 0;
1429}
1430
1431/* The function returns 1 if the initialisation is complete, 0 if
1432 * an errors occurs and -1 if more data are required for initializing
1433 * the applet.
1434 */
1435static int promex_appctx_init(struct appctx *appctx, struct proxy *px, struct stream *strm)
1436{
1437 appctx->st0 = PROMEX_ST_INIT;
1438 return 1;
1439}
1440
1441/* The main I/O handler for the promex applet. */
1442static void promex_appctx_handle_io(struct appctx *appctx)
1443{
1444 struct stream_interface *si = appctx->owner;
1445 struct stream *s = si_strm(si);
1446 struct channel *req = si_oc(si);
1447 struct channel *res = si_ic(si);
1448 struct htx *req_htx, *res_htx;
1449 int ret;
1450
1451 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf959d082019-02-07 15:38:42 +01001452 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1453 goto out;
1454
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001455 /* Check if the input buffer is available. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001456 if (!b_size(&res->buf)) {
1457 si_rx_room_blk(si);
1458 goto out;
1459 }
1460
1461 switch (appctx->st0) {
1462 case PROMEX_ST_INIT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001463 ret = promex_parse_uri(appctx, si);
1464 if (ret <= 0) {
1465 if (ret == -1)
1466 goto error;
1467 goto out;
1468 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001469 appctx->st0 = PROMEX_ST_HEAD;
1470 appctx->st1 = PROMEX_DUMPER_INIT;
1471 /* fall through */
1472
1473 case PROMEX_ST_HEAD:
1474 if (!promex_send_headers(appctx, si, res_htx))
1475 goto out;
1476 appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP);
1477 /* fall through */
1478
1479 case PROMEX_ST_DUMP:
1480 ret = promex_dump_metrics(appctx, si, res_htx);
1481 if (ret <= 0) {
1482 if (ret == -1)
1483 goto error;
1484 goto out;
1485 }
1486 appctx->st0 = PROMEX_ST_DONE;
1487 /* fall through */
1488
1489 case PROMEX_ST_DONE:
Christopher Faulet54b5e212019-06-04 10:08:28 +02001490 /* Don't add TLR because mux-h1 will take care of it */
Willy Tarreauf1ea47d2020-07-23 06:53:27 +02001491 res_htx->flags |= HTX_FL_EOI; /* no more data are expected. Only EOM remains to add now */
Christopher Fauletf959d082019-02-07 15:38:42 +01001492 if (!htx_add_endof(res_htx, HTX_BLK_EOM)) {
1493 si_rx_room_blk(si);
1494 goto out;
1495 }
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001496 channel_add_input(res, 1);
1497 appctx->st0 = PROMEX_ST_END;
1498 /* fall through */
Christopher Fauletf959d082019-02-07 15:38:42 +01001499
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001500 case PROMEX_ST_END:
1501 if (!(res->flags & CF_SHUTR)) {
1502 res->flags |= CF_READ_NULL;
1503 si_shutr(si);
1504 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001505 }
1506
Christopher Fauletf959d082019-02-07 15:38:42 +01001507 out:
1508 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001509
1510 /* eat the whole request */
1511 if (co_data(req)) {
1512 req_htx = htx_from_buf(&req->buf);
1513 co_htx_skip(req, req_htx, co_data(req));
1514 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001515 return;
1516
1517 error:
1518 res->flags |= CF_READ_NULL;
1519 si_shutr(si);
1520 si_shutw(si);
1521}
1522
1523struct applet promex_applet = {
1524 .obj_type = OBJ_TYPE_APPLET,
1525 .name = "<PROMEX>", /* used for logging */
1526 .init = promex_appctx_init,
1527 .fct = promex_appctx_handle_io,
1528};
1529
1530static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px,
1531 struct act_rule *rule, char **err)
1532{
1533 /* Prometheus exporter service is only available on "http-request" rulesets */
1534 if (rule->from != ACT_F_HTTP_REQ) {
1535 memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets");
1536 return ACT_RET_PRS_ERR;
1537 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001538
1539 /* Add applet pointer in the rule. */
1540 rule->applet = promex_applet;
1541
1542 return ACT_RET_PRS_OK;
1543}
1544static void promex_register_build_options(void)
1545{
1546 char *ptr = NULL;
1547
1548 memprintf(&ptr, "Built with the Prometheus exporter as a service");
1549 hap_register_build_opts(ptr, 1);
1550}
1551
1552
1553static struct action_kw_list service_actions = { ILH, {
1554 { "prometheus-exporter", service_parse_prometheus_exporter },
1555 { /* END */ }
1556}};
1557
1558INITCALL1(STG_REGISTER, service_keywords_register, &service_actions);
1559INITCALL0(STG_REGISTER, promex_register_build_options);