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

out_stackdriver: add project_id_key override to allow specifying gcp project id from the record #8209

Merged
merged 1 commit into from
Jan 10, 2024

Conversation

ryanohnemus
Copy link
Contributor

@ryanohnemus ryanohnemus commented Nov 22, 2023

A new configuration option for the out_stackdriver plugin project_id_key allows you to specify a key within the jsonPayload that will be used to set the GCP Project ID to export to. This allows a stackdriver out plugin that has logWriter permissions in multiple projects to write to different gcp projects based on a jsonPayload (record) key. This also introduces a default logging.googleapis.com/projectId key that will be used if present in the record. The key that is used from the record is removed similar to how severity_key and label_key work.

Addresses #8195

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

  • Example configuration file for the change
[FILTER]
    Name modify
    Match k8s_*project1*
    Add logging_project_id prj-fluent-bit-test1

[FILTER]
    Name modify
    Match k8s_*project2*
    Add logging_project_id prj-fluent-bit-test2

[OUTPUT]
    Name stackdriver
    Match k8s_*
    export_to_project_key logging_project_id
    resource k8s_container
    k8s_cluster_name my-test-cluster
    k8s_cluster_location us-central1
  • Debug log output from testing the change
  • Attached Valgrind output that shows no leaks or memory corruption was found
==97479== 
==97479== HEAP SUMMARY:
==97479==     in use at exit: 0 bytes in 0 blocks
==97479==   total heap usage: 995 allocs, 995 frees, 238,168 bytes allocated
==97479== 
==97479== All heap blocks were freed -- no leaks are possible
==97479== 
==97479== For lists of detected and suppressed errors, rerun with: -s
==97479== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

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

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

Documentation

Configuration Parameters

Key Description default
project_id_key The value of this field is used by the Stackdriver output plugin to find the gcp project id from jsonPayload and then extract the value of it to set the PROJECT_ID within LogEntry logName, which controls the gcp project that should receive these logs. logging.googleapis.com/projectId, if not present within the jsonPayload the value of export_to_project_id is used as the gcp project id.

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.

@braydonk
Copy link
Contributor

Thank you for the PR! I am sorry that I didn't see the issue opened last week, thank you for opening a PR about it.

This seems like a good change that we'll probably accept, however I think it would be best for it to match the implementation of the other special keys. Generally with other special keys that this plugin recognizes, we have a default in the format logging.googleapis.com/<keyName>, where the logging.googleapis.com allows it to be obvious in the log structure what fields are intended for the plugin to handle in a special way.

You can see for example where severity key is defined in the header:

#define DEFAULT_SEVERITY_KEY "logging.googleapis.com/severity"

Then we have config options for alternatives to those keys to be specified:
FLB_CONFIG_MAP_STR, "severity_key", DEFAULT_SEVERITY_KEY,

What you have is most of the way to what is needed. The changes would essentially be:

  1. Add a new constant in the header, DEFAULT_PROJECT_ID_KEY, and I would recommend making the value logging.googleapis.com/projectId
  2. Change export_to_project_key in struct flb_stackdriver and the config to project_id_key. The new meaning of this config would be to override the default project ID key. The default value of this should be the constant. When the config is read, the value of ctx->project_id_key will always be set, either to the default or to the override from the config.
  3. Add a test similar to the one you already added (thank you for doing that btw) that also tests logging.googleapis.com/projectId has the same behaviour as overriding the key.

Once those changes are made, I'm happy to accept the change!

(P.S. for that DCO check; all commits in this repo are required to be signed, so that's git commit -s. To correct the current one you can run git commit --amend -s --allow-empty and force push)

@@ -2410,8 +2415,19 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
}

