Skip to content

Conversation

@chitoku-k
Copy link
Contributor

@chitoku-k chitoku-k commented Feb 23, 2022

This patch adds support of lowercase option for the systemd input plugin. The name of option is aligned to
https://github.com/fluent-plugin-systemd/fluent-plugin-systemd.

Addresses #1543


Testing
Before we can approve your change; please submit the following in a comment:

  • Example configuration file for the change
  • Debug log output from testing the change
  • Attached Valgrind output that shows no leaks or memory corruption was found

If this is a change to packaging of containers or native binaries then please confirm it works for all targets.

Documentation

  • Documentation required for this feature

Backporting

  • Backport to latest stable release.

Fluent Bit is licensed under Apache 2.0, by submitting this pull request I understand that this code will be released under the terms of that license.

@chitoku-k
Copy link
Contributor Author

chitoku-k commented Feb 23, 2022

Config

[INPUT]
    Name                systemd
    Systemd_Filter      _SYSTEMD_UNIT=systemd-logind.service
    Read_From_Tail      On
    Lowercase           On

[OUTPUT]
    Name                stdout

Log

$ bin/fluent-bit -c config
Fluent Bit v1.9.0
* Copyright (C) 2015-2021 The Fluent Bit Authors
* Fluent Bit is a CNCF sub-project under the umbrella of Fluentd
* https://fluentbit.io

[2022/03/01 14:02:27] [ info] [engine] started (pid=799003)
[2022/03/01 14:02:27] [ info] [storage] version=1.1.6, initializing...
[2022/03/01 14:02:27] [ info] [storage] in-memory
[2022/03/01 14:02:27] [ info] [storage] normal synchronization mode, checksum disabled, max_chunks_up=128
[2022/03/01 14:02:27] [ info] [cmetrics] version=0.3.0
[2022/03/01 14:02:27] [ info] [sp] stream processor started
[2022/03/01 14:02:27] [ info] [output:stdout:stdout.0] worker #0 started
[0] systemd.0: [1646110949.135873000, {"_boot_id"=>"31e922b589f345c0b07a11f9ec5251bb", "_machine_id"=>"09cb1f91a4af4686bc1b57cad2710c32", "_hostname"=>"lychee", "priority"=>"6", "_uid"=>"0", "_gid"=>"0", "_systemd_slice"=>"system.slice", "_transport"=>"journal", "syslog_facility"=>"4", "tid"=>"490", "syslog_identifier"=>"systemd-logind", "_pid"=>"490", "_comm"=>"systemd-logind", "_exe"=>"/usr/lib/systemd/systemd-logind", "_cmdline"=>"/usr/lib/systemd/systemd-logind", "_cap_effective"=>"24420020f", "_systemd_cgroup"=>"/system.slice/systemd-logind.service", "_systemd_unit"=>"systemd-logind.service", "_systemd_invocation_id"=>"14002dd430fe4cb7a6b53761e2c0557f", "code_file"=>"src/login/logind-session.c", "code_line"=>"712", "code_func"=>"session_start", "message_id"=>"8d45620c1a4348dbb17410da57c60c66", "user_id"=>"chitoku", "session_id"=>"5", "leader"=>"799013", "message"=>"New session 5 of user chitoku.", "_source_realtime_timestamp"=>"1646110949135851"}]
[0] systemd.0: [1646110953.403568000, {"_boot_id"=>"31e922b589f345c0b07a11f9ec5251bb", "_machine_id"=>"09cb1f91a4af4686bc1b57cad2710c32", "_hostname"=>"lychee", "priority"=>"6", "_uid"=>"0", "_gid"=>"0", "_systemd_slice"=>"system.slice", "_transport"=>"journal", "syslog_facility"=>"4", "tid"=>"490", "syslog_identifier"=>"systemd-logind", "_pid"=>"490", "_comm"=>"systemd-logind", "_exe"=>"/usr/lib/systemd/systemd-logind", "_cmdline"=>"/usr/lib/systemd/systemd-logind", "_cap_effective"=>"24420020f", "_systemd_cgroup"=>"/system.slice/systemd-logind.service", "_systemd_unit"=>"systemd-logind.service", "_systemd_invocation_id"=>"14002dd430fe4cb7a6b53761e2c0557f", "code_file"=>"src/login/logind-session.c", "user_id"=>"chitoku", "code_line"=>"783", "code_func"=>"session_stop_scope", "session_id"=>"5", "leader"=>"799013", "message"=>"Session 5 logged out. Waiting for processes to exit.", "_source_realtime_timestamp"=>"1646110953403543"}]
[1] systemd.0: [1646110953.404097000, {"_boot_id"=>"31e922b589f345c0b07a11f9ec5251bb", "_machine_id"=>"09cb1f91a4af4686bc1b57cad2710c32", "_hostname"=>"lychee", "priority"=>"6", "_uid"=>"0", "_gid"=>"0", "_systemd_slice"=>"system.slice", "_transport"=>"journal", "syslog_facility"=>"4", "tid"=>"490", "syslog_identifier"=>"systemd-logind", "_pid"=>"490", "_comm"=>"systemd-logind", "_exe"=>"/usr/lib/systemd/systemd-logind", "_cmdline"=>"/usr/lib/systemd/systemd-logind", "_cap_effective"=>"24420020f", "_systemd_cgroup"=>"/system.slice/systemd-logind.service", "_systemd_unit"=>"systemd-logind.service", "_systemd_invocation_id"=>"14002dd430fe4cb7a6b53761e2c0557f", "code_file"=>"src/login/logind-session.c", "user_id"=>"chitoku", "code_line"=>"840", "code_func"=>"session_finalize", "message_id"=>"3354939424b4456d9802ca8333ed424a", "session_id"=>"5", "leader"=>"799013", "message"=>"Removed session 5.", "_source_realtime_timestamp"=>"1646110953404075"}]
^C[2022/03/01 14:02:37] [engine] caught signal (SIGINT)
[2022/03/01 14:02:37] [ info] [input] pausing systemd.0
[2022/03/01 14:02:37] [ warn] [engine] service will shutdown in max 5 seconds
[2022/03/01 14:02:37] [ info] [engine] service has stopped (0 pending tasks)
[2022/03/01 14:02:38] [ info] [output:stdout:stdout.0] thread worker #0 stopping...
[2022/03/01 14:02:38] [ info] [output:stdout:stdout.0] thread worker #0 stopped

