Skip to content

feat(outputs.nats): Allow providing a subject layout #17213

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

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

Conversation

mgale
Copy link
Contributor

@mgale mgale commented Jun 21, 2025

Summary

This PR provides the following feature:

  1. Provides a subject_layout option to direct metrics to subjects based on tags, metric name and field.

Checklist

  • No AI generated code was used in this PR

Related issues

resolves #16558

@telegraf-tiger telegraf-tiger bot added feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin plugin/output 1. Request for new output plugins 2. Issues/PRs that are related to out plugins labels Jun 21, 2025
Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution @mgale! Can we please split this PR into two, one for the subject templating and one for the externally managed streams!? Feel free to create a new one and modify this for the other subject...

@srebhan srebhan self-assigned this Jun 23, 2025
@mgale
Copy link
Contributor Author

mgale commented Jun 25, 2025

Thanks for your contribution @mgale! Can we please split this PR into two, one for the subject templating and one for the externally managed streams!? Feel free to create a new one and modify this for the other subject...

I have moved #17072 into:
#17244

I will complete the cleanup of this PR shortly.

@mgale mgale force-pushed the GH-16558/layout-support branch from b17b3ac to 0a06dd2 Compare July 3, 2025 05:30
@mgale mgale changed the title feat(outputs.nats): Allow providing a subject layout and working with… feat(outputs.nats): Allow providing a subject layout Jul 3, 2025
@mgale mgale force-pushed the GH-16558/layout-support branch from 0a06dd2 to e90ffbc Compare July 3, 2025 05:33
@mgale mgale requested a review from srebhan July 3, 2025 05:46
Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @mgale! My question is, why not just treat the subject setting as template instead of introducing another (redundant) setting? We've done the same in e.g. the remote file output...

@mgale
Copy link
Contributor Author

mgale commented Jul 4, 2025

remote file output

Would you consider changing the subject from a string to a list with this change?

If not, the primary concern with that approach arises when disable_stream_creation = false and the client is responsible for creating the stream. In that case, the stream subject needs to include a wildcard to support the dynamic components—for example, using a pattern like "telegraf.>".

If the subject template is embedded as a single string (e.g., subject = "telegraf.{{ .GetTag "region" }}.{{ .GetTag "host" }}"), we run into a few challenges:
• We would need to ensure that the subject starts with a static prefix, a base that is not part of the dynamic portion.
• We’d have to parse out the static portion of the subject up to the first dynamic tag. It’s unclear how straightforward or reliable that parsing would be, especially across varying templates.

I think this would make it difficult to correctly derive the subject wildcard needed for stream creation.

@srebhan
Copy link
Member

srebhan commented Jul 7, 2025

@mgale I'm a bit lost here. Why does the first element has to be static if Telegraf is responsible for generating the stream? If we go for subject templates, you have to CreateOrUpdate the stream for every batch in the Write method. That's the cost of dynamically computing the subject. You could cache existing streams to minimize the number of calls to the method though...

@mgale mgale force-pushed the GH-16558/layout-support branch from cca97ad to 4757861 Compare July 7, 2025 22:28
@mgale
Copy link
Contributor Author

mgale commented Jul 7, 2025

@mgale I'm a bit lost here. Why does the first element has to be static if Telegraf is responsible for generating the stream? If we go for subject templates, you have to CreateOrUpdate the stream for every batch in the Write method. That's the cost of dynamically computing the subject. You could cache existing streams to minimize the number of calls to the method though...

You’re right, the first element doesn’t need to be static. I had initially leaned that way for simplicity, but I’ve since refactored the code to better align with dynamic subject handling.

Specifically:
• The code now uses the full subject template directly.
• I have updated the behavior so that only the subjects explicitly listed under outputs.nats.jetstream.subjects are registered to the stream if a dynamic (templated) subject is in use.

This avoids relying on the computed subject during Write to update the stream, which I think is important. I’d prefer to avoid performing stream registration or modification at write time if we can.

Let me know if you’re okay with this approach and constraint. I’m open to alternatives, but I believe this keeps stream creation/update logic clean and predictable.

@mgale mgale requested a review from srebhan July 7, 2025 22:37
Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

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

Thanks for the update @mgale! Much better but too complex. ;-) Please find my suggestions and comments in the code.

@mgale mgale requested a review from srebhan July 9, 2025 05:06
Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

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

Thanks @mgale! This looks much better. Some more comments from my side...

@mgale
Copy link
Contributor Author

mgale commented Jul 11, 2025

@srebhan I believe all feedback has now been addressed. Given the extensive back-and-forth on this PR, I’ve resolved all comments from previous reviews and believe all outstanding items have been covered.
Please feel free to re-open anything I may have missed or raise any remaining concerns. I really appreciate you working through these changes with me.

@mgale mgale requested a review from srebhan July 11, 2025 21:39
Copy link
Member

@srebhan srebhan left a comment

Choose a reason for hiding this comment

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

Thanks @mgale for your patience! The code looks much cleaner now. Some more comments, especially regarding looping over the metrics only once...

@mgale mgale requested a review from srebhan July 18, 2025 16:16
@mgale mgale force-pushed the GH-16558/layout-support branch from b99f25d to fbe1668 Compare July 22, 2025 18:28
@mgale mgale force-pushed the GH-16558/layout-support branch from ec6aa39 to cb9030f Compare July 23, 2025 18:29
@telegraf-tiger
Copy link
Contributor

Download PR build artifacts for linux_amd64.tar.gz, darwin_arm64.tar.gz, and windows_amd64.zip.
Downloads for additional architectures and packages are available below.

☺️ This pull request doesn't significantly change the Telegraf binary size (less than 1%)

📦 Click here to get additional PR build artifacts

Artifact URLs

. DEB . RPM . TAR . GZ . ZIP
[[amd64.deb aarch64.rpm darwin_amd64.tar.gz windows_amd64.zip] [arm64.deb armel.rpm darwin_arm64.tar.gz windows_arm64.zip] [armel.deb armv6hl.rpm freebsd_amd64.tar.gz windows_i386.zip] [armhf.deb i386.rpm freebsd_armv7.tar.gz ] [i386.deb ppc64le.rpm freebsd_i386.tar.gz ] [mips.deb riscv64.rpm linux_amd64.tar.gz ] [mipsel.deb s390x.rpm linux_arm64.tar.gz ] [ppc64el.deb x86_64.rpm linux_armel.tar.gz ] [riscv64.deb linux_armhf.tar.gz ] [s390x.deb linux_i386.tar.gz ] [ linux_mips.tar.gz ] [ linux_mipsel.tar.gz ] [ linux_ppc64le.tar.gz ] [ linux_riscv64.tar.gz ] [ linux_s390x.tar.gz ]]

@mgale
Copy link
Contributor Author

mgale commented Jul 28, 2025

@srebhan I believe all comments have been addressed, let me know if there is further work required on this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feat Improvement on an existing feature such as adding a new setting/mode to an existing plugin plugin/output 1. Request for new output plugins 2. Issues/PRs that are related to out plugins
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add layout option to NATS output plugin
2 participants