@@ -386,28 +386,64 @@ vector:
386386 for_each(append([], .items) ?? []) -> |_index, item| {
387387 item.metadata.labels.node_name = item.metadata.name
388388 item.metadata.labels.metrics_kind = .kind
389-
390- metrics = push(metrics, {
391- "name": "cpu_ratio",
392- "kind": "absolute",
393- "gauge": {
394- "value": to_float(replace(item.usage.cpu, "n", "") ?? null) / 1000000000
395- },
396- "tags": item.metadata.labels,
397- "dt": .timestamp,
398- "interval_ms": parse_duration(item.window, "ms") ?? null
399- })
400-
401- metrics = push(metrics, {
402- "name": "memory_bytes",
403- "kind": "absolute",
404- "gauge": {
405- "value": 1024 * to_int(replace(item.usage.memory, "Ki", "") ?? null) ?? null
406- },
407- "tags": item.metadata.labels,
408- "dt": .timestamp,
409- "interval_ms": parse_duration(item.window, "ms") ?? null
410- })
389+
390+ cpu_string = to_string(item.usage.cpu) ?? ""
391+ cpu_ratio = 0
392+ cpu_err = null
393+ if ends_with(cpu_string, "n") {
394+ cpu_ratio, cpu_err = 1 / 1000000000 * to_float(replace(cpu_string, "n", ""))
395+ } else if ends_with(cpu_string, "u") {
396+ cpu_ratio, cpu_err = 1 / 1000000 * to_float(replace(cpu_string, "u", ""))
397+ } else if ends_with(cpu_string, "m") {
398+ cpu_ratio, cpu_err = 1 / 1000 * to_float(replace(cpu_string, "m", ""))
399+ } else {
400+ cpu_ratio, cpu_err = to_float(cpu_string)
401+ }
402+
403+ if (cpu_err == null) {
404+ metrics = push(metrics, {
405+ "name": "cpu_ratio",
406+ "kind": "absolute",
407+ "gauge": {
408+ "value": cpu_ratio
409+ },
410+ "tags": item.metadata.labels,
411+ "dt": .timestamp,
412+ "interval_ms": parse_duration(item.window, "ms") ?? null
413+ })
414+ }
415+
416+ memory_string = to_string(item.usage.memory) ?? ""
417+ memory_bytes = 0
418+ memory_err = null
419+ if ends_with(memory_string, "Ki") {
420+ memory_bytes, memory_err = 1024 * to_float(replace(memory_string, "Ki", ""))
421+ } else if ends_with(memory_string, "K") {
422+ memory_bytes, memory_err = 1000 * to_float(replace(memory_string, "K", ""))
423+ } else if ends_with(memory_string, "Mi") {
424+ memory_bytes, memory_err = 1024 * 1024 * to_float(replace(memory_string, "Mi", ""))
425+ } else if ends_with(memory_string, "M") {
426+ memory_bytes, memory_err = 1000 * 1000 * to_float(replace(memory_string, "M", ""))
427+ } else if ends_with(memory_string, "Gi") {
428+ memory_bytes, memory_err = 1024 * 1024 * 1024 * to_float(replace(memory_string, "Gi", ""))
429+ } else if ends_with(memory_string, "G") {
430+ memory_bytes, memory_err = 1000 * 1000 * 1000 * to_float(replace(memory_string, "G", ""))
431+ } else {
432+ memory_bytes, memory_err = to_float(memory_string)
433+ }
434+
435+ if (memory_err == null) {
436+ metrics = push(metrics, {
437+ "name": "memory_bytes",
438+ "kind": "absolute",
439+ "gauge": {
440+ "value": to_int(memory_bytes)
441+ },
442+ "tags": item.metadata.labels,
443+ "dt": .timestamp,
444+ "interval_ms": parse_duration(item.window, "ms") ?? null
445+ })
446+ }
411447 }
412448 . = metrics
413449 better_stack_kubernetes_metrics_pods_parser :
@@ -423,30 +459,67 @@ vector:
423459 tags.pod_name = del(item.metadata.name)
424460 tags.pod_namespace = del(item.metadata.namespace)
425461 tags.metrics_kind = .kind
426-
462+
427463 for_each(append([], item.containers) ?? []) -> |_index, container| {
428464 tags.container_name = del(container.name)
429- metrics = push(metrics, {
430- "name": "cpu_ratio",
431- "kind": "absolute",
432- "gauge": {
433- "value": to_float(replace(container.usage.cpu, "n", "") ?? null) / 1000000000
434- },
435- "tags": tags,
436- "dt": .timestamp,
437- "interval_ms": parse_duration(item.window, "ms") ?? null
438- })
439-
440- metrics = push(metrics, {
441- "name": "memory_bytes",
442- "kind": "absolute",
443- "gauge": {
444- "value": 1024 * to_int(replace(container.usage.memory, "Ki", "") ?? null) ?? null
445- },
446- "tags": tags,
447- "dt": .timestamp,
448- "interval_ms": parse_duration(item.window, "ms") ?? null
449- })
465+
466+ cpu_string = to_string(container.usage.cpu) ?? ""
467+ cpu_ratio = 0
468+ cpu_err = null
469+ if ends_with(cpu_string, "n") {
470+ cpu_ratio, cpu_err = 1 / 1000000000 * to_float(replace(cpu_string, "n", ""))
471+ } else if ends_with(cpu_string, "u") {
472+ cpu_ratio, cpu_err = 1 / 1000000 * to_float(replace(cpu_string, "u", ""))
473+ } else if ends_with(cpu_string, "m") {
474+ cpu_ratio, cpu_err = 1 / 1000 * to_float(replace(cpu_string, "m", ""))
475+ } else {
476+ cpu_ratio, cpu_err = to_float(cpu_string)
477+ }
478+
479+ if (cpu_err == null) {
480+ metrics = push(metrics, {
481+ "name": "cpu_ratio",
482+ "kind": "absolute",
483+ "gauge": {
484+ "value": cpu_ratio
485+ },
486+ "tags": tags,
487+ "dt": .timestamp,
488+ "interval_ms": parse_duration(item.window, "ms") ?? null
489+ })
490+ }
491+
492+ memory_string = to_string(container.usage.memory) ?? ""
493+ memory_bytes = 0
494+ memory_err = null
495+ if ends_with(memory_string, "Ki") {
496+ memory_bytes, memory_err = 1024 * to_float(replace(memory_string, "Ki", ""))
497+ } else if ends_with(memory_string, "K") {
498+ memory_bytes, memory_err = 1000 * to_float(replace(memory_string, "K", ""))
499+ } else if ends_with(memory_string, "Mi") {
500+ memory_bytes, memory_err = 1024 * 1024 * to_float(replace(memory_string, "Mi", ""))
501+ } else if ends_with(memory_string, "M") {
502+ memory_bytes, memory_err = 1000 * 1000 * to_float(replace(memory_string, "M", ""))
503+ } else if ends_with(memory_string, "Gi") {
504+ memory_bytes, memory_err = 1024 * 1024 * 1024 * to_float(replace(memory_string, "Gi", ""))
505+ } else if ends_with(memory_string, "G") {
506+ memory_bytes, memory_err = 1000 * 1000 * 1000 * to_float(replace(memory_string, "G", ""))
507+ } else {
508+ memory_bytes, memory_err = to_float(memory_string)
509+ }
510+
511+ if (memory_err == null) {
512+ metrics = push(metrics, {
513+ "name": "memory_bytes",
514+ "kind": "absolute",
515+ "gauge": {
516+ "value": to_int(memory_bytes)
517+ },
518+ "tags": tags,
519+ "dt": .timestamp,
520+ "interval_ms": parse_duration(item.window, "ms") ?? null
521+ })
522+ }
450523 }
451524 }
452525 . = metrics
0 commit comments