/* logName */
len = snprintf(path, sizeof(path) - 1,
project_key_extracted = FLB_FALSE;
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the extraction here should be moved up with the rest of the extractions, like line 2212ish.

The /* logName */ comment should also be moved down to where the log name check is now.

@ryanohnemus ryanohnemus force-pushed the feature/stackdriver_project_key branch 2 times, most recently from 58da0a3 to 0eed9ec Compare November 22, 2023 20:01
@ryanohnemus
Copy link
Contributor Author

@braydonk I appreciate the quick review and the feedback! I just force pushed the latest changes... I hope this is in line with your comments. If I missed anything please let me know and I'll give it another shot!

Copy link
Contributor

@braydonk braydonk left a comment

Choose a reason for hiding this comment

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

LGTM! One last thing

@@ -2410,8 +2422,13 @@ static flb_sds_t stackdriver_format(struct flb_stackdriver *ctx,
}

/* logName */
Copy link
Contributor

Choose a reason for hiding this comment

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

This comment needs to be moved down to where the logName section is (line 2432)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

missed that one... Just pushed the latest commit!

@ryanohnemus ryanohnemus force-pushed the feature/stackdriver_project_key branch from 670d4a1 to dec9f21 Compare November 22, 2023 20:35
braydonk
braydonk previously approved these changes Nov 22, 2023
Copy link
Contributor

@braydonk braydonk left a comment

Choose a reason for hiding this comment

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

Thank you for the useful feature and for working with me in the review!

@ryanohnemus
Copy link
Contributor Author

Thank you for guiding me through it @braydonk! I really appreciate the help!

@ryanohnemus
Copy link
Contributor Author

@braydonk I had to push another commit to fix the memory leak due to not cleaning up the extract project_id_key, so this will probably need another review/approval to kick off the workflow tests again.

I ran valgrind ./bin/flb-rt-out_stackdriver and with the following output:

SUCCESS: All unit tests have passed.
==62935== 
==62935== HEAP SUMMARY:
==62935==     in use at exit: 0 bytes in 0 blocks
==62935==   total heap usage: 201,986 allocs, 201,986 frees, 96,592,089 bytes allocated
==62935== 
==62935== All heap blocks were freed -- no leaks are possible
==62935== 
==62935== For lists of detected and suppressed errors, rerun with: -s
==62935== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

@ryanohnemus ryanohnemus requested a review from braydonk November 28, 2023 17:34
@ryanohnemus ryanohnemus changed the title out_stackdriver: add export_to_project_key override to allow specifying gcp project id from the record out_stackdriver: add project_id_key override to allow specifying gcp project id from the record Dec 1, 2023
Copy link
Contributor

@braydonk braydonk left a comment

Choose a reason for hiding this comment

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

Looks like the action failed is a flake.

Thank you for the PR!

@ryanohnemus
Copy link
Contributor Author

I'm happy to have been able to contribute! As far as merging goes, does that happen in a standard cadence after approvals? (Not sure if I need to do anything else for this PR)

Do I wait until this is fully merged to create a documentation pull request? (Or will someone else handle the documentation update after merge?)

@braydonk
Copy link
Contributor

braydonk commented Dec 5, 2023

I've got the Doc PR ready (added it into the description of this PR).

I was intending to merge this here now, but it turns out there's a file here we aren't covered as a CODEOWNER where we should be (the testdata file). I'm going to file a separate PR to get us added to that so I can subsequently merge this one.

@braydonk
Copy link
Contributor

braydonk commented Dec 5, 2023

Opened the CODEOWNERS PR here: #8254
Once it's merged, I can merge this.

@ryanohnemus
Copy link
Contributor Author

@braydonk not sure if you're still around because of the holidays, but wanted to check in and see if this could be merged now that the code owners pr was merged. It looks like there will be a release cut today from @edsiper's comments in #8289

@braydonk braydonk merged commit 697a413 into fluent:master Jan 10, 2024
45 of 47 checks passed
shaerpour pushed a commit to shaerpour/fluent-bit that referenced this pull request Jan 16, 2024
shaerpour pushed a commit to shaerpour/fluent-bit that referenced this pull request Jan 16, 2024
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.

2 participants