summaryrefslogtreecommitdiffstats
path: root/src/plugins/prom/prom.c
blob: 0b03a7adb0b473941a261b4ff9857ab47a596667 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*
 * Copyright (c) 2022 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include <vnet/plugin/plugin.h>
#include <vpp/app/version.h>

#include <prom/prom.h>
#include <vpp-api/client/stat_client.h>
#include <vlib/stats/stats.h>
#include <ctype.h>

static prom_main_t prom_main;

static u8 *
make_stat_name (char *name)
{
  prom_main_t *pm = &prom_main;
  char *p = name;

  while (*p)
    {
      if (!isalnum (*p))
	*p = '_';
      p++;
    }

  /* Reuse vector, instead of always allocating, when building a name. */
  vec_reset_length (pm->name_scratch_pad);
  pm->name_scratch_pad =
    format (pm->name_scratch_pad, "%v%s", pm->stat_name_prefix, name);
  return pm->name_scratch_pad;
}

static u8 *
dump_counter_vector_simple (stat_segment_data_t *res, u8 *s, u8 used_only)
{
  u8 need_header = 1;
  int j, k;
  u8 *name;

  name = make_stat_name (res->name);

  for (k = 0; k < vec_len (res->simple_counter_vec); k++)
    for (j = 0; j < vec_len (res->simple_counter_vec[k]); j++)
      {
	if (used_only && !res->simple_counter_vec[k][j])
	  continue;
	if (need_header)
	  {
	    s = format (s, "# TYPE %v counter\n", name);
	    need_header = 0;
	  }
	s = format (s, "%v{thread=\"%d\",interface=\"%d\"} %lld\n", name, k, j,
		    res->simple_counter_vec[k][j]);
      }

  return s;
}

static u8 *
dump_counter_vector_combined (stat_segment_data_t *res, u8 *s, u8 used_only)
{
  u8 need_header = 1;
  int j, k;
  u8 *name;

  name = make_stat_name (res->name);

  for (k = 0; k < vec_len (res->simple_counter_vec); k++)
    for (j = 0; j < vec_len (res->combined_counter_vec[k]); j++)
      {
	if (used_only && !res->combined_counter_vec[k][j].packets)
	  continue;
	if (need_header)
	  {
	    s = format (s, "# TYPE %v_packets counter\n", name);
	    s = format (s, "# TYPE %v_bytes counter\n", name);
	    need_header = 0;
	  }
	s = format (s, "%v_packets{thread=\"%d\",interface=\"%d\"} %lld\n",
		    name, k, j, res->combined_counter_vec[k][j].packets);
	s = format (s, "%v_bytes{thread=\"%d\",interface=\"%d\"} %lld\n", name,
		    k, j, res->combined_counter_vec[k][j].bytes);
      }

  return s;
}

static u8 *
dump_error_index (stat_segment_data_t *res, u8 *s, u8 used_only)
{
  u8 *name;
  int j;

  name = make_stat_name (res->name);

  for (j = 0; j < vec_len (res->error_vector); j++)
    {
      if (used_only && !res->error_vector[j])
	continue;
      s = format (s, "# TYPE %v counter\n", name);
      s =
	format (s, "%v{thread=\"%d\"} %lld\n", name, j, res->error_vector[j]);
    }

  return s;
}

static u8 *
dump_scalar_index (stat_segment_data_t *res, u8 *s, u8 used_only)
{
  u8 *name;

  if (used_only && !res->scalar_value)
    return s;

  name = make_stat_name (res->name);

  s = format (s, "# TYPE %v counter\n", name);
  s = format (s, "%v %.2f\n", name, res->scalar_value);

  return s;
}

static u8 *
dump_name_vector (stat_segment_data_t *res, u8 *s, u8 used_only)
{
  u8 *name;
  int k;

  name = make_stat_name (res->name);

  s = format (s, "# TYPE %v_info gauge\n", name);
  for (k = 0; k < vec_len (res->name_vector); k++)
    s = format (s, "%v_info{index=\"%d\",name=\"%s\"} 1\n", name, k,
		res->name_vector[k]);

  return s;
}