Valgrind output

$ valgrind --leak-check=full bin/fluent-bit -c config
==799349== Memcheck, a memory error detector
==799349== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==799349== Using Valgrind-3.18.1 and LibVEX; rerun with -h for copyright info
==799349== Command: bin/fluent-bit -c config
==799349==
Fluent Bit v1.9.0
* Copyright (C) 2015-2021 The Fluent Bit Authors
* Fluent Bit is a CNCF sub-project under the umbrella of Fluentd
* https://fluentbit.io

[2022/03/01 14:02:49] [ info] [engine] started (pid=799349)
[2022/03/01 14:02:49] [ info] [storage] version=1.1.6, initializing...
[2022/03/01 14:02:49] [ info] [storage] in-memory
[2022/03/01 14:02:49] [ info] [storage] normal synchronization mode, checksum disabled, max_chunks_up=128
[2022/03/01 14:02:49] [ info] [cmetrics] version=0.3.0
[2022/03/01 14:02:49] [ info] [sp] stream processor started
[2022/03/01 14:02:49] [ info] [output:stdout:stdout.0] worker #0 started
==799349== Warning: client switching stacks?  SP change: 0x398778d8 --> 0x536d210
==799349==          to suppress, use: --max-stackframe=877700808 or greater
==799349== Warning: client switching stacks?  SP change: 0x536d1b8 --> 0x398778d8
==799349==          to suppress, use: --max-stackframe=877700896 or greater
==799349== Warning: client switching stacks?  SP change: 0x398778d8 --> 0x536d1b8
==799349==          to suppress, use: --max-stackframe=877700896 or greater
==799349==          further instances of this message will not be shown.
[0] systemd.0: [1646110971.390436000, {"_boot_id"=>"31e922b589f345c0b07a11f9ec5251bb", "_machine_id"=>"09cb1f91a4af4686bc1b57cad2710c32", "_hostname"=>"lychee", "priority"=>"6", "_uid"=>"0", "_gid"=>"0", "_systemd_slice"=>"system.slice", "_transport"=>"journal", "syslog_facility"=>"4", "tid"=>"490", "syslog_identifier"=>"systemd-logind", "_pid"=>"490", "_comm"=>"systemd-logind", "_exe"=>"/usr/lib/systemd/systemd-logind", "_cmdline"=>"/usr/lib/systemd/systemd-logind", "_cap_effective"=>"24420020f", "_systemd_cgroup"=>"/system.slice/systemd-logind.service", "_systemd_unit"=>"systemd-logind.service", "_systemd_invocation_id"=>"14002dd430fe4cb7a6b53761e2c0557f", "code_file"=>"src/login/logind-session.c", "code_line"=>"712", "code_func"=>"session_start", "message_id"=>"8d45620c1a4348dbb17410da57c60c66", "user_id"=>"chitoku", "session_id"=>"6", "leader"=>"799365", "message"=>"New session 6 of user chitoku.", "_source_realtime_timestamp"=>"1646110971390402"}]
[0] systemd.0: [1646110976.162489000, {"_boot_id"=>"31e922b589f345c0b07a11f9ec5251bb", "_machine_id"=>"09cb1f91a4af4686bc1b57cad2710c32", "_hostname"=>"lychee", "priority"=>"6", "_uid"=>"0", "_gid"=>"0", "_systemd_slice"=>"system.slice", "_transport"=>"journal", "syslog_facility"=>"4", "tid"=>"490", "syslog_identifier"=>"systemd-logind", "_pid"=>"490", "_comm"=>"systemd-logind", "_exe"=>"/usr/lib/systemd/systemd-logind", "_cmdline"=>"/usr/lib/systemd/systemd-logind", "_cap_effective"=>"24420020f", "_systemd_cgroup"=>"/system.slice/systemd-logind.service", "_systemd_unit"=>"systemd-logind.service", "_systemd_invocation_id"=>"14002dd430fe4cb7a6b53761e2c0557f", "code_file"=>"src/login/logind-session.c", "user_id"=>"chitoku", "code_line"=>"783", "code_func"=>"session_stop_scope", "session_id"=>"6", "leader"=>"799365", "message"=>"Session 6 logged out. Waiting for processes to exit.", "_source_realtime_timestamp"=>"1646110976162473"}]
[1] systemd.0: [1646110976.162964000, {"_boot_id"=>"31e922b589f345c0b07a11f9ec5251bb", "_machine_id"=>"09cb1f91a4af4686bc1b57cad2710c32", "_hostname"=>"lychee", "priority"=>"6", "_uid"=>"0", "_gid"=>"0", "_systemd_slice"=>"system.slice", "_transport"=>"journal", "syslog_facility"=>"4", "tid"=>"490", "syslog_identifier"=>"systemd-logind", "_pid"=>"490", "_comm"=>"systemd-logind", "_exe"=>"/usr/lib/systemd/systemd-logind", "_cmdline"=>"/usr/lib/systemd/systemd-logind", "_cap_effective"=>"24420020f", "_systemd_cgroup"=>"/system.slice/systemd-logind.service", "_systemd_unit"=>"systemd-logind.service", "_systemd_invocation_id"=>"14002dd430fe4cb7a6b53761e2c0557f", "code_file"=>"src/login/logind-session.c", "user_id"=>"chitoku", "code_line"=>"840", "code_func"=>"session_finalize", "message_id"=>"3354939424b4456d9802ca8333ed424a", "session_id"=>"6", "leader"=>"799365", "message"=>"Removed session 6.", "_source_realtime_timestamp"=>"1646110976162940"}]
^C[2022/03/01 14:03:00] [engine] caught signal (SIGINT)
[2022/03/01 14:03:00] [ info] [input] pausing systemd.0
[2022/03/01 14:03:00] [ warn] [engine] service will shutdown in max 5 seconds
[2022/03/01 14:03:01] [ info] [engine] service has stopped (0 pending tasks)
[2022/03/01 14:03:01] [ info] [output:stdout:stdout.0] thread worker #0 stopping...
[2022/03/01 14:03:01] [ info] [output:stdout:stdout.0] thread worker #0 stopped
==799349==
==799349== HEAP SUMMARY:
==799349==     in use at exit: 0 bytes in 0 blocks
==799349==   total heap usage: 1,745 allocs, 1,745 frees, 1,509,074 bytes allocated
==799349==
==799349== All heap blocks were freed -- no leaks are possible
==799349==
==799349== For lists of detected and suppressed errors, rerun with: -s
==799349== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

