blob: 6a42a47c308175f9b9377fbeb22cd1fa11ac8694 [file] [log] [blame]
Christopher Fauletf959d082019-02-07 15:38:42 +01001/*
2 * Promex is a Prometheus exporter for HAProxy
3 *
4 * It is highly inspired by the official Prometheus exporter.
5 * See: https://github.com/prometheus/haproxy_exporter
6 *
7 * Copyright 2019 Christopher Faulet <cfaulet@haproxy.com>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version
12 * 2 of the License, or (at your option) any later version.
13 *
14 */
15
Willy Tarreau122eba92020-06-04 10:15:32 +020016#include <haproxy/action-t.h>
Willy Tarreaub2551052020-06-09 09:07:15 +020017#include <haproxy/api.h>
Willy Tarreau3f0f82e2020-06-04 19:42:41 +020018#include <haproxy/applet.h>
Willy Tarreau49801602020-06-04 22:50:02 +020019#include <haproxy/backend.h>
Willy Tarreau6be78492020-06-05 00:00:29 +020020#include <haproxy/cfgparse.h>
William Dauchyde3c3262021-02-01 13:11:51 +010021#include <haproxy/check.h>
Willy Tarreau762d7a52020-06-04 11:23:07 +020022#include <haproxy/frontend.h>
Willy Tarreauf268ee82020-06-04 17:05:57 +020023#include <haproxy/global.h>
Willy Tarreaucd72d8c2020-06-02 19:11:26 +020024#include <haproxy/http.h>
Willy Tarreau87735332020-06-04 09:08:41 +020025#include <haproxy/http_htx.h>
Willy Tarreau16f958c2020-06-03 08:44:35 +020026#include <haproxy/htx.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020027#include <haproxy/list.h>
Willy Tarreau213e9902020-06-04 14:58:24 +020028#include <haproxy/listener.h>
Willy Tarreau36979d92020-06-05 17:27:29 +020029#include <haproxy/log.h>
Willy Tarreaua264d962020-06-04 22:29:18 +020030#include <haproxy/proxy.h>
Willy Tarreaue6ce10b2020-06-04 15:33:47 +020031#include <haproxy/sample.h>
Willy Tarreau1e56f922020-06-04 23:20:13 +020032#include <haproxy/server.h>
Willy Tarreau2eec9b52020-06-04 19:58:55 +020033#include <haproxy/stats.h>
Willy Tarreaudfd3de82020-06-04 23:46:14 +020034#include <haproxy/stream.h>
Willy Tarreau5e539c92020-06-04 20:45:39 +020035#include <haproxy/stream_interface.h>
Willy Tarreaucea0e1b2020-06-04 17:25:40 +020036#include <haproxy/task.h>
Willy Tarreau0fd04fd2021-05-08 12:58:12 +020037#include <haproxy/tools.h>
William Dauchy5a982a72021-01-08 13:18:06 +010038#include <haproxy/version.h>
Christopher Fauletf959d082019-02-07 15:38:42 +010039
40/* Prometheus exporter applet states (appctx->st0) */
41enum {
42 PROMEX_ST_INIT = 0, /* initialized */
43 PROMEX_ST_HEAD, /* send headers before dump */
44 PROMEX_ST_DUMP, /* dumping stats */
45 PROMEX_ST_DONE, /* finished */
Christopher Faulet9744f7c2019-03-27 15:48:53 +010046 PROMEX_ST_END, /* treatment terminated */
Christopher Fauletf959d082019-02-07 15:38:42 +010047};
48
49/* Prometheus exporter dumper states (appctx->st1) */
50enum {
William Dauchy69164222021-02-07 20:42:38 +010051 PROMEX_DUMPER_INIT = 0, /* initialized */
52 PROMEX_DUMPER_GLOBAL, /* dump metrics of globals */
53 PROMEX_DUMPER_FRONT, /* dump metrics of frontend proxies */
54 PROMEX_DUMPER_BACK, /* dump metrics of backend proxies */
55 PROMEX_DUMPER_LI, /* dump metrics of listeners */
56 PROMEX_DUMPER_SRV, /* dump metrics of servers */
57 PROMEX_DUMPER_STICKTABLE, /* dump metrics of stick tables */
58 PROMEX_DUMPER_DONE, /* finished */
Christopher Fauletf959d082019-02-07 15:38:42 +010059};
60
61/* Prometheus exporter flags (appctx->ctx.stats.flags) */
William Dauchy69164222021-02-07 20:42:38 +010062#define PROMEX_FL_METRIC_HDR 0x00000001
63#define PROMEX_FL_INFO_METRIC 0x00000002
64#define PROMEX_FL_FRONT_METRIC 0x00000004
65#define PROMEX_FL_BACK_METRIC 0x00000008
66#define PROMEX_FL_SRV_METRIC 0x00000010
William Dauchye3f7bd52021-02-14 23:22:56 +010067#define PROMEX_FL_LI_METRIC 0x00000020
68#define PROMEX_FL_STICKTABLE_METRIC 0x00000040
69#define PROMEX_FL_SCOPE_GLOBAL 0x00000080
70#define PROMEX_FL_SCOPE_FRONT 0x00000100
71#define PROMEX_FL_SCOPE_BACK 0x00000200
72#define PROMEX_FL_SCOPE_SERVER 0x00000400
73#define PROMEX_FL_SCOPE_LI 0x00000800
74#define PROMEX_FL_SCOPE_STICKTABLE 0x00001000
75#define PROMEX_FL_NO_MAINT_SRV 0x00002000
Christopher Faulet78407ce2019-11-18 14:47:08 +010076
William Dauchy69164222021-02-07 20:42:38 +010077#define PROMEX_FL_SCOPE_ALL (PROMEX_FL_SCOPE_GLOBAL | PROMEX_FL_SCOPE_FRONT | \
William Dauchye3f7bd52021-02-14 23:22:56 +010078 PROMEX_FL_SCOPE_LI | PROMEX_FL_SCOPE_BACK | \
79 PROMEX_FL_SCOPE_SERVER | PROMEX_FL_SCOPE_STICKTABLE)
Christopher Fauletf959d082019-02-07 15:38:42 +010080
Christopher Faulet0312c0d2021-01-20 15:19:12 +010081/* Promtheus metric type (gauge or counter) */
82enum promex_mt_type {
83 PROMEX_MT_GAUGE = 1,
84 PROMEX_MT_COUNTER = 2,
85};
86
Christopher Fauletf959d082019-02-07 15:38:42 +010087/* The max length for metrics name. It is a hard limit but it should be
Ilya Shipitsince7b00f2020-03-23 22:28:40 +050088 * enough.
Christopher Fauletf959d082019-02-07 15:38:42 +010089 */
90#define PROMEX_MAX_NAME_LEN 128
91
92/* The expected max length for a metric dump, including its header lines. It is
93 * just a soft limit to avoid extra work. We don't try to dump a metric if less
94 * than this size is available in the HTX.
95 */
96#define PROMEX_MAX_METRIC_LENGTH 512
97
Christopher Faulet5a2f9382021-01-28 11:24:17 +010098/* The max number of labels per metric */
99#define PROMEX_MAX_LABELS 8
William Dauchy5a982a72021-01-08 13:18:06 +0100100
Christopher Faulet0312c0d2021-01-20 15:19:12 +0100101/* Describe a prometheus metric */
102struct promex_metric {
103 const struct ist n; /* The metric name */
104 enum promex_mt_type type; /* The metric type (gauge or counter) */
105 unsigned int flags; /* PROMEX_FL_* flags */
106};
107
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100108/* Describe a prometheus metric label. It is just a key/value pair */
109struct promex_label {
110 struct ist name;
111 struct ist value;
112};
113
Christopher Faulet37286a52021-01-20 15:20:53 +0100114/* Global metrics */
115const struct promex_metric promex_global_metrics[INF_TOTAL_FIELDS] = {
116 //[INF_NAME] ignored
117 //[INF_VERSION], ignored
118 //[INF_RELEASE_DATE] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100119 [INF_NBTHREAD] = { .n = IST("nbthread"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
120 [INF_NBPROC] = { .n = IST("nbproc"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
121 [INF_PROCESS_NUM] = { .n = IST("relative_process_id"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
122 //[INF_PID] ignored
123 //[INF_UPTIME] ignored
124 [INF_UPTIME_SEC] = { .n = IST("uptime_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
125 [INF_START_TIME_SEC] = { .n = IST("start_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100126 //[INF_MEMMAX_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100127 [INF_MEMMAX_BYTES] = { .n = IST("max_memory_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100128 //[INF_POOL_ALLOC_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100129 [INF_POOL_ALLOC_BYTES] = { .n = IST("pool_allocated_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100130 //[INF_POOL_USED_MB] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100131 [INF_POOL_USED_BYTES] = { .n = IST("pool_used_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
132 [INF_POOL_FAILED] = { .n = IST("pool_failures_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
133 [INF_ULIMIT_N] = { .n = IST("max_fds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
134 [INF_MAXSOCK] = { .n = IST("max_sockets"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
135 [INF_MAXCONN] = { .n = IST("max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
136 [INF_HARD_MAXCONN] = { .n = IST("hard_max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
137 [INF_CURR_CONN] = { .n = IST("current_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
138 [INF_CUM_CONN] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
139 [INF_CUM_REQ] = { .n = IST("requests_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
140 [INF_MAX_SSL_CONNS] = { .n = IST("max_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
141 [INF_CURR_SSL_CONNS] = { .n = IST("current_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
142 [INF_CUM_SSL_CONNS] = { .n = IST("ssl_connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
143 [INF_MAXPIPES] = { .n = IST("max_pipes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
144 [INF_PIPES_USED] = { .n = IST("pipes_used_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
145 [INF_PIPES_FREE] = { .n = IST("pipes_free_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
146 [INF_CONN_RATE] = { .n = IST("current_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
147 [INF_CONN_RATE_LIMIT] = { .n = IST("limit_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
148 [INF_MAX_CONN_RATE] = { .n = IST("max_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
149 [INF_SESS_RATE] = { .n = IST("current_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
150 [INF_SESS_RATE_LIMIT] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
151 [INF_MAX_SESS_RATE] = { .n = IST("max_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
152 [INF_SSL_RATE] = { .n = IST("current_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
153 [INF_SSL_RATE_LIMIT] = { .n = IST("limit_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
154 [INF_MAX_SSL_RATE] = { .n = IST("max_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
155 [INF_SSL_FRONTEND_KEY_RATE] = { .n = IST("current_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
156 [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .n = IST("max_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
157 [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .n = IST("frontend_ssl_reuse"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
158 [INF_SSL_BACKEND_KEY_RATE] = { .n = IST("current_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
159 [INF_SSL_BACKEND_MAX_KEY_RATE] = { .n = IST("max_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
160 [INF_SSL_CACHE_LOOKUPS] = { .n = IST("ssl_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
161 [INF_SSL_CACHE_MISSES] = { .n = IST("ssl_cache_misses_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
162 [INF_COMPRESS_BPS_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
163 [INF_COMPRESS_BPS_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
164 [INF_COMPRESS_BPS_RATE_LIM] = { .n = IST("limit_http_comp"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
165 [INF_ZLIB_MEM_USAGE] = { .n = IST("current_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
166 [INF_MAX_ZLIB_MEM_USAGE] = { .n = IST("max_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
167 [INF_TASKS] = { .n = IST("current_tasks"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
168 [INF_RUN_QUEUE] = { .n = IST("current_run_queue"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
169 [INF_IDLE_PCT] = { .n = IST("idle_time_percent"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
170 //[INF_NODE] ignored
171 //[INF_DESCRIPTION] ignored
172 [INF_STOPPING] = { .n = IST("stopping"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
173 [INF_JOBS] = { .n = IST("jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
174 [INF_UNSTOPPABLE_JOBS] = { .n = IST("unstoppable_jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
175 [INF_LISTENERS] = { .n = IST("listeners"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
176 [INF_ACTIVE_PEERS] = { .n = IST("active_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
177 [INF_CONNECTED_PEERS] = { .n = IST("connected_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
178 [INF_DROPPED_LOGS] = { .n = IST("dropped_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
179 [INF_BUSY_POLLING] = { .n = IST("busy_polling_enabled"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
180 [INF_FAILED_RESOLUTIONS] = { .n = IST("failed_resolutions"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
181 [INF_TOTAL_BYTES_OUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
182 [INF_TOTAL_SPLICED_BYTES_OUT] = { .n = IST("spliced_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
183 [INF_BYTES_OUT_RATE] = { .n = IST("bytes_out_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
184 //[INF_DEBUG_COMMANDS_ISSUED] ignored
William Dauchy7741c332021-02-01 13:11:57 +0100185 [INF_CUM_LOG_MSGS] = { .n = IST("recv_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC },
William Dauchydf9a05d2021-02-01 13:11:59 +0100186 [INF_BUILD_INFO] = { .n = IST("build_info"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC },
Christopher Fauletf959d082019-02-07 15:38:42 +0100187};
188
Christopher Faulet37286a52021-01-20 15:20:53 +0100189/* frontend/backend/server fields */
190const struct promex_metric promex_st_metrics[ST_F_TOTAL_FIELDS] = {
191 //[ST_F_PXNAME] ignored
192 //[ST_F_SVNAME] ignored
William Dauchye3f7bd52021-02-14 23:22:56 +0100193 [ST_F_QCUR] = { .n = IST("current_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
194 [ST_F_QMAX] = { .n = IST("max_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
195 [ST_F_SCUR] = { .n = IST("current_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
196 [ST_F_SMAX] = { .n = IST("max_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
197 [ST_F_SLIM] = { .n = IST("limit_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
198 [ST_F_STOT] = { .n = IST("sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
199 [ST_F_BIN] = { .n = IST("bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
200 [ST_F_BOUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
201 [ST_F_DREQ] = { .n = IST("requests_denied_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC ) },
202 [ST_F_DRESP] = { .n = IST("responses_denied_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
203 [ST_F_EREQ] = { .n = IST("request_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
204 [ST_F_ECON] = { .n = IST("connection_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
205 [ST_F_ERESP] = { .n = IST("response_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
206 [ST_F_WRETR] = { .n = IST("retry_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
207 [ST_F_WREDIS] = { .n = IST("redispatch_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
208 [ST_F_STATUS] = { .n = IST("status"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
209 [ST_F_WEIGHT] = { .n = IST("weight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
210 [ST_F_ACT] = { .n = IST("active_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
211 [ST_F_BCK] = { .n = IST("backup_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) },
212 [ST_F_CHKFAIL] = { .n = IST("check_failures_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_SRV_METRIC) },
213 [ST_F_CHKDOWN] = { .n = IST("check_up_down_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
214 [ST_F_LASTCHG] = { .n = IST("check_last_change_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
215 [ST_F_DOWNTIME] = { .n = IST("downtime_seconds_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
216 [ST_F_QLIMIT] = { .n = IST("queue_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
Christopher Faulet37286a52021-01-20 15:20:53 +0100217 //[ST_F_PID] ignored
218 //[ST_F_IID] ignored
219 //[ST_F_SID] ignored
William Dauchye3f7bd52021-02-14 23:22:56 +0100220 [ST_F_THROTTLE] = { .n = IST("current_throttle"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
221 [ST_F_LBTOT] = { .n = IST("loadbalanced_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
Christopher Faulet37286a52021-01-20 15:20:53 +0100222 //[ST_F_TRACKED] ignored
223 //[ST_F_TYPE] ignored
224 //[ST_F_RATE] ignored
William Dauchye3f7bd52021-02-14 23:22:56 +0100225 [ST_F_RATE_LIM] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
226 [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) },
227 [ST_F_CHECK_STATUS] = { .n = IST("check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
228 [ST_F_CHECK_CODE] = { .n = IST("check_code"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
229 [ST_F_CHECK_DURATION] = { .n = IST("check_duration_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
230 [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) },
231 [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) },
232 [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) },
233 [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) },
234 [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) },
235 [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) },
Christopher Faulet37286a52021-01-20 15:20:53 +0100236 //[ST_F_HANAFAIL] ignored
237 //[ST_F_REQ_RATE] ignored
William Dauchye3f7bd52021-02-14 23:22:56 +0100238 [ST_F_REQ_RATE_MAX] = { .n = IST("http_requests_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
239 [ST_F_REQ_TOT] = { .n = IST("http_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
240 [ST_F_CLI_ABRT] = { .n = IST("client_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
241 [ST_F_SRV_ABRT] = { .n = IST("server_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
242 [ST_F_COMP_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
243 [ST_F_COMP_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
244 [ST_F_COMP_BYP] = { .n = IST("http_comp_bytes_bypassed_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
245 [ST_F_COMP_RSP] = { .n = IST("http_comp_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
246 [ST_F_LASTSESS] = { .n = IST("last_session_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500247 //[ST_F_LAST_CHK] ignored
Christopher Faulet37286a52021-01-20 15:20:53 +0100248 //[ST_F_LAST_AGT] ignored
William Dauchye3f7bd52021-02-14 23:22:56 +0100249 [ST_F_QTIME] = { .n = IST("queue_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
250 [ST_F_CTIME] = { .n = IST("connect_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
251 [ST_F_RTIME] = { .n = IST("response_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
252 [ST_F_TTIME] = { .n = IST("total_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
Christopher Faulet37286a52021-01-20 15:20:53 +0100253 //[ST_F_AGENT_STATUS] ignored
254 //[ST_F_AGENT_CODE] ignored
255 //[ST_F_AGENT_DURATION] ignored
256 //[ST_F_CHECK_DESC] ignored
257 //[ST_F_AGENT_DESC] ignored
258 //[ST_F_CHECK_RISE] ignored
259 //[ST_F_CHECK_FALL] ignored
260 //[ST_F_CHECK_HEALTH] ignored
261 //[ST_F_AGENT_RISE] ignored
262 //[ST_F_AGENT_FALL] ignored
263 //[ST_F_AGENT_HEALTH] ignored
264 //[ST_F_ADDR] ignored
265 //[ST_F_COOKIE] ignored
266 //[ST_F_MODE] ignored
267 //[ST_F_ALGO] ignored
268 //[ST_F_CONN_RATE] ignored
William Dauchye3f7bd52021-02-14 23:22:56 +0100269 [ST_F_CONN_RATE_MAX] = { .n = IST("connections_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) },
270 [ST_F_CONN_TOT] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
271 [ST_F_INTERCEPTED] = { .n = IST("intercepted_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) },
272 [ST_F_DCON] = { .n = IST("denied_connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
273 [ST_F_DSES] = { .n = IST("denied_sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) },
274 [ST_F_WREW] = { .n = IST("failed_header_rewriting_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
275 [ST_F_CONNECT] = { .n = IST("connection_attempts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
276 [ST_F_REUSE] = { .n = IST("connection_reuses_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
277 [ST_F_CACHE_LOOKUPS] = { .n = IST("http_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
278 [ST_F_CACHE_HITS] = { .n = IST("http_cache_hits_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) },
279 [ST_F_SRV_ICUR] = { .n = IST("idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
280 [ST_F_SRV_ILIM] = { .n = IST("idle_connections_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
281 [ST_F_QT_MAX] = { .n = IST("max_queue_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
282 [ST_F_CT_MAX] = { .n = IST("max_connect_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
283 [ST_F_RT_MAX] = { .n = IST("max_response_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
284 [ST_F_TT_MAX] = { .n = IST("max_total_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
285 [ST_F_EINT] = { .n = IST("internal_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
286 [ST_F_IDLE_CONN_CUR] = { .n = IST("unsafe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
287 [ST_F_SAFE_CONN_CUR] = { .n = IST("safe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
288 [ST_F_USED_CONN_CUR] = { .n = IST("used_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
289 [ST_F_NEED_CONN_EST] = { .n = IST("need_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) },
290 [ST_F_UWEIGHT] = { .n = IST("uweight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) },
Christopher Fauletf959d082019-02-07 15:38:42 +0100291};
292
Ilya Shipitsinacf84592021-02-06 22:29:08 +0500293/* Description of overridden stats fields */
Christopher Fauletf959d082019-02-07 15:38:42 +0100294const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = {
William Dauchya1da7ba2021-02-01 13:11:52 +0100295 [ST_F_STATUS] = IST("Current status of the service, per state label value."),
William Dauchyde3c3262021-02-01 13:11:51 +0100296 [ST_F_CHECK_STATUS] = IST("Status of last health check, per state label value."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100297 [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."),
Christopher Faulet2711e512020-02-27 16:12:07 +0100298 [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."),
Christopher Fauletf959d082019-02-07 15:38:42 +0100299 [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."),
300 [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."),
301 [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."),
302 [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."),
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100303 [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"),
304 [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"),
305 [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"),
306 [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100307};
308
William Dauchy69164222021-02-07 20:42:38 +0100309/* stick table base fields */
310enum sticktable_field {
311 STICKTABLE_SIZE = 0,
312 STICKTABLE_USED,
313 /* must always be the last one */
314 STICKTABLE_TOTAL_FIELDS
315};
316
317const struct promex_metric promex_sticktable_metrics[STICKTABLE_TOTAL_FIELDS] = {
318 [STICKTABLE_SIZE] = { .n = IST("size"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC },
319 [STICKTABLE_USED] = { .n = IST("used"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC },
320};
321
322/* stick table base description */
323const struct ist promex_sticktable_metric_desc[STICKTABLE_TOTAL_FIELDS] = {
324 [STICKTABLE_SIZE] = IST("Stick table size."),
325 [STICKTABLE_USED] = IST("Number of entries used in this stick table."),
326};
327
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100328/* Specific labels for all ST_F_HRSP_* fields */
329const struct ist promex_hrsp_code[1 + ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = {
330 [ST_F_HRSP_1XX - ST_F_HRSP_1XX] = IST("1xx"),
331 [ST_F_HRSP_2XX - ST_F_HRSP_1XX] = IST("2xx"),
332 [ST_F_HRSP_3XX - ST_F_HRSP_1XX] = IST("3xx"),
333 [ST_F_HRSP_4XX - ST_F_HRSP_1XX] = IST("4xx"),
334 [ST_F_HRSP_5XX - ST_F_HRSP_1XX] = IST("5xx"),
335 [ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = IST("other"),
Christopher Fauletf959d082019-02-07 15:38:42 +0100336};
337
William Dauchy54938212021-01-27 22:40:16 +0100338enum promex_front_state {
339 PROMEX_FRONT_STATE_DOWN = 0,
340 PROMEX_FRONT_STATE_UP,
341
342 PROMEX_FRONT_STATE_COUNT /* must be last */
343};
344
345const struct ist promex_front_st[PROMEX_FRONT_STATE_COUNT] = {
346 [PROMEX_FRONT_STATE_DOWN] = IST("DOWN"),
347 [PROMEX_FRONT_STATE_UP] = IST("UP"),
348};
349
350enum promex_back_state {
351 PROMEX_BACK_STATE_DOWN = 0,
352 PROMEX_BACK_STATE_UP,
353
354 PROMEX_BACK_STATE_COUNT /* must be last */
355};
356
357const struct ist promex_back_st[PROMEX_BACK_STATE_COUNT] = {
358 [PROMEX_BACK_STATE_DOWN] = IST("DOWN"),
359 [PROMEX_BACK_STATE_UP] = IST("UP"),
360};
361
362enum promex_srv_state {
363 PROMEX_SRV_STATE_DOWN = 0,
364 PROMEX_SRV_STATE_UP,
365 PROMEX_SRV_STATE_MAINT,
366 PROMEX_SRV_STATE_DRAIN,
367 PROMEX_SRV_STATE_NOLB,
368
369 PROMEX_SRV_STATE_COUNT /* must be last */
370};
371
372const struct ist promex_srv_st[PROMEX_SRV_STATE_COUNT] = {
373 [PROMEX_SRV_STATE_DOWN] = IST("DOWN"),
374 [PROMEX_SRV_STATE_UP] = IST("UP"),
375 [PROMEX_SRV_STATE_MAINT] = IST("MAINT"),
376 [PROMEX_SRV_STATE_DRAIN] = IST("DRAIN"),
377 [PROMEX_SRV_STATE_NOLB] = IST("NOLB"),
378};
379
380/* Return the server status. */
381enum promex_srv_state promex_srv_status(struct server *sv)
Christopher Fauletf959d082019-02-07 15:38:42 +0100382{
William Dauchy54938212021-01-27 22:40:16 +0100383 int state = PROMEX_SRV_STATE_DOWN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100384
Christopher Fauletf959d082019-02-07 15:38:42 +0100385 if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) {
William Dauchy54938212021-01-27 22:40:16 +0100386 state = PROMEX_SRV_STATE_UP;
Christopher Fauletf959d082019-02-07 15:38:42 +0100387 if (sv->cur_admin & SRV_ADMF_DRAIN)
William Dauchy54938212021-01-27 22:40:16 +0100388 state = PROMEX_SRV_STATE_DRAIN;
Christopher Fauletf959d082019-02-07 15:38:42 +0100389 }
Christopher Fauletd45d1052019-09-06 16:10:19 +0200390 else if (sv->cur_state == SRV_ST_STOPPING)
William Dauchy54938212021-01-27 22:40:16 +0100391 state = PROMEX_SRV_STATE_NOLB;
Christopher Fauletd45d1052019-09-06 16:10:19 +0200392
393 if (sv->cur_admin & SRV_ADMF_MAINT)
William Dauchy54938212021-01-27 22:40:16 +0100394 state = PROMEX_SRV_STATE_MAINT;
Christopher Fauletf959d082019-02-07 15:38:42 +0100395
396 return state;
397}
398
399/* Convert a field to its string representation and write it in <out>, followed
400 * by a newline, if there is enough space. non-numeric value are converted in
William Dauchy18a2c6e2021-01-22 21:09:47 +0100401 * "NaN" because Prometheus only support numerical values (but it is unexepceted
Christopher Fauletf959d082019-02-07 15:38:42 +0100402 * to process this kind of value). It returns 1 on success. Otherwise, it
403 * returns 0. The buffer's length must not exceed <max> value.
404 */
405static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max)
406{
407 int ret = 0;
408
409 switch (field_format(f, 0)) {
William Dauchy18a2c6e2021-01-22 21:09:47 +0100410 case FF_EMPTY: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100411 case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break;
412 case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break;
413 case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break;
414 case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200415 case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break;
William Dauchy18a2c6e2021-01-22 21:09:47 +0100416 case FF_STR: ret = chunk_strcat(out, "NaN\n"); break;
417 default: ret = chunk_strcat(out, "NaN\n"); break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100418 }
419 if (!ret || out->data > max)
420 return 0;
421 return 1;
422}
423
Christopher Fauletf959d082019-02-07 15:38:42 +0100424/* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It
425 * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0.
426 */
427static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx,
Christopher Faulet37286a52021-01-20 15:20:53 +0100428 const struct promex_metric *metric, const struct ist name,
429 struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100430{
Christopher Faulet37286a52021-01-20 15:20:53 +0100431 struct ist type;
William Dauchy82b2ce22021-02-01 13:11:55 +0100432 struct ist desc;
Christopher Faulet37286a52021-01-20 15:20:53 +0100433
434 switch (metric->type) {
435 case PROMEX_MT_COUNTER:
436 type = ist("counter");
437 break;
438 default:
439 type = ist("gauge");
440 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100441
William Dauchya191b772021-01-15 22:41:39 +0100442 if (istcat(out, ist("# HELP "), max) == -1 ||
443 istcat(out, name, max) == -1 ||
444 istcat(out, ist(" "), max) == -1)
445 goto full;
446
William Dauchy82b2ce22021-02-01 13:11:55 +0100447 if (metric->flags & PROMEX_FL_INFO_METRIC)
448 desc = ist(info_fields[appctx->st2].desc);
William Dauchy69164222021-02-07 20:42:38 +0100449 else if (metric->flags & PROMEX_FL_STICKTABLE_METRIC)
450 desc = promex_sticktable_metric_desc[appctx->st2];
William Dauchy82b2ce22021-02-01 13:11:55 +0100451 else if (!isttest(promex_st_metric_desc[appctx->st2]))
452 desc = ist(stat_fields[appctx->st2].desc);
453 else
454 desc = promex_st_metric_desc[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100455
William Dauchy82b2ce22021-02-01 13:11:55 +0100456 if (istcat(out, desc, max) == -1 ||
457 istcat(out, ist("\n# TYPE "), max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100458 istcat(out, name, max) == -1 ||
459 istcat(out, ist(" "), max) == -1 ||
Christopher Faulet37286a52021-01-20 15:20:53 +0100460 istcat(out, type, max) == -1 ||
Christopher Fauletf959d082019-02-07 15:38:42 +0100461 istcat(out, ist("\n"), max) == -1)
462 goto full;
463
464 return 1;
465
466 full:
467 return 0;
468}
469
470/* Dump the line for <metric>. It starts by the metric name followed by its
471 * labels (proxy name, server name...) between braces and finally its value. If
472 * not already done, the header lines are dumped first. It returns 1 on
473 * success. Otherwise if <out> length exceeds <max>, it returns 0.
474 */
Christopher Faulet37286a52021-01-20 15:20:53 +0100475static int promex_dump_metric(struct appctx *appctx, struct htx *htx, struct ist prefix,
William Dauchyc6464592021-01-27 22:40:17 +0100476 const struct promex_metric *metric, struct field *val,
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100477 struct promex_label *labels, struct ist *out, size_t max)
Christopher Fauletf959d082019-02-07 15:38:42 +0100478{
479 struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 };
480 size_t len = out->len;
481
482 if (out->len + PROMEX_MAX_METRIC_LENGTH > max)
483 return 0;
484
Christopher Faulet37286a52021-01-20 15:20:53 +0100485 /* Fill the metric name */
486 istcat(&name, prefix, PROMEX_MAX_NAME_LEN);
487 istcat(&name, metric->n, PROMEX_MAX_NAME_LEN);
488
489
Christopher Fauletf959d082019-02-07 15:38:42 +0100490 if ((appctx->ctx.stats.flags & PROMEX_FL_METRIC_HDR) &&
Christopher Faulet37286a52021-01-20 15:20:53 +0100491 !promex_dump_metric_header(appctx, htx, metric, name, out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100492 goto full;
493
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100494 if (istcat(out, name, max) == -1)
495 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100496
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100497 if (isttest(labels[0].name)) {
498 int i;
499
500 if (istcat(out, ist("{"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100501 goto full;
Christopher Fauletf959d082019-02-07 15:38:42 +0100502
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100503 for (i = 0; isttest(labels[i].name); i++) {
504 if (!isttest(labels[i].value))
505 continue;
506
507 if ((i && istcat(out, ist(","), max) == -1) ||
508 istcat(out, labels[i].name, max) == -1 ||
509 istcat(out, ist("=\""), max) == -1 ||
510 istcat(out, labels[i].value, max) == -1 ||
511 istcat(out, ist("\""), max) == -1)
512 goto full;
513 }
514
515 if (istcat(out, ist("}"), max) == -1)
Christopher Fauletf959d082019-02-07 15:38:42 +0100516 goto full;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100517
Christopher Fauletf959d082019-02-07 15:38:42 +0100518 }
519
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100520 if (istcat(out, ist(" "), max) == -1)
521 goto full;
522
Christopher Fauletf959d082019-02-07 15:38:42 +0100523 trash.data = out->len;
Christopher Faulet37286a52021-01-20 15:20:53 +0100524 if (!promex_metric_to_str(&trash, val, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100525 goto full;
526 out->len = trash.data;
527
528 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
529 return 1;
530 full:
531 // Restore previous length
532 out->len = len;
533 return 0;
534
535}
536
537
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500538/* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100539 * 0 if <htx> is full and -1 in case of any error. */
540static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx)
541{
542 static struct ist prefix = IST("haproxy_process_");
Christopher Faulet37286a52021-01-20 15:20:53 +0100543 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100544 struct channel *chn = si_ic(appctx->owner);
545 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200546 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
Christopher Fauletf959d082019-02-07 15:38:42 +0100547 int ret = 1;
548
Willy Tarreau0b26b382021-05-08 07:43:53 +0200549 if (!stats_fill_info(info, INF_TOTAL_FIELDS, 0))
William Dauchy5d9b8f32021-01-11 20:07:49 +0100550 return -1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100551
Christopher Faulet37286a52021-01-20 15:20:53 +0100552 for (; appctx->st2 < INF_TOTAL_FIELDS; appctx->st2++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100553 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
554
Christopher Faulet37286a52021-01-20 15:20:53 +0100555 if (!(promex_global_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
556 continue;
557
Christopher Fauletf959d082019-02-07 15:38:42 +0100558 switch (appctx->st2) {
William Dauchy5a982a72021-01-08 13:18:06 +0100559 case INF_BUILD_INFO:
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100560 labels[0].name = ist("version");
561 labels[0].value = ist(HAPROXY_VERSION);
Christopher Faulet37286a52021-01-20 15:20:53 +0100562 val = mkf_u32(FN_GAUGE, 1);
William Dauchy5a982a72021-01-08 13:18:06 +0100563 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100564
565 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100566 val = info[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100567 }
568
William Dauchyc6464592021-01-27 22:40:17 +0100569 if (!promex_dump_metric(appctx, htx, prefix, &promex_global_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100570 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100571 goto full;
572
Christopher Fauletf959d082019-02-07 15:38:42 +0100573 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Christopher Fauletf959d082019-02-07 15:38:42 +0100574 }
575
576 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200577 if (out.len) {
578 if (!htx_add_data_atonce(htx, out))
579 return -1; /* Unexpected and unrecoverable error */
580 channel_add_input(chn, out.len);
581 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100582 return ret;
583 full:
584 ret = 0;
585 goto end;
586}
587
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500588/* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100589 * 0 if <htx> is full and -1 in case of any error. */
590static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx)
591{
592 static struct ist prefix = IST("haproxy_frontend_");
593 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100594 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100595 struct channel *chn = si_ic(appctx->owner);
596 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200597 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchyb9577452021-01-17 18:27:46 +0100598 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100599 int ret = 1;
William Dauchyc6464592021-01-27 22:40:17 +0100600 enum promex_front_state state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100601
Christopher Faulet37286a52021-01-20 15:20:53 +0100602 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
603 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
604 continue;
605
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200606 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100607 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
608
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200609 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100610
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100611 labels[0].name = ist("proxy");
612 labels[0].value = ist2(px->id, strlen(px->id));
613
Christopher Fauletf959d082019-02-07 15:38:42 +0100614 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200615 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100616 goto next_px;
617
William Dauchyb9577452021-01-17 18:27:46 +0100618 if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
619 return -1;
620
Christopher Fauletf959d082019-02-07 15:38:42 +0100621 switch (appctx->st2) {
622 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100623 state = !px->disabled;
Christopher Faulet040b1192021-02-01 15:05:21 +0100624 for (; appctx->ctx.stats.st_code < PROMEX_FRONT_STATE_COUNT; appctx->ctx.stats.st_code++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100625 labels[1].name = ist("state");
Christopher Faulet040b1192021-02-01 15:05:21 +0100626 labels[1].value = promex_front_st[appctx->ctx.stats.st_code];
627 val = mkf_u32(FO_STATUS, state == appctx->ctx.stats.st_code);
William Dauchyc6464592021-01-27 22:40:17 +0100628 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100629 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100630 goto full;
631 }
Christopher Faulet040b1192021-02-01 15:05:21 +0100632 appctx->ctx.stats.st_code = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100633 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100634 case ST_F_REQ_RATE_MAX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100635 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100636 case ST_F_INTERCEPTED:
Christopher Fauletf959d082019-02-07 15:38:42 +0100637 case ST_F_CACHE_LOOKUPS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100638 case ST_F_CACHE_HITS:
Christopher Fauletf959d082019-02-07 15:38:42 +0100639 case ST_F_COMP_IN:
Christopher Fauletf959d082019-02-07 15:38:42 +0100640 case ST_F_COMP_OUT:
Christopher Fauletf959d082019-02-07 15:38:42 +0100641 case ST_F_COMP_BYP:
William Dauchyb9577452021-01-17 18:27:46 +0100642 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100643 if (px->mode != PR_MODE_HTTP)
644 goto next_px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100645 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100646 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100647 case ST_F_HRSP_1XX:
William Dauchyb9577452021-01-17 18:27:46 +0100648 case ST_F_HRSP_2XX:
649 case ST_F_HRSP_3XX:
650 case ST_F_HRSP_4XX:
651 case ST_F_HRSP_5XX:
652 case ST_F_HRSP_OTHER:
Christopher Fauletf959d082019-02-07 15:38:42 +0100653 if (px->mode != PR_MODE_HTTP)
654 goto next_px;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100655 if (appctx->st2 != ST_F_HRSP_1XX)
656 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100657 labels[1].name = ist("code");
658 labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
659 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100660 break;
661
662 default:
Christopher Faulet37286a52021-01-20 15:20:53 +0100663 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100664 }
665
William Dauchyc6464592021-01-27 22:40:17 +0100666 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100667 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100668 goto full;
669 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200670 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100671 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100672 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200673 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100674 }
675
676 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200677 if (out.len) {
678 if (!htx_add_data_atonce(htx, out))
679 return -1; /* Unexpected and unrecoverable error */
680 channel_add_input(chn, out.len);
681 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100682 return ret;
683 full:
684 ret = 0;
685 goto end;
686}
687
William Dauchye3f7bd52021-02-14 23:22:56 +0100688/* Dump listener metrics (prefixed by "haproxy_listen_"). It returns 1 on
689 * success, 0 if <htx> is full and -1 in case of any error. */
690static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx)
691{
692 static struct ist prefix = IST("haproxy_listener_");
693 struct proxy *px;
694 struct field val;
695 struct channel *chn = si_ic(appctx->owner);
696 struct ist out = ist2(trash.area, 0);
697 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
698 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
699 struct listener *li;
700 int ret = 1;
701 enum li_status status;
702
703 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
704 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
705 continue;
706
707 while (appctx->ctx.stats.obj1) {
708 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
709
710 px = appctx->ctx.stats.obj1;
711
712 labels[0].name = ist("proxy");
713 labels[0].value = ist2(px->id, strlen(px->id));
714
715 /* skip the disabled proxies, global frontend and non-networked ones */
716 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_FE))
717 goto next_px;
718
719 li = appctx->ctx.stats.obj2;
720 list_for_each_entry_from(li, &px->conf.listeners, by_fe) {
721
William Dauchye3f7bd52021-02-14 23:22:56 +0100722 if (!li->counters)
723 continue;
724
William Dauchybaf22732021-02-25 00:53:13 +0100725 labels[1].name = ist("listener");
726 labels[1].value = ist2(li->name, strlen(li->name));
727
William Dauchye3f7bd52021-02-14 23:22:56 +0100728 if (!stats_fill_li_stats(px, li, 0, stats,
729 ST_F_TOTAL_FIELDS, &(appctx->st2)))
730 return -1;
731
732 switch (appctx->st2) {
733 case ST_F_STATUS:
734 status = get_li_status(li);
735 for (; appctx->ctx.stats.st_code < LI_STATE_COUNT; appctx->ctx.stats.st_code++) {
736 val = mkf_u32(FO_STATUS, status == appctx->ctx.stats.st_code);
737 labels[2].name = ist("state");
738 labels[2].value = ist(li_status_st[appctx->ctx.stats.st_code]);
739 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
740 &val, labels, &out, max))
741 goto full;
742 }
743 appctx->ctx.stats.st_code = 0;
744 continue;
745 default:
746 val = stats[appctx->st2];
747 }
748
749 if (!promex_dump_metric(appctx, htx, prefix,
750 &promex_st_metrics[appctx->st2],
751 &val, labels, &out, max))
752 goto full;
753 }
754
755 next_px:
756 px = px->next;
757 appctx->ctx.stats.obj1 = px;
758 appctx->ctx.stats.obj2 = (px ? LIST_NEXT(&px->conf.listeners, struct listener *, by_fe) : NULL);
759 }
760 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
761 appctx->ctx.stats.obj1 = proxies_list;
762 appctx->ctx.stats.obj2 = LIST_NEXT(&proxies_list->conf.listeners, struct listener *, by_fe);
763 }
764
765 end:
766 if (out.len) {
767 if (!htx_add_data_atonce(htx, out))
768 return -1; /* Unexpected and unrecoverable error */
769 channel_add_input(chn, out.len);
770 }
771 return ret;
772 full:
773 appctx->ctx.stats.obj2 = li;
774 ret = 0;
775 goto end;
776}
777
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500778/* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100779 * 0 if <htx> is full and -1 in case of any error. */
780static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx)
781{
782 static struct ist prefix = IST("haproxy_backend_");
783 struct proxy *px;
Christopher Faulet37286a52021-01-20 15:20:53 +0100784 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100785 struct channel *chn = si_ic(appctx->owner);
786 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200787 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchy3c6f0062021-01-25 17:29:02 +0100788 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100789 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200790 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100791 enum promex_back_state state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100792
Christopher Faulet37286a52021-01-20 15:20:53 +0100793 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
794 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
795 continue;
796
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200797 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100798 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
799
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200800 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100801
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100802 labels[0].name = ist("proxy");
803 labels[0].value = ist2(px->id, strlen(px->id));
804
Christopher Fauletf959d082019-02-07 15:38:42 +0100805 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200806 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100807 goto next_px;
808
William Dauchy3c6f0062021-01-25 17:29:02 +0100809 if (!stats_fill_be_stats(px, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
810 return -1;
811
Christopher Fauletf959d082019-02-07 15:38:42 +0100812 switch (appctx->st2) {
813 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100814 state = ((px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0);
Christopher Faulet040b1192021-02-01 15:05:21 +0100815 for (; appctx->ctx.stats.st_code < PROMEX_BACK_STATE_COUNT; appctx->ctx.stats.st_code++) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100816 labels[1].name = ist("state");
Christopher Faulet040b1192021-02-01 15:05:21 +0100817 labels[1].value = promex_back_st[appctx->ctx.stats.st_code];
818 val = mkf_u32(FO_STATUS, state == appctx->ctx.stats.st_code);
William Dauchyc6464592021-01-27 22:40:17 +0100819 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100820 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100821 goto full;
822 }
Christopher Faulet040b1192021-02-01 15:05:21 +0100823 appctx->ctx.stats.st_code = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100824 goto next_px;
Christopher Fauletf959d082019-02-07 15:38:42 +0100825 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200826 secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100827 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100828 break;
829 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200830 secs = (double)swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100831 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100832 break;
833 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200834 secs = (double)swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100835 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100836 break;
837 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200838 secs = (double)swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100839 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100840 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100841 case ST_F_QT_MAX:
842 secs = (double)px->be_counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100843 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100844 break;
845 case ST_F_CT_MAX:
846 secs = (double)px->be_counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100847 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100848 break;
849 case ST_F_RT_MAX:
850 secs = (double)px->be_counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100851 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100852 break;
853 case ST_F_TT_MAX:
854 secs = (double)px->be_counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100855 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100856 break;
Christopher Fauletf959d082019-02-07 15:38:42 +0100857 case ST_F_REQ_TOT:
William Dauchy3c6f0062021-01-25 17:29:02 +0100858 case ST_F_CACHE_LOOKUPS:
859 case ST_F_CACHE_HITS:
860 case ST_F_COMP_IN:
861 case ST_F_COMP_OUT:
862 case ST_F_COMP_BYP:
863 case ST_F_COMP_RSP:
Christopher Fauletf959d082019-02-07 15:38:42 +0100864 if (px->mode != PR_MODE_HTTP)
865 goto next_px;
William Dauchy3c6f0062021-01-25 17:29:02 +0100866 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100867 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100868 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100869 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100870 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100871 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100872 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +0100873 case ST_F_HRSP_OTHER:
874 if (px->mode != PR_MODE_HTTP)
875 goto next_px;
Christopher Faulet32ef48e2021-02-01 14:55:37 +0100876 if (appctx->st2 != ST_F_HRSP_1XX)
877 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100878 labels[1].name = ist("code");
879 labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
William Dauchy3c6f0062021-01-25 17:29:02 +0100880 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100881 break;
882
883 default:
William Dauchy3c6f0062021-01-25 17:29:02 +0100884 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +0100885 }
886
William Dauchy3c6f0062021-01-25 17:29:02 +0100887 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100888 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +0100889 goto full;
890 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200891 appctx->ctx.stats.obj1 = px->next;
Christopher Fauletf959d082019-02-07 15:38:42 +0100892 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100893 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200894 appctx->ctx.stats.obj1 = proxies_list;
Christopher Fauletf959d082019-02-07 15:38:42 +0100895 }
896
897 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +0200898 if (out.len) {
899 if (!htx_add_data_atonce(htx, out))
900 return -1; /* Unexpected and unrecoverable error */
901 channel_add_input(chn, out.len);
902 }
Christopher Fauletf959d082019-02-07 15:38:42 +0100903 return ret;
904 full:
905 ret = 0;
906 goto end;
907}
908
Ilya Shipitsince7b00f2020-03-23 22:28:40 +0500909/* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success,
Christopher Fauletf959d082019-02-07 15:38:42 +0100910 * 0 if <htx> is full and -1 in case of any error. */
911static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx)
912{
913 static struct ist prefix = IST("haproxy_server_");
914 struct proxy *px;
915 struct server *sv;
Christopher Faulet37286a52021-01-20 15:20:53 +0100916 struct field val;
Christopher Fauletf959d082019-02-07 15:38:42 +0100917 struct channel *chn = si_ic(appctx->owner);
918 struct ist out = ist2(trash.area, 0);
Christopher Faulet11921e62019-07-03 11:43:17 +0200919 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
William Dauchybde2bf62021-01-25 17:29:04 +0100920 struct field *stats = stat_l[STATS_DOMAIN_PROXY];
Christopher Fauletf959d082019-02-07 15:38:42 +0100921 int ret = 1;
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200922 double secs;
William Dauchyc6464592021-01-27 22:40:17 +0100923 enum promex_srv_state state;
William Dauchyde3c3262021-02-01 13:11:51 +0100924 const char *check_state;
Christopher Fauletf959d082019-02-07 15:38:42 +0100925
Christopher Faulet37286a52021-01-20 15:20:53 +0100926 for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) {
927 if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
928 continue;
929
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200930 while (appctx->ctx.stats.obj1) {
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100931 struct promex_label labels[PROMEX_MAX_LABELS-1] = {};
932
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200933 px = appctx->ctx.stats.obj1;
Christopher Fauletf959d082019-02-07 15:38:42 +0100934
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100935 labels[0].name = ist("proxy");
936 labels[0].value = ist2(px->id, strlen(px->id));
937
Christopher Fauletf959d082019-02-07 15:38:42 +0100938 /* skip the disabled proxies, global frontend and non-networked ones */
Willy Tarreauc3914d42020-09-24 08:39:22 +0200939 if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE))
Christopher Fauletf959d082019-02-07 15:38:42 +0100940 goto next_px;
941
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +0200942 while (appctx->ctx.stats.obj2) {
943 sv = appctx->ctx.stats.obj2;
Christopher Fauletf959d082019-02-07 15:38:42 +0100944
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100945 labels[1].name = ist("server");
946 labels[1].value = ist2(sv->id, strlen(sv->id));
947
William Dauchybde2bf62021-01-25 17:29:04 +0100948 if (!stats_fill_sv_stats(px, sv, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2)))
949 return -1;
950
Christopher Fauleteba22942019-11-19 14:18:24 +0100951 if ((appctx->ctx.stats.flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT))
952 goto next_sv;
953
Christopher Fauletf959d082019-02-07 15:38:42 +0100954 switch (appctx->st2) {
955 case ST_F_STATUS:
William Dauchyc6464592021-01-27 22:40:17 +0100956 state = promex_srv_status(sv);
William Dauchy64a38052021-02-14 22:26:24 +0100957 for (; appctx->ctx.stats.st_code < PROMEX_SRV_STATE_COUNT; appctx->ctx.stats.st_code++) {
Christopher Faulet040b1192021-02-01 15:05:21 +0100958 val = mkf_u32(FO_STATUS, state == appctx->ctx.stats.st_code);
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100959 labels[2].name = ist("state");
Christopher Faulet040b1192021-02-01 15:05:21 +0100960 labels[2].value = promex_srv_st[appctx->ctx.stats.st_code];
William Dauchyc6464592021-01-27 22:40:17 +0100961 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +0100962 &val, labels, &out, max))
William Dauchyc6464592021-01-27 22:40:17 +0100963 goto full;
964 }
Christopher Faulet040b1192021-02-01 15:05:21 +0100965 appctx->ctx.stats.st_code = 0;
William Dauchyc6464592021-01-27 22:40:17 +0100966 goto next_sv;
Christopher Fauletf959d082019-02-07 15:38:42 +0100967 case ST_F_QTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200968 secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100969 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100970 break;
971 case ST_F_CTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200972 secs = (double)swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100973 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100974 break;
975 case ST_F_RTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200976 secs = (double)swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100977 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100978 break;
979 case ST_F_TTIME:
Christopher Fauletaf4bf142019-09-24 16:35:19 +0200980 secs = (double)swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES) / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100981 val = mkf_flt(FN_AVG, secs);
Christopher Fauletf959d082019-02-07 15:38:42 +0100982 break;
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100983 case ST_F_QT_MAX:
984 secs = (double)sv->counters.qtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100985 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100986 break;
987 case ST_F_CT_MAX:
988 secs = (double)sv->counters.ctime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100989 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100990 break;
991 case ST_F_RT_MAX:
992 secs = (double)sv->counters.dtime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100993 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100994 break;
995 case ST_F_TT_MAX:
996 secs = (double)sv->counters.ttime_max / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +0100997 val = mkf_flt(FN_MAX, secs);
Christopher Faulet8fc027d2019-11-08 15:05:31 +0100998 break;
Christopher Fauletcf403f32019-11-21 14:35:46 +0100999 case ST_F_CHECK_STATUS:
1000 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1001 goto next_sv;
William Dauchyde3c3262021-02-01 13:11:51 +01001002
Christopher Faulet040b1192021-02-01 15:05:21 +01001003 for (; appctx->ctx.stats.st_code < HCHK_STATUS_SIZE; appctx->ctx.stats.st_code++) {
1004 if (get_check_status_result(appctx->ctx.stats.st_code) < CHK_RES_FAILED)
William Dauchyde3c3262021-02-01 13:11:51 +01001005 continue;
Christopher Faulet040b1192021-02-01 15:05:21 +01001006 val = mkf_u32(FO_STATUS, sv->check.status == appctx->ctx.stats.st_code);
1007 check_state = get_check_status_info(appctx->ctx.stats.st_code);
William Dauchyde3c3262021-02-01 13:11:51 +01001008 labels[2].name = ist("state");
1009 labels[2].value = ist2(check_state, strlen(check_state));
1010 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
1011 &val, labels, &out, max))
1012 goto full;
1013 }
Christopher Faulet040b1192021-02-01 15:05:21 +01001014 appctx->ctx.stats.st_code = 0;
William Dauchyde3c3262021-02-01 13:11:51 +01001015 goto next_sv;
Christopher Fauletcf403f32019-11-21 14:35:46 +01001016 case ST_F_CHECK_CODE:
1017 if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED)
1018 goto next_sv;
Christopher Faulet37286a52021-01-20 15:20:53 +01001019 val = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code);
Christopher Fauletcf403f32019-11-21 14:35:46 +01001020 break;
Christopher Faulet2711e512020-02-27 16:12:07 +01001021 case ST_F_CHECK_DURATION:
1022 if (sv->check.status < HCHK_STATUS_CHECKED)
1023 goto next_sv;
1024 secs = (double)sv->check.duration / 1000.0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001025 val = mkf_flt(FN_DURATION, secs);
Christopher Faulet2711e512020-02-27 16:12:07 +01001026 break;
Marcin Deranek3c27dda2020-05-15 18:32:51 +02001027 case ST_F_REQ_TOT:
Christopher Fauletf959d082019-02-07 15:38:42 +01001028 if (px->mode != PR_MODE_HTTP)
1029 goto next_px;
William Dauchybde2bf62021-01-25 17:29:04 +01001030 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001031 break;
Christopher Faulet32ef48e2021-02-01 14:55:37 +01001032 case ST_F_HRSP_1XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001033 case ST_F_HRSP_2XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001034 case ST_F_HRSP_3XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001035 case ST_F_HRSP_4XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001036 case ST_F_HRSP_5XX:
Christopher Fauletf959d082019-02-07 15:38:42 +01001037 case ST_F_HRSP_OTHER:
1038 if (px->mode != PR_MODE_HTTP)
1039 goto next_px;
Christopher Faulet32ef48e2021-02-01 14:55:37 +01001040 if (appctx->st2 != ST_F_HRSP_1XX)
1041 appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR;
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001042 labels[2].name = ist("code");
1043 labels[2].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX];
William Dauchybde2bf62021-01-25 17:29:04 +01001044 val = stats[appctx->st2];
Christopher Fauletc55a6262020-07-10 15:39:39 +02001045 break;
Christopher Fauletf959d082019-02-07 15:38:42 +01001046
1047 default:
William Dauchybde2bf62021-01-25 17:29:04 +01001048 val = stats[appctx->st2];
Christopher Fauletf959d082019-02-07 15:38:42 +01001049 }
1050
William Dauchyc6464592021-01-27 22:40:17 +01001051 if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2],
Christopher Faulet5a2f9382021-01-28 11:24:17 +01001052 &val, labels, &out, max))
Christopher Fauletf959d082019-02-07 15:38:42 +01001053 goto full;
Christopher Fauleteba22942019-11-19 14:18:24 +01001054 next_sv:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001055 appctx->ctx.stats.obj2 = sv->next;
Christopher Fauletf959d082019-02-07 15:38:42 +01001056 }
1057
1058 next_px:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001059 appctx->ctx.stats.obj1 = px->next;
1060 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001061 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001062 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001063 appctx->ctx.stats.obj1 = proxies_list;
1064 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletf959d082019-02-07 15:38:42 +01001065 }
1066
1067
1068 end:
Christopher Faulet0c55a152019-07-04 10:03:28 +02001069 if (out.len) {
1070 if (!htx_add_data_atonce(htx, out))
1071 return -1; /* Unexpected and unrecoverable error */
1072 channel_add_input(chn, out.len);
1073 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001074 return ret;
1075 full:
1076 ret = 0;
1077 goto end;
1078}
1079
William Dauchy69164222021-02-07 20:42:38 +01001080/* Dump stick table metrics (prefixed by "haproxy_sticktable_"). It returns 1 on success,
1081 * 0 if <htx> is full and -1 in case of any error. */
1082static int promex_dump_sticktable_metrics(struct appctx *appctx, struct htx *htx)
1083{
1084 static struct ist prefix = IST("haproxy_sticktable_");
1085 struct field val;
1086 struct channel *chn = si_ic(appctx->owner);
1087 struct ist out = ist2(trash.area, 0);
1088 size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx));
1089 int ret = 1;
1090 struct stktable *t;
1091
1092 for (; appctx->st2 < STICKTABLE_TOTAL_FIELDS; appctx->st2++) {
1093 if (!(promex_sticktable_metrics[appctx->st2].flags & appctx->ctx.stats.flags))
1094 continue;
1095
1096 while (appctx->ctx.stats.obj1) {
1097 struct promex_label labels[PROMEX_MAX_LABELS - 1] = {};
1098
1099 t = appctx->ctx.stats.obj1;
1100 if (!t->size)
1101 goto next_px;
1102
1103 labels[0].name = ist("name");
1104 labels[0].value = ist2(t->id, strlen(t->id));
1105 labels[1].name = ist("type");
1106 labels[1].value = ist2(stktable_types[t->type].kw, strlen(stktable_types[t->type].kw));
1107 switch (appctx->st2) {
1108 case STICKTABLE_SIZE:
1109 val = mkf_u32(FN_GAUGE, t->size);
1110 break;
1111 case STICKTABLE_USED:
1112 val = mkf_u32(FN_GAUGE, t->current);
1113 break;
1114 default:
1115 goto next_px;
1116 }
1117
1118 if (!promex_dump_metric(appctx, htx, prefix,
1119 &promex_sticktable_metrics[appctx->st2],
1120 &val, labels, &out, max))
1121 goto full;
1122
1123 next_px:
1124 appctx->ctx.stats.obj1 = t->next;
1125 }
1126 appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR;
1127 appctx->ctx.stats.obj1 = stktables_list;
1128 }
1129
1130 end:
1131 if (out.len) {
1132 if (!htx_add_data_atonce(htx, out))
1133 return -1; /* Unexpected and unrecoverable error */
1134 channel_add_input(chn, out.len);
1135 }
1136 return ret;
1137 full:
1138 ret = 0;
1139 goto end;
1140}
1141
Christopher Fauletf959d082019-02-07 15:38:42 +01001142/* Dump all metrics (global, frontends, backends and servers) depending on the
1143 * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001144 * -1 in case of any error.
1145 * Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as
1146 * a pointer to the current server/listener. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001147static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1148{
1149 int ret;
1150
1151 switch (appctx->st1) {
1152 case PROMEX_DUMPER_INIT:
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001153 appctx->ctx.stats.obj1 = NULL;
1154 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001155 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC);
Christopher Faulet040b1192021-02-01 15:05:21 +01001156 appctx->ctx.stats.st_code = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001157 appctx->st2 = INF_NAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001158 appctx->st1 = PROMEX_DUMPER_GLOBAL;
1159 /* fall through */
1160
1161 case PROMEX_DUMPER_GLOBAL:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001162 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_GLOBAL) {
1163 ret = promex_dump_global_metrics(appctx, htx);
1164 if (ret <= 0) {
1165 if (ret == -1)
1166 goto error;
1167 goto full;
1168 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001169 }
1170
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001171 appctx->ctx.stats.obj1 = proxies_list;
1172 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletefde9552020-06-05 08:18:56 +02001173 appctx->ctx.stats.flags &= ~PROMEX_FL_INFO_METRIC;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001174 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC);
Christopher Faulet040b1192021-02-01 15:05:21 +01001175 appctx->ctx.stats.st_code = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001176 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001177 appctx->st1 = PROMEX_DUMPER_FRONT;
1178 /* fall through */
1179
1180 case PROMEX_DUMPER_FRONT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001181 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_FRONT) {
1182 ret = promex_dump_front_metrics(appctx, htx);
1183 if (ret <= 0) {
1184 if (ret == -1)
1185 goto error;
1186 goto full;
1187 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001188 }
1189
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001190 appctx->ctx.stats.obj1 = proxies_list;
William Dauchye3f7bd52021-02-14 23:22:56 +01001191 appctx->ctx.stats.obj2 = LIST_NEXT(&proxies_list->conf.listeners, struct listener *, by_fe);
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001192 appctx->ctx.stats.flags &= ~PROMEX_FL_FRONT_METRIC;
William Dauchye3f7bd52021-02-14 23:22:56 +01001193 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_LI_METRIC);
1194 appctx->ctx.stats.st_code = 0;
1195 appctx->st2 = ST_F_PXNAME;
1196 appctx->st1 = PROMEX_DUMPER_LI;
1197 /* fall through */
1198
1199 case PROMEX_DUMPER_LI:
1200 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_LI) {
1201 ret = promex_dump_listener_metrics(appctx, htx);
1202 if (ret <= 0) {
1203 if (ret == -1)
1204 goto error;
1205 goto full;
1206 }
1207 }
1208
1209 appctx->ctx.stats.obj1 = proxies_list;
1210 appctx->ctx.stats.obj2 = NULL;
1211 appctx->ctx.stats.flags &= ~PROMEX_FL_LI_METRIC;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001212 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC);
Christopher Faulet040b1192021-02-01 15:05:21 +01001213 appctx->ctx.stats.st_code = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001214 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001215 appctx->st1 = PROMEX_DUMPER_BACK;
1216 /* fall through */
1217
1218 case PROMEX_DUMPER_BACK:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001219 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_BACK) {
1220 ret = promex_dump_back_metrics(appctx, htx);
1221 if (ret <= 0) {
1222 if (ret == -1)
1223 goto error;
1224 goto full;
1225 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001226 }
1227
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001228 appctx->ctx.stats.obj1 = proxies_list;
1229 appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL);
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001230 appctx->ctx.stats.flags &= ~PROMEX_FL_BACK_METRIC;
1231 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
Christopher Faulet040b1192021-02-01 15:05:21 +01001232 appctx->ctx.stats.st_code = 0;
Christopher Faulet37286a52021-01-20 15:20:53 +01001233 appctx->st2 = ST_F_PXNAME;
Christopher Fauletf959d082019-02-07 15:38:42 +01001234 appctx->st1 = PROMEX_DUMPER_SRV;
1235 /* fall through */
1236
1237 case PROMEX_DUMPER_SRV:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001238 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_SERVER) {
1239 ret = promex_dump_srv_metrics(appctx, htx);
1240 if (ret <= 0) {
1241 if (ret == -1)
1242 goto error;
1243 goto full;
1244 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001245 }
1246
William Dauchy69164222021-02-07 20:42:38 +01001247 appctx->ctx.stats.obj1 = stktables_list;
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001248 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletb713c4f2021-01-20 15:02:50 +01001249 appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC);
William Dauchy69164222021-02-07 20:42:38 +01001250 appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC);
1251 appctx->st2 = STICKTABLE_SIZE;
1252 appctx->st1 = PROMEX_DUMPER_STICKTABLE;
1253 /* fall through */
1254
1255 case PROMEX_DUMPER_STICKTABLE:
1256 if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_STICKTABLE) {
1257 ret = promex_dump_sticktable_metrics(appctx, htx);
1258 if (ret <= 0) {
1259 if (ret == -1)
1260 goto error;
1261 goto full;
1262 }
1263 }
1264
1265 appctx->ctx.stats.obj1 = NULL;
1266 appctx->ctx.stats.obj2 = NULL;
1267 appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC);
Christopher Fauletf959d082019-02-07 15:38:42 +01001268 appctx->st2 = 0;
1269 appctx->st1 = PROMEX_DUMPER_DONE;
1270 /* fall through */
1271
1272 case PROMEX_DUMPER_DONE:
1273 default:
1274 break;
1275 }
1276
1277 return 1;
1278
1279 full:
1280 si_rx_room_blk(si);
1281 return 0;
1282 error:
1283 /* unrecoverable error */
Amaury Denoyelleda5b6d12020-10-02 18:32:02 +02001284 appctx->ctx.stats.obj1 = NULL;
1285 appctx->ctx.stats.obj2 = NULL;
Christopher Fauletf959d082019-02-07 15:38:42 +01001286 appctx->ctx.stats.flags = 0;
1287 appctx->st2 = 0;
1288 appctx->st1 = PROMEX_DUMPER_DONE;
1289 return -1;
1290}
1291
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001292/* Parse the query string of request URI to filter the metrics. It returns 1 on
Christopher Faulet78407ce2019-11-18 14:47:08 +01001293 * success and -1 on error. */
1294static int promex_parse_uri(struct appctx *appctx, struct stream_interface *si)
1295{
1296 struct channel *req = si_oc(si);
1297 struct channel *res = si_ic(si);
1298 struct htx *req_htx, *res_htx;
1299 struct htx_sl *sl;
William Dauchyc65f6562019-11-26 12:56:26 +01001300 char *p, *key, *value;
1301 const char *end;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001302 struct buffer *err;
1303 int default_scopes = PROMEX_FL_SCOPE_ALL;
1304 int len;
1305
1306 /* Get the query-string */
1307 req_htx = htxbuf(&req->buf);
1308 sl = http_get_stline(req_htx);
1309 if (!sl)
1310 goto error;
1311 p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?');
1312 if (!p)
1313 goto end;
1314 end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl);
Christopher Faulet78407ce2019-11-18 14:47:08 +01001315
William Dauchyc65f6562019-11-26 12:56:26 +01001316 /* copy the query-string */
1317 len = end - p;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001318 chunk_reset(&trash);
1319 memcpy(trash.area, p, len);
1320 trash.area[len] = 0;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001321 p = trash.area;
William Dauchyc65f6562019-11-26 12:56:26 +01001322 end = trash.area + len;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001323
1324 /* Parse the query-string */
William Dauchyc65f6562019-11-26 12:56:26 +01001325 while (p < end && *p && *p != '#') {
1326 value = NULL;
1327
1328 /* decode parameter name */
1329 key = p;
1330 while (p < end && *p != '=' && *p != '&' && *p != '#')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001331 ++p;
William Dauchyc65f6562019-11-26 12:56:26 +01001332 /* found a value */
1333 if (*p == '=') {
1334 *(p++) = 0;
1335 value = p;
1336 }
1337 else if (*p == '&')
1338 *(p++) = 0;
1339 else if (*p == '#')
1340 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001341 len = url_decode(key, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001342 if (len == -1)
1343 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001344
William Dauchyc65f6562019-11-26 12:56:26 +01001345 /* decode value */
1346 if (value) {
1347 while (p < end && *p != '=' && *p != '&' && *p != '#')
1348 ++p;
1349 if (*p == '=')
1350 goto error;
1351 if (*p == '&')
1352 *(p++) = 0;
1353 else if (*p == '#')
1354 *p = 0;
Willy Tarreau62ba9ba2020-04-23 17:54:47 +02001355 len = url_decode(value, 1);
William Dauchyc65f6562019-11-26 12:56:26 +01001356 if (len == -1)
1357 goto error;
1358 }
1359
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001360 if (strcmp(key, "scope") == 0) {
William Dauchyc65f6562019-11-26 12:56:26 +01001361 default_scopes = 0; /* at least a scope defined, unset default scopes */
1362 if (!value)
1363 goto error;
1364 else if (*value == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001365 appctx->ctx.stats.flags &= ~PROMEX_FL_SCOPE_ALL;
William Dauchyc65f6562019-11-26 12:56:26 +01001366 else if (*value == '*')
Christopher Faulet78407ce2019-11-18 14:47:08 +01001367 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_ALL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001368 else if (strcmp(value, "global") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001369 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_GLOBAL;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001370 else if (strcmp(value, "server") == 0)
William Dauchyc65f6562019-11-26 12:56:26 +01001371 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_SERVER;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001372 else if (strcmp(value, "backend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001373 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_BACK;
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001374 else if (strcmp(value, "frontend") == 0)
Christopher Faulet78407ce2019-11-18 14:47:08 +01001375 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_FRONT;
William Dauchye3f7bd52021-02-14 23:22:56 +01001376 else if (strcmp(value, "listener") == 0)
1377 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_LI;
William Dauchy69164222021-02-07 20:42:38 +01001378 else if (strcmp(value, "sticktable") == 0)
1379 appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_STICKTABLE;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001380 else
1381 goto error;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001382 }
Tim Duesterhus8cb12a82021-01-02 22:31:55 +01001383 else if (strcmp(key, "no-maint") == 0)
Christopher Fauleteba22942019-11-19 14:18:24 +01001384 appctx->ctx.stats.flags |= PROMEX_FL_NO_MAINT_SRV;
Christopher Faulet78407ce2019-11-18 14:47:08 +01001385 }
1386
1387 end:
1388 appctx->ctx.stats.flags |= default_scopes;
1389 return 1;
1390
1391 error:
1392 err = &http_err_chunks[HTTP_ERR_400];
1393 channel_erase(res);
1394 res->buf.data = b_data(err);
1395 memcpy(res->buf.area, b_head(err), b_data(err));
1396 res_htx = htx_from_buf(&res->buf);
1397 channel_add_input(res, res_htx->data);
1398 appctx->st0 = PROMEX_ST_END;
1399 return -1;
1400}
1401
Christopher Fauletf959d082019-02-07 15:38:42 +01001402/* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is
1403 * full. */
1404static int promex_send_headers(struct appctx *appctx, struct stream_interface *si, struct htx *htx)
1405{
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001406 struct channel *chn = si_ic(appctx->owner);
Christopher Fauletf959d082019-02-07 15:38:42 +01001407 struct htx_sl *sl;
1408 unsigned int flags;
1409
1410 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);
1411 sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK"));
1412 if (!sl)
1413 goto full;
1414 sl->info.res.status = 200;
1415 if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) ||
Christopher Fauletf959d082019-02-07 15:38:42 +01001416 !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) ||
1417 !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) ||
1418 !htx_add_endof(htx, HTX_BLK_EOH))
1419 goto full;
1420
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001421 channel_add_input(chn, htx->data);
Christopher Fauletf959d082019-02-07 15:38:42 +01001422 return 1;
1423 full:
1424 htx_reset(htx);
1425 si_rx_room_blk(si);
1426 return 0;
1427}
1428
1429/* The function returns 1 if the initialisation is complete, 0 if
1430 * an errors occurs and -1 if more data are required for initializing
1431 * the applet.
1432 */
1433static int promex_appctx_init(struct appctx *appctx, struct proxy *px, struct stream *strm)
1434{
1435 appctx->st0 = PROMEX_ST_INIT;
1436 return 1;
1437}
1438
1439/* The main I/O handler for the promex applet. */
1440static void promex_appctx_handle_io(struct appctx *appctx)
1441{
1442 struct stream_interface *si = appctx->owner;
1443 struct stream *s = si_strm(si);
1444 struct channel *req = si_oc(si);
1445 struct channel *res = si_ic(si);
1446 struct htx *req_htx, *res_htx;
1447 int ret;
1448
1449 res_htx = htx_from_buf(&res->buf);
Christopher Fauletf959d082019-02-07 15:38:42 +01001450 if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO))
1451 goto out;
1452
Ilya Shipitsince7b00f2020-03-23 22:28:40 +05001453 /* Check if the input buffer is available. */
Christopher Fauletf959d082019-02-07 15:38:42 +01001454 if (!b_size(&res->buf)) {
1455 si_rx_room_blk(si);
1456 goto out;
1457 }
1458
1459 switch (appctx->st0) {
1460 case PROMEX_ST_INIT:
Christopher Faulet78407ce2019-11-18 14:47:08 +01001461 ret = promex_parse_uri(appctx, si);
1462 if (ret <= 0) {
1463 if (ret == -1)
1464 goto error;
1465 goto out;
1466 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001467 appctx->st0 = PROMEX_ST_HEAD;
1468 appctx->st1 = PROMEX_DUMPER_INIT;
1469 /* fall through */
1470
1471 case PROMEX_ST_HEAD:
1472 if (!promex_send_headers(appctx, si, res_htx))
1473 goto out;
1474 appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP);
1475 /* fall through */
1476
1477 case PROMEX_ST_DUMP:
1478 ret = promex_dump_metrics(appctx, si, res_htx);
1479 if (ret <= 0) {
1480 if (ret == -1)
1481 goto error;
1482 goto out;
1483 }
1484 appctx->st0 = PROMEX_ST_DONE;
1485 /* fall through */
1486
1487 case PROMEX_ST_DONE:
Christopher Fauletd1ac2b92020-12-02 19:12:22 +01001488 /* no more data are expected. Don't add TLR because mux-h1 will take care of it */
1489 res_htx->flags |= HTX_FL_EOM;
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001490 appctx->st0 = PROMEX_ST_END;
1491 /* fall through */
Christopher Fauletf959d082019-02-07 15:38:42 +01001492
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001493 case PROMEX_ST_END:
1494 if (!(res->flags & CF_SHUTR)) {
1495 res->flags |= CF_READ_NULL;
1496 si_shutr(si);
1497 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001498 }
1499
Christopher Fauletf959d082019-02-07 15:38:42 +01001500 out:
1501 htx_to_buf(res_htx, &res->buf);
Christopher Faulet9744f7c2019-03-27 15:48:53 +01001502
1503 /* eat the whole request */
1504 if (co_data(req)) {
1505 req_htx = htx_from_buf(&req->buf);
1506 co_htx_skip(req, req_htx, co_data(req));
1507 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001508 return;
1509
1510 error:
1511 res->flags |= CF_READ_NULL;
1512 si_shutr(si);
1513 si_shutw(si);
1514}
1515
1516struct applet promex_applet = {
1517 .obj_type = OBJ_TYPE_APPLET,
1518 .name = "<PROMEX>", /* used for logging */
1519 .init = promex_appctx_init,
1520 .fct = promex_appctx_handle_io,
1521};
1522
1523static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px,
1524 struct act_rule *rule, char **err)
1525{
1526 /* Prometheus exporter service is only available on "http-request" rulesets */
1527 if (rule->from != ACT_F_HTTP_REQ) {
1528 memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets");
1529 return ACT_RET_PRS_ERR;
1530 }
Christopher Fauletf959d082019-02-07 15:38:42 +01001531
1532 /* Add applet pointer in the rule. */
1533 rule->applet = promex_applet;
1534
1535 return ACT_RET_PRS_OK;
1536}
1537static void promex_register_build_options(void)
1538{
1539 char *ptr = NULL;
1540
1541 memprintf(&ptr, "Built with the Prometheus exporter as a service");
1542 hap_register_build_opts(ptr, 1);
1543}
1544
1545
1546static struct action_kw_list service_actions = { ILH, {
1547 { "prometheus-exporter", service_parse_prometheus_exporter },
1548 { /* END */ }
1549}};
1550
1551INITCALL1(STG_REGISTER, service_keywords_register, &service_actions);
1552INITCALL0(STG_REGISTER, promex_register_build_options);