Skip to content

fix(agent): Skip output buffer initialization in test mode#18918

Merged
skartikey merged 1 commit into
influxdata:masterfrom
WZH8898:fix-test-disk-buffer-init
May 19, 2026
Merged

fix(agent): Skip output buffer initialization in test mode#18918
skartikey merged 1 commit into
influxdata:masterfrom
WZH8898:fix-test-disk-buffer-init

Conversation

@WZH8898
Copy link
Copy Markdown
Contributor

@WZH8898 WZH8898 commented May 15, 2026

Summary

This PR updates telegraf --test and --test-wait to avoid initializing runtime output buffers during config loading, addressing issue #18917.

In test mode, outputs are not used. However, Telegraf still initialized output buffers while loading the configuration. When buffer_strategy = "disk" was configured, this attempted to create/open the disk-backed WAL under buffer_directory, causing telegraf --test to fail if the current user did not have write access to that directory.

This PR marks config loading as test mode for --test and --test-wait. Output configs still parse normally, including serializers, but runtime output buffer initialization is skipped for parsed-but-unused outputs in test mode.

This PR also adds an internal discard buffer for outputs parsed in test mode.

Tests added in this PR cover:

  • Config parsing in test mode with an unusable disk buffer path
  • CLI config loading in test mode
  • Serializer parsing in test mode
  • Ensuring disk-backed output buffers are not initialized during test-mode config loading

Checklist

  • No AI generated code was used in this PR
  • AI generated code used in this PR follows the InfluxData Policy on AI-Generated Code Contributions

Related issues

resolves #18917

@telegraf-tiger telegraf-tiger Bot added the fix pr to fix corresponding bug label May 15, 2026
@WZH8898 WZH8898 force-pushed the fix-test-disk-buffer-init branch 2 times, most recently from e9464ee to 4bc0ec7 Compare May 15, 2026 08:31
Copy link
Copy Markdown
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 @WZH8898 for tackling this issue! I do have some comments in the code... Furthermore, I really like the idea of creating a "discard buffer" for test-runs of Telegraf! Can we please

  1. Add a function to buffer.go to just test the buffer settings and do nothing else?
  2. Simply override the buffer_strategy in config and avoid adding all those flags?

With this implemented, the config could (in test-mode) first check the buffer settings using the function of item 1 and then override the buffer_strategy with "discard"...

Comment thread cmd/telegraf/telegraf_config_test.go Outdated
Comment on lines +27 to +35
[agent]
buffer_strategy = "disk"
buffer_directory = ` + strconv.Quote(bufferDirectory) + `

[[inputs.cpu]]

[[outputs.influxdb]]
urls = ["http://localhost:8086"]
database = "telegraf"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about moving this to a file in the testdata directory instead of writing it somewhere in every test?

Comment thread cmd/telegraf/telegraf_config_test.go Outdated
Comment on lines +47 to +52
agent = &Telegraf{
GlobalFlags: GlobalFlags{
config: []string{configFile},
test: true,
},
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we reuse the agent here and just set test?

Comment thread config/config_test.go Outdated
bufferDirectory := filepath.Join(root, "buffer")
require.NoError(t, os.WriteFile(bufferDirectory, []byte("not a directory"), 0600))

cfg := []byte(fmt.Sprintf(`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Same as above. Please create a real file in testdata instead of writing it everytime.

Comment thread models/buffer.go Outdated
Comment on lines +180 to +213

type discardBuffer struct {
BufferStats
}

func newDiscardBuffer(stats BufferStats) *discardBuffer {
return &discardBuffer{BufferStats: stats}
}

func (*discardBuffer) Len() int {
return 0
}

func (b *discardBuffer) Add(metrics ...telegraf.Metric) int {
for _, m := range metrics {
b.metricDropped(m)
}
return len(metrics)
}

func (*discardBuffer) BeginTransaction(int) *Transaction {
return &Transaction{}
}

func (*discardBuffer) EndTransaction(*Transaction) {
}

func (b *discardBuffer) Stats() BufferStats {
return b.BufferStats
}

func (*discardBuffer) Close() error {
return nil
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Please move this to its own buffer_discard.go file to follow the pattern of the other implementations!

@srebhan srebhan self-assigned this May 19, 2026
@srebhan srebhan added area/agent plugin/output 1. Request for new output plugins 2. Issues/PRs that are related to out plugins labels May 19, 2026
@WZH8898 WZH8898 force-pushed the fix-test-disk-buffer-init branch from 4bc0ec7 to 38af8fe Compare May 19, 2026 11:04
@telegraf-tiger
Copy link
Copy Markdown
Contributor

Copy link
Copy Markdown
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.

Awesome! Thanks @WZH8898!

@srebhan srebhan added the ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review. label May 19, 2026
@srebhan srebhan assigned skartikey and unassigned srebhan May 19, 2026
Copy link
Copy Markdown
Contributor

@skartikey skartikey left a comment

Choose a reason for hiding this comment

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

@WZH8898 Thanks for the contribution!

@skartikey skartikey merged commit a084d92 into influxdata:master May 19, 2026
27 checks passed
@github-actions github-actions Bot added this to the v1.38.5 milestone May 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent fix pr to fix corresponding bug plugin/output 1. Request for new output plugins 2. Issues/PRs that are related to out plugins ready for final review This pull request has been reviewed and/or tested by multiple users and is ready for a final review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

telegraf --test fails to load config when buffer_strategy = "disk" and the running user lacks write access to buffer_directory

3 participants