msgpack_pack_str_body(&mp_pck, key, len);

if (ctx->lowercase == FLB_TRUE) {
tmp = flb_sds_create_len(key, len);
Copy link
Member

Choose a reason for hiding this comment

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

I was wondering if we could re-use the original systemd buffer (pointed by key) to perform the lowercase conversion, can you check it ?

if the previous step is not possible, we might use a pre-allocated buffer for key conversion to avoid a tons of malloc/free on each record iteration. I see in the docs that usually records gets truncated at 64kb:

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for taking the time to review this pull request!

I've seen the manpage saying that ”returned data is in a read-only memory map” so chose not to overwritten the data directly; however, I see your point, so how about pre-allocating a buffer whose size is equal to the threshold from sd_journal_get_data_threshold() and increase it when it’s insufficient to hold the entire key? I’ve pushed the branch trying to reflect this.

This threshold is a hint only: it indicates that the client program is interested only in the initial parts of the data fields, up to the threshold in size — but the library might still return larger data objects. That means applications should not rely exclusively on this setting to limit the size of the data fields returned, but need to apply an explicit size limit on the returned data as well.

Copy link
Member

Choose a reason for hiding this comment

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

that looks pretty good :) , thank you!

@chitoku-k chitoku-k force-pushed the in-systemd-lowercase branch from 549e4ad to 4c0d4aa Compare February 25, 2022 16:05
@edsiper
Copy link
Member

