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