Skip to content
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

mp: fix flb_mp_accessor_keys_remove for sibling keys removal #10013

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

shirolimit
Copy link

@shirolimit shirolimit commented Feb 26, 2025

Fixes #9091

The PR aims to make flb_mp_accessor_keys_remove work for sibling subkeys.
The existing versions of the function can only remove one (the first) subkey of a given key. This method is used by several output plugins including loki which my team uses. We noticed that not all keys are removed from log records when there are multiple subkeys specified.

A good example is provided in the added unit test, but here is another example of existing behaviour the PR is fixing.

Payload:

{
    "kubernetes": {
        "pod_id": "id",
        "pod_name": "name",
        "host": "localhost"
    },
    "msg": "foo"
}

Accessor keys to remove:

"$kubernetes['pod_name']"
"$kubernetes['pod_id']"

Will result in only pod_name being removed. I.e.:

{
    "kubernetes": {
        "pod_id": "id",
        "host": "localhost"
    },
    "msg": "foo"
}

With the PR change the output will be:

{
    "kubernetes": {
        "host": "localhost"
    },
    "msg": "foo"
}
Valgrind output for unit tests
==32115== Memcheck, a memory error detector
==32115== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==32115== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==32115== Command: ./bin/flb-it-mp
==32115== Parent PID: 32050
==32115== 
==32115== Warning: invalid file descriptor -1 in syscall close()
==32115== Warning: invalid file descriptor -1 in syscall close()
==32115== Warning: invalid file descriptor -1 in syscall close()
==32115== Warning: invalid file descriptor -1 in syscall close()
==32115== Warning: invalid file descriptor -1 in syscall close()
==32115== Conditional jump or move depends on uninitialised value(s)
==32115==    at 0x483BC98: strlen (vg_replace_strmem.c:459)
==32115==    by 0x4EA3C27: __vfprintf_internal (vfprintf-internal.c:1647)
==32115==    by 0x4E8ED75: fprintf (fprintf.c:32)
==32115==    by 0x330EB6: cfl_variant_print (cfl_variant.c:44)
==32115==    by 0x326272: cfl_kvlist_print (cfl_kvlist.c:451)
==32115==    by 0x331053: cfl_variant_print (cfl_variant.c:81)
==32115==    by 0x326272: cfl_kvlist_print (cfl_kvlist.c:451)
==32115==    by 0x331053: cfl_variant_print (cfl_variant.c:81)
==32115==    by 0x32D91A: cfl_array_print (cfl_array.c:441)
==32115==    by 0x331037: cfl_variant_print (cfl_variant.c:77)
==32115==    by 0x326272: cfl_kvlist_print (cfl_kvlist.c:451)
==32115==    by 0x331053: cfl_variant_print (cfl_variant.c:81)
==32115== 
==32115== Warning: invalid file descriptor -1 in syscall close()
==32115== Warning: invalid file descriptor -1 in syscall close()
==32115== 
==32115== HEAP SUMMARY:
==32115==     in use at exit: 0 bytes in 0 blocks
==32115==   total heap usage: 8,107 allocs, 8,107 frees, 2,438,380 bytes allocated
==32115== 
==32115== All heap blocks were freed -- no leaks are possible
==32115== 
==32115== Use --track-origins=yes to see where uninitialised values come from
==32115== For lists of detected and suppressed errors, rerun with: -s
==32115== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 0 from 0)
Valgrind output for main binary (with config)
[SERVICE]
    flush     1
    log_level info

[INPUT]
    name      dummy
    dummy     {"logType":"UFL","location":"location","runInfo":{},"message":"test","kubernetes":{"container_name":"test","pod_id":"e900ac60-70eb-4f0c-872a-7127884ccf45","host":"ip-10-193-25-96.ec2.internal","pod_name":"b59ce932-f732-49e3-bb89-f15a3908f096-cqktm","annotations":{"connector-schema-name":"schema","connector-detail":"Ft/Group/postgres_rds/schema"}}}
    samples   1

[OUTPUT]
    name                 loki
    match                *
    host                 localhost
    port                 8000
    line_format          json
    labels               source=fluent-bit
    structured_metadata  pod_name=$kubernetes['pod_name'],foo=$kubernetes['foo'],schema=$kubernetes['annotations']['connector-schema-name'],pod_id=$kubernetes['pod_id']
    remove_keys          _p
    drop_single_key      off
==32110== Memcheck, a memory error detector
==32110== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==32110== Using Valgrind-3.16.1 and LibVEX; rerun with -h for copyright info
==32110== Command: ./bin/fluent-bit -c /home/arkady/simplified-config.conf
==32110== Parent PID: 32050
==32110== 
==32110== 
==32110== HEAP SUMMARY:
==32110==     in use at exit: 0 bytes in 0 blocks
==32110==   total heap usage: 3,679 allocs, 3,679 frees, 1,116,370 bytes allocated
==32110== 
==32110== All heap blocks were freed -- no leaks are possible
==32110== 
==32110== For lists of detected and suppressed errors, rerun with: -s
==32110== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Enter [N/A] in the box, if an item is not applicable to your change.

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

  • Example configuration file for the change
  • [N/A] 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.

  • [N/A] Run local packaging test showing all targets (including any new ones) build.
  • [N/A] Set ok-package-test label to test for all targets (requires maintainer to do).

Documentation

  • [N/A] 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.

@shirolimit shirolimit force-pushed the allow-removing-sibling-keys branch from ca703cc to 5c96491 Compare February 26, 2025 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

output loki remove_keys in nest key not work properly
1 participant