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