2 * Copyright (c) 2022 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
15 #include <vnet/plugin/plugin.h>
16 #include <vpp/app/version.h>
18 #include <prom/prom.h>
19 #include <vpp-api/client/stat_client.h>
20 #include <vlib/stats/stats.h>
23 static prom_main_t prom_main;
26 make_stat_name (char *name)
28 prom_main_t *pm = &prom_main;
38 /* Reuse vector, instead of always allocating, when building a name. */
39 vec_reset_length (pm->name_scratch_pad);
40 pm->name_scratch_pad =
41 format (pm->name_scratch_pad, "%v%s", pm->stat_name_prefix, name);
42 return pm->name_scratch_pad;
46 dump_counter_vector_simple (stat_segment_data_t *res, u8 *s, u8 used_only)
52 name = make_stat_name (res->name);
54 for (k = 0; k < vec_len (res->simple_counter_vec); k++)
55 for (j = 0; j < vec_len (res->simple_counter_vec[k]); j++)
57 if (used_only && !res->simple_counter_vec[k][j])
61 s = format (s, "# TYPE %v counter\n", name);
64 s = format (s, "%v{thread=\"%d\",interface=\"%d\"} %lld\n", name, k, j,
65 res->simple_counter_vec[k][j]);
72 dump_counter_vector_combined (stat_segment_data_t *res, u8 *s, u8 used_only)
78 name = make_stat_name (res->name);
80 for (k = 0; k < vec_len (res->simple_counter_vec); k++)
81 for (j = 0; j < vec_len (res->combined_counter_vec[k]); j++)
83 if (used_only && !res->combined_counter_vec[k][j].packets)
87 s = format (s, "# TYPE %v_packets counter\n", name);
88 s = format (s, "# TYPE %v_bytes counter\n", name);
91 s = format (s, "%v_packets{thread=\"%d\",interface=\"%d\"} %lld\n",
92 name, k, j, res->combined_counter_vec[k][j].packets);
93 s = format (s, "%v_bytes{thread=\"%d\",interface=\"%d\"} %lld\n", name,
94 k, j, res->combined_counter_vec[k][j].bytes);
101 dump_scalar_index (stat_segment_data_t *res, u8 *s, u8 used_only)
105 if (used_only && !res->scalar_value)
108 name = make_stat_name (res->name);
110 s = format (s, "# TYPE %v counter\n", name);
111 s = format (s, "%v %.2f\n", name, res->scalar_value);
117 dump_name_vector (stat_segment_data_t *res, u8 *s, u8 used_only)
122 name = make_stat_name (res->name);
124 s = format (s, "# TYPE %v_info gauge\n", name);
125 for (k = 0; k < vec_len (res->name_vector); k++)
126 s = format (s, "%v_info{index=\"%d\",name=\"%s\"} 1\n", name, k,
127 res->name_vector[k]);
133 scrape_stats_segment (u8 *s, u8 **patterns, u8 used_only)
135 stat_segment_data_t *res;
136 static u32 *stats = 0;
139 stats = stat_segment_ls (patterns);
142 res = stat_segment_dump (stats);
144 { /* Memory layout has changed */
147 stats = stat_segment_ls (patterns);
151 for (i = 0; i < vec_len (res); i++)
155 case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
156 s = dump_counter_vector_simple (&res[i], s, used_only);
159 case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
160 s = dump_counter_vector_combined (&res[i], s, used_only);
163 case STAT_DIR_TYPE_SCALAR_INDEX:
164 s = dump_scalar_index (&res[i], s, used_only);
167 case STAT_DIR_TYPE_NAME_VECTOR:
168 s = dump_name_vector (&res[i], s, used_only);
171 case STAT_DIR_TYPE_EMPTY:
175 clib_warning ("Unknown value %d\n", res[i].type);
179 stat_segment_data_free (res);
185 send_data_to_hss (hss_session_handle_t sh)
187 hss_url_handler_args_t args = {};
188 prom_main_t *pm = &prom_main;
191 args.data = vec_dup (pm->stats);
192 args.data_len = vec_len (pm->stats);
193 args.sc = HTTP_STATUS_OK;
194 args.free_vec_data = 1;
196 pm->send_data (&args);
200 send_data_to_hss_rpc (void *rpc_args)
202 send_data_to_hss (*(hss_session_handle_t *) rpc_args);
206 prom_scraper_process (vlib_main_t *vm, vlib_node_runtime_t *rt,
209 uword *event_data = 0, event_type;
210 prom_main_t *pm = &prom_main;
211 hss_session_handle_t sh;
212 f64 timeout = 10000.0;
216 vlib_process_wait_for_event_or_clock (vm, timeout);
217 event_type = vlib_process_get_events (vm, (uword **) &event_data);
221 /* timeout, do nothing */
223 case PROM_SCRAPER_EVT_RUN:
224 sh.as_u64 = event_data[0];
225 vec_reset_length (pm->stats);
226 pm->stats = scrape_stats_segment (pm->stats, pm->stats_patterns,
228 session_send_rpc_evt_to_thread_force (sh.thread_index,
229 send_data_to_hss_rpc, &sh);
230 pm->last_scrape = vlib_time_now (vm);
233 clib_warning ("unexpected event %u", event_type);
237 vec_reset_length (event_data);
242 VLIB_REGISTER_NODE (prom_scraper_process_node) = {
243 .function = prom_scraper_process,
244 .type = VLIB_NODE_TYPE_PROCESS,
245 .name = "prom-scraper-process",
246 .state = VLIB_NODE_STATE_DISABLED,
250 prom_scraper_process_enable (vlib_main_t *vm)
252 prom_main_t *pm = &prom_main;
255 vlib_node_set_state (vm, prom_scraper_process_node.index,
256 VLIB_NODE_STATE_POLLING);
257 n = vlib_get_node (vm, prom_scraper_process_node.index);
258 vlib_start_process (vm, n->runtime_index);
260 pm->scraper_node_index = n->index;
264 signal_run_to_scraper (uword *args)
266 prom_main_t *pm = &prom_main;
267 ASSERT (vlib_get_thread_index () == 0);
268 vlib_process_signal_event (pm->vm, pm->scraper_node_index,
269 PROM_SCRAPER_EVT_RUN, *args);
273 prom_stats_dump (hss_url_handler_args_t *args)
275 vlib_main_t *vm = vlib_get_main ();
276 f64 now = vlib_time_now (vm);
277 prom_main_t *pm = &prom_main;
279 /* If we've recently scraped stats, return data */
280 if ((now - pm->last_scrape) < pm->min_scrape_interval)
282 send_data_to_hss (args->sh);
283 return HSS_URL_HANDLER_ASYNC;
286 if (vm->thread_index != 0)
287 vl_api_rpc_call_main_thread (signal_run_to_scraper, (u8 *) &args->sh,
290 signal_run_to_scraper (&args->sh.as_u64);
292 return HSS_URL_HANDLER_ASYNC;
296 prom_stat_patterns_add (u8 **patterns)
298 prom_main_t *pm = &prom_main;
300 u8 **pattern, **existing;
304 vec_foreach (pattern, patterns)
307 len = vec_len (*pattern);
308 vec_foreach (existing, pm->stats_patterns)
310 if (vec_len (*existing) != len)
312 if (!memcmp (*existing, *pattern, len - 1))
319 vec_add1 (pm->stats_patterns, *pattern);
324 prom_stat_patterns_free (void)
326 prom_main_t *pm = &prom_main;
329 vec_foreach (pattern, pm->stats_patterns)
331 vec_free (pm->stats_patterns);
335 prom_stat_patterns_set (u8 **patterns)
337 prom_stat_patterns_free ();
338 prom_stat_patterns_add (patterns);
342 prom_stat_patterns_get (void)
344 return prom_main.stats_patterns;
348 prom_stat_name_prefix_set (u8 *prefix)
350 prom_main_t *pm = &prom_main;
352 vec_free (pm->stat_name_prefix);
353 pm->stat_name_prefix = prefix;
357 prom_report_used_only (u8 used_only)
359 prom_main_t *pm = &prom_main;
361 pm->used_only = used_only;
365 prom_stat_segment_client_init (void)
367 stat_client_main_t *scm = &stat_client_main;
368 vlib_stats_segment_t *sm = vlib_stats_get_segment ();
371 size = sm->memory_size ? sm->memory_size : STAT_SEGMENT_DEFAULT_SIZE;
372 scm->memory_size = size;
373 scm->shared_header = sm->shared_header;
374 scm->directory_vector =
375 stat_segment_adjust (scm, (void *) scm->shared_header->directory_vector);
379 prom_enable (vlib_main_t *vm)
381 prom_main_t *pm = &prom_main;
383 pm->register_url = vlib_get_plugin_symbol ("http_static_plugin.so",
384 "hss_register_url_handler");
386 vlib_get_plugin_symbol ("http_static_plugin.so", "hss_session_send_data");
387 pm->register_url (prom_stats_dump, "stats.prom", HTTP_REQ_GET);
391 if (!pm->stat_name_prefix)
392 pm->stat_name_prefix = format (0, "vpp");
394 prom_scraper_process_enable (vm);
395 prom_stat_segment_client_init ();
398 static clib_error_t *
399 prom_init (vlib_main_t *vm)
401 prom_main_t *pm = &prom_main;
404 pm->min_scrape_interval = 1;
406 pm->stat_name_prefix = 0;
417 VLIB_INIT_FUNCTION (prom_init) = {
418 .runs_after = VLIB_INITS ("hss_main_init"),
421 VLIB_PLUGIN_REGISTER () = {
422 .version = VPP_BUILD_VER,
423 .description = "Prometheus Stats Exporter",
424 .default_disabled = 0,
428 * fd.io coding-style-patch-verification: ON
431 * eval: (c-set-style "gnu")