Skip to content

Commit a2ba37a

Browse files
cosmo0920edsiper
authored andcommitted
encoding: splunk_hec: Support histogram and summary metrics
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 3f2a5ce commit a2ba37a

File tree

2 files changed

+457
-6
lines changed

2 files changed

+457
-6
lines changed

src/cmt_encode_splunk_hec.c

Lines changed: 256 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,10 @@ static cfl_sds_t double_to_string(double val)
4949
return str;
5050
}
5151

52-
static cfl_sds_t format_metric_name(cfl_sds_t *buf, struct cmt_map *map, const char *suffix)
52+
static void format_metric_name(cfl_sds_t *buf, struct cmt_map *map, const char *suffix)
5353
{
5454
int mlen = 0;
55+
int slen = 0;
5556
cfl_sds_t metric_name = NULL;
5657
struct cmt_opts *opts;
5758

@@ -74,13 +75,13 @@ static cfl_sds_t format_metric_name(cfl_sds_t *buf, struct cmt_map *map, const c
7475
cfl_sds_cat_safe(&metric_name, opts->name, cfl_sds_len(opts->name));
7576
}
7677
if (suffix != NULL) {
77-
cfl_sds_cat_safe(&metric_name, suffix, strlen(suffix));
78+
slen = strlen(suffix);
79+
mlen += slen;
80+
cfl_sds_cat_safe(&metric_name, suffix, slen);
7881
}
7982
cfl_sds_cat_safe(&metric_name, "\":", 2);
8083
cfl_sds_cat_safe(buf, metric_name, mlen);
8184
cfl_sds_destroy(metric_name);
82-
83-
return metric_name;
8485
}
8586

8687
static void append_metric_value(cfl_sds_t *buf, struct cmt_map *map,
@@ -249,6 +250,255 @@ static void format_metric_labels(struct cmt_splunk_hec_context *context, cfl_sds
249250
}
250251
}
251252

