@cryptotaxi247 / netdata-1 / commits / 61c641e46

cgroups.plugin: add image label (#14872)

Ilya Mashchenko committed Apr 9, 2023 at 11:46 UTC 61c641e468c741a63681b2ee82aae89503f72574
5 files changed +53 -34
collectors/cgroups.plugin/cgroup-name.sh
+28 -13
@@ -47,11 +47,14 @@ fatal() {
47
48 function parse_docker_like_inspect_output() {
49 local output="${1}"
50 - eval "$(grep -E "^(NOMAD_NAMESPACE|NOMAD_JOB_NAME|NOMAD_TASK_NAME|NOMAD_SHORT_ALLOC_ID|CONT_NAME)=" <<<"$output")"
50 + eval "$(grep -E "^(NOMAD_NAMESPACE|NOMAD_JOB_NAME|NOMAD_TASK_NAME|NOMAD_SHORT_ALLOC_ID|CONT_NAME|IMAGE_NAME)=" <<<"$output")"
51 if [ -n "$NOMAD_NAMESPACE" ] && [ -n "$NOMAD_JOB_NAME" ] && [ -n "$NOMAD_TASK_NAME" ] && [ -n "$NOMAD_SHORT_ALLOC_ID" ]; then
52 - echo "${NOMAD_NAMESPACE}-${NOMAD_JOB_NAME}-${NOMAD_TASK_NAME}-${NOMAD_SHORT_ALLOC_ID}"
52 + NAME="${NOMAD_NAMESPACE}-${NOMAD_JOB_NAME}-${NOMAD_TASK_NAME}-${NOMAD_SHORT_ALLOC_ID}"
53 else
54 - echo "${CONT_NAME}" | sed 's|^/||'
54 + NAME=$(echo "${CONT_NAME}" | sed 's|^/||')
55 + fi
56 + if [ -n "${IMAGE_NAME}" ]; then
57 + LABELS="image=\"${IMAGE_NAME}\""
58 fi
59 }
60
@@ -59,9 +62,9 @@ function docker_like_get_name_command() {
62 local command="${1}"
63 local id="${2}"
64 info "Running command: ${command} inspect --format='{{range .Config.Env}}{{println .}}{{end}}CONT_NAME={{ .Name}}' \"${id}\""
62 - if OUTPUT="$(${command} inspect --format='{{range .Config.Env}}{{println .}}{{end}}CONT_NAME={{ .Name}}' "${id}")" &&
65 + if OUTPUT="$(${command} inspect --format='{{range .Config.Env}}{{println .}}{{end}}CONT_NAME={{ .Name}}{{println}}IMAGE_NAME={{ .Config.Image}}' "${id}")" &&
66 [ -n "$OUTPUT" ]; then
64 - NAME="$(parse_docker_like_inspect_output "$OUTPUT")"
67 + parse_docker_like_inspect_output "$OUTPUT"
68 fi
69 return 0
70 }
@@ -85,8 +88,8 @@ function docker_like_get_name_api() {
88 info "Running API command: curl \"${host}${path}\""
89 JSON=$(curl -sS "${host}${path}")
90 fi
88 - if OUTPUT=$(echo "${JSON}" | jq -r '.Config.Env[],"CONT_NAME=\(.Name)"') && [ -n "$OUTPUT" ]; then
89 - NAME="$(parse_docker_like_inspect_output "$OUTPUT")"
91 + if OUTPUT=$(echo "${JSON}" | jq -r '.Config.Env[],"CONT_NAME=\(.Name)","IMAGE_NAME=\(.Config.Image)"') && [ -n "$OUTPUT" ]; then
92 + parse_docker_like_inspect_output "$OUTPUT"
93 fi
94 return 0
95 }
@@ -413,20 +416,25 @@ function k8s_get_name() {
416 local fn="${FUNCNAME[0]}"
417 local cgroup_path="${1}"
418 local id="${2}"
419 + local kubepod_name=""
420
417 - NAME=$(k8s_get_kubepod_name "$cgroup_path" "$id")
421 + kubepod_name=$(k8s_get_kubepod_name "$cgroup_path" "$id")
422
423 case "$?" in
424 0)
421 - NAME="k8s_${NAME}"
425 + kubepod_name="k8s_${kubepod_name}"
426
427 local name labels
424 - name=${NAME%% *}
425 - labels=${NAME#* }
428 + name=${kubepod_name%% *}
429 + labels=${kubepod_name#* }
430 +
431 if [ "$name" != "$labels" ]; then
432 info "${fn}: cgroup '${id}' has chart name '${name}', labels '${labels}"
433 + NAME="$name"
434 + LABELS="$labels"
435 else
436 info "${fn}: cgroup '${id}' has chart name '${NAME}'"
437 + NAME="$name"
438 fi
439 EXIT_CODE=$EXIT_SUCCESS
440 ;;
@@ -512,6 +520,7 @@ EXIT_RETRY=2
520 EXIT_DISABLE=3
521 EXIT_CODE=$EXIT_SUCCESS
522 NAME=
523 +LABELS=
524
525 # -----------------------------------------------------------------------------
526
@@ -591,7 +600,13 @@ if [ -z "${NAME}" ]; then
600 [ ${#NAME} -gt 100 ] && NAME="${NAME:0:100}"
601 fi
602
594 -info "cgroup '${CGROUP}' is called '${NAME}'"
595 -echo "${NAME}"
603 +NAME="${NAME// /_}"
604 +
605 +info "cgroup '${CGROUP}' is called '${NAME}', labels '${LABELS}'"
606 +if [ -n "$LABELS" ]; then
607 + echo "${NAME} ${LABELS}"
608 +else
609 + echo "${NAME}"
610 +fi
611
612 exit ${EXIT_CODE}
collectors/cgroups.plugin/sys_fs_cgroup.c
+20 -13
@@ -1769,7 +1769,7 @@ static inline void substitute_dots_in_id(char *s) {
1769 // ----------------------------------------------------------------------------
1770 // parse k8s labels
1771
1772 -char *k8s_parse_resolved_name_and_labels(DICTIONARY *labels, char *data) {
1772 +char *cgroup_parse_resolved_name_and_labels(DICTIONARY *labels, char *data) {
1773 // the first word, up to the first space is the name
1774 char *name = mystrsep(&data, " ");
1775
@@ -1898,19 +1898,21 @@ static inline void discovery_rename_cgroup(struct cgroup *cg) {
1898 break;
1899 }
1900
1901 - if(cg->pending_renames || cg->processed) return;
1902 - if(!new_name || !*new_name || *new_name == '\n') return;
1903 - if(!(new_name = trim(new_name))) return;
1901 + if (cg->pending_renames || cg->processed)
1902 + return;
1903 + if (!new_name || !*new_name || *new_name == '\n')
1904 + return;
1905 + if (!(new_name = trim(new_name)))
1906 + return;
1907
1908 char *name = new_name;
1906 - if (!strncmp(new_name, "k8s_", 4)) {
1907 - if(!cg->chart_labels) cg->chart_labels = rrdlabels_create();
1909
1909 - // read the new labels and remove the obsolete ones
1910 - rrdlabels_unmark_all(cg->chart_labels);
1911 - name = k8s_parse_resolved_name_and_labels(cg->chart_labels, new_name);
1912 - rrdlabels_remove_all_unmarked(cg->chart_labels);
1913 - }
1910 + if (!cg->chart_labels)
1911 + cg->chart_labels = rrdlabels_create();
1912 + // read the new labels and remove the obsolete ones
1913 + rrdlabels_unmark_all(cg->chart_labels);
1914 + name = cgroup_parse_resolved_name_and_labels(cg->chart_labels, new_name);
1915 + rrdlabels_remove_all_unmarked(cg->chart_labels);
1916
1917 freez(cg->chart_title);
1918 cg->chart_title = cgroup_title_strdupz(name);
@@ -2713,13 +2715,18 @@ static inline void discovery_process_cgroup(struct cgroup *cg) {
2715 return;
2716 }
2717
2716 - worker_is_busy(WORKER_DISCOVERY_PROCESS_NETWORK);
2717 - read_cgroup_network_interfaces(cg);
2718 if (!cg->chart_labels)
2719 cg->chart_labels = rrdlabels_create();
2720 +
2721 if (!k8s_is_kubepod(cg)) {
2722 rrdlabels_add(cg->chart_labels, "cgroup_name", cg->chart_id, RRDLABEL_SRC_AUTO);
2723 + if (!dictionary_get(cg->chart_labels, "image")) {
2724 + rrdlabels_add(cg->chart_labels, "image", "", RRDLABEL_SRC_AUTO);
2725 + }
2726 }
2727 +
2728 + worker_is_busy(WORKER_DISCOVERY_PROCESS_NETWORK);
2729 + read_cgroup_network_interfaces(cg);
2730 }
2731
2732 static inline void discovery_find_all_cgroups() {
collectors/cgroups.plugin/sys_fs_cgroup.h
+1 -1
@@ -39,6 +39,6 @@ typedef struct netdata_ebpf_cgroup_shm {
39
40 #include "../proc.plugin/plugin_proc.h"
41
42 -char *k8s_parse_resolved_name_and_labels(DICTIONARY *labels, char *data);
42 +char *cgroup_parse_resolved_name_and_labels(DICTIONARY *labels, char *data);
43
44 #endif //NETDATA_SYS_FS_CGROUP_H
collectors/cgroups.plugin/tests/test_cgroups_plugin.c
+4 -4
@@ -33,7 +33,7 @@ static int read_label_callback(const char *name, const char *value, RRDLABEL_SRC
33 return 1;
34 }
35
36 -static void test_k8s_parse_resolved_name(void **state)
36 +static void test_cgroup_parse_resolved_name(void **state)
37 {
38 UNUSED(state);
39
@@ -96,7 +96,7 @@ static void test_k8s_parse_resolved_name(void **state)
96 for (int i = 0; test_data[i].data != NULL; i++) {
97 char *data = strdup(test_data[i].data);
98
99 - char *name = k8s_parse_resolved_name_and_labels(labels, data);
99 + char *name = cgroup_parse_resolved_name_and_labels(labels, data);
100
101 assert_string_equal(name, test_data[i].name);
102
@@ -122,10 +122,10 @@ static void test_k8s_parse_resolved_name(void **state)
122 int main(void)
123 {
124 const struct CMUnitTest tests[] = {
125 - cmocka_unit_test(test_k8s_parse_resolved_name),
125 + cmocka_unit_test(test_cgroup_parse_resolved_name),
126 };
127
128 - int test_res = cmocka_run_group_tests_name("test_k8s_parse_resolved_name", tests, NULL, NULL);
128 + int test_res = cmocka_run_group_tests_name("test_cgroup_parse_resolved_name", tests, NULL, NULL);
129
130 return test_res;
131 }
collectors/proc.plugin/proc_net_dev.c
-3
@@ -519,9 +519,6 @@ static inline void netdev_rename_cgroup(struct netdev *d, struct netdev_rename *
519 d->chart_family = strdupz("net");
520
521 rrdlabels_copy(d->chart_labels, r->chart_labels);
522 - if (strncmp(r->ctx_prefix, "k8s", 3)) {
523 - rrdlabels_add(d->chart_labels, "cgroup_name", r->container_name, RRDLABEL_SRC_AUTO);
524 - }
522
523 d->priority = NETDATA_CHART_PRIO_CGROUP_NET_IFACE;
524 d->flipped = 1;