static u8 *
scrape_stats_segment (u8 *s, u8 **patterns, u8 used_only)
{
  stat_segment_data_t *res;
  static u32 *stats = 0;
  int i;

  stats = stat_segment_ls (patterns);

retry:
  res = stat_segment_dump (stats);
  if (res == 0)
    { /* Memory layout has changed */
      if (stats)
	vec_free (stats);
      stats = stat_segment_ls (patterns);
      goto retry;
    }

  for (i = 0; i < vec_len (res); i++)
    {
      switch (res[i].type)
	{
	case STAT_DIR_TYPE_COUNTER_VECTOR_SIMPLE:
	  s = dump_counter_vector_simple (&res[i], s, used_only);
	  break;

	case STAT_DIR_TYPE_COUNTER_VECTOR_COMBINED:
	  s = dump_counter_vector_combined (&res[i], s, used_only);
	  break;
	case STAT_DIR_TYPE_ERROR_INDEX:
	  s = dump_error_index (&res[i], s, used_only);
	  break;

	case STAT_DIR_TYPE_SCALAR_INDEX:
	  s = dump_scalar_index (&res[i], s, used_only);
	  break;

	case STAT_DIR_TYPE_NAME_VECTOR:
	  s = dump_name_vector (&res[i], s, used_only);
	  break;

	case STAT_DIR_TYPE_EMPTY:
	  break;

	default:
	  clib_warning ("Unknown value %d\n", res[i].type);
	  ;
	}
    }
  stat_segment_data_free (res);

  return s;
}

static void
send_data_to_hss (hss_session_handle_t sh)
{
  hss_url_handler_args_t args = {};
  prom_main_t *pm = &prom_main;

  args.sh = sh;
  args.data = vec_dup (pm->stats);
  args.data_len = vec_len (pm->stats);
  args.sc = HTTP_STATUS_OK;
  args.free_vec_data = 1;

  pm->send_data (&args);
}

static void
send_data_to_hss_rpc (void *rpc_args)
{
  send_data_to_hss (*(hss_session_handle_t *) rpc_args);
}

static uword
prom_scraper_process (vlib_main_t *vm, vlib_node_runtime_t *rt,
		      vlib_frame_t *f)
{
  uword *event_data = 0, event_type;
  prom_main_t *pm = &prom_main;
  hss_session_handle_t sh;
  f64 timeout = 10000.0;

  while (1)
    {
      vlib_process_wait_for_event_or_clock (vm, timeout);
      event_type = vlib_process_get_events (vm, (uword **) &event_data);
      switch (event_type)
	{
	case ~0:
	  /* timeout, do nothing */
	  break;
	case PROM_SCRAPER_EVT_RUN:
	  sh.as_u64 = event_data[0];
	  vec_reset_length (pm->stats);
	  pm->stats = scrape_stats_segment (pm->stats, pm->stats_patterns,
					    pm->used_only);
	  session_send_rpc_evt_to_thread_force (sh.thread_index,
						send_data_to_hss_rpc, &sh);
	  pm->last_scrape = vlib_time_now (vm);
	  break;
	default:
	  clib_warning ("unexpected event %u", event_type);
	  break;
	}

      vec_reset_length (event_data);
    }
  return 0;
}

VLIB_REGISTER_NODE (prom_scraper_process_node) = {
  .function = prom_scraper_process,
  .type = VLIB_NODE_TYPE_PROCESS,
  .name = "prom-scraper-process",
  .state = VLIB_NODE_STATE_DISABLED,
};

static void
prom_scraper_process_enable (vlib_main_t *vm)
{
  prom_main_t *pm = &prom_main;
  vlib_node_t *n;

  vlib_node_set_state (vm, prom_scraper_process_node.index,
		       VLIB_NODE_STATE_POLLING);
  n = vlib_get_node (vm, prom_scraper_process_node.index);
  vlib_start_process (vm, n->runtime_index);

  pm->scraper_node_index = n->index;
}

static void
signal_run_to_scraper (uword *args)
{
  prom_main_t *pm = &prom_main;
  ASSERT (vlib_get_thread_index () == 0);
  vlib_process_signal_event (pm->vm, pm->scraper_node_index,
			     PROM_SCRAPER_EVT_RUN, *args);
}