edsiper commented Mar 1, 2022

ah, pls rebase on top of GIT master to fix the conflicts so we can merge it

@edsiper edsiper added waiting-for-user Waiting for more information, tests or requested changes and removed docs-required labels Mar 1, 2022
Signed-off-by: Chitoku <odango@chitoku.jp>
Signed-off-by: Chitoku <odango@chitoku.jp>
@chitoku-k chitoku-k force-pushed the in-systemd-lowercase branch from 4c0d4aa to 5e835b6 Compare March 1, 2022 05:00
@chitoku-k
Copy link
Contributor Author

@edsiper
Thanks for having a look at it! I’ve updated the branch so it’s compliant with the config_map.

@edsiper edsiper merged commit 6ab7380 into fluent:master Mar 4, 2022
@edsiper
Copy link
Member

edsiper commented Mar 4, 2022

thank you!, it will be part of next release next week.

would you please submit a PR for the docs too ? (cc: @lecaros)

@chitoku-k chitoku-k deleted the in-systemd-lowercase branch March 5, 2022 02:25
@chitoku-k
Copy link
Contributor Author

The PR for the docs is here: fluent/fluent-bit-docs#724

mgeriesa pushed a commit to mgeriesa/fluent-bit that referenced this pull request Oct 25, 2022
* in_systemd: add 'lowercase' option

Signed-off-by: Chitoku <odango@chitoku.jp>
Signed-off-by: Manal Geries <mgeriesa@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

waiting-for-user Waiting for more information, tests or requested changes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants