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> |
William Dauchy | de3c326 | 2021-02-01 13:11:51 +0100 | [diff] [blame] | 21 | #include <haproxy/check.h> |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 22 | #include <haproxy/conn_stream.h> |
| 23 | #include <haproxy/cs_utils.h> |
Willy Tarreau | 762d7a5 | 2020-06-04 11:23:07 +0200 | [diff] [blame] | 24 | #include <haproxy/frontend.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 25 | #include <haproxy/global.h> |
Willy Tarreau | cd72d8c | 2020-06-02 19:11:26 +0200 | [diff] [blame] | 26 | #include <haproxy/http.h> |
Willy Tarreau | b7fc4c4 | 2021-10-06 18:56:42 +0200 | [diff] [blame] | 27 | #include <haproxy/http_ana.h> |
Willy Tarreau | 8773533 | 2020-06-04 09:08:41 +0200 | [diff] [blame] | 28 | #include <haproxy/http_htx.h> |
Willy Tarreau | 16f958c | 2020-06-03 08:44:35 +0200 | [diff] [blame] | 29 | #include <haproxy/htx.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 30 | #include <haproxy/list.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 31 | #include <haproxy/listener.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 32 | #include <haproxy/log.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 | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 36 | #include <haproxy/stats.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 37 | #include <haproxy/stream.h> |
Willy Tarreau | 5e539c9 | 2020-06-04 20:45:39 +0200 | [diff] [blame] | 38 | #include <haproxy/stream_interface.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 39 | #include <haproxy/task.h> |
Willy Tarreau | 0fd04fd | 2021-05-08 12:58:12 +0200 | [diff] [blame] | 40 | #include <haproxy/tools.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 { |
William Dauchy | 6916422 | 2021-02-07 20:42:38 +0100 | [diff] [blame] | 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_STICKTABLE, /* dump metrics of stick tables */ |
| 61 | PROMEX_DUMPER_DONE, /* finished */ |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | /* Prometheus exporter flags (appctx->ctx.stats.flags) */ |
William Dauchy | 6916422 | 2021-02-07 20:42:38 +0100 | [diff] [blame] | 65 | #define PROMEX_FL_METRIC_HDR 0x00000001 |
| 66 | #define PROMEX_FL_INFO_METRIC 0x00000002 |
| 67 | #define PROMEX_FL_FRONT_METRIC 0x00000004 |
| 68 | #define PROMEX_FL_BACK_METRIC 0x00000008 |
| 69 | #define PROMEX_FL_SRV_METRIC 0x00000010 |
William Dauchy | e3f7bd5 | 2021-02-14 23:22:56 +0100 | [diff] [blame] | 70 | #define PROMEX_FL_LI_METRIC 0x00000020 |
| 71 | #define PROMEX_FL_STICKTABLE_METRIC 0x00000040 |
| 72 | #define PROMEX_FL_SCOPE_GLOBAL 0x00000080 |
| 73 | #define PROMEX_FL_SCOPE_FRONT 0x00000100 |
| 74 | #define PROMEX_FL_SCOPE_BACK 0x00000200 |
| 75 | #define PROMEX_FL_SCOPE_SERVER 0x00000400 |
| 76 | #define PROMEX_FL_SCOPE_LI 0x00000800 |
| 77 | #define PROMEX_FL_SCOPE_STICKTABLE 0x00001000 |
| 78 | #define PROMEX_FL_NO_MAINT_SRV 0x00002000 |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 79 | |
William Dauchy | 6916422 | 2021-02-07 20:42:38 +0100 | [diff] [blame] | 80 | #define PROMEX_FL_SCOPE_ALL (PROMEX_FL_SCOPE_GLOBAL | PROMEX_FL_SCOPE_FRONT | \ |
William Dauchy | e3f7bd5 | 2021-02-14 23:22:56 +0100 | [diff] [blame] | 81 | PROMEX_FL_SCOPE_LI | PROMEX_FL_SCOPE_BACK | \ |
| 82 | PROMEX_FL_SCOPE_SERVER | PROMEX_FL_SCOPE_STICKTABLE) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 83 | |
Christopher Faulet | 0312c0d | 2021-01-20 15:19:12 +0100 | [diff] [blame] | 84 | /* Promtheus metric type (gauge or counter) */ |
| 85 | enum promex_mt_type { |
| 86 | PROMEX_MT_GAUGE = 1, |
| 87 | PROMEX_MT_COUNTER = 2, |
| 88 | }; |
| 89 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 90 | /* 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] | 91 | * enough. |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 92 | */ |
| 93 | #define PROMEX_MAX_NAME_LEN 128 |
| 94 | |
| 95 | /* The expected max length for a metric dump, including its header lines. It is |
| 96 | * just a soft limit to avoid extra work. We don't try to dump a metric if less |
| 97 | * than this size is available in the HTX. |
| 98 | */ |
| 99 | #define PROMEX_MAX_METRIC_LENGTH 512 |
| 100 | |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 101 | /* The max number of labels per metric */ |
| 102 | #define PROMEX_MAX_LABELS 8 |
William Dauchy | 5a982a7 | 2021-01-08 13:18:06 +0100 | [diff] [blame] | 103 | |
Christopher Faulet | 0312c0d | 2021-01-20 15:19:12 +0100 | [diff] [blame] | 104 | /* Describe a prometheus metric */ |
| 105 | struct promex_metric { |
| 106 | const struct ist n; /* The metric name */ |
| 107 | enum promex_mt_type type; /* The metric type (gauge or counter) */ |
| 108 | unsigned int flags; /* PROMEX_FL_* flags */ |
| 109 | }; |
| 110 | |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 111 | /* Describe a prometheus metric label. It is just a key/value pair */ |
| 112 | struct promex_label { |
| 113 | struct ist name; |
| 114 | struct ist value; |
| 115 | }; |
| 116 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 117 | /* Global metrics */ |
| 118 | const struct promex_metric promex_global_metrics[INF_TOTAL_FIELDS] = { |
| 119 | //[INF_NAME] ignored |
| 120 | //[INF_VERSION], ignored |
| 121 | //[INF_RELEASE_DATE] ignored |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 122 | [INF_NBTHREAD] = { .n = IST("nbthread"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 123 | [INF_NBPROC] = { .n = IST("nbproc"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 124 | [INF_PROCESS_NUM] = { .n = IST("relative_process_id"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 125 | //[INF_PID] ignored |
| 126 | //[INF_UPTIME] ignored |
| 127 | [INF_UPTIME_SEC] = { .n = IST("uptime_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 128 | [INF_START_TIME_SEC] = { .n = IST("start_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
William Dauchy | df9a05d | 2021-02-01 13:11:59 +0100 | [diff] [blame] | 129 | //[INF_MEMMAX_MB] ignored |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 130 | [INF_MEMMAX_BYTES] = { .n = IST("max_memory_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
William Dauchy | df9a05d | 2021-02-01 13:11:59 +0100 | [diff] [blame] | 131 | //[INF_POOL_ALLOC_MB] ignored |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 132 | [INF_POOL_ALLOC_BYTES] = { .n = IST("pool_allocated_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
William Dauchy | df9a05d | 2021-02-01 13:11:59 +0100 | [diff] [blame] | 133 | //[INF_POOL_USED_MB] ignored |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 134 | [INF_POOL_USED_BYTES] = { .n = IST("pool_used_bytes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 135 | [INF_POOL_FAILED] = { .n = IST("pool_failures_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
| 136 | [INF_ULIMIT_N] = { .n = IST("max_fds"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 137 | [INF_MAXSOCK] = { .n = IST("max_sockets"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 138 | [INF_MAXCONN] = { .n = IST("max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 139 | [INF_HARD_MAXCONN] = { .n = IST("hard_max_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 140 | [INF_CURR_CONN] = { .n = IST("current_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 141 | [INF_CUM_CONN] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
| 142 | [INF_CUM_REQ] = { .n = IST("requests_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
| 143 | [INF_MAX_SSL_CONNS] = { .n = IST("max_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 144 | [INF_CURR_SSL_CONNS] = { .n = IST("current_ssl_connections"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 145 | [INF_CUM_SSL_CONNS] = { .n = IST("ssl_connections_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
| 146 | [INF_MAXPIPES] = { .n = IST("max_pipes"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 147 | [INF_PIPES_USED] = { .n = IST("pipes_used_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
| 148 | [INF_PIPES_FREE] = { .n = IST("pipes_free_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
| 149 | [INF_CONN_RATE] = { .n = IST("current_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 150 | [INF_CONN_RATE_LIMIT] = { .n = IST("limit_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 151 | [INF_MAX_CONN_RATE] = { .n = IST("max_connection_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 152 | [INF_SESS_RATE] = { .n = IST("current_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 153 | [INF_SESS_RATE_LIMIT] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 154 | [INF_MAX_SESS_RATE] = { .n = IST("max_session_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 155 | [INF_SSL_RATE] = { .n = IST("current_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 156 | [INF_SSL_RATE_LIMIT] = { .n = IST("limit_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 157 | [INF_MAX_SSL_RATE] = { .n = IST("max_ssl_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 158 | [INF_SSL_FRONTEND_KEY_RATE] = { .n = IST("current_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 159 | [INF_SSL_FRONTEND_MAX_KEY_RATE] = { .n = IST("max_frontend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 160 | [INF_SSL_FRONTEND_SESSION_REUSE_PCT] = { .n = IST("frontend_ssl_reuse"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 161 | [INF_SSL_BACKEND_KEY_RATE] = { .n = IST("current_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 162 | [INF_SSL_BACKEND_MAX_KEY_RATE] = { .n = IST("max_backend_ssl_key_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 163 | [INF_SSL_CACHE_LOOKUPS] = { .n = IST("ssl_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
| 164 | [INF_SSL_CACHE_MISSES] = { .n = IST("ssl_cache_misses_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
| 165 | [INF_COMPRESS_BPS_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
| 166 | [INF_COMPRESS_BPS_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
| 167 | [INF_COMPRESS_BPS_RATE_LIM] = { .n = IST("limit_http_comp"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 168 | [INF_ZLIB_MEM_USAGE] = { .n = IST("current_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 169 | [INF_MAX_ZLIB_MEM_USAGE] = { .n = IST("max_zlib_memory"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 170 | [INF_TASKS] = { .n = IST("current_tasks"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 171 | [INF_RUN_QUEUE] = { .n = IST("current_run_queue"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 172 | [INF_IDLE_PCT] = { .n = IST("idle_time_percent"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 173 | //[INF_NODE] ignored |
| 174 | //[INF_DESCRIPTION] ignored |
| 175 | [INF_STOPPING] = { .n = IST("stopping"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 176 | [INF_JOBS] = { .n = IST("jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 177 | [INF_UNSTOPPABLE_JOBS] = { .n = IST("unstoppable_jobs"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 178 | [INF_LISTENERS] = { .n = IST("listeners"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 179 | [INF_ACTIVE_PEERS] = { .n = IST("active_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 180 | [INF_CONNECTED_PEERS] = { .n = IST("connected_peers"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 181 | [INF_DROPPED_LOGS] = { .n = IST("dropped_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
| 182 | [INF_BUSY_POLLING] = { .n = IST("busy_polling_enabled"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 183 | [INF_FAILED_RESOLUTIONS] = { .n = IST("failed_resolutions"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
| 184 | [INF_TOTAL_BYTES_OUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
| 185 | [INF_TOTAL_SPLICED_BYTES_OUT] = { .n = IST("spliced_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
| 186 | [INF_BYTES_OUT_RATE] = { .n = IST("bytes_out_rate"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
| 187 | //[INF_DEBUG_COMMANDS_ISSUED] ignored |
William Dauchy | 7741c33 | 2021-02-01 13:11:57 +0100 | [diff] [blame] | 188 | [INF_CUM_LOG_MSGS] = { .n = IST("recv_logs_total"), .type = PROMEX_MT_COUNTER, .flags = PROMEX_FL_INFO_METRIC }, |
William Dauchy | df9a05d | 2021-02-01 13:11:59 +0100 | [diff] [blame] | 189 | [INF_BUILD_INFO] = { .n = IST("build_info"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_INFO_METRIC }, |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 190 | }; |
| 191 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 192 | /* frontend/backend/server fields */ |
| 193 | const struct promex_metric promex_st_metrics[ST_F_TOTAL_FIELDS] = { |
William Dauchy | 42d7c40 | 2021-11-07 10:18:47 +0100 | [diff] [blame] | 194 | //[ST_F_PXNAME] ignored |
| 195 | //[ST_F_SVNAME] ignored |
| 196 | [ST_F_QCUR] = { .n = IST("current_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 197 | [ST_F_QMAX] = { .n = IST("max_queue"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 198 | [ST_F_SCUR] = { .n = IST("current_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 199 | [ST_F_SMAX] = { .n = IST("max_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 200 | [ST_F_SLIM] = { .n = IST("limit_sessions"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 201 | [ST_F_STOT] = { .n = IST("sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 202 | [ST_F_BIN] = { .n = IST("bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 203 | [ST_F_BOUT] = { .n = IST("bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 204 | [ST_F_DREQ] = { .n = IST("requests_denied_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC ) }, |
| 205 | [ST_F_DRESP] = { .n = IST("responses_denied_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 206 | [ST_F_EREQ] = { .n = IST("request_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) }, |
| 207 | [ST_F_ECON] = { .n = IST("connection_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 208 | [ST_F_ERESP] = { .n = IST("response_errors_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 209 | [ST_F_WRETR] = { .n = IST("retry_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 210 | [ST_F_WREDIS] = { .n = IST("redispatch_warnings_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 211 | [ST_F_STATUS] = { .n = IST("status"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 212 | [ST_F_WEIGHT] = { .n = IST("weight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 213 | [ST_F_ACT] = { .n = IST("active_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) }, |
| 214 | [ST_F_BCK] = { .n = IST("backup_servers"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) }, |
| 215 | [ST_F_CHKFAIL] = { .n = IST("check_failures_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_SRV_METRIC) }, |
| 216 | [ST_F_CHKDOWN] = { .n = IST("check_up_down_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 217 | [ST_F_LASTCHG] = { .n = IST("check_last_change_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 218 | [ST_F_DOWNTIME] = { .n = IST("downtime_seconds_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 219 | [ST_F_QLIMIT] = { .n = IST("queue_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) }, |
| 220 | //[ST_F_PID] ignored |
| 221 | //[ST_F_IID] ignored |
| 222 | //[ST_F_SID] ignored |
| 223 | [ST_F_THROTTLE] = { .n = IST("current_throttle"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) }, |
| 224 | [ST_F_LBTOT] = { .n = IST("loadbalanced_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 225 | //[ST_F_TRACKED] ignored |
| 226 | //[ST_F_TYPE] ignored |
| 227 | //[ST_F_RATE] ignored |
| 228 | [ST_F_RATE_LIM] = { .n = IST("limit_session_rate"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) }, |
| 229 | [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) }, |
| 230 | [ST_F_CHECK_STATUS] = { .n = IST("check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) }, |
| 231 | [ST_F_CHECK_CODE] = { .n = IST("check_code"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) }, |
| 232 | [ST_F_CHECK_DURATION] = { .n = IST("check_duration_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) }, |
| 233 | [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) }, |
| 234 | [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) }, |
| 235 | [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) }, |
| 236 | [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) }, |
| 237 | [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) }, |
| 238 | [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) }, |
| 239 | //[ST_F_HANAFAIL] ignored |
| 240 | //[ST_F_REQ_RATE] ignored |
| 241 | [ST_F_REQ_RATE_MAX] = { .n = IST("http_requests_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) }, |
| 242 | [ST_F_REQ_TOT] = { .n = IST("http_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) }, |
| 243 | [ST_F_CLI_ABRT] = { .n = IST("client_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 244 | [ST_F_SRV_ABRT] = { .n = IST("server_aborts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 245 | [ST_F_COMP_IN] = { .n = IST("http_comp_bytes_in_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) }, |
| 246 | [ST_F_COMP_OUT] = { .n = IST("http_comp_bytes_out_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) }, |
| 247 | [ST_F_COMP_BYP] = { .n = IST("http_comp_bytes_bypassed_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) }, |
| 248 | [ST_F_COMP_RSP] = { .n = IST("http_comp_responses_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) }, |
| 249 | [ST_F_LASTSESS] = { .n = IST("last_session_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 250 | //[ST_F_LAST_CHK] ignored |
| 251 | //[ST_F_LAST_AGT] ignored |
| 252 | [ST_F_QTIME] = { .n = IST("queue_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 253 | [ST_F_CTIME] = { .n = IST("connect_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 254 | [ST_F_RTIME] = { .n = IST("response_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 255 | [ST_F_TTIME] = { .n = IST("total_time_average_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 256 | //[ST_F_AGENT_STATUS] ignored |
| 257 | //[ST_F_AGENT_CODE] ignored |
| 258 | //[ST_F_AGENT_DURATION] ignored |
| 259 | //[ST_F_CHECK_DESC] ignored |
| 260 | //[ST_F_AGENT_DESC] ignored |
| 261 | //[ST_F_CHECK_RISE] ignored |
| 262 | //[ST_F_CHECK_FALL] ignored |
| 263 | //[ST_F_CHECK_HEALTH] ignored |
| 264 | //[ST_F_AGENT_RISE] ignored |
| 265 | //[ST_F_AGENT_FALL] ignored |
| 266 | //[ST_F_AGENT_HEALTH] ignored |
| 267 | //[ST_F_ADDR] ignored |
| 268 | //[ST_F_COOKIE] ignored |
| 269 | //[ST_F_MODE] ignored |
| 270 | //[ST_F_ALGO] ignored |
| 271 | //[ST_F_CONN_RATE] ignored |
| 272 | [ST_F_CONN_RATE_MAX] = { .n = IST("connections_rate_max"), .type = PROMEX_MT_GAUGE, .flags = (PROMEX_FL_FRONT_METRIC ) }, |
| 273 | [ST_F_CONN_TOT] = { .n = IST("connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) }, |
| 274 | [ST_F_INTERCEPTED] = { .n = IST("intercepted_requests_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC ) }, |
| 275 | [ST_F_DCON] = { .n = IST("denied_connections_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) }, |
| 276 | [ST_F_DSES] = { .n = IST("denied_sessions_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC ) }, |
| 277 | [ST_F_WREW] = { .n = IST("failed_header_rewriting_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 278 | [ST_F_CONNECT] = { .n = IST("connection_attempts_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 279 | [ST_F_REUSE] = { .n = IST("connection_reuses_total"), .type = PROMEX_MT_COUNTER, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 280 | [ST_F_CACHE_LOOKUPS] = { .n = IST("http_cache_lookups_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) }, |
| 281 | [ST_F_CACHE_HITS] = { .n = IST("http_cache_hits_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_BACK_METRIC ) }, |
| 282 | [ST_F_SRV_ICUR] = { .n = IST("idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) }, |
| 283 | [ST_F_SRV_ILIM] = { .n = IST("idle_connections_limit"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) }, |
| 284 | [ST_F_QT_MAX] = { .n = IST("max_queue_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 285 | [ST_F_CT_MAX] = { .n = IST("max_connect_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 286 | [ST_F_RT_MAX] = { .n = IST("max_response_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 287 | [ST_F_TT_MAX] = { .n = IST("max_total_time_seconds"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 288 | [ST_F_EINT] = { .n = IST("internal_errors_total"), .type = PROMEX_MT_COUNTER, .flags = (PROMEX_FL_FRONT_METRIC | PROMEX_FL_LI_METRIC | PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 289 | [ST_F_IDLE_CONN_CUR] = { .n = IST("unsafe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) }, |
| 290 | [ST_F_SAFE_CONN_CUR] = { .n = IST("safe_idle_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) }, |
| 291 | [ST_F_USED_CONN_CUR] = { .n = IST("used_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) }, |
| 292 | [ST_F_NEED_CONN_EST] = { .n = IST("need_connections_current"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_SRV_METRIC) }, |
| 293 | [ST_F_UWEIGHT] = { .n = IST("uweight"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC | PROMEX_FL_SRV_METRIC) }, |
| 294 | [ST_F_AGG_SRV_CHECK_STATUS] = { .n = IST("agg_server_check_status"), .type = PROMEX_MT_GAUGE, .flags = ( PROMEX_FL_BACK_METRIC ) }, |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 295 | }; |
| 296 | |
Ilya Shipitsin | acf8459 | 2021-02-06 22:29:08 +0500 | [diff] [blame] | 297 | /* Description of overridden stats fields */ |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 298 | const struct ist promex_st_metric_desc[ST_F_TOTAL_FIELDS] = { |
William Dauchy | a1da7ba | 2021-02-01 13:11:52 +0100 | [diff] [blame] | 299 | [ST_F_STATUS] = IST("Current status of the service, per state label value."), |
William Dauchy | de3c326 | 2021-02-01 13:11:51 +0100 | [diff] [blame] | 300 | [ST_F_CHECK_STATUS] = IST("Status of last health check, per state label value."), |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 301 | [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] | 302 | [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] | 303 | [ST_F_QTIME] = IST("Avg. queue time for last 1024 successful connections."), |
| 304 | [ST_F_CTIME] = IST("Avg. connect time for last 1024 successful connections."), |
| 305 | [ST_F_RTIME] = IST("Avg. response time for last 1024 successful connections."), |
| 306 | [ST_F_TTIME] = IST("Avg. total time for last 1024 successful connections."), |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 307 | [ST_F_QT_MAX] = IST("Maximum observed time spent in the queue"), |
| 308 | [ST_F_CT_MAX] = IST("Maximum observed time spent waiting for a connection to complete"), |
| 309 | [ST_F_RT_MAX] = IST("Maximum observed time spent waiting for a server response"), |
| 310 | [ST_F_TT_MAX] = IST("Maximum observed total request+response time (request+queue+connect+response+processing)"), |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 311 | }; |
| 312 | |
William Dauchy | 6916422 | 2021-02-07 20:42:38 +0100 | [diff] [blame] | 313 | /* stick table base fields */ |
| 314 | enum sticktable_field { |
| 315 | STICKTABLE_SIZE = 0, |
| 316 | STICKTABLE_USED, |
| 317 | /* must always be the last one */ |
| 318 | STICKTABLE_TOTAL_FIELDS |
| 319 | }; |
| 320 | |
| 321 | const struct promex_metric promex_sticktable_metrics[STICKTABLE_TOTAL_FIELDS] = { |
| 322 | [STICKTABLE_SIZE] = { .n = IST("size"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC }, |
| 323 | [STICKTABLE_USED] = { .n = IST("used"), .type = PROMEX_MT_GAUGE, .flags = PROMEX_FL_STICKTABLE_METRIC }, |
| 324 | }; |
| 325 | |
| 326 | /* stick table base description */ |
| 327 | const struct ist promex_sticktable_metric_desc[STICKTABLE_TOTAL_FIELDS] = { |
| 328 | [STICKTABLE_SIZE] = IST("Stick table size."), |
| 329 | [STICKTABLE_USED] = IST("Number of entries used in this stick table."), |
| 330 | }; |
| 331 | |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 332 | /* Specific labels for all ST_F_HRSP_* fields */ |
| 333 | const struct ist promex_hrsp_code[1 + ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = { |
| 334 | [ST_F_HRSP_1XX - ST_F_HRSP_1XX] = IST("1xx"), |
| 335 | [ST_F_HRSP_2XX - ST_F_HRSP_1XX] = IST("2xx"), |
| 336 | [ST_F_HRSP_3XX - ST_F_HRSP_1XX] = IST("3xx"), |
| 337 | [ST_F_HRSP_4XX - ST_F_HRSP_1XX] = IST("4xx"), |
| 338 | [ST_F_HRSP_5XX - ST_F_HRSP_1XX] = IST("5xx"), |
| 339 | [ST_F_HRSP_OTHER - ST_F_HRSP_1XX] = IST("other"), |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 340 | }; |
| 341 | |
William Dauchy | 5493821 | 2021-01-27 22:40:16 +0100 | [diff] [blame] | 342 | enum promex_front_state { |
| 343 | PROMEX_FRONT_STATE_DOWN = 0, |
| 344 | PROMEX_FRONT_STATE_UP, |
| 345 | |
| 346 | PROMEX_FRONT_STATE_COUNT /* must be last */ |
| 347 | }; |
| 348 | |
| 349 | const struct ist promex_front_st[PROMEX_FRONT_STATE_COUNT] = { |
| 350 | [PROMEX_FRONT_STATE_DOWN] = IST("DOWN"), |
| 351 | [PROMEX_FRONT_STATE_UP] = IST("UP"), |
| 352 | }; |
| 353 | |
| 354 | enum promex_back_state { |
| 355 | PROMEX_BACK_STATE_DOWN = 0, |
| 356 | PROMEX_BACK_STATE_UP, |
| 357 | |
| 358 | PROMEX_BACK_STATE_COUNT /* must be last */ |
| 359 | }; |
| 360 | |
| 361 | const struct ist promex_back_st[PROMEX_BACK_STATE_COUNT] = { |
| 362 | [PROMEX_BACK_STATE_DOWN] = IST("DOWN"), |
| 363 | [PROMEX_BACK_STATE_UP] = IST("UP"), |
| 364 | }; |
| 365 | |
| 366 | enum promex_srv_state { |
| 367 | PROMEX_SRV_STATE_DOWN = 0, |
| 368 | PROMEX_SRV_STATE_UP, |
| 369 | PROMEX_SRV_STATE_MAINT, |
| 370 | PROMEX_SRV_STATE_DRAIN, |
| 371 | PROMEX_SRV_STATE_NOLB, |
| 372 | |
| 373 | PROMEX_SRV_STATE_COUNT /* must be last */ |
| 374 | }; |
| 375 | |
| 376 | const struct ist promex_srv_st[PROMEX_SRV_STATE_COUNT] = { |
| 377 | [PROMEX_SRV_STATE_DOWN] = IST("DOWN"), |
| 378 | [PROMEX_SRV_STATE_UP] = IST("UP"), |
| 379 | [PROMEX_SRV_STATE_MAINT] = IST("MAINT"), |
| 380 | [PROMEX_SRV_STATE_DRAIN] = IST("DRAIN"), |
| 381 | [PROMEX_SRV_STATE_NOLB] = IST("NOLB"), |
| 382 | }; |
| 383 | |
| 384 | /* Return the server status. */ |
| 385 | enum promex_srv_state promex_srv_status(struct server *sv) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 386 | { |
William Dauchy | 5493821 | 2021-01-27 22:40:16 +0100 | [diff] [blame] | 387 | int state = PROMEX_SRV_STATE_DOWN; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 388 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 389 | 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] | 390 | state = PROMEX_SRV_STATE_UP; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 391 | if (sv->cur_admin & SRV_ADMF_DRAIN) |
William Dauchy | 5493821 | 2021-01-27 22:40:16 +0100 | [diff] [blame] | 392 | state = PROMEX_SRV_STATE_DRAIN; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 393 | } |
Christopher Faulet | d45d105 | 2019-09-06 16:10:19 +0200 | [diff] [blame] | 394 | else if (sv->cur_state == SRV_ST_STOPPING) |
William Dauchy | 5493821 | 2021-01-27 22:40:16 +0100 | [diff] [blame] | 395 | state = PROMEX_SRV_STATE_NOLB; |
Christopher Faulet | d45d105 | 2019-09-06 16:10:19 +0200 | [diff] [blame] | 396 | |
| 397 | if (sv->cur_admin & SRV_ADMF_MAINT) |
William Dauchy | 5493821 | 2021-01-27 22:40:16 +0100 | [diff] [blame] | 398 | state = PROMEX_SRV_STATE_MAINT; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 399 | |
| 400 | return state; |
| 401 | } |
| 402 | |
| 403 | /* Convert a field to its string representation and write it in <out>, followed |
| 404 | * 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] | 405 | * "NaN" because Prometheus only support numerical values (but it is unexepceted |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 406 | * to process this kind of value). It returns 1 on success. Otherwise, it |
| 407 | * returns 0. The buffer's length must not exceed <max> value. |
| 408 | */ |
| 409 | static int promex_metric_to_str(struct buffer *out, struct field *f, size_t max) |
| 410 | { |
| 411 | int ret = 0; |
| 412 | |
| 413 | switch (field_format(f, 0)) { |
William Dauchy | 18a2c6e | 2021-01-22 21:09:47 +0100 | [diff] [blame] | 414 | case FF_EMPTY: ret = chunk_strcat(out, "NaN\n"); break; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 415 | case FF_S32: ret = chunk_appendf(out, "%d\n", f->u.s32); break; |
| 416 | case FF_U32: ret = chunk_appendf(out, "%u\n", f->u.u32); break; |
| 417 | case FF_S64: ret = chunk_appendf(out, "%lld\n", (long long)f->u.s64); break; |
| 418 | 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] | 419 | 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] | 420 | case FF_STR: ret = chunk_strcat(out, "NaN\n"); break; |
| 421 | default: ret = chunk_strcat(out, "NaN\n"); break; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 422 | } |
| 423 | if (!ret || out->data > max) |
| 424 | return 0; |
| 425 | return 1; |
| 426 | } |
| 427 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 428 | /* Dump the header lines for <metric>. It is its #HELP and #TYPE strings. It |
| 429 | * returns 1 on success. Otherwise, if <out> length exceeds <max>, it returns 0. |
| 430 | */ |
| 431 | static int promex_dump_metric_header(struct appctx *appctx, struct htx *htx, |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 432 | const struct promex_metric *metric, const struct ist name, |
| 433 | struct ist *out, size_t max) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 434 | { |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 435 | struct ist type; |
William Dauchy | 82b2ce2 | 2021-02-01 13:11:55 +0100 | [diff] [blame] | 436 | struct ist desc; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 437 | |
| 438 | switch (metric->type) { |
| 439 | case PROMEX_MT_COUNTER: |
| 440 | type = ist("counter"); |
| 441 | break; |
| 442 | default: |
| 443 | type = ist("gauge"); |
| 444 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 445 | |
William Dauchy | a191b77 | 2021-01-15 22:41:39 +0100 | [diff] [blame] | 446 | if (istcat(out, ist("# HELP "), max) == -1 || |
| 447 | istcat(out, name, max) == -1 || |
| 448 | istcat(out, ist(" "), max) == -1) |
| 449 | goto full; |
| 450 | |
William Dauchy | 82b2ce2 | 2021-02-01 13:11:55 +0100 | [diff] [blame] | 451 | if (metric->flags & PROMEX_FL_INFO_METRIC) |
| 452 | desc = ist(info_fields[appctx->st2].desc); |
William Dauchy | 6916422 | 2021-02-07 20:42:38 +0100 | [diff] [blame] | 453 | else if (metric->flags & PROMEX_FL_STICKTABLE_METRIC) |
| 454 | desc = promex_sticktable_metric_desc[appctx->st2]; |
William Dauchy | 82b2ce2 | 2021-02-01 13:11:55 +0100 | [diff] [blame] | 455 | else if (!isttest(promex_st_metric_desc[appctx->st2])) |
| 456 | desc = ist(stat_fields[appctx->st2].desc); |
| 457 | else |
| 458 | desc = promex_st_metric_desc[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 459 | |
William Dauchy | 82b2ce2 | 2021-02-01 13:11:55 +0100 | [diff] [blame] | 460 | if (istcat(out, desc, max) == -1 || |
| 461 | istcat(out, ist("\n# TYPE "), max) == -1 || |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 462 | istcat(out, name, max) == -1 || |
| 463 | istcat(out, ist(" "), max) == -1 || |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 464 | istcat(out, type, max) == -1 || |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 465 | istcat(out, ist("\n"), max) == -1) |
| 466 | goto full; |
| 467 | |
| 468 | return 1; |
| 469 | |
| 470 | full: |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | /* Dump the line for <metric>. It starts by the metric name followed by its |
| 475 | * labels (proxy name, server name...) between braces and finally its value. If |
| 476 | * not already done, the header lines are dumped first. It returns 1 on |
| 477 | * success. Otherwise if <out> length exceeds <max>, it returns 0. |
| 478 | */ |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 479 | 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] | 480 | const struct promex_metric *metric, struct field *val, |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 481 | struct promex_label *labels, struct ist *out, size_t max) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 482 | { |
| 483 | struct ist name = { .ptr = (char[PROMEX_MAX_NAME_LEN]){ 0 }, .len = 0 }; |
| 484 | size_t len = out->len; |
| 485 | |
| 486 | if (out->len + PROMEX_MAX_METRIC_LENGTH > max) |
| 487 | return 0; |
| 488 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 489 | /* Fill the metric name */ |
| 490 | istcat(&name, prefix, PROMEX_MAX_NAME_LEN); |
| 491 | istcat(&name, metric->n, PROMEX_MAX_NAME_LEN); |
| 492 | |
| 493 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 494 | if ((appctx->ctx.stats.flags & PROMEX_FL_METRIC_HDR) && |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 495 | !promex_dump_metric_header(appctx, htx, metric, name, out, max)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 496 | goto full; |
| 497 | |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 498 | if (istcat(out, name, max) == -1) |
| 499 | goto full; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 500 | |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 501 | if (isttest(labels[0].name)) { |
| 502 | int i; |
| 503 | |
| 504 | if (istcat(out, ist("{"), max) == -1) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 505 | goto full; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 506 | |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 507 | for (i = 0; isttest(labels[i].name); i++) { |
| 508 | if (!isttest(labels[i].value)) |
| 509 | continue; |
| 510 | |
| 511 | if ((i && istcat(out, ist(","), max) == -1) || |
| 512 | istcat(out, labels[i].name, max) == -1 || |
| 513 | istcat(out, ist("=\""), max) == -1 || |
| 514 | istcat(out, labels[i].value, max) == -1 || |
| 515 | istcat(out, ist("\""), max) == -1) |
| 516 | goto full; |
| 517 | } |
| 518 | |
| 519 | if (istcat(out, ist("}"), max) == -1) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 520 | goto full; |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 521 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 522 | } |
| 523 | |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 524 | if (istcat(out, ist(" "), max) == -1) |
| 525 | goto full; |
| 526 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 527 | trash.data = out->len; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 528 | if (!promex_metric_to_str(&trash, val, max)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 529 | goto full; |
| 530 | out->len = trash.data; |
| 531 | |
| 532 | appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR; |
| 533 | return 1; |
| 534 | full: |
| 535 | // Restore previous length |
| 536 | out->len = len; |
| 537 | return 0; |
| 538 | |
| 539 | } |
| 540 | |
| 541 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 542 | /* Dump global metrics (prefixed by "haproxy_process_"). It returns 1 on success, |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 543 | * 0 if <htx> is full and -1 in case of any error. */ |
| 544 | static int promex_dump_global_metrics(struct appctx *appctx, struct htx *htx) |
| 545 | { |
| 546 | static struct ist prefix = IST("haproxy_process_"); |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 547 | struct field val; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 548 | struct channel *chn = cs_ic(appctx->owner); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 549 | struct ist out = ist2(trash.area, 0); |
Christopher Faulet | 11921e6 | 2019-07-03 11:43:17 +0200 | [diff] [blame] | 550 | 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] | 551 | int ret = 1; |
| 552 | |
Willy Tarreau | 0b26b38 | 2021-05-08 07:43:53 +0200 | [diff] [blame] | 553 | if (!stats_fill_info(info, INF_TOTAL_FIELDS, 0)) |
William Dauchy | 5d9b8f3 | 2021-01-11 20:07:49 +0100 | [diff] [blame] | 554 | return -1; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 555 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 556 | for (; appctx->st2 < INF_TOTAL_FIELDS; appctx->st2++) { |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 557 | struct promex_label labels[PROMEX_MAX_LABELS-1] = {}; |
| 558 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 559 | if (!(promex_global_metrics[appctx->st2].flags & appctx->ctx.stats.flags)) |
| 560 | continue; |
| 561 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 562 | switch (appctx->st2) { |
William Dauchy | 5a982a7 | 2021-01-08 13:18:06 +0100 | [diff] [blame] | 563 | case INF_BUILD_INFO: |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 564 | labels[0].name = ist("version"); |
| 565 | labels[0].value = ist(HAPROXY_VERSION); |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 566 | val = mkf_u32(FN_GAUGE, 1); |
William Dauchy | 5a982a7 | 2021-01-08 13:18:06 +0100 | [diff] [blame] | 567 | break; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 568 | |
| 569 | default: |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 570 | val = info[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 571 | } |
| 572 | |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 573 | if (!promex_dump_metric(appctx, htx, prefix, &promex_global_metrics[appctx->st2], |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 574 | &val, labels, &out, max)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 575 | goto full; |
| 576 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 577 | appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 578 | } |
| 579 | |
| 580 | end: |
Christopher Faulet | 0c55a15 | 2019-07-04 10:03:28 +0200 | [diff] [blame] | 581 | if (out.len) { |
| 582 | if (!htx_add_data_atonce(htx, out)) |
| 583 | return -1; /* Unexpected and unrecoverable error */ |
| 584 | channel_add_input(chn, out.len); |
| 585 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 586 | return ret; |
| 587 | full: |
| 588 | ret = 0; |
| 589 | goto end; |
| 590 | } |
| 591 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 592 | /* Dump frontends metrics (prefixed by "haproxy_frontend_"). It returns 1 on success, |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 593 | * 0 if <htx> is full and -1 in case of any error. */ |
| 594 | static int promex_dump_front_metrics(struct appctx *appctx, struct htx *htx) |
| 595 | { |
| 596 | static struct ist prefix = IST("haproxy_frontend_"); |
| 597 | struct proxy *px; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 598 | struct field val; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 599 | struct channel *chn = cs_ic(appctx->owner); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 600 | struct ist out = ist2(trash.area, 0); |
Christopher Faulet | 11921e6 | 2019-07-03 11:43:17 +0200 | [diff] [blame] | 601 | 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] | 602 | struct field *stats = stat_l[STATS_DOMAIN_PROXY]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 603 | int ret = 1; |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 604 | enum promex_front_state state; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 605 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 606 | for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) { |
| 607 | if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags)) |
| 608 | continue; |
| 609 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 610 | while (appctx->ctx.stats.obj1) { |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 611 | struct promex_label labels[PROMEX_MAX_LABELS-1] = {}; |
| 612 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 613 | px = appctx->ctx.stats.obj1; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 614 | |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 615 | labels[0].name = ist("proxy"); |
| 616 | labels[0].value = ist2(px->id, strlen(px->id)); |
| 617 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 618 | /* skip the disabled proxies, global frontend and non-networked ones */ |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 619 | if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_FE)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 620 | goto next_px; |
| 621 | |
William Dauchy | b957745 | 2021-01-17 18:27:46 +0100 | [diff] [blame] | 622 | if (!stats_fill_fe_stats(px, stats, ST_F_TOTAL_FIELDS, &(appctx->st2))) |
| 623 | return -1; |
| 624 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 625 | switch (appctx->st2) { |
| 626 | case ST_F_STATUS: |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 627 | state = !(px->flags & PR_FL_STOPPED); |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 628 | for (; appctx->ctx.stats.st_code < PROMEX_FRONT_STATE_COUNT; appctx->ctx.stats.st_code++) { |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 629 | labels[1].name = ist("state"); |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 630 | labels[1].value = promex_front_st[appctx->ctx.stats.st_code]; |
| 631 | val = mkf_u32(FO_STATUS, state == appctx->ctx.stats.st_code); |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 632 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 633 | &val, labels, &out, max)) |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 634 | goto full; |
| 635 | } |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 636 | appctx->ctx.stats.st_code = 0; |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 637 | goto next_px; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 638 | case ST_F_REQ_RATE_MAX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 639 | case ST_F_REQ_TOT: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 640 | case ST_F_INTERCEPTED: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 641 | case ST_F_CACHE_LOOKUPS: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 642 | case ST_F_CACHE_HITS: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 643 | case ST_F_COMP_IN: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 644 | case ST_F_COMP_OUT: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 645 | case ST_F_COMP_BYP: |
William Dauchy | b957745 | 2021-01-17 18:27:46 +0100 | [diff] [blame] | 646 | case ST_F_COMP_RSP: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 647 | if (px->mode != PR_MODE_HTTP) |
| 648 | goto next_px; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 649 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 650 | break; |
Christopher Faulet | 32ef48e | 2021-02-01 14:55:37 +0100 | [diff] [blame] | 651 | case ST_F_HRSP_1XX: |
William Dauchy | b957745 | 2021-01-17 18:27:46 +0100 | [diff] [blame] | 652 | case ST_F_HRSP_2XX: |
| 653 | case ST_F_HRSP_3XX: |
| 654 | case ST_F_HRSP_4XX: |
| 655 | case ST_F_HRSP_5XX: |
| 656 | case ST_F_HRSP_OTHER: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 657 | if (px->mode != PR_MODE_HTTP) |
| 658 | goto next_px; |
Christopher Faulet | 32ef48e | 2021-02-01 14:55:37 +0100 | [diff] [blame] | 659 | if (appctx->st2 != ST_F_HRSP_1XX) |
| 660 | appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR; |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 661 | labels[1].name = ist("code"); |
| 662 | labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX]; |
| 663 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 664 | break; |
| 665 | |
| 666 | default: |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 667 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 668 | } |
| 669 | |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 670 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 671 | &val, labels, &out, max)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 672 | goto full; |
| 673 | next_px: |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 674 | appctx->ctx.stats.obj1 = px->next; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 675 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 676 | appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR; |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 677 | appctx->ctx.stats.obj1 = proxies_list; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | end: |
Christopher Faulet | 0c55a15 | 2019-07-04 10:03:28 +0200 | [diff] [blame] | 681 | if (out.len) { |
| 682 | if (!htx_add_data_atonce(htx, out)) |
| 683 | return -1; /* Unexpected and unrecoverable error */ |
| 684 | channel_add_input(chn, out.len); |
| 685 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 686 | return ret; |
| 687 | full: |
| 688 | ret = 0; |
| 689 | goto end; |
| 690 | } |
| 691 | |
William Dauchy | e3f7bd5 | 2021-02-14 23:22:56 +0100 | [diff] [blame] | 692 | /* Dump listener metrics (prefixed by "haproxy_listen_"). It returns 1 on |
| 693 | * success, 0 if <htx> is full and -1 in case of any error. */ |
| 694 | static int promex_dump_listener_metrics(struct appctx *appctx, struct htx *htx) |
| 695 | { |
| 696 | static struct ist prefix = IST("haproxy_listener_"); |
| 697 | struct proxy *px; |
| 698 | struct field val; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 699 | struct channel *chn = cs_ic(appctx->owner); |
William Dauchy | e3f7bd5 | 2021-02-14 23:22:56 +0100 | [diff] [blame] | 700 | struct ist out = ist2(trash.area, 0); |
| 701 | size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx)); |
| 702 | struct field *stats = stat_l[STATS_DOMAIN_PROXY]; |
| 703 | struct listener *li; |
| 704 | int ret = 1; |
| 705 | enum li_status status; |
| 706 | |
| 707 | for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) { |
| 708 | if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags)) |
| 709 | continue; |
| 710 | |
| 711 | while (appctx->ctx.stats.obj1) { |
| 712 | struct promex_label labels[PROMEX_MAX_LABELS-1] = {}; |
| 713 | |
| 714 | px = appctx->ctx.stats.obj1; |
| 715 | |
| 716 | labels[0].name = ist("proxy"); |
| 717 | labels[0].value = ist2(px->id, strlen(px->id)); |
| 718 | |
| 719 | /* skip the disabled proxies, global frontend and non-networked ones */ |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 720 | if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_FE)) |
William Dauchy | e3f7bd5 | 2021-02-14 23:22:56 +0100 | [diff] [blame] | 721 | goto next_px; |
| 722 | |
| 723 | li = appctx->ctx.stats.obj2; |
| 724 | list_for_each_entry_from(li, &px->conf.listeners, by_fe) { |
| 725 | |
William Dauchy | e3f7bd5 | 2021-02-14 23:22:56 +0100 | [diff] [blame] | 726 | if (!li->counters) |
| 727 | continue; |
| 728 | |
William Dauchy | baf2273 | 2021-02-25 00:53:13 +0100 | [diff] [blame] | 729 | labels[1].name = ist("listener"); |
| 730 | labels[1].value = ist2(li->name, strlen(li->name)); |
| 731 | |
William Dauchy | e3f7bd5 | 2021-02-14 23:22:56 +0100 | [diff] [blame] | 732 | if (!stats_fill_li_stats(px, li, 0, stats, |
| 733 | ST_F_TOTAL_FIELDS, &(appctx->st2))) |
| 734 | return -1; |
| 735 | |
| 736 | switch (appctx->st2) { |
| 737 | case ST_F_STATUS: |
| 738 | status = get_li_status(li); |
| 739 | for (; appctx->ctx.stats.st_code < LI_STATE_COUNT; appctx->ctx.stats.st_code++) { |
| 740 | val = mkf_u32(FO_STATUS, status == appctx->ctx.stats.st_code); |
| 741 | labels[2].name = ist("state"); |
| 742 | labels[2].value = ist(li_status_st[appctx->ctx.stats.st_code]); |
| 743 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
| 744 | &val, labels, &out, max)) |
| 745 | goto full; |
| 746 | } |
| 747 | appctx->ctx.stats.st_code = 0; |
| 748 | continue; |
| 749 | default: |
| 750 | val = stats[appctx->st2]; |
| 751 | } |
| 752 | |
| 753 | if (!promex_dump_metric(appctx, htx, prefix, |
| 754 | &promex_st_metrics[appctx->st2], |
| 755 | &val, labels, &out, max)) |
| 756 | goto full; |
| 757 | } |
| 758 | |
| 759 | next_px: |
| 760 | px = px->next; |
| 761 | appctx->ctx.stats.obj1 = px; |
| 762 | appctx->ctx.stats.obj2 = (px ? LIST_NEXT(&px->conf.listeners, struct listener *, by_fe) : NULL); |
| 763 | } |
| 764 | appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR; |
| 765 | appctx->ctx.stats.obj1 = proxies_list; |
| 766 | appctx->ctx.stats.obj2 = LIST_NEXT(&proxies_list->conf.listeners, struct listener *, by_fe); |
| 767 | } |
| 768 | |
| 769 | end: |
| 770 | if (out.len) { |
| 771 | if (!htx_add_data_atonce(htx, out)) |
| 772 | return -1; /* Unexpected and unrecoverable error */ |
| 773 | channel_add_input(chn, out.len); |
| 774 | } |
| 775 | return ret; |
| 776 | full: |
| 777 | appctx->ctx.stats.obj2 = li; |
| 778 | ret = 0; |
| 779 | goto end; |
| 780 | } |
| 781 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 782 | /* Dump backends metrics (prefixed by "haproxy_backend_"). It returns 1 on success, |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 783 | * 0 if <htx> is full and -1 in case of any error. */ |
| 784 | static int promex_dump_back_metrics(struct appctx *appctx, struct htx *htx) |
| 785 | { |
| 786 | static struct ist prefix = IST("haproxy_backend_"); |
| 787 | struct proxy *px; |
William Dauchy | 42d7c40 | 2021-11-07 10:18:47 +0100 | [diff] [blame] | 788 | struct server *sv; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 789 | struct field val; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 790 | struct channel *chn = cs_ic(appctx->owner); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 791 | struct ist out = ist2(trash.area, 0); |
Christopher Faulet | 11921e6 | 2019-07-03 11:43:17 +0200 | [diff] [blame] | 792 | 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] | 793 | struct field *stats = stat_l[STATS_DOMAIN_PROXY]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 794 | int ret = 1; |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 795 | double secs; |
William Dauchy | 42d7c40 | 2021-11-07 10:18:47 +0100 | [diff] [blame] | 796 | enum promex_back_state bkd_state; |
| 797 | enum promex_srv_state srv_state; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 798 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 799 | for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) { |
| 800 | if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags)) |
| 801 | continue; |
| 802 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 803 | while (appctx->ctx.stats.obj1) { |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 804 | struct promex_label labels[PROMEX_MAX_LABELS-1] = {}; |
William Dauchy | 42d7c40 | 2021-11-07 10:18:47 +0100 | [diff] [blame] | 805 | unsigned int srv_state_count[PROMEX_SRV_STATE_COUNT] = { 0 }; |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 806 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 807 | px = appctx->ctx.stats.obj1; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 808 | |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 809 | labels[0].name = ist("proxy"); |
| 810 | labels[0].value = ist2(px->id, strlen(px->id)); |
| 811 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 812 | /* skip the disabled proxies, global frontend and non-networked ones */ |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 813 | if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_BE)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 814 | goto next_px; |
| 815 | |
William Dauchy | 3c6f006 | 2021-01-25 17:29:02 +0100 | [diff] [blame] | 816 | if (!stats_fill_be_stats(px, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2))) |
| 817 | return -1; |
| 818 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 819 | switch (appctx->st2) { |
William Dauchy | 42d7c40 | 2021-11-07 10:18:47 +0100 | [diff] [blame] | 820 | case ST_F_AGG_SRV_CHECK_STATUS: |
| 821 | if (!px->srv) |
| 822 | goto next_px; |
| 823 | sv = px->srv; |
| 824 | while (sv) { |
| 825 | srv_state = promex_srv_status(sv); |
| 826 | srv_state_count[srv_state] += 1; |
| 827 | sv = sv->next; |
| 828 | } |
| 829 | for (; appctx->ctx.stats.st_code < PROMEX_SRV_STATE_COUNT; appctx->ctx.stats.st_code++) { |
| 830 | val = mkf_u32(FN_GAUGE, srv_state_count[appctx->ctx.stats.st_code]); |
| 831 | labels[1].name = ist("state"); |
| 832 | labels[1].value = promex_srv_st[appctx->ctx.stats.st_code]; |
| 833 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
| 834 | &val, labels, &out, max)) |
| 835 | goto full; |
| 836 | } |
| 837 | appctx->ctx.stats.st_code = 0; |
| 838 | goto next_px; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 839 | case ST_F_STATUS: |
William Dauchy | 42d7c40 | 2021-11-07 10:18:47 +0100 | [diff] [blame] | 840 | bkd_state = ((px->lbprm.tot_weight > 0 || !px->srv) ? 1 : 0); |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 841 | for (; appctx->ctx.stats.st_code < PROMEX_BACK_STATE_COUNT; appctx->ctx.stats.st_code++) { |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 842 | labels[1].name = ist("state"); |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 843 | labels[1].value = promex_back_st[appctx->ctx.stats.st_code]; |
William Dauchy | 42d7c40 | 2021-11-07 10:18:47 +0100 | [diff] [blame] | 844 | val = mkf_u32(FO_STATUS, bkd_state == appctx->ctx.stats.st_code); |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 845 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 846 | &val, labels, &out, max)) |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 847 | goto full; |
| 848 | } |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 849 | appctx->ctx.stats.st_code = 0; |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 850 | goto next_px; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 851 | case ST_F_QTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 852 | 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] | 853 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 854 | break; |
| 855 | case ST_F_CTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 856 | 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] | 857 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 858 | break; |
| 859 | case ST_F_RTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 860 | 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] | 861 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 862 | break; |
| 863 | case ST_F_TTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 864 | 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] | 865 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 866 | break; |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 867 | case ST_F_QT_MAX: |
| 868 | secs = (double)px->be_counters.qtime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 869 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 870 | break; |
| 871 | case ST_F_CT_MAX: |
| 872 | secs = (double)px->be_counters.ctime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 873 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 874 | break; |
| 875 | case ST_F_RT_MAX: |
| 876 | secs = (double)px->be_counters.dtime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 877 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 878 | break; |
| 879 | case ST_F_TT_MAX: |
| 880 | secs = (double)px->be_counters.ttime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 881 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 882 | break; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 883 | case ST_F_REQ_TOT: |
William Dauchy | 3c6f006 | 2021-01-25 17:29:02 +0100 | [diff] [blame] | 884 | case ST_F_CACHE_LOOKUPS: |
| 885 | case ST_F_CACHE_HITS: |
| 886 | case ST_F_COMP_IN: |
| 887 | case ST_F_COMP_OUT: |
| 888 | case ST_F_COMP_BYP: |
| 889 | case ST_F_COMP_RSP: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 890 | if (px->mode != PR_MODE_HTTP) |
| 891 | goto next_px; |
William Dauchy | 3c6f006 | 2021-01-25 17:29:02 +0100 | [diff] [blame] | 892 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 893 | break; |
Christopher Faulet | 32ef48e | 2021-02-01 14:55:37 +0100 | [diff] [blame] | 894 | case ST_F_HRSP_1XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 895 | case ST_F_HRSP_2XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 896 | case ST_F_HRSP_3XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 897 | case ST_F_HRSP_4XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 898 | case ST_F_HRSP_5XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 899 | case ST_F_HRSP_OTHER: |
| 900 | if (px->mode != PR_MODE_HTTP) |
| 901 | goto next_px; |
Christopher Faulet | 32ef48e | 2021-02-01 14:55:37 +0100 | [diff] [blame] | 902 | if (appctx->st2 != ST_F_HRSP_1XX) |
| 903 | appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR; |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 904 | labels[1].name = ist("code"); |
| 905 | labels[1].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX]; |
William Dauchy | 3c6f006 | 2021-01-25 17:29:02 +0100 | [diff] [blame] | 906 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 907 | break; |
| 908 | |
| 909 | default: |
William Dauchy | 3c6f006 | 2021-01-25 17:29:02 +0100 | [diff] [blame] | 910 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 911 | } |
| 912 | |
William Dauchy | 3c6f006 | 2021-01-25 17:29:02 +0100 | [diff] [blame] | 913 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 914 | &val, labels, &out, max)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 915 | goto full; |
| 916 | next_px: |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 917 | appctx->ctx.stats.obj1 = px->next; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 918 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 919 | appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR; |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 920 | appctx->ctx.stats.obj1 = proxies_list; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | end: |
Christopher Faulet | 0c55a15 | 2019-07-04 10:03:28 +0200 | [diff] [blame] | 924 | if (out.len) { |
| 925 | if (!htx_add_data_atonce(htx, out)) |
| 926 | return -1; /* Unexpected and unrecoverable error */ |
| 927 | channel_add_input(chn, out.len); |
| 928 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 929 | return ret; |
| 930 | full: |
| 931 | ret = 0; |
| 932 | goto end; |
| 933 | } |
| 934 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 935 | /* Dump servers metrics (prefixed by "haproxy_server_"). It returns 1 on success, |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 936 | * 0 if <htx> is full and -1 in case of any error. */ |
| 937 | static int promex_dump_srv_metrics(struct appctx *appctx, struct htx *htx) |
| 938 | { |
| 939 | static struct ist prefix = IST("haproxy_server_"); |
| 940 | struct proxy *px; |
| 941 | struct server *sv; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 942 | struct field val; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 943 | struct channel *chn = cs_ic(appctx->owner); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 944 | struct ist out = ist2(trash.area, 0); |
Christopher Faulet | 11921e6 | 2019-07-03 11:43:17 +0200 | [diff] [blame] | 945 | 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] | 946 | struct field *stats = stat_l[STATS_DOMAIN_PROXY]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 947 | int ret = 1; |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 948 | double secs; |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 949 | enum promex_srv_state state; |
William Dauchy | de3c326 | 2021-02-01 13:11:51 +0100 | [diff] [blame] | 950 | const char *check_state; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 951 | |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 952 | for (;appctx->st2 < ST_F_TOTAL_FIELDS; appctx->st2++) { |
| 953 | if (!(promex_st_metrics[appctx->st2].flags & appctx->ctx.stats.flags)) |
| 954 | continue; |
| 955 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 956 | while (appctx->ctx.stats.obj1) { |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 957 | struct promex_label labels[PROMEX_MAX_LABELS-1] = {}; |
| 958 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 959 | px = appctx->ctx.stats.obj1; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 960 | |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 961 | labels[0].name = ist("proxy"); |
| 962 | labels[0].value = ist2(px->id, strlen(px->id)); |
| 963 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 964 | /* skip the disabled proxies, global frontend and non-networked ones */ |
Christopher Faulet | dfd10ab | 2021-10-06 14:24:19 +0200 | [diff] [blame] | 965 | if ((px->flags & PR_FL_DISABLED) || px->uuid <= 0 || !(px->cap & PR_CAP_BE)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 966 | goto next_px; |
| 967 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 968 | while (appctx->ctx.stats.obj2) { |
| 969 | sv = appctx->ctx.stats.obj2; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 970 | |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 971 | labels[1].name = ist("server"); |
| 972 | labels[1].value = ist2(sv->id, strlen(sv->id)); |
| 973 | |
William Dauchy | bde2bf6 | 2021-01-25 17:29:04 +0100 | [diff] [blame] | 974 | if (!stats_fill_sv_stats(px, sv, 0, stats, ST_F_TOTAL_FIELDS, &(appctx->st2))) |
| 975 | return -1; |
| 976 | |
Christopher Faulet | eba2294 | 2019-11-19 14:18:24 +0100 | [diff] [blame] | 977 | if ((appctx->ctx.stats.flags & PROMEX_FL_NO_MAINT_SRV) && (sv->cur_admin & SRV_ADMF_MAINT)) |
| 978 | goto next_sv; |
| 979 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 980 | switch (appctx->st2) { |
| 981 | case ST_F_STATUS: |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 982 | state = promex_srv_status(sv); |
William Dauchy | 64a3805 | 2021-02-14 22:26:24 +0100 | [diff] [blame] | 983 | for (; appctx->ctx.stats.st_code < PROMEX_SRV_STATE_COUNT; appctx->ctx.stats.st_code++) { |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 984 | val = mkf_u32(FO_STATUS, state == appctx->ctx.stats.st_code); |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 985 | labels[2].name = ist("state"); |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 986 | labels[2].value = promex_srv_st[appctx->ctx.stats.st_code]; |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 987 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 988 | &val, labels, &out, max)) |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 989 | goto full; |
| 990 | } |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 991 | appctx->ctx.stats.st_code = 0; |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 992 | goto next_sv; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 993 | case ST_F_QTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 994 | 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] | 995 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 996 | break; |
| 997 | case ST_F_CTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 998 | 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] | 999 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1000 | break; |
| 1001 | case ST_F_RTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 1002 | 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] | 1003 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1004 | break; |
| 1005 | case ST_F_TTIME: |
Christopher Faulet | af4bf14 | 2019-09-24 16:35:19 +0200 | [diff] [blame] | 1006 | 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] | 1007 | val = mkf_flt(FN_AVG, secs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1008 | break; |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 1009 | case ST_F_QT_MAX: |
| 1010 | secs = (double)sv->counters.qtime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 1011 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 1012 | break; |
| 1013 | case ST_F_CT_MAX: |
| 1014 | secs = (double)sv->counters.ctime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 1015 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 1016 | break; |
| 1017 | case ST_F_RT_MAX: |
| 1018 | secs = (double)sv->counters.dtime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 1019 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 1020 | break; |
| 1021 | case ST_F_TT_MAX: |
| 1022 | secs = (double)sv->counters.ttime_max / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 1023 | val = mkf_flt(FN_MAX, secs); |
Christopher Faulet | 8fc027d | 2019-11-08 15:05:31 +0100 | [diff] [blame] | 1024 | break; |
Christopher Faulet | cf403f3 | 2019-11-21 14:35:46 +0100 | [diff] [blame] | 1025 | case ST_F_CHECK_STATUS: |
| 1026 | if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED) |
| 1027 | goto next_sv; |
William Dauchy | de3c326 | 2021-02-01 13:11:51 +0100 | [diff] [blame] | 1028 | |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 1029 | for (; appctx->ctx.stats.st_code < HCHK_STATUS_SIZE; appctx->ctx.stats.st_code++) { |
| 1030 | if (get_check_status_result(appctx->ctx.stats.st_code) < CHK_RES_FAILED) |
William Dauchy | de3c326 | 2021-02-01 13:11:51 +0100 | [diff] [blame] | 1031 | continue; |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 1032 | val = mkf_u32(FO_STATUS, sv->check.status == appctx->ctx.stats.st_code); |
| 1033 | check_state = get_check_status_info(appctx->ctx.stats.st_code); |
William Dauchy | de3c326 | 2021-02-01 13:11:51 +0100 | [diff] [blame] | 1034 | labels[2].name = ist("state"); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 1035 | labels[2].value = ist(check_state); |
William Dauchy | de3c326 | 2021-02-01 13:11:51 +0100 | [diff] [blame] | 1036 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
| 1037 | &val, labels, &out, max)) |
| 1038 | goto full; |
| 1039 | } |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 1040 | appctx->ctx.stats.st_code = 0; |
William Dauchy | de3c326 | 2021-02-01 13:11:51 +0100 | [diff] [blame] | 1041 | goto next_sv; |
Christopher Faulet | cf403f3 | 2019-11-21 14:35:46 +0100 | [diff] [blame] | 1042 | case ST_F_CHECK_CODE: |
| 1043 | if ((sv->check.state & (CHK_ST_ENABLED|CHK_ST_PAUSED)) != CHK_ST_ENABLED) |
| 1044 | goto next_sv; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 1045 | 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] | 1046 | break; |
Christopher Faulet | 2711e51 | 2020-02-27 16:12:07 +0100 | [diff] [blame] | 1047 | case ST_F_CHECK_DURATION: |
| 1048 | if (sv->check.status < HCHK_STATUS_CHECKED) |
| 1049 | goto next_sv; |
| 1050 | secs = (double)sv->check.duration / 1000.0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 1051 | val = mkf_flt(FN_DURATION, secs); |
Christopher Faulet | 2711e51 | 2020-02-27 16:12:07 +0100 | [diff] [blame] | 1052 | break; |
Marcin Deranek | 3c27dda | 2020-05-15 18:32:51 +0200 | [diff] [blame] | 1053 | case ST_F_REQ_TOT: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1054 | if (px->mode != PR_MODE_HTTP) |
| 1055 | goto next_px; |
William Dauchy | bde2bf6 | 2021-01-25 17:29:04 +0100 | [diff] [blame] | 1056 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1057 | break; |
Christopher Faulet | 32ef48e | 2021-02-01 14:55:37 +0100 | [diff] [blame] | 1058 | case ST_F_HRSP_1XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1059 | case ST_F_HRSP_2XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1060 | case ST_F_HRSP_3XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1061 | case ST_F_HRSP_4XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1062 | case ST_F_HRSP_5XX: |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1063 | case ST_F_HRSP_OTHER: |
| 1064 | if (px->mode != PR_MODE_HTTP) |
| 1065 | goto next_px; |
Christopher Faulet | 32ef48e | 2021-02-01 14:55:37 +0100 | [diff] [blame] | 1066 | if (appctx->st2 != ST_F_HRSP_1XX) |
| 1067 | appctx->ctx.stats.flags &= ~PROMEX_FL_METRIC_HDR; |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 1068 | labels[2].name = ist("code"); |
| 1069 | labels[2].value = promex_hrsp_code[appctx->st2 - ST_F_HRSP_1XX]; |
William Dauchy | bde2bf6 | 2021-01-25 17:29:04 +0100 | [diff] [blame] | 1070 | val = stats[appctx->st2]; |
Christopher Faulet | c55a626 | 2020-07-10 15:39:39 +0200 | [diff] [blame] | 1071 | break; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1072 | |
| 1073 | default: |
William Dauchy | bde2bf6 | 2021-01-25 17:29:04 +0100 | [diff] [blame] | 1074 | val = stats[appctx->st2]; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1075 | } |
| 1076 | |
William Dauchy | c646459 | 2021-01-27 22:40:17 +0100 | [diff] [blame] | 1077 | if (!promex_dump_metric(appctx, htx, prefix, &promex_st_metrics[appctx->st2], |
Christopher Faulet | 5a2f938 | 2021-01-28 11:24:17 +0100 | [diff] [blame] | 1078 | &val, labels, &out, max)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1079 | goto full; |
Christopher Faulet | eba2294 | 2019-11-19 14:18:24 +0100 | [diff] [blame] | 1080 | next_sv: |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1081 | appctx->ctx.stats.obj2 = sv->next; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | next_px: |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1085 | appctx->ctx.stats.obj1 = px->next; |
| 1086 | 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] | 1087 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1088 | appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR; |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1089 | appctx->ctx.stats.obj1 = proxies_list; |
| 1090 | 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] | 1091 | } |
| 1092 | |
| 1093 | |
| 1094 | end: |
Christopher Faulet | 0c55a15 | 2019-07-04 10:03:28 +0200 | [diff] [blame] | 1095 | if (out.len) { |
| 1096 | if (!htx_add_data_atonce(htx, out)) |
| 1097 | return -1; /* Unexpected and unrecoverable error */ |
| 1098 | channel_add_input(chn, out.len); |
| 1099 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1100 | return ret; |
| 1101 | full: |
| 1102 | ret = 0; |
| 1103 | goto end; |
| 1104 | } |
| 1105 | |
William Dauchy | 6916422 | 2021-02-07 20:42:38 +0100 | [diff] [blame] | 1106 | /* Dump stick table metrics (prefixed by "haproxy_sticktable_"). It returns 1 on success, |
| 1107 | * 0 if <htx> is full and -1 in case of any error. */ |
| 1108 | static int promex_dump_sticktable_metrics(struct appctx *appctx, struct htx *htx) |
| 1109 | { |
| 1110 | static struct ist prefix = IST("haproxy_sticktable_"); |
| 1111 | struct field val; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1112 | struct channel *chn = cs_ic(appctx->owner); |
William Dauchy | 6916422 | 2021-02-07 20:42:38 +0100 | [diff] [blame] | 1113 | struct ist out = ist2(trash.area, 0); |
| 1114 | size_t max = htx_get_max_blksz(htx, channel_htx_recv_max(chn, htx)); |
| 1115 | int ret = 1; |
| 1116 | struct stktable *t; |
| 1117 | |
| 1118 | for (; appctx->st2 < STICKTABLE_TOTAL_FIELDS; appctx->st2++) { |
| 1119 | if (!(promex_sticktable_metrics[appctx->st2].flags & appctx->ctx.stats.flags)) |
| 1120 | continue; |
| 1121 | |
| 1122 | while (appctx->ctx.stats.obj1) { |
| 1123 | struct promex_label labels[PROMEX_MAX_LABELS - 1] = {}; |
| 1124 | |
| 1125 | t = appctx->ctx.stats.obj1; |
| 1126 | if (!t->size) |
| 1127 | goto next_px; |
| 1128 | |
| 1129 | labels[0].name = ist("name"); |
| 1130 | labels[0].value = ist2(t->id, strlen(t->id)); |
| 1131 | labels[1].name = ist("type"); |
| 1132 | labels[1].value = ist2(stktable_types[t->type].kw, strlen(stktable_types[t->type].kw)); |
| 1133 | switch (appctx->st2) { |
| 1134 | case STICKTABLE_SIZE: |
| 1135 | val = mkf_u32(FN_GAUGE, t->size); |
| 1136 | break; |
| 1137 | case STICKTABLE_USED: |
| 1138 | val = mkf_u32(FN_GAUGE, t->current); |
| 1139 | break; |
| 1140 | default: |
| 1141 | goto next_px; |
| 1142 | } |
| 1143 | |
| 1144 | if (!promex_dump_metric(appctx, htx, prefix, |
| 1145 | &promex_sticktable_metrics[appctx->st2], |
| 1146 | &val, labels, &out, max)) |
| 1147 | goto full; |
| 1148 | |
| 1149 | next_px: |
| 1150 | appctx->ctx.stats.obj1 = t->next; |
| 1151 | } |
| 1152 | appctx->ctx.stats.flags |= PROMEX_FL_METRIC_HDR; |
| 1153 | appctx->ctx.stats.obj1 = stktables_list; |
| 1154 | } |
| 1155 | |
| 1156 | end: |
| 1157 | if (out.len) { |
| 1158 | if (!htx_add_data_atonce(htx, out)) |
| 1159 | return -1; /* Unexpected and unrecoverable error */ |
| 1160 | channel_add_input(chn, out.len); |
| 1161 | } |
| 1162 | return ret; |
| 1163 | full: |
| 1164 | ret = 0; |
| 1165 | goto end; |
| 1166 | } |
| 1167 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1168 | /* Dump all metrics (global, frontends, backends and servers) depending on the |
| 1169 | * 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] | 1170 | * -1 in case of any error. |
| 1171 | * Uses <appctx.ctx.stats.obj1> as a pointer to the current proxy and <obj2> as |
| 1172 | * a pointer to the current server/listener. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1173 | static int promex_dump_metrics(struct appctx *appctx, struct conn_stream *cs, struct htx *htx) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1174 | { |
| 1175 | int ret; |
| 1176 | |
| 1177 | switch (appctx->st1) { |
| 1178 | case PROMEX_DUMPER_INIT: |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1179 | appctx->ctx.stats.obj1 = NULL; |
| 1180 | appctx->ctx.stats.obj2 = NULL; |
Christopher Faulet | efde955 | 2020-06-05 08:18:56 +0200 | [diff] [blame] | 1181 | appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_INFO_METRIC); |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 1182 | appctx->ctx.stats.st_code = 0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 1183 | appctx->st2 = INF_NAME; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1184 | appctx->st1 = PROMEX_DUMPER_GLOBAL; |
| 1185 | /* fall through */ |
| 1186 | |
| 1187 | case PROMEX_DUMPER_GLOBAL: |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1188 | if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_GLOBAL) { |
| 1189 | ret = promex_dump_global_metrics(appctx, htx); |
| 1190 | if (ret <= 0) { |
| 1191 | if (ret == -1) |
| 1192 | goto error; |
| 1193 | goto full; |
| 1194 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1195 | } |
| 1196 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1197 | appctx->ctx.stats.obj1 = proxies_list; |
| 1198 | appctx->ctx.stats.obj2 = NULL; |
Christopher Faulet | efde955 | 2020-06-05 08:18:56 +0200 | [diff] [blame] | 1199 | appctx->ctx.stats.flags &= ~PROMEX_FL_INFO_METRIC; |
Christopher Faulet | b713c4f | 2021-01-20 15:02:50 +0100 | [diff] [blame] | 1200 | appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_FRONT_METRIC); |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 1201 | appctx->ctx.stats.st_code = 0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 1202 | appctx->st2 = ST_F_PXNAME; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1203 | appctx->st1 = PROMEX_DUMPER_FRONT; |
| 1204 | /* fall through */ |
| 1205 | |
| 1206 | case PROMEX_DUMPER_FRONT: |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1207 | if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_FRONT) { |
| 1208 | ret = promex_dump_front_metrics(appctx, htx); |
| 1209 | if (ret <= 0) { |
| 1210 | if (ret == -1) |
| 1211 | goto error; |
| 1212 | goto full; |
| 1213 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1214 | } |
| 1215 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1216 | appctx->ctx.stats.obj1 = proxies_list; |
William Dauchy | e3f7bd5 | 2021-02-14 23:22:56 +0100 | [diff] [blame] | 1217 | appctx->ctx.stats.obj2 = LIST_NEXT(&proxies_list->conf.listeners, struct listener *, by_fe); |
Christopher Faulet | b713c4f | 2021-01-20 15:02:50 +0100 | [diff] [blame] | 1218 | appctx->ctx.stats.flags &= ~PROMEX_FL_FRONT_METRIC; |
William Dauchy | e3f7bd5 | 2021-02-14 23:22:56 +0100 | [diff] [blame] | 1219 | appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_LI_METRIC); |
| 1220 | appctx->ctx.stats.st_code = 0; |
| 1221 | appctx->st2 = ST_F_PXNAME; |
| 1222 | appctx->st1 = PROMEX_DUMPER_LI; |
| 1223 | /* fall through */ |
| 1224 | |
| 1225 | case PROMEX_DUMPER_LI: |
| 1226 | if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_LI) { |
| 1227 | ret = promex_dump_listener_metrics(appctx, htx); |
| 1228 | if (ret <= 0) { |
| 1229 | if (ret == -1) |
| 1230 | goto error; |
| 1231 | goto full; |
| 1232 | } |
| 1233 | } |
| 1234 | |
| 1235 | appctx->ctx.stats.obj1 = proxies_list; |
| 1236 | appctx->ctx.stats.obj2 = NULL; |
| 1237 | appctx->ctx.stats.flags &= ~PROMEX_FL_LI_METRIC; |
Christopher Faulet | b713c4f | 2021-01-20 15:02:50 +0100 | [diff] [blame] | 1238 | appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_BACK_METRIC); |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 1239 | appctx->ctx.stats.st_code = 0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 1240 | appctx->st2 = ST_F_PXNAME; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1241 | appctx->st1 = PROMEX_DUMPER_BACK; |
| 1242 | /* fall through */ |
| 1243 | |
| 1244 | case PROMEX_DUMPER_BACK: |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1245 | if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_BACK) { |
| 1246 | ret = promex_dump_back_metrics(appctx, htx); |
| 1247 | if (ret <= 0) { |
| 1248 | if (ret == -1) |
| 1249 | goto error; |
| 1250 | goto full; |
| 1251 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1252 | } |
| 1253 | |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1254 | appctx->ctx.stats.obj1 = proxies_list; |
| 1255 | 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] | 1256 | appctx->ctx.stats.flags &= ~PROMEX_FL_BACK_METRIC; |
| 1257 | appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC); |
Christopher Faulet | 040b119 | 2021-02-01 15:05:21 +0100 | [diff] [blame] | 1258 | appctx->ctx.stats.st_code = 0; |
Christopher Faulet | 37286a5 | 2021-01-20 15:20:53 +0100 | [diff] [blame] | 1259 | appctx->st2 = ST_F_PXNAME; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1260 | appctx->st1 = PROMEX_DUMPER_SRV; |
| 1261 | /* fall through */ |
| 1262 | |
| 1263 | case PROMEX_DUMPER_SRV: |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1264 | if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_SERVER) { |
| 1265 | ret = promex_dump_srv_metrics(appctx, htx); |
| 1266 | if (ret <= 0) { |
| 1267 | if (ret == -1) |
| 1268 | goto error; |
| 1269 | goto full; |
| 1270 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1271 | } |
| 1272 | |
William Dauchy | 6916422 | 2021-02-07 20:42:38 +0100 | [diff] [blame] | 1273 | appctx->ctx.stats.obj1 = stktables_list; |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1274 | appctx->ctx.stats.obj2 = NULL; |
Christopher Faulet | b713c4f | 2021-01-20 15:02:50 +0100 | [diff] [blame] | 1275 | appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_SRV_METRIC); |
William Dauchy | 6916422 | 2021-02-07 20:42:38 +0100 | [diff] [blame] | 1276 | appctx->ctx.stats.flags |= (PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC); |
| 1277 | appctx->st2 = STICKTABLE_SIZE; |
| 1278 | appctx->st1 = PROMEX_DUMPER_STICKTABLE; |
| 1279 | /* fall through */ |
| 1280 | |
| 1281 | case PROMEX_DUMPER_STICKTABLE: |
| 1282 | if (appctx->ctx.stats.flags & PROMEX_FL_SCOPE_STICKTABLE) { |
| 1283 | ret = promex_dump_sticktable_metrics(appctx, htx); |
| 1284 | if (ret <= 0) { |
| 1285 | if (ret == -1) |
| 1286 | goto error; |
| 1287 | goto full; |
| 1288 | } |
| 1289 | } |
| 1290 | |
| 1291 | appctx->ctx.stats.obj1 = NULL; |
| 1292 | appctx->ctx.stats.obj2 = NULL; |
| 1293 | appctx->ctx.stats.flags &= ~(PROMEX_FL_METRIC_HDR|PROMEX_FL_STICKTABLE_METRIC); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1294 | appctx->st2 = 0; |
| 1295 | appctx->st1 = PROMEX_DUMPER_DONE; |
| 1296 | /* fall through */ |
| 1297 | |
| 1298 | case PROMEX_DUMPER_DONE: |
| 1299 | default: |
| 1300 | break; |
| 1301 | } |
| 1302 | |
| 1303 | return 1; |
| 1304 | |
| 1305 | full: |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame^] | 1306 | cs_rx_room_blk(cs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1307 | return 0; |
| 1308 | error: |
| 1309 | /* unrecoverable error */ |
Amaury Denoyelle | da5b6d1 | 2020-10-02 18:32:02 +0200 | [diff] [blame] | 1310 | appctx->ctx.stats.obj1 = NULL; |
| 1311 | appctx->ctx.stats.obj2 = NULL; |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1312 | appctx->ctx.stats.flags = 0; |
| 1313 | appctx->st2 = 0; |
| 1314 | appctx->st1 = PROMEX_DUMPER_DONE; |
| 1315 | return -1; |
| 1316 | } |
| 1317 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 1318 | /* 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] | 1319 | * success and -1 on error. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1320 | static int promex_parse_uri(struct appctx *appctx, struct conn_stream *cs) |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1321 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1322 | struct channel *req = cs_oc(cs); |
| 1323 | struct channel *res = cs_ic(cs); |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1324 | struct htx *req_htx, *res_htx; |
| 1325 | struct htx_sl *sl; |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1326 | char *p, *key, *value; |
| 1327 | const char *end; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1328 | struct buffer *err; |
| 1329 | int default_scopes = PROMEX_FL_SCOPE_ALL; |
| 1330 | int len; |
| 1331 | |
| 1332 | /* Get the query-string */ |
| 1333 | req_htx = htxbuf(&req->buf); |
| 1334 | sl = http_get_stline(req_htx); |
| 1335 | if (!sl) |
| 1336 | goto error; |
| 1337 | p = http_find_param_list(HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl), '?'); |
| 1338 | if (!p) |
| 1339 | goto end; |
| 1340 | end = HTX_SL_REQ_UPTR(sl) + HTX_SL_REQ_ULEN(sl); |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1341 | |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1342 | /* copy the query-string */ |
| 1343 | len = end - p; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1344 | chunk_reset(&trash); |
| 1345 | memcpy(trash.area, p, len); |
| 1346 | trash.area[len] = 0; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1347 | p = trash.area; |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1348 | end = trash.area + len; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1349 | |
| 1350 | /* Parse the query-string */ |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1351 | while (p < end && *p && *p != '#') { |
| 1352 | value = NULL; |
| 1353 | |
| 1354 | /* decode parameter name */ |
| 1355 | key = p; |
| 1356 | while (p < end && *p != '=' && *p != '&' && *p != '#') |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1357 | ++p; |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1358 | /* found a value */ |
| 1359 | if (*p == '=') { |
| 1360 | *(p++) = 0; |
| 1361 | value = p; |
| 1362 | } |
| 1363 | else if (*p == '&') |
| 1364 | *(p++) = 0; |
| 1365 | else if (*p == '#') |
| 1366 | *p = 0; |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 1367 | len = url_decode(key, 1); |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1368 | if (len == -1) |
| 1369 | goto error; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1370 | |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1371 | /* decode value */ |
| 1372 | if (value) { |
| 1373 | while (p < end && *p != '=' && *p != '&' && *p != '#') |
| 1374 | ++p; |
| 1375 | if (*p == '=') |
| 1376 | goto error; |
| 1377 | if (*p == '&') |
| 1378 | *(p++) = 0; |
| 1379 | else if (*p == '#') |
| 1380 | *p = 0; |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 1381 | len = url_decode(value, 1); |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1382 | if (len == -1) |
| 1383 | goto error; |
| 1384 | } |
| 1385 | |
Tim Duesterhus | 8cb12a8 | 2021-01-02 22:31:55 +0100 | [diff] [blame] | 1386 | if (strcmp(key, "scope") == 0) { |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1387 | default_scopes = 0; /* at least a scope defined, unset default scopes */ |
| 1388 | if (!value) |
| 1389 | goto error; |
| 1390 | else if (*value == 0) |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1391 | appctx->ctx.stats.flags &= ~PROMEX_FL_SCOPE_ALL; |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1392 | else if (*value == '*') |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1393 | appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_ALL; |
Tim Duesterhus | 8cb12a8 | 2021-01-02 22:31:55 +0100 | [diff] [blame] | 1394 | else if (strcmp(value, "global") == 0) |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1395 | appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_GLOBAL; |
Tim Duesterhus | 8cb12a8 | 2021-01-02 22:31:55 +0100 | [diff] [blame] | 1396 | else if (strcmp(value, "server") == 0) |
William Dauchy | c65f656 | 2019-11-26 12:56:26 +0100 | [diff] [blame] | 1397 | appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_SERVER; |
Tim Duesterhus | 8cb12a8 | 2021-01-02 22:31:55 +0100 | [diff] [blame] | 1398 | else if (strcmp(value, "backend") == 0) |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1399 | appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_BACK; |
Tim Duesterhus | 8cb12a8 | 2021-01-02 22:31:55 +0100 | [diff] [blame] | 1400 | else if (strcmp(value, "frontend") == 0) |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1401 | appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_FRONT; |
William Dauchy | e3f7bd5 | 2021-02-14 23:22:56 +0100 | [diff] [blame] | 1402 | else if (strcmp(value, "listener") == 0) |
| 1403 | appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_LI; |
William Dauchy | 6916422 | 2021-02-07 20:42:38 +0100 | [diff] [blame] | 1404 | else if (strcmp(value, "sticktable") == 0) |
| 1405 | appctx->ctx.stats.flags |= PROMEX_FL_SCOPE_STICKTABLE; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1406 | else |
| 1407 | goto error; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1408 | } |
Tim Duesterhus | 8cb12a8 | 2021-01-02 22:31:55 +0100 | [diff] [blame] | 1409 | else if (strcmp(key, "no-maint") == 0) |
Christopher Faulet | eba2294 | 2019-11-19 14:18:24 +0100 | [diff] [blame] | 1410 | appctx->ctx.stats.flags |= PROMEX_FL_NO_MAINT_SRV; |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1411 | } |
| 1412 | |
| 1413 | end: |
| 1414 | appctx->ctx.stats.flags |= default_scopes; |
| 1415 | return 1; |
| 1416 | |
| 1417 | error: |
| 1418 | err = &http_err_chunks[HTTP_ERR_400]; |
| 1419 | channel_erase(res); |
| 1420 | res->buf.data = b_data(err); |
| 1421 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 1422 | res_htx = htx_from_buf(&res->buf); |
| 1423 | channel_add_input(res, res_htx->data); |
| 1424 | appctx->st0 = PROMEX_ST_END; |
| 1425 | return -1; |
| 1426 | } |
| 1427 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1428 | /* Send HTTP headers of the response. It returns 1 on success and 0 if <htx> is |
| 1429 | * full. */ |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1430 | static int promex_send_headers(struct appctx *appctx, struct conn_stream *cs, struct htx *htx) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1431 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1432 | struct channel *chn = cs_ic(cs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1433 | struct htx_sl *sl; |
| 1434 | unsigned int flags; |
| 1435 | |
| 1436 | 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); |
| 1437 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist("200"), ist("OK")); |
| 1438 | if (!sl) |
| 1439 | goto full; |
| 1440 | sl->info.res.status = 200; |
| 1441 | if (!htx_add_header(htx, ist("Cache-Control"), ist("no-cache")) || |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1442 | !htx_add_header(htx, ist("Content-Type"), ist("text/plain; version=0.0.4")) || |
| 1443 | !htx_add_header(htx, ist("Transfer-Encoding"), ist("chunked")) || |
| 1444 | !htx_add_endof(htx, HTX_BLK_EOH)) |
| 1445 | goto full; |
| 1446 | |
Christopher Faulet | 9744f7c | 2019-03-27 15:48:53 +0100 | [diff] [blame] | 1447 | channel_add_input(chn, htx->data); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1448 | return 1; |
| 1449 | full: |
| 1450 | htx_reset(htx); |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame^] | 1451 | cs_rx_room_blk(cs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1452 | return 0; |
| 1453 | } |
| 1454 | |
| 1455 | /* The function returns 1 if the initialisation is complete, 0 if |
| 1456 | * an errors occurs and -1 if more data are required for initializing |
| 1457 | * the applet. |
| 1458 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 1459 | static int promex_appctx_init(struct appctx *appctx) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1460 | { |
| 1461 | appctx->st0 = PROMEX_ST_INIT; |
| 1462 | return 1; |
| 1463 | } |
| 1464 | |
| 1465 | /* The main I/O handler for the promex applet. */ |
| 1466 | static void promex_appctx_handle_io(struct appctx *appctx) |
| 1467 | { |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1468 | struct conn_stream *cs = appctx->owner; |
| 1469 | struct stream *s = __cs_strm(cs); |
| 1470 | struct channel *req = cs_oc(cs); |
| 1471 | struct channel *res = cs_ic(cs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1472 | struct htx *req_htx, *res_htx; |
| 1473 | int ret; |
| 1474 | |
| 1475 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | 62e7574 | 2022-03-31 09:16:34 +0200 | [diff] [blame] | 1476 | if (unlikely(cs->state == CS_ST_DIS || cs->state == CS_ST_CLO)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1477 | goto out; |
| 1478 | |
Ilya Shipitsin | ce7b00f | 2020-03-23 22:28:40 +0500 | [diff] [blame] | 1479 | /* Check if the input buffer is available. */ |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1480 | if (!b_size(&res->buf)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame^] | 1481 | cs_rx_room_blk(cs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1482 | goto out; |
| 1483 | } |
| 1484 | |
| 1485 | switch (appctx->st0) { |
| 1486 | case PROMEX_ST_INIT: |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1487 | ret = promex_parse_uri(appctx, cs); |
Christopher Faulet | 78407ce | 2019-11-18 14:47:08 +0100 | [diff] [blame] | 1488 | if (ret <= 0) { |
| 1489 | if (ret == -1) |
| 1490 | goto error; |
| 1491 | goto out; |
| 1492 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1493 | appctx->st0 = PROMEX_ST_HEAD; |
| 1494 | appctx->st1 = PROMEX_DUMPER_INIT; |
| 1495 | /* fall through */ |
| 1496 | |
| 1497 | case PROMEX_ST_HEAD: |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1498 | if (!promex_send_headers(appctx, cs, res_htx)) |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1499 | goto out; |
| 1500 | appctx->st0 = ((s->txn->meth == HTTP_METH_HEAD) ? PROMEX_ST_DONE : PROMEX_ST_DUMP); |
| 1501 | /* fall through */ |
| 1502 | |
| 1503 | case PROMEX_ST_DUMP: |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1504 | ret = promex_dump_metrics(appctx, cs, res_htx); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1505 | if (ret <= 0) { |
| 1506 | if (ret == -1) |
| 1507 | goto error; |
| 1508 | goto out; |
| 1509 | } |
| 1510 | appctx->st0 = PROMEX_ST_DONE; |
| 1511 | /* fall through */ |
| 1512 | |
| 1513 | case PROMEX_ST_DONE: |
Christopher Faulet | be69cbd | 2022-04-07 10:19:46 +0200 | [diff] [blame] | 1514 | /* no more data are expected. If the response buffer is |
| 1515 | * empty, be sure to add something (EOT block in this |
| 1516 | * case) to have something to send. It is important to |
| 1517 | * be sure the EOM flags will be handled by the |
| 1518 | * endpoint. |
| 1519 | */ |
| 1520 | if (htx_is_empty(res_htx)) { |
| 1521 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Christopher Faulet | a0bdec3 | 2022-04-04 07:51:21 +0200 | [diff] [blame^] | 1522 | cs_rx_room_blk(cs); |
Christopher Faulet | be69cbd | 2022-04-07 10:19:46 +0200 | [diff] [blame] | 1523 | goto out; |
| 1524 | } |
| 1525 | channel_add_input(res, 1); |
| 1526 | } |
| 1527 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 908628c | 2022-03-25 16:43:49 +0100 | [diff] [blame] | 1528 | cs->endp->flags |= CS_EP_EOI; |
Christopher Faulet | bef64b2 | 2022-03-07 15:56:20 +0100 | [diff] [blame] | 1529 | res->flags |= CF_EOI; |
Christopher Faulet | 9744f7c | 2019-03-27 15:48:53 +0100 | [diff] [blame] | 1530 | appctx->st0 = PROMEX_ST_END; |
| 1531 | /* fall through */ |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1532 | |
Christopher Faulet | 9744f7c | 2019-03-27 15:48:53 +0100 | [diff] [blame] | 1533 | case PROMEX_ST_END: |
| 1534 | if (!(res->flags & CF_SHUTR)) { |
| 1535 | res->flags |= CF_READ_NULL; |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 1536 | cs_shutr(cs); |
Christopher Faulet | 9744f7c | 2019-03-27 15:48:53 +0100 | [diff] [blame] | 1537 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1538 | } |
| 1539 | |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1540 | out: |
| 1541 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9744f7c | 2019-03-27 15:48:53 +0100 | [diff] [blame] | 1542 | |
| 1543 | /* eat the whole request */ |
| 1544 | if (co_data(req)) { |
| 1545 | req_htx = htx_from_buf(&req->buf); |
| 1546 | co_htx_skip(req, req_htx, co_data(req)); |
| 1547 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1548 | return; |
| 1549 | |
| 1550 | error: |
| 1551 | res->flags |= CF_READ_NULL; |
Christopher Faulet | da098e6 | 2022-03-31 17:44:45 +0200 | [diff] [blame] | 1552 | cs_shutr(cs); |
| 1553 | cs_shutw(cs); |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1554 | } |
| 1555 | |
| 1556 | struct applet promex_applet = { |
| 1557 | .obj_type = OBJ_TYPE_APPLET, |
| 1558 | .name = "<PROMEX>", /* used for logging */ |
| 1559 | .init = promex_appctx_init, |
| 1560 | .fct = promex_appctx_handle_io, |
| 1561 | }; |
| 1562 | |
| 1563 | static enum act_parse_ret service_parse_prometheus_exporter(const char **args, int *cur_arg, struct proxy *px, |
| 1564 | struct act_rule *rule, char **err) |
| 1565 | { |
| 1566 | /* Prometheus exporter service is only available on "http-request" rulesets */ |
| 1567 | if (rule->from != ACT_F_HTTP_REQ) { |
| 1568 | memprintf(err, "Prometheus exporter service only available on 'http-request' rulesets"); |
| 1569 | return ACT_RET_PRS_ERR; |
| 1570 | } |
Christopher Faulet | f959d08 | 2019-02-07 15:38:42 +0100 | [diff] [blame] | 1571 | |
| 1572 | /* Add applet pointer in the rule. */ |
| 1573 | rule->applet = promex_applet; |
| 1574 | |
| 1575 | return ACT_RET_PRS_OK; |
| 1576 | } |
| 1577 | static void promex_register_build_options(void) |
| 1578 | { |
| 1579 | char *ptr = NULL; |
| 1580 | |
| 1581 | memprintf(&ptr, "Built with the Prometheus exporter as a service"); |
| 1582 | hap_register_build_opts(ptr, 1); |
| 1583 | } |
| 1584 | |
| 1585 | |
| 1586 | static struct action_kw_list service_actions = { ILH, { |
| 1587 | { "prometheus-exporter", service_parse_prometheus_exporter }, |
| 1588 | { /* END */ } |
| 1589 | }}; |
| 1590 | |
| 1591 | INITCALL1(STG_REGISTER, service_keywords_register, &service_actions); |
| 1592 | INITCALL0(STG_REGISTER, promex_register_build_options); |