hss_url_handler_rc_t
prom_stats_dump (hss_url_handler_args_t *args)
{
  vlib_main_t *vm = vlib_get_main ();
  f64 now = vlib_time_now (vm);
  prom_main_t *pm = &prom_main;

  /* If we've recently scraped stats, return data */
  if ((now - pm->last_scrape) < pm->min_scrape_interval)
    {
      send_data_to_hss (args->sh);
      return HSS_URL_HANDLER_ASYNC;
    }

  if (vm->thread_index != 0)
    vl_api_rpc_call_main_thread (signal_run_to_scraper, (u8 *) &args->sh,
				 sizeof (args->sh));
  else
    signal_run_to_scraper (&args->sh.as_u64);

  return HSS_URL_HANDLER_ASYNC;
}

void
prom_stat_patterns_add (u8 **patterns)
{
  prom_main_t *pm = &prom_main;

  u8 **pattern, **existing;
  u8 found;
  u32 len;

  vec_foreach (pattern, patterns)
    {
      found = 0;
      len = vec_len (*pattern);
      vec_foreach (existing, pm->stats_patterns)
	{
	  if (vec_len (*existing) != len)
	    continue;
	  if (!memcmp (*existing, *pattern, len - 1))
	    {
	      found = 1;
	      break;
	    }
	}
      if (!found)
	vec_add1 (pm->stats_patterns, *pattern);
    }
}

void
prom_stat_patterns_free (void)
{
  prom_main_t *pm = &prom_main;
  u8 **pattern;

  vec_foreach (pattern, pm->stats_patterns)
    vec_free (*pattern);
  vec_free (pm->stats_patterns);
}

void
prom_stat_patterns_set (u8 **patterns)
{
  prom_stat_patterns_free ();
  prom_stat_patterns_add (patterns);
}

u8 **
prom_stat_patterns_get (void)
{
  return prom_main.stats_patterns;
}

void
prom_stat_name_prefix_set (u8 *prefix)
{
  prom_main_t *pm = &prom_main;

  vec_free (pm->stat_name_prefix);
  pm->stat_name_prefix = prefix;
}

void
prom_report_used_only (u8 used_only)
{
  prom_main_t *pm = &prom_main;

  pm->used_only = used_only;
}

static void
prom_stat_segment_client_init (void)
{
  stat_client_main_t *scm = &stat_client_main;
  vlib_stats_segment_t *sm = vlib_stats_get_segment ();
  uword size;

  size = sm->memory_size ? sm->memory_size : STAT_SEGMENT_DEFAULT_SIZE;
  scm->memory_size = size;
  scm->shared_header = sm->shared_header;
  scm->directory_vector =
    stat_segment_adjust (scm, (void *) scm->shared_header->directory_vector);
}

void
prom_enable (vlib_main_t *vm)
{
  prom_main_t *pm = &prom_main;

  pm->register_url = vlib_get_plugin_symbol ("http_static_plugin.so",
					     "hss_register_url_handler");
  pm->send_data =
    vlib_get_plugin_symbol ("http_static_plugin.so", "hss_session_send_data");
  pm->register_url (prom_stats_dump, "stats.prom", HTTP_REQ_GET);

  pm->is_enabled = 1;
  pm->vm = vm;
  if (!pm->stat_name_prefix)
    pm->stat_name_prefix = format (0, "vpp");

  prom_scraper_process_enable (vm);
  prom_stat_segment_client_init ();
}

static clib_error_t *
prom_init (vlib_main_t *vm)
{
  prom_main_t *pm = &prom_main;

  pm->is_enabled = 0;
  pm->min_scrape_interval = 1;
  pm->used_only = 0;
  pm->stat_name_prefix = 0;

  return 0;
}

prom_main_t *
prom_get_main (void)
{
  return &prom_main;
}

VLIB_INIT_FUNCTION (prom_init) = {
  .runs_after = VLIB_INITS ("hss_main_init"),
};

VLIB_PLUGIN_REGISTER () = {
  .version = VPP_BUILD_VER,
  .description = "Prometheus Stats Exporter",
  .default_disabled = 0,
};

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */