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