Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1 | /* |
| 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 Tarreau | 122eba9 | 2020-06-04 10:15:32 +0200 | [diff] [blame] | 16 | #include <haproxy/action-t.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 17 | #include <haproxy/api.h> |
Willy Tarreau | 3f0f82e | 2020-06-04 19:42:41 +0200 | [diff] [blame] | 18 | #include <haproxy/applet.h> |
Willy Tarreau | 4980160 | 2020-06-04 22:50:02 +0200 | [diff] [blame] | 19 | #include <haproxy/backend.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 20 | #include <haproxy/cfgparse.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 21 | #include <haproxy/compression.h> |
Christopher Faulet | c55a626 | 2020-07-10 15:39:39 +0200 | [diff] [blame] | 22 | #include <haproxy/dns.h> |
Willy Tarreau | 762d7a5 | 2020-06-04 11:23:07 +0200 | [diff] [blame] | 23 | #include <haproxy/frontend.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 24 | #include <haproxy/global.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 25 | #include <haproxy/http.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 26 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 27 | #include <haproxy/htx.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 28 | #include <haproxy/list.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 29 | #include <haproxy/listener.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 30 | #include <haproxy/log.h> |
Willy Tarreau | 551271d | 2020-06-04 08:32:23 +0200 | [diff] [blame] | 31 | #include <haproxy/pipe.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 32 | #include <haproxy/pool.h> |
Willy Tarreau | a264d96 | 2020-06-04 22:29:18 +0200 | [diff] [blame] | 33 | #include <haproxy/proxy.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 34 | #include <haproxy/sample.h> |
Willy Tarreau | 1e56f92 | 2020-06-04 23:20:13 +0200 | [diff] [blame] | 35 | #include <haproxy/server.h> |
Willy Tarreau | 209108d | 2020-06-04 20:30:20 +0200 | [diff] [blame] | 36 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 37 | #include <haproxy/stats.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 38 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 39 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 40 | #include <haproxy/task.h> |
William Dauchy | 5a982a7 | 2021-01-08 13:18:06 +0100 | [diff] [blame] | 41 | #include <haproxy/version.h> |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 42 | |
| 43 | /* Prometheus exporter applet states (appctx->st0) */ |
| 44 | enum { |
| 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 Faulet | 9744f7c | 2019-03-27 15:48:53 +0100 | [diff] [blame] | 49 | PROMEX_ST_END, /* treatment terminated */ |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | /* Prometheus exporter dumper states (appctx->st1) */ |
| 53 | enum { |
| 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 Faulet | b713c4f | 2021-01-20 15:02:50 +0100 | [diff] [blame] | 66 | #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 Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 74 | |
| 75 | #define PROMEX_FL_SCOPE_ALL (PROMEX_FL_SCOPE_GLOBAL|PROMEX_FL_SCOPE_FRONT|PROMEX_FL_SCOPE_BACK|PROMEX_FL_SCOPE_SERVER) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 76 | |
Christopher Faulet | 0312c0d | 2021-01-20 15:19:12 +0100 | [diff] [blame] | 77 | /* Promtheus metric type (gauge or counter) */ |
| 78 | enum promex_mt_type { |
| 79 | PROMEX_MT_GAUGE = 1, |
| 80 | PROMEX_MT_COUNTER = 2, |
| 81 | }; |
| 82 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 83 | /* The max length for metrics name. It is a hard limit but it should be |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 84 | * enough. |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 85 | */ |
| 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 Dauchy | 5a982a7 | 2021-01-08 13:18:06 +0100 | [diff] [blame] | 94 | /* Some labels for build_info */ |
| 95 | #define PROMEX_VERSION_LABEL "version=\"" HAPROXY_VERSION "\"" |
| 96 | #define PROMEX_BUILDINFO_LABEL PROMEX_VERSION_LABEL |
| 97 | |
Christopher Faulet | 0312c0d | 2021-01-20 15:19:12 +0100 | [diff] [blame] | 98 | /* Describe a prometheus metric */ |
| 99 | struct 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 Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 105 | /* Global metrics */ |
| 106 | const 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 Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 174 | }; |
| 175 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 176 | /* frontend/backend/server fields */ |
| 177 | const 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 Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 277 | }; |
| 278 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 279 | /* Description of all stats fields */ |
| 280 | const 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."), |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame^] | 298 | [ST_F_STATUS] = IST("Current status of the service."), |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 299 | [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 Faulet | cf403f3 | 2019-11-21 14:35:46 +0100 | [diff] [blame] | 317 | [ST_F_CHECK_STATUS] = IST("Status of last health check (HCHK_STATUS_* values)."), |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 318 | [ST_F_CHECK_CODE] = IST("layer5-7 code, if available of the last health check."), |
Christopher Faulet | 2711e51 | 2020-02-27 16:12:07 +0100 | [diff] [blame] | 319 | [ST_F_CHECK_DURATION] = IST("Total duration of the latest server health check, in seconds."), |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 320 | [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 Faulet | 20ab80c | 2019-11-08 15:24:32 +0100 | [diff] [blame] | 369 | [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 Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 371 | [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 Faulet | e4a2c8d | 2019-12-16 14:44:01 +0100 | [diff] [blame] | 375 | [ST_F_EINT] = IST("Total number of internal errors."), |
Christopher Faulet | c55a626 | 2020-07-10 15:39:39 +0200 | [diff] [blame] | 376 | [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 Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 380 | }; |
| 381 | |
| 382 | /* Specific labels for all info fields. Empty by default. */ |
| 383 | const struct ist promex_inf_metric_labels[INF_TOTAL_FIELDS] = { |
Christopher Faulet | 0175b1f | 2021-01-20 15:28:22 +0100 | [diff] [blame] | 384 | [INF_BUILD_INFO] = IST(PROMEX_BUILDINFO_LABEL), |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | /* Specific labels for all stats fields. Empty by default. */ |
| 388 | const struct ist promex_st_metric_labels[ST_F_TOTAL_FIELDS] = { |
Christopher Faulet | 0175b1f | 2021-01-20 15:28:22 +0100 | [diff] [blame] | 389 | [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 Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 395 | }; |
| 396 | |
William Dauchy | 5493821 | 2021-01-27 22:40:16 +0100 | [diff] [blame] | 397 | enum promex_front_state { |
| 398 | PROMEX_FRONT_STATE_DOWN = 0, |
| 399 | PROMEX_FRONT_STATE_UP, |
| 400 | |
| 401 | PROMEX_FRONT_STATE_COUNT /* must be last */ |
| 402 | }; |
| 403 | |
| 404 | const struct ist promex_front_st[PROMEX_FRONT_STATE_COUNT] = { |
| 405 | [PROMEX_FRONT_STATE_DOWN] = IST("DOWN"), |
| 406 | [PROMEX_FRONT_STATE_UP] = IST("UP"), |
| 407 | }; |
| 408 | |
| 409 | enum promex_back_state { |
| 410 | PROMEX_BACK_STATE_DOWN = 0, |
| 411 | PROMEX_BACK_STATE_UP, |
| 412 | |
| 413 | PROMEX_BACK_STATE_COUNT /* must be last */ |
| 414 | }; |
| 415 | |
| 416 | const struct ist promex_back_st[PROMEX_BACK_STATE_COUNT] = { |
| 417 | [PROMEX_BACK_STATE_DOWN] = IST("DOWN"), |
| 418 | [PROMEX_BACK_STATE_UP] = IST("UP"), |
| 419 | }; |
| 420 | |
| 421 | enum promex_srv_state { |
| 422 | PROMEX_SRV_STATE_DOWN = 0, |
| 423 | PROMEX_SRV_STATE_UP, |
| 424 | PROMEX_SRV_STATE_MAINT, |
| 425 | PROMEX_SRV_STATE_DRAIN, |
| 426 | PROMEX_SRV_STATE_NOLB, |
| 427 | |
| 428 | PROMEX_SRV_STATE_COUNT /* must be last */ |
| 429 | }; |
| 430 | |
| 431 | const struct ist promex_srv_st[PROMEX_SRV_STATE_COUNT] = { |
| 432 | [PROMEX_SRV_STATE_DOWN] = IST("DOWN"), |
| 433 | [PROMEX_SRV_STATE_UP] = IST("UP"), |
| 434 | [PROMEX_SRV_STATE_MAINT] = IST("MAINT"), |
| 435 | [PROMEX_SRV_STATE_DRAIN] = IST("DRAIN"), |
| 436 | [PROMEX_SRV_STATE_NOLB] = IST("NOLB"), |
| 437 | }; |
| 438 | |
| 439 | /* Return the server status. */ |
| 440 | enum promex_srv_state promex_srv_status(struct server *sv) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 441 | { |
William Dauchy | 5493821 | 2021-01-27 22:40:16 +0100 | [diff] [blame] | 442 | int state = PROMEX_SRV_STATE_DOWN; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 443 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 444 | if (sv->cur_state == SRV_ST_RUNNING || sv->cur_state == SRV_ST_STARTING) { |
William Dauchy | 5493821 | 2021-01-27 22:40:16 +0100 | [diff] [blame] | 445 | state = PROMEX_SRV_STATE_UP; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 446 | if (sv->cur_admin & SRV_ADMF_DRAIN) |
William Dauchy | 5493821 | 2021-01-27 22:40:16 +0100 | [diff] [blame] | 447 | state = PROMEX_SRV_STATE_DRAIN; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 448 | } |
Christopher Faulet | d45d105 | 2019-09-06 16:10:19 +0200 | [diff] [blame] | 449 | else if (sv->cur_state == SRV_ST_STOPPING) |
William Dauchy | 5493821 | 2021-01-27 22:40:16 +0100 | [diff] [blame] | 450 | state = PROMEX_SRV_STATE_NOLB; |
Christopher Faulet | d45d105 | 2019-09-06 16:10:19 +0200 | [diff] [blame] | 451 | |
| 452 | if (sv->cur_admin & SRV_ADMF_MAINT) |
William Dauchy | 5493821 | 2021-01-27 22:40:16 +0100 | [diff] [blame] | 453 | state = PROMEX_SRV_STATE_MAINT; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 454 | |
| 455 | return state; |
| 456 | } |
| 457 | |
| 458 | /* Convert a field to its string representation and write it in <out>, followed |
| 459 | * by a newline, if there is enough space. non-numeric value are converted in |
William Dauchy | 18a2c6e | 2021-01-22 21:09:47 +0100 | [diff] [blame] | 460 | * "NaN" because Prometheus only support numerical values (but it is unexepceted |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 461 | * to process this kind of value). It returns 1 on success. Otherwise, it |
| 462 | * returns 0. The buffer's length must not exceed <max> value. |
| 463 | */ |
| 464 | static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max) |
| 465 | { |
| 466 | int ret = 0; |
| 467 | |
| 468 | switch (field_format(f, 0)) { |
William Dauchy | 18a2c6e | 2021-01-22 21:09:47 +0100 | [diff] [blame] | 469 | case FF_EMPTY: ret = chunk_strcat(out, "NaN\n"); break; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 470 | case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break; |
| 471 | case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break; |
| 472 | case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break; |
| 473 | case FF_U64: ret = chunk_appendf(out, "%llu\n", (unsigned long long)f->u.u64); break; |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 474 | case FF_FLT: ret = chunk_appendf(out, "%f\n", f->u.flt); break; |
William Dauchy | 18a2c6e | 2021-01-22 21:09:47 +0100 | [diff] [blame] | 475 | case FF_STR: ret = chunk_strcat(out, "NaN\n"); break; |
| 476 | default: ret = chunk_strcat(out, "NaN\n"); break; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 477 | } |
| 478 | if (!ret || out->data > max) |
| 479 | return 0; |
| 480 | return 1; |
| 481 | } |
| 482 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 483 | /* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It |
| 484 | * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0. |
| 485 | */ |
| 486 | static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx, |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 487 | const struct promex_metric *metric, const struct ist name, |
| 488 | struct ist *out, size_t max) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 489 | { |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 490 | struct ist type; |
| 491 | |
| 492 | switch (metric->type) { |
| 493 | case PROMEX_MT_COUNTER: |
| 494 | type = ist("counter"); |
| 495 | break; |
| 496 | default: |
| 497 | type = ist("gauge"); |
| 498 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 499 | |
William Dauchy | a191b77 | 2021-01-15 22:41:39 +0100 | [diff] [blame] | 500 | if (istcat(out, ist("# HELP "), max) == -1 || |
| 501 | istcat(out, name, max) == -1 || |
| 502 | istcat(out, ist(" "), max) == -1) |
| 503 | goto full; |
| 504 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 505 | if (metric->flags & PROMEX_FL_INFO_METRIC) { |
William Dauchy | a191b77 | 2021-01-15 22:41:39 +0100 | [diff] [blame] | 506 | if (istcat(out, ist(info_fields[appctx->st2].desc), max) == -1) |
| 507 | goto full; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 508 | } |
| 509 | else { |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 510 | if (istcat(out, promex_st_metric_desc[appctx->st2], max) == -1) |
William Dauchy | a191b77 | 2021-01-15 22:41:39 +0100 | [diff] [blame] | 511 | goto full; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 512 | } |
| 513 | |
William Dauchy | a191b77 | 2021-01-15 22:41:39 +0100 | [diff] [blame] | 514 | if (istcat(out, ist("\n# TYPE "), max) == -1 || |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 515 | istcat(out, name, max) == -1 || |
| 516 | istcat(out, ist(" "), max) == -1 || |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 517 | istcat(out, type, max) == -1 || |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 518 | istcat(out, ist("\n"), max) == -1) |
| 519 | goto full; |
| 520 | |
| 521 | return 1; |
| 522 | |
| 523 | full: |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | /* Dump the line for <metric>. It starts by the metric name followed by its |
| 528 | * labels (proxy name, server name...) between braces and finally its value. If |
| 529 | * not already done, the header lines are dumped first. It returns 1 on |
| 530 | * success. Otherwise if <out> length exceeds <max>, it returns 0. |
| 531 | */ |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 532 | static int promex_dump_metric(struct appctx *appctx, struct htx *htx, struct ist prefix, |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame^] | 533 | const struct promex_metric *metric, struct field *val, |
| 534 | struct ist metric_label, struct ist *out, size_t max) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 535 | { |
| 536 | struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 }; |
| 537 | size_t len = out->len; |
| 538 | |
| 539 | if (out->len + PROMEX_MAX_METRIC_LENGTH > max) |
| 540 | return 0; |
| 541 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 542 | /* Fill the metric name */ |
| 543 | istcat(&name, prefix, PROMEX_MAX_NAME_LEN); |
| 544 | istcat(&name, metric->n, PROMEX_MAX_NAME_LEN); |
| 545 | |
| 546 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 547 | if ((appctx->ctx.stats.flags & PROMEX_FL_METRIC_HDR) && |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 548 | !promex_dump_metric_header(appctx, htx, metric, name, out, max)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 549 | goto full; |
| 550 | |
| 551 | if (appctx->ctx.stats.flags & PROMEX_FL_INFO_METRIC) { |
| 552 | const struct ist label = promex_inf_metric_labels[appctx->st2]; |
| 553 | |
| 554 | if (istcat(out, name, max) == -1 || |
| 555 | (label.len && istcat(out, ist("{"), max) == -1) || |
| 556 | (label.len && istcat(out, label, max) == -1) || |
| 557 | (label.len && istcat(out, ist("}"), max) == -1) || |
| 558 | istcat(out, ist(" "), max) == -1) |
| 559 | goto full; |
| 560 | } |
| 561 | else { |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 562 | struct proxy *px = appctx->ctx.stats.obj1; |
| 563 | struct server *srv = appctx->ctx.stats.obj2; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 564 | const struct ist label = promex_st_metric_labels[appctx->st2]; |
| 565 | |
| 566 | if (istcat(out, name, max) == -1 || |
| 567 | istcat(out, ist("{proxy=\""), max) == -1 || |
| 568 | istcat(out, ist2(px->id, strlen(px->id)), max) == -1 || |
| 569 | istcat(out, ist("\""), max) == -1 || |
| 570 | (srv && istcat(out, ist(",server=\""), max) == -1) || |
| 571 | (srv && istcat(out, ist2(srv->id, strlen(srv->id)), max) == -1) || |
| 572 | (srv && istcat(out, ist("\""), max) == -1) || |
| 573 | (label.len && istcat(out, ist(","), max) == -1) || |
| 574 | (label.len && istcat(out, label, max) == -1) || |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame^] | 575 | (metric_label.len && istcat(out, ist(","), max) == -1) || |
| 576 | (metric_label.len && istcat(out, metric_label, max) == -1) || |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 577 | istcat(out, ist("} "), max) == -1) |
| 578 | goto full; |
| 579 | } |
| 580 | |
| 581 | trash.data = out->len; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 582 | if (!promex_metric_to_str(&trash, val, max)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 583 | goto full; |
| 584 | out->len = trash.data; |
| 585 | |
| 586 | appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR; |
| 587 | return 1; |
| 588 | full: |
| 589 | // Restore previous length |
| 590 | out->len = len; |
| 591 | return 0; |
| 592 | |
| 593 | } |
| 594 | |
| 595 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 596 | /* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success, |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 597 | * 0 if <htx> is full and -1 in case of any error. */ |
| 598 | static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx) |
| 599 | { |
| 600 | static struct ist prefix = IST("haproxy_process_"); |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 601 | struct field val; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 602 | struct channel *chn = si_ic(appctx->owner); |
| 603 | struct ist out = ist2(trash.area, 0); |
Christopher Faulet | 11921e6 | 2019-07-03 11:43:17 +0200 | [diff] [blame] | 604 | size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx)); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 605 | int ret = 1; |
| 606 | |
William Dauchy | 5d9b8f3 | 2021-01-11 20:07:49 +0100 | [diff] [blame] | 607 | if (!stats_fill_info(info, INF_TOTAL_FIELDS)) |
| 608 | return -1; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 609 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 610 | for (; appctx->st2 < INF_TOTAL_FIELDS; appctx->st2++) { |
| 611 | if (!(promex_global_metrics[appctx->st2].flags & appctx->ctx.stats.flags)) |
| 612 | continue; |
| 613 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 614 | switch (appctx->st2) { |
William Dauchy | 5a982a7 | 2021-01-08 13:18:06 +0100 | [diff] [blame] | 615 | case INF_BUILD_INFO: |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 616 | val = mkf_u32(FN_GAUGE, 1); |
William Dauchy | 5a982a7 | 2021-01-08 13:18:06 +0100 | [diff] [blame] | 617 | break; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 618 | |
| 619 | default: |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 620 | val = info[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 621 | } |
| 622 | |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame^] | 623 | if (!promex_dump_metric(appctx, htx, prefix, &promex_global_metrics[appctx->st2], |
| 624 | &val, IST_NULL, &out, max)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 625 | goto full; |
| 626 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 627 | appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 628 | } |
| 629 | |
| 630 | end: |
Christopher Faulet | 0c55a15 | 2019-07-04 10:03:28 +0200 | [diff] [blame] | 631 | if (out.len) { |
| 632 | if (!htx_add_data_atonce(htx, out)) |
| 633 | return -1; /* Unexpected and unrecoverable error */ |
| 634 | channel_add_input(chn, out.len); |
| 635 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 636 | return ret; |
| 637 | full: |
| 638 | ret = 0; |
| 639 | goto end; |
| 640 | } |
| 641 | |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame^] | 642 | /* Helper to generate state labels for ftd/bkd/srv */ |
| 643 | static int promex_state_label(struct ist *metric_label, struct ist state) |
| 644 | { |
| 645 | struct buffer *state_label = get_trash_chunk(); |
| 646 | |
| 647 | *metric_label = ist2(state_label->area, 0); |
| 648 | if (istcat(metric_label, ist("state=\""), state_label->size) == -1 || |
| 649 | istcat(metric_label, state, state_label->size) == -1 || |
| 650 | istcat(metric_label, ist("\""), state_label->size) == -1) |
| 651 | return -1; |
| 652 | return 1; |
| 653 | } |
| 654 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 655 | /* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success, |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 656 | * 0 if <htx> is full and -1 in case of any error. */ |
| 657 | static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx) |
| 658 | { |
| 659 | static struct ist prefix = IST("haproxy_frontend_"); |
| 660 | struct proxy *px; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 661 | struct field val; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 662 | struct channel *chn = si_ic(appctx->owner); |
| 663 | struct ist out = ist2(trash.area, 0); |
Christopher Faulet | 11921e6 | 2019-07-03 11:43:17 +0200 | [diff] [blame] | 664 | size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx)); |
William Dauchy | b957745 | 2021-01-17 18:27:46 +0100 | [diff] [blame] | 665 | struct field *stats = stat_l[STATS_DOMAIN_PROXY]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 666 | int ret = 1; |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame^] | 667 | enum promex_front_state state; |
| 668 | struct ist metric_label; |
| 669 | int i; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 670 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 671 | for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) { |
| 672 | if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags)) |
| 673 | continue; |
| 674 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 675 | while (appctx->ctx.stats.obj1) { |
| 676 | px = appctx->ctx.stats.obj1; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 677 | |
| 678 | /* skip the disabled proxies, global frontend and non-networked ones */ |
Willy Tarreau | c3914d4 | 2020-09-24 08:39:22 +0200 | [diff] [blame] | 679 | if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_FE)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 680 | goto next_px; |
| 681 | |
William Dauchy | b957745 | 2021-01-17 18:27:46 +0100 | [diff] [blame] | 682 | if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(appctx->st2))) |
| 683 | return -1; |
| 684 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 685 | switch (appctx->st2) { |
| 686 | case ST_F_STATUS: |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame^] | 687 | state = !px->disabled; |
| 688 | for (i = 0; i < PROMEX_FRONT_STATE_COUNT; i++) { |
| 689 | val = mkf_u32(FO_STATUS, state == i); |
| 690 | if (promex_state_label(&metric_label, promex_front_st[i]) == -1) |
| 691 | goto full; |
| 692 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
| 693 | &val, metric_label, &out, max)) |
| 694 | goto full; |
| 695 | } |
| 696 | goto next_px; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 697 | case ST_F_REQ_RATE_MAX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 698 | case ST_F_REQ_TOT: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 699 | case ST_F_HRSP_1XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 700 | case ST_F_INTERCEPTED: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 701 | case ST_F_CACHE_LOOKUPS: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 702 | case ST_F_CACHE_HITS: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 703 | case ST_F_COMP_IN: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 704 | case ST_F_COMP_OUT: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 705 | case ST_F_COMP_BYP: |
William Dauchy | b957745 | 2021-01-17 18:27:46 +0100 | [diff] [blame] | 706 | case ST_F_COMP_RSP: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 707 | if (px->mode != PR_MODE_HTTP) |
| 708 | goto next_px; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 709 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 710 | break; |
William Dauchy | b957745 | 2021-01-17 18:27:46 +0100 | [diff] [blame] | 711 | case ST_F_HRSP_2XX: |
| 712 | case ST_F_HRSP_3XX: |
| 713 | case ST_F_HRSP_4XX: |
| 714 | case ST_F_HRSP_5XX: |
| 715 | case ST_F_HRSP_OTHER: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 716 | if (px->mode != PR_MODE_HTTP) |
| 717 | goto next_px; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 718 | val = stats[appctx->st2]; |
William Dauchy | b957745 | 2021-01-17 18:27:46 +0100 | [diff] [blame] | 719 | appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 720 | break; |
| 721 | |
| 722 | default: |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 723 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 724 | } |
| 725 | |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame^] | 726 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
| 727 | &val, IST_NULL, &out, max)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 728 | goto full; |
| 729 | next_px: |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 730 | appctx->ctx.stats.obj1 = px->next; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 731 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 732 | appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR; |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 733 | appctx->ctx.stats.obj1 = proxies_list; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | end: |
Christopher Faulet | 0c55a15 | 2019-07-04 10:03:28 +0200 | [diff] [blame] | 737 | if (out.len) { |
| 738 | if (!htx_add_data_atonce(htx, out)) |
| 739 | return -1; /* Unexpected and unrecoverable error */ |
| 740 | channel_add_input(chn, out.len); |
| 741 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 742 | return ret; |
| 743 | full: |
| 744 | ret = 0; |
| 745 | goto end; |
| 746 | } |
| 747 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 748 | /* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success, |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 749 | * 0 if <htx> is full and -1 in case of any error. */ |
| 750 | static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx) |
| 751 | { |
| 752 | static struct ist prefix = IST("haproxy_backend_"); |
| 753 | struct proxy *px; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 754 | struct field val; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 755 | struct channel *chn = si_ic(appctx->owner); |
| 756 | struct ist out = ist2(trash.area, 0); |
Christopher Faulet | 11921e6 | 2019-07-03 11:43:17 +0200 | [diff] [blame] | 757 | size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx)); |
William Dauchy | 3c6f006 | 2021-01-25 17:29:02 +0100 | [diff] [blame] | 758 | struct field *stats = stat_l[STATS_DOMAIN_PROXY]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 759 | int ret = 1; |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 760 | double secs; |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame^] | 761 | enum promex_back_state state; |
| 762 | struct ist metric_label; |
| 763 | int i; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 764 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 765 | for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) { |
| 766 | if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags)) |
| 767 | continue; |
| 768 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 769 | while (appctx->ctx.stats.obj1) { |
| 770 | px = appctx->ctx.stats.obj1; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 771 | |
| 772 | /* skip the disabled proxies, global frontend and non-networked ones */ |
Willy Tarreau | c3914d4 | 2020-09-24 08:39:22 +0200 | [diff] [blame] | 773 | if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 774 | goto next_px; |
| 775 | |
William Dauchy | 3c6f006 | 2021-01-25 17:29:02 +0100 | [diff] [blame] | 776 | if (!stats_fill_be_stats(px, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2))) |
| 777 | return -1; |
| 778 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 779 | switch (appctx->st2) { |
| 780 | case ST_F_STATUS: |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame^] | 781 | state = ((px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0); |
| 782 | for (i = 0; i < PROMEX_BACK_STATE_COUNT; i++) { |
| 783 | val = mkf_u32(FO_STATUS, state == i); |
| 784 | if (promex_state_label(&metric_label, promex_back_st[i]) == -1) |
| 785 | goto full; |
| 786 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
| 787 | &val, metric_label, &out, max)) |
| 788 | goto full; |
| 789 | } |
| 790 | goto next_px; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 791 | case ST_F_QTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 792 | secs = (double)swrate_avg(px->be_counters.q_time, TIME_STATS_SAMPLES) / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 793 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 794 | break; |
| 795 | case ST_F_CTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 796 | secs = (double)swrate_avg(px->be_counters.c_time, TIME_STATS_SAMPLES) / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 797 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 798 | break; |
| 799 | case ST_F_RTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 800 | secs = (double)swrate_avg(px->be_counters.d_time, TIME_STATS_SAMPLES) / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 801 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 802 | break; |
| 803 | case ST_F_TTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 804 | secs = (double)swrate_avg(px->be_counters.t_time, TIME_STATS_SAMPLES) / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 805 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 806 | break; |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 807 | case ST_F_QT_MAX: |
| 808 | secs = (double)px->be_counters.qtime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 809 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 810 | break; |
| 811 | case ST_F_CT_MAX: |
| 812 | secs = (double)px->be_counters.ctime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 813 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 814 | break; |
| 815 | case ST_F_RT_MAX: |
| 816 | secs = (double)px->be_counters.dtime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 817 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 818 | break; |
| 819 | case ST_F_TT_MAX: |
| 820 | secs = (double)px->be_counters.ttime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 821 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 822 | break; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 823 | case ST_F_REQ_TOT: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 824 | case ST_F_HRSP_1XX: |
William Dauchy | 3c6f006 | 2021-01-25 17:29:02 +0100 | [diff] [blame] | 825 | case ST_F_CACHE_LOOKUPS: |
| 826 | case ST_F_CACHE_HITS: |
| 827 | case ST_F_COMP_IN: |
| 828 | case ST_F_COMP_OUT: |
| 829 | case ST_F_COMP_BYP: |
| 830 | case ST_F_COMP_RSP: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 831 | if (px->mode != PR_MODE_HTTP) |
| 832 | goto next_px; |
William Dauchy | 3c6f006 | 2021-01-25 17:29:02 +0100 | [diff] [blame] | 833 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 834 | break; |
| 835 | case ST_F_HRSP_2XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 836 | case ST_F_HRSP_3XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 837 | case ST_F_HRSP_4XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 838 | case ST_F_HRSP_5XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 839 | case ST_F_HRSP_OTHER: |
| 840 | if (px->mode != PR_MODE_HTTP) |
| 841 | goto next_px; |
| 842 | appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR; |
William Dauchy | 3c6f006 | 2021-01-25 17:29:02 +0100 | [diff] [blame] | 843 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 844 | break; |
| 845 | |
| 846 | default: |
William Dauchy | 3c6f006 | 2021-01-25 17:29:02 +0100 | [diff] [blame] | 847 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 848 | } |
| 849 | |
William Dauchy | 3c6f006 | 2021-01-25 17:29:02 +0100 | [diff] [blame] | 850 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame^] | 851 | &val, IST_NULL, &out, max)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 852 | goto full; |
| 853 | next_px: |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 854 | appctx->ctx.stats.obj1 = px->next; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 855 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 856 | appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR; |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 857 | appctx->ctx.stats.obj1 = proxies_list; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 858 | } |
| 859 | |
| 860 | end: |
Christopher Faulet | 0c55a15 | 2019-07-04 10:03:28 +0200 | [diff] [blame] | 861 | if (out.len) { |
| 862 | if (!htx_add_data_atonce(htx, out)) |
| 863 | return -1; /* Unexpected and unrecoverable error */ |
| 864 | channel_add_input(chn, out.len); |
| 865 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 866 | return ret; |
| 867 | full: |
| 868 | ret = 0; |
| 869 | goto end; |
| 870 | } |
| 871 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 872 | /* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success, |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 873 | * 0 if <htx> is full and -1 in case of any error. */ |
| 874 | static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx) |
| 875 | { |
| 876 | static struct ist prefix = IST("haproxy_server_"); |
| 877 | struct proxy *px; |
| 878 | struct server *sv; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 879 | struct field val; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 880 | struct channel *chn = si_ic(appctx->owner); |
| 881 | struct ist out = ist2(trash.area, 0); |
Christopher Faulet | 11921e6 | 2019-07-03 11:43:17 +0200 | [diff] [blame] | 882 | size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx)); |
William Dauchy | bde2bf6 | 2021-01-25 17:29:04 +0100 | [diff] [blame] | 883 | struct field *stats = stat_l[STATS_DOMAIN_PROXY]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 884 | int ret = 1; |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 885 | double secs; |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame^] | 886 | enum promex_srv_state state; |
| 887 | struct ist metric_label; |
| 888 | int i; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 889 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 890 | for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) { |
| 891 | if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags)) |
| 892 | continue; |
| 893 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 894 | while (appctx->ctx.stats.obj1) { |
| 895 | px = appctx->ctx.stats.obj1; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 896 | |
| 897 | /* skip the disabled proxies, global frontend and non-networked ones */ |
Willy Tarreau | c3914d4 | 2020-09-24 08:39:22 +0200 | [diff] [blame] | 898 | if (px->disabled || px->uuid <= 0 || !(px->cap & PR_CAP_BE)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 899 | goto next_px; |
| 900 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 901 | while (appctx->ctx.stats.obj2) { |
| 902 | sv = appctx->ctx.stats.obj2; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 903 | |
William Dauchy | bde2bf6 | 2021-01-25 17:29:04 +0100 | [diff] [blame] | 904 | if (!stats_fill_sv_stats(px, sv, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2))) |
| 905 | return -1; |
| 906 | |
Christopher Faulet | eba2294 | 2019-11-19 14:18:24 +0100 | [diff] [blame] | 907 | if ((appctx->ctx.stats.flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT)) |
| 908 | goto next_sv; |
| 909 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 910 | switch (appctx->st2) { |
| 911 | case ST_F_STATUS: |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame^] | 912 | state = promex_srv_status(sv); |
| 913 | for (i = 0; i < PROMEX_SRV_STATE_COUNT; i++) { |
| 914 | val = mkf_u32(FO_STATUS, state == i); |
| 915 | if (promex_state_label(&metric_label, promex_srv_st[i]) == -1) |
| 916 | goto full; |
| 917 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
| 918 | &val, metric_label, &out, max)) |
| 919 | goto full; |
| 920 | } |
| 921 | goto next_sv; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 922 | case ST_F_QTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 923 | secs = (double)swrate_avg(sv->counters.q_time, TIME_STATS_SAMPLES) / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 924 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 925 | break; |
| 926 | case ST_F_CTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 927 | secs = (double)swrate_avg(sv->counters.c_time, TIME_STATS_SAMPLES) / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 928 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 929 | break; |
| 930 | case ST_F_RTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 931 | secs = (double)swrate_avg(sv->counters.d_time, TIME_STATS_SAMPLES) / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 932 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 933 | break; |
| 934 | case ST_F_TTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 935 | secs = (double)swrate_avg(sv->counters.t_time, TIME_STATS_SAMPLES) / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 936 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 937 | break; |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 938 | case ST_F_QT_MAX: |
| 939 | secs = (double)sv->counters.qtime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 940 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 941 | break; |
| 942 | case ST_F_CT_MAX: |
| 943 | secs = (double)sv->counters.ctime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 944 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 945 | break; |
| 946 | case ST_F_RT_MAX: |
| 947 | secs = (double)sv->counters.dtime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 948 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 949 | break; |
| 950 | case ST_F_TT_MAX: |
| 951 | secs = (double)sv->counters.ttime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 952 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 953 | break; |
Christopher Faulet | cf403f3 | 2019-11-21 14:35:46 +0100 | [diff] [blame] | 954 | case ST_F_CHECK_STATUS: |
| 955 | if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED) |
| 956 | goto next_sv; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 957 | val = mkf_u32(FN_OUTPUT, sv->check.status); |
Christopher Faulet | cf403f3 | 2019-11-21 14:35:46 +0100 | [diff] [blame] | 958 | break; |
| 959 | case ST_F_CHECK_CODE: |
| 960 | if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED) |
| 961 | goto next_sv; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 962 | val = mkf_u32(FN_OUTPUT, (sv->check.status < HCHK_STATUS_L57DATA) ? 0 : sv->check.code); |
Christopher Faulet | cf403f3 | 2019-11-21 14:35:46 +0100 | [diff] [blame] | 963 | break; |
Christopher Faulet | 2711e51 | 2020-02-27 16:12:07 +0100 | [diff] [blame] | 964 | case ST_F_CHECK_DURATION: |
| 965 | if (sv->check.status < HCHK_STATUS_CHECKED) |
| 966 | goto next_sv; |
| 967 | secs = (double)sv->check.duration / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 968 | val = mkf_flt(FN_DURATION, secs); |
Christopher Faulet | 2711e51 | 2020-02-27 16:12:07 +0100 | [diff] [blame] | 969 | break; |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 970 | case ST_F_REQ_TOT: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 971 | case ST_F_HRSP_1XX: |
| 972 | if (px->mode != PR_MODE_HTTP) |
| 973 | goto next_px; |
William Dauchy | bde2bf6 | 2021-01-25 17:29:04 +0100 | [diff] [blame] | 974 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 975 | break; |
| 976 | case ST_F_HRSP_2XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 977 | case ST_F_HRSP_3XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 978 | case ST_F_HRSP_4XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 979 | case ST_F_HRSP_5XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 980 | case ST_F_HRSP_OTHER: |
| 981 | if (px->mode != PR_MODE_HTTP) |
| 982 | goto next_px; |
| 983 | appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR; |
William Dauchy | bde2bf6 | 2021-01-25 17:29:04 +0100 | [diff] [blame] | 984 | val = stats[appctx->st2]; |
Christopher Faulet | c55a626 | 2020-07-10 15:39:39 +0200 | [diff] [blame] | 985 | break; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 986 | |
| 987 | default: |
William Dauchy | bde2bf6 | 2021-01-25 17:29:04 +0100 | [diff] [blame] | 988 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 989 | } |
| 990 | |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame^] | 991 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
| 992 | &val, IST_NULL, &out, max)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 993 | goto full; |
Christopher Faulet | eba2294 | 2019-11-19 14:18:24 +0100 | [diff] [blame] | 994 | next_sv: |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 995 | appctx->ctx.stats.obj2 = sv->next; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 996 | } |
| 997 | |
| 998 | next_px: |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 999 | appctx->ctx.stats.obj1 = px->next; |
| 1000 | appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1001 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1002 | appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR; |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1003 | appctx->ctx.stats.obj1 = proxies_list; |
| 1004 | appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1005 | } |
| 1006 | |
| 1007 | |
| 1008 | end: |
Christopher Faulet | 0c55a15 | 2019-07-04 10:03:28 +0200 | [diff] [blame] | 1009 | if (out.len) { |
| 1010 | if (!htx_add_data_atonce(htx, out)) |
| 1011 | return -1; /* Unexpected and unrecoverable error */ |
| 1012 | channel_add_input(chn, out.len); |
| 1013 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1014 | return ret; |
| 1015 | full: |
| 1016 | ret = 0; |
| 1017 | goto end; |
| 1018 | } |
| 1019 | |
| 1020 | /* Dump all metrics (global, frontends, backends and servers) depending on the |
| 1021 | * dumper state (appctx->st1). It returns 1 on success, 0 if <htx> is full and |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1022 | * -1 in case of any error. |
| 1023 | * Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as |
| 1024 | * a pointer to the current server/listener. */ |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1025 | static int promex_dump_metrics(struct appctx *appctx, struct stream_interface *si, struct htx *htx) |
| 1026 | { |
| 1027 | int ret; |
| 1028 | |
| 1029 | switch (appctx->st1) { |
| 1030 | case PROMEX_DUMPER_INIT: |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1031 | appctx->ctx.stats.obj1 = NULL; |
| 1032 | appctx->ctx.stats.obj2 = NULL; |
Christopher Faulet | efde955 | 2020-06-05 08:18:56 +0200 | [diff] [blame] | 1033 | appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC); |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 1034 | appctx->st2 = INF_NAME; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1035 | appctx->st1 = PROMEX_DUMPER_GLOBAL; |
| 1036 | /* fall through */ |
| 1037 | |
| 1038 | case PROMEX_DUMPER_GLOBAL: |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1039 | if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_GLOBAL) { |
| 1040 | ret = promex_dump_global_metrics(appctx, htx); |
| 1041 | if (ret <= 0) { |
| 1042 | if (ret == -1) |
| 1043 | goto error; |
| 1044 | goto full; |
| 1045 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1046 | } |
| 1047 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1048 | appctx->ctx.stats.obj1 = proxies_list; |
| 1049 | appctx->ctx.stats.obj2 = NULL; |
Christopher Faulet | efde955 | 2020-06-05 08:18:56 +0200 | [diff] [blame] | 1050 | appctx->ctx.stats.flags &= ~PROMEX_FL_INFO_METRIC; |
Christopher Faulet | b713c4f | 2021-01-20 15:02:50 +0100 | [diff] [blame] | 1051 | appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC); |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 1052 | appctx->st2 = ST_F_PXNAME; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1053 | appctx->st1 = PROMEX_DUMPER_FRONT; |
| 1054 | /* fall through */ |
| 1055 | |
| 1056 | case PROMEX_DUMPER_FRONT: |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1057 | if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_FRONT) { |
| 1058 | ret = promex_dump_front_metrics(appctx, htx); |
| 1059 | if (ret <= 0) { |
| 1060 | if (ret == -1) |
| 1061 | goto error; |
| 1062 | goto full; |
| 1063 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1064 | } |
| 1065 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1066 | appctx->ctx.stats.obj1 = proxies_list; |
| 1067 | appctx->ctx.stats.obj2 = NULL; |
Christopher Faulet | b713c4f | 2021-01-20 15:02:50 +0100 | [diff] [blame] | 1068 | appctx->ctx.stats.flags &= ~PROMEX_FL_FRONT_METRIC; |
| 1069 | appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC); |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 1070 | appctx->st2 = ST_F_PXNAME; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1071 | appctx->st1 = PROMEX_DUMPER_BACK; |
| 1072 | /* fall through */ |
| 1073 | |
| 1074 | case PROMEX_DUMPER_BACK: |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1075 | if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_BACK) { |
| 1076 | ret = promex_dump_back_metrics(appctx, htx); |
| 1077 | if (ret <= 0) { |
| 1078 | if (ret == -1) |
| 1079 | goto error; |
| 1080 | goto full; |
| 1081 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1082 | } |
| 1083 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1084 | appctx->ctx.stats.obj1 = proxies_list; |
| 1085 | appctx->ctx.stats.obj2 = (appctx->ctx.stats.obj1 ? ((struct proxy *)appctx->ctx.stats.obj1)->srv : NULL); |
Christopher Faulet | b713c4f | 2021-01-20 15:02:50 +0100 | [diff] [blame] | 1086 | appctx->ctx.stats.flags &= ~PROMEX_FL_BACK_METRIC; |
| 1087 | appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC); |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 1088 | appctx->st2 = ST_F_PXNAME; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1089 | appctx->st1 = PROMEX_DUMPER_SRV; |
| 1090 | /* fall through */ |
| 1091 | |
| 1092 | case PROMEX_DUMPER_SRV: |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1093 | if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_SERVER) { |
| 1094 | ret = promex_dump_srv_metrics(appctx, htx); |
| 1095 | if (ret <= 0) { |
| 1096 | if (ret == -1) |
| 1097 | goto error; |
| 1098 | goto full; |
| 1099 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1100 | } |
| 1101 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1102 | appctx->ctx.stats.obj1 = NULL; |
| 1103 | appctx->ctx.stats.obj2 = NULL; |
Christopher Faulet | b713c4f | 2021-01-20 15:02:50 +0100 | [diff] [blame] | 1104 | appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1105 | appctx->st2 = 0; |
| 1106 | appctx->st1 = PROMEX_DUMPER_DONE; |
| 1107 | /* fall through */ |
| 1108 | |
| 1109 | case PROMEX_DUMPER_DONE: |
| 1110 | default: |
| 1111 | break; |
| 1112 | } |
| 1113 | |
| 1114 | return 1; |
| 1115 | |
| 1116 | full: |
| 1117 | si_rx_room_blk(si); |
| 1118 | return 0; |
| 1119 | error: |
| 1120 | /* unrecoverable error */ |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1121 | appctx->ctx.stats.obj1 = NULL; |
| 1122 | appctx->ctx.stats.obj2 = NULL; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1123 | appctx->ctx.stats.flags = 0; |
| 1124 | appctx->st2 = 0; |
| 1125 | appctx->st1 = PROMEX_DUMPER_DONE; |
| 1126 | return -1; |
| 1127 | } |
| 1128 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 1129 | /* Parse the query string of request URI to filter the metrics. It returns 1 on |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1130 | * success and -1 on error. */ |
| 1131 | static int promex_parse_uri(struct appctx *appctx, struct stream_interface *si) |
| 1132 | { |
| 1133 | struct channel *req = si_oc(si); |
| 1134 | struct channel *res = si_ic(si); |
| 1135 | struct htx *req_htx, *res_htx; |
| 1136 | struct htx_sl *sl; |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1137 | char *p, *key, *value; |
| 1138 | const char *end; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1139 | struct buffer *err; |
| 1140 | int default_scopes = PROMEX_FL_SCOPE_ALL; |
| 1141 | int len; |
| 1142 | |
| 1143 | /* Get the query-string */ |
| 1144 | req_htx = htxbuf(&req->buf); |
| 1145 | sl = http_get_stline(req_htx); |
| 1146 | if (!sl) |
| 1147 | goto error; |
| 1148 | p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?'); |
| 1149 | if (!p) |
| 1150 | goto end; |
| 1151 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1152 | |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1153 | /* copy the query-string */ |
| 1154 | len = end - p; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1155 | chunk_reset(&trash); |
| 1156 | memcpy(trash.area, p, len); |
| 1157 | trash.area[len] = 0; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1158 | p = trash.area; |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1159 | end = trash.area + len; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1160 | |
| 1161 | /* Parse the query-string */ |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1162 | while (p < end && *p && *p != '#') { |
| 1163 | value = NULL; |
| 1164 | |
| 1165 | /* decode parameter name */ |
| 1166 | key = p; |
| 1167 | while (p < end && *p != '=' && *p != '&' && *p != '#') |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1168 | ++p; |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1169 | /* found a value */ |
| 1170 | if (*p == '=') { |
| 1171 | *(p++) = 0; |
| 1172 | value = p; |
| 1173 | } |
| 1174 | else if (*p == '&') |
| 1175 | *(p++) = 0; |
| 1176 | else if (*p == '#') |
| 1177 | *p = 0; |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 1178 | len = url_decode(key, 1); |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1179 | if (len == -1) |
| 1180 | goto error; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1181 | |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1182 | /* decode value */ |
| 1183 | if (value) { |
| 1184 | while (p < end && *p != '=' && *p != '&' && *p != '#') |
| 1185 | ++p; |
| 1186 | if (*p == '=') |
| 1187 | goto error; |
| 1188 | if (*p == '&') |
| 1189 | *(p++) = 0; |
| 1190 | else if (*p == '#') |
| 1191 | *p = 0; |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 1192 | len = url_decode(value, 1); |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1193 | if (len == -1) |
| 1194 | goto error; |
| 1195 | } |
| 1196 | |
Tim Duesterhus | 8cb12a8 | 2021-01-02 22:31:55 +0100 | [diff] [blame] | 1197 | if (strcmp(key, "scope") == 0) { |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1198 | default_scopes = 0; /* at least a scope defined, unset default scopes */ |
| 1199 | if (!value) |
| 1200 | goto error; |
| 1201 | else if (*value == 0) |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1202 | appctx->ctx.stats.flags &= ~PROMEX_FL_SCOPE_ALL; |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1203 | else if (*value == '*') |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1204 | appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_ALL; |
Tim Duesterhus | 8cb12a8 | 2021-01-02 22:31:55 +0100 | [diff] [blame] | 1205 | else if (strcmp(value, "global") == 0) |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1206 | appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_GLOBAL; |
Tim Duesterhus | 8cb12a8 | 2021-01-02 22:31:55 +0100 | [diff] [blame] | 1207 | else if (strcmp(value, "server") == 0) |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1208 | appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_SERVER; |
Tim Duesterhus | 8cb12a8 | 2021-01-02 22:31:55 +0100 | [diff] [blame] | 1209 | else if (strcmp(value, "backend") == 0) |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1210 | appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_BACK; |
Tim Duesterhus | 8cb12a8 | 2021-01-02 22:31:55 +0100 | [diff] [blame] | 1211 | else if (strcmp(value, "frontend") == 0) |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1212 | appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_FRONT; |
| 1213 | else |
| 1214 | goto error; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1215 | } |
Tim Duesterhus | 8cb12a8 | 2021-01-02 22:31:55 +0100 | [diff] [blame] | 1216 | else if (strcmp(key, "no-maint") == 0) |
Christopher Faulet | eba2294 | 2019-11-19 14:18:24 +0100 | [diff] [blame] | 1217 | appctx->ctx.stats.flags |= PROMEX_FL_NO_MAINT_SRV; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1218 | } |
| 1219 | |
| 1220 | end: |
| 1221 | appctx->ctx.stats.flags |= default_scopes; |
| 1222 | return 1; |
| 1223 | |
| 1224 | error: |
| 1225 | err = &http_err_chunks[HTTP_ERR_400]; |
| 1226 | channel_erase(res); |
| 1227 | res->buf.data = b_data(err); |
| 1228 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 1229 | res_htx = htx_from_buf(&res->buf); |
| 1230 | channel_add_input(res, res_htx->data); |
| 1231 | appctx->st0 = PROMEX_ST_END; |
| 1232 | return -1; |
| 1233 | } |
| 1234 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1235 | /* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is |
| 1236 | * full. */ |
| 1237 | static int promex_send_headers(struct appctx *appctx, struct stream_interface *si, struct htx *htx) |
| 1238 | { |
Christopher Faulet | 9744f7c | 2019-03-27 15:48:53 +0100 | [diff] [blame] | 1239 | struct channel *chn = si_ic(appctx->owner); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1240 | struct htx_sl *sl; |
| 1241 | unsigned int flags; |
| 1242 | |
| 1243 | 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); |
| 1244 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK")); |
| 1245 | if (!sl) |
| 1246 | goto full; |
| 1247 | sl->info.res.status = 200; |
| 1248 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) || |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1249 | !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) || |
| 1250 | !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) || |
| 1251 | !htx_add_endof(htx, HTX_BLK_EOH)) |
| 1252 | goto full; |
| 1253 | |
Christopher Faulet | 9744f7c | 2019-03-27 15:48:53 +0100 | [diff] [blame] | 1254 | channel_add_input(chn, htx->data); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1255 | return 1; |
| 1256 | full: |
| 1257 | htx_reset(htx); |
| 1258 | si_rx_room_blk(si); |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | /* The function returns 1 if the initialisation is complete, 0 if |
| 1263 | * an errors occurs and -1 if more data are required for initializing |
| 1264 | * the applet. |
| 1265 | */ |
| 1266 | static int promex_appctx_init(struct appctx *appctx, struct proxy *px, struct stream *strm) |
| 1267 | { |
| 1268 | appctx->st0 = PROMEX_ST_INIT; |
| 1269 | return 1; |
| 1270 | } |
| 1271 | |
| 1272 | /* The main I/O handler for the promex applet. */ |
| 1273 | static void promex_appctx_handle_io(struct appctx *appctx) |
| 1274 | { |
| 1275 | struct stream_interface *si = appctx->owner; |
| 1276 | struct stream *s = si_strm(si); |
| 1277 | struct channel *req = si_oc(si); |
| 1278 | struct channel *res = si_ic(si); |
| 1279 | struct htx *req_htx, *res_htx; |
| 1280 | int ret; |
| 1281 | |
| 1282 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1283 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 1284 | goto out; |
| 1285 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 1286 | /* Check if the input buffer is available. */ |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1287 | if (!b_size(&res->buf)) { |
| 1288 | si_rx_room_blk(si); |
| 1289 | goto out; |
| 1290 | } |
| 1291 | |
| 1292 | switch (appctx->st0) { |
| 1293 | case PROMEX_ST_INIT: |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1294 | ret = promex_parse_uri(appctx, si); |
| 1295 | if (ret <= 0) { |
| 1296 | if (ret == -1) |
| 1297 | goto error; |
| 1298 | goto out; |
| 1299 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1300 | appctx->st0 = PROMEX_ST_HEAD; |
| 1301 | appctx->st1 = PROMEX_DUMPER_INIT; |
| 1302 | /* fall through */ |
| 1303 | |
| 1304 | case PROMEX_ST_HEAD: |
| 1305 | if (!promex_send_headers(appctx, si, res_htx)) |
| 1306 | goto out; |
| 1307 | appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP); |
| 1308 | /* fall through */ |
| 1309 | |
| 1310 | case PROMEX_ST_DUMP: |
| 1311 | ret = promex_dump_metrics(appctx, si, res_htx); |
| 1312 | if (ret <= 0) { |
| 1313 | if (ret == -1) |
| 1314 | goto error; |
| 1315 | goto out; |
| 1316 | } |
| 1317 | appctx->st0 = PROMEX_ST_DONE; |
| 1318 | /* fall through */ |
| 1319 | |
| 1320 | case PROMEX_ST_DONE: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 1321 | /* no more data are expected. Don't add TLR because mux-h1 will take care of it */ |
| 1322 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 9744f7c | 2019-03-27 15:48:53 +0100 | [diff] [blame] | 1323 | appctx->st0 = PROMEX_ST_END; |
| 1324 | /* fall through */ |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1325 | |
Christopher Faulet | 9744f7c | 2019-03-27 15:48:53 +0100 | [diff] [blame] | 1326 | case PROMEX_ST_END: |
| 1327 | if (!(res->flags & CF_SHUTR)) { |
| 1328 | res->flags |= CF_READ_NULL; |
| 1329 | si_shutr(si); |
| 1330 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1331 | } |
| 1332 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1333 | out: |
| 1334 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9744f7c | 2019-03-27 15:48:53 +0100 | [diff] [blame] | 1335 | |
| 1336 | /* eat the whole request */ |
| 1337 | if (co_data(req)) { |
| 1338 | req_htx = htx_from_buf(&req->buf); |
| 1339 | co_htx_skip(req, req_htx, co_data(req)); |
| 1340 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1341 | return; |
| 1342 | |
| 1343 | error: |
| 1344 | res->flags |= CF_READ_NULL; |
| 1345 | si_shutr(si); |
| 1346 | si_shutw(si); |
| 1347 | } |
| 1348 | |
| 1349 | struct applet promex_applet = { |
| 1350 | .obj_type = OBJ_TYPE_APPLET, |
| 1351 | .name = "<PROMEX>", /* used for logging */ |
| 1352 | .init = promex_appctx_init, |
| 1353 | .fct = promex_appctx_handle_io, |
| 1354 | }; |
| 1355 | |
| 1356 | static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px, |
| 1357 | struct act_rule *rule, char **err) |
| 1358 | { |
| 1359 | /* Prometheus exporter service is only available on "http-request" rulesets */ |
| 1360 | if (rule->from != ACT_F_HTTP_REQ) { |
| 1361 | memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets"); |
| 1362 | return ACT_RET_PRS_ERR; |
| 1363 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1364 | |
| 1365 | /* Add applet pointer in the rule. */ |
| 1366 | rule->applet = promex_applet; |
| 1367 | |
| 1368 | return ACT_RET_PRS_OK; |
| 1369 | } |
| 1370 | static void promex_register_build_options(void) |
| 1371 | { |
| 1372 | char *ptr = NULL; |
| 1373 | |
| 1374 | memprintf(&ptr, "Built with the Prometheus exporter as a service"); |
| 1375 | hap_register_build_opts(ptr, 1); |
| 1376 | } |
| 1377 | |
| 1378 | |
| 1379 | static struct action_kw_list service_actions = { ILH, { |
| 1380 | { "prometheus-exporter", service_parse_prometheus_exporter }, |
| 1381 | { /* END */ } |
| 1382 | }}; |
| 1383 | |
| 1384 | INITCALL1(STG_REGISTER, service_keywords_register, &service_actions); |
| 1385 | INITCALL0(STG_REGISTER, promex_register_build_options); |