253+
static void append_bucket_metric(cfl_sds_t *buf, struct cmt_map *map,
254+
struct cmt_metric *metric, int index)
255+
{
256+
int len = 0;
257+
double val;
258+
char tmp[128];
259+
cfl_sds_t metric_val;
260+
261+
/* metric name for bucket */
262+
format_metric_name(buf, map, "_bucket");
263+
264+
/* Retrieve metric value */
265+
val = cmt_metric_hist_get_value(metric, index);
266+
metric_val = double_to_string(val);
267+
268+
len = snprintf(tmp, sizeof(tmp) - 1, "%s", metric_val);
269+
cfl_sds_cat_safe(buf, tmp, len);
270+
cfl_sds_destroy(metric_val);
271+
}
272+
273+
static void format_histogram_bucket(struct cmt_splunk_hec_context *context, cfl_sds_t *buf, struct cmt_map *map,
274+
struct cmt_metric *metric)
275+
{
276+
int index;
277+
int len = 0;
278+
char tmp[128];
279+
cfl_sds_t val;
280+
uint64_t metric_val;
281+
struct cmt_histogram *histogram;
282+
struct cmt_histogram_buckets *buckets;
283+
cfl_sds_t metric_str;
284+
285+
histogram = (struct cmt_histogram *) map->parent;
286+
buckets = histogram->buckets;
287+
288+
for (index = 0; index <= buckets->count; index++) {
289+
/* Common fields */
290+
format_context_common(context, buf, map, metric);
291+
292+
/* Other fields */
293+
cfl_sds_cat_safe(buf, "\"fields\":{", 10);
294+
295+
/* bucket metric */
296+
append_bucket_metric(buf, map, metric, index);
297+
298+
/* upper bound */
299+
cfl_sds_cat_safe(buf, ",\"le\":", 6);
300+
301+
if (index < buckets->count) {
302+
cfl_sds_cat_safe(buf, "\"", 1);
303+
val = double_to_string(buckets->upper_bounds[index]);
304+
cfl_sds_cat_safe(buf, val, cfl_sds_len(val));
305+
cfl_sds_destroy(val);
306+
cfl_sds_cat_safe(buf, "\"", 1);
307+
}
308+
else {
309+
cfl_sds_cat_safe(buf, "\"+Inf\"", 6);
310+
}
311+
312+
/* Format labels */
313+
format_metric_labels(context, buf, map, metric);
314+
315+
/* Close parenthesis for fields */
316+
cfl_sds_cat_safe(buf, "}", 1);
317+
318+
/* Close parenthesis */
319+
cfl_sds_cat_safe(buf, "}", 1);
320+
}
321+
322+
/* Format histogram sum */
323+
{
324+
/* Common fields */
325+
format_context_common(context, buf, map, metric);
326+
327+
/* Other fields */
328+
cfl_sds_cat_safe(buf, "\"fields\":{", 10);
329+
330+
/* metric name for bucket */
331+
format_metric_name(buf, map, "_sum");
332+
333+
/* Retrieve metric value */
334+
metric_val = cmt_metric_hist_get_sum_value(metric);
335+
metric_str = double_to_string(metric_val);
336+
337+
len = snprintf(tmp, sizeof(tmp) - 1, "%s", metric_str);
338+
cfl_sds_cat_safe(buf, tmp, len);
339+
cfl_sds_destroy(metric_str);
340+
341+
/* Format labels */
342+
format_metric_labels(context, buf, map, metric);
343+
344+
/* Close parenthesis for fields */
345+
cfl_sds_cat_safe(buf, "}", 1);
346+
347+
/* Close parenthesis */
348+
cfl_sds_cat_safe(buf, "}", 1);
349+
}
350+
351+
/* Format histogram sum */
352+
{
353+
/* Common fields */
354+
format_context_common(context, buf, map, metric);
355+
356+
/* Other fields */
357+
cfl_sds_cat_safe(buf, "\"fields\":{", 10);
358+
359+
/* metric name for bucket */
360+
format_metric_name(buf, map, "_count");
361+
362+
/* Retrieve metric value */
363+
metric_val = cmt_metric_hist_get_count_value(metric);
364+
metric_str = double_to_string(metric_val);
365+
366+
len = snprintf(tmp, sizeof(tmp) - 1, "%s", metric_str);
367+
cfl_sds_cat_safe(buf, tmp, len);
368+
cfl_sds_destroy(metric_str);
369+
370+
/* Format labels */
371+
format_metric_labels(context, buf, map, metric);
372+
373+
/* Close parenthesis for fields */
374+
cfl_sds_cat_safe(buf, "}", 1);
375+
376+
/* Close parenthesis */
377+
cfl_sds_cat_safe(buf, "}", 1);
378+
}
379+
}
380+
381+
static void append_quantiles_metric(cfl_sds_t *buf, struct cmt_map *map,
382+
struct cmt_metric *metric, int index)
383+
{
384+
int len = 0;
385+
double val;
386+
char tmp[128];
387+
cfl_sds_t metric_val;
388+
389+
/* metric name for bucket */
390+
format_metric_name(buf, map, NULL);
391+
392+
/* Retrieve metric value */
393+
val = cmt_summary_quantile_get_value(metric, index);
394+
metric_val = double_to_string(val);
395+
396+
len = snprintf(tmp, sizeof(tmp) - 1, "%s", metric_val);
397+
cfl_sds_cat_safe(buf, tmp, len);
398+
cfl_sds_destroy(metric_val);
399+
}
400+
401+
static void format_summary_metric(struct cmt_splunk_hec_context *context, cfl_sds_t *buf, struct cmt_map *map,
402+
struct cmt_metric *metric)
403+
{
404+
int index;
405+
int len = 0;
406+
char tmp[128];
407+
cfl_sds_t val;
408+
uint64_t metric_val;
409+
struct cmt_summary *summary;
410+
cfl_sds_t metric_str;
411+
412+
summary = (struct cmt_summary *) map->parent;
413+
414+
if (metric->sum_quantiles_set) {
415+
for (index = 0; index < summary->quantiles_count; index++) {
416+
/* Common fields */
417+
format_context_common(context, buf, map, metric);
418+
419+
/* Other fields */
420+
cfl_sds_cat_safe(buf, "\"fields\":{", 10);
421+
422+
/* bucket metric */
423+
append_quantiles_metric(buf, map, metric, index);
424+
425+
/* quantiles */
426+
cfl_sds_cat_safe(buf, ",\"qt\":\"", 7);
427+
val = double_to_string(summary->quantiles[index]);
428+
cfl_sds_cat_safe(buf, val, cfl_sds_len(val));
429+
cfl_sds_destroy(val);
430+
cfl_sds_cat_safe(buf, "\"", 1);
431+
432+
/* Format labels */
433+
format_metric_labels(context, buf, map, metric);
434+
435+
/* Close parenthesis for fields */
436+
cfl_sds_cat_safe(buf, "}", 1);
437+
438+
/* Close parenthesis */
439+
cfl_sds_cat_safe(buf, "}", 1);
440+
}
441+
}
442+
443+
/* Format Summary sum */
444+
{
445+
/* Common fields */
446+
format_context_common(context, buf, map, metric);
447+
448+
/* Other fields */
449+
cfl_sds_cat_safe(buf, "\"fields\":{", 10);
450+
451+
/* metric name for bucket */
452+
format_metric_name(buf, map, "_sum");
453+
454+
/* Retrieve metric value */
455+
metric_val = cmt_summary_get_sum_value(metric);
456+
metric_str = double_to_string(metric_val);
457+
458+
len = snprintf(tmp, sizeof(tmp) - 1, "%s", metric_str);
459+
cfl_sds_cat_safe(buf, tmp, len);
460+
cfl_sds_destroy(metric_str);
461+
462+
/* Format labels */
463+
format_metric_labels(context, buf, map, metric);
464+
465+
/* Close parenthesis for fields */
466+
cfl_sds_cat_safe(buf, "}", 1);
467+
468+
/* Close parenthesis */
469+
cfl_sds_cat_safe(buf, "}", 1);
470+
}
471+
472+
/* Format summary count */
473+
{
474+
/* Common fields */
475+
format_context_common(context, buf, map, metric);
476+
477+
/* Other fields */
478+
cfl_sds_cat_safe(buf, "\"fields\":{", 10);
479+
480+
/* metric name for bucket */
481+
format_metric_name(buf, map, "_count");
482+
483+
/* Retrieve metric value */
484+
metric_val = cmt_summary_get_count_value(metric);
485+
metric_str = double_to_string(metric_val);
486+
487+
len = snprintf(tmp, sizeof(tmp) - 1, "%s", metric_str);
488+
cfl_sds_cat_safe(buf, tmp, len);
489+
cfl_sds_destroy(metric_str);
490+
491+
/* Format labels */
492+
format_metric_labels(context, buf, map, metric);
493+
494+
/* Close parenthesis for fields */
495+
cfl_sds_cat_safe(buf, "}", 1);
496+
497+
/* Close parenthesis */
498+
cfl_sds_cat_safe(buf, "}", 1);
499+
}
500+
}
501+
252502
static void format_metric_data_points(struct cmt_splunk_hec_context *context, cfl_sds_t *buf, struct cmt_map *map,
253503
struct cmt_metric *metric)
254504
{
@@ -275,10 +525,10 @@ static void format_metric(struct cmt_splunk_hec_context *context, cfl_sds_t *buf
275525
struct cmt_metric *metric)
276526
{
277527
if (map->type == CMT_HISTOGRAM) {
278-
/* return format_histogram_bucket(context, buf, map, metric); */
528+
return format_histogram_bucket(context, buf, map, metric);
279529
}
280530
else if (map->type == CMT_SUMMARY) {
281-
/* return format_summary_metric(context, buf, map, metric); */
531+
return format_summary_metric(context, buf, map, metric);
282532
}
283533
else {
284534
/* For Counter, Gauge, and Untyped types */

0 commit comments

Comments
 (0)