Skip to content

Add container labels to JournalD logs #553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

OZoneGuy
Copy link

@OZoneGuy OZoneGuy commented Apr 14, 2025

Add the ability to add the container labels to the journald logs as extra parameters.

Relies on the caller to pass the label key and value.

Usage:

conmon ... --log-labels label1=val1,label2=val2

The labels and values will be added to the journald entry.

This is my first contribution to a C codebase. Feedback is most

Still need to test the changes locally.

Signed-off-by: OZoneGuy <[email protected]>
pass NULL as the first argument for subsequent calls

Signed-off-by: OZoneGuy <[email protected]>
Comment on lines +184 to +185
container_labels = g_alloca((sizeof(char *)) * container_labels_count);
container_labels_lengths = g_alloca((sizeof(int *)) * container_labels_count);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when are these ever going to be freed? In this way they are just leaked

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The idea is to create them once in at the beginning of the program lifecycle and reuse them. I will look into freeing them at the at exit.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is fine, could you just add an inline comment saying it is by design?

}
container_labels = g_alloca((sizeof(char *)) * container_labels_count);
container_labels_lengths = g_alloca((sizeof(int *)) * container_labels_count);
char *pair = strtok(log_labels, ",");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strtok modifies the buffer, so log_labels will be modified. The correct way to do it is to g_strdup the string before passing it to strtok.

Comment on lines +350 to +352
if (writev_buffer_append_segment_no_flush(&bufv, container_labels[i], container_labels_lengths[i]) < 0)
return -1;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest to not create the arrays before and then have to worry about cleaning them up.

Just strdup container_labels here and call writev_buffer_append_segment_no_flush while you parse it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants