Skip to content

fix(agent): watch for changes in configuration files in config directories#10379

Closed
conorevans wants to merge 6 commits intoinfluxdata:masterfrom
conorevans:conorevans/telegraf9985-watch-configs-in-directories
Closed

fix(agent): watch for changes in configuration files in config directories#10379
conorevans wants to merge 6 commits intoinfluxdata:masterfrom
conorevans:conorevans/telegraf9985-watch-configs-in-directories

Conversation

@conorevans
Copy link
Copy Markdown
Contributor

@conorevans conorevans commented Jan 5, 2022

Signed-off-by: Conor Evans coevans@tcd.ie

Required for all PRs:

  • Updated associated README.md. [I don't think this is necessary]
  • Wrote appropriate unit tests. [TODO]
  • Pull request title or commits are in conventional commit format [when I do tests and stuff I will squash the commits into one that follows this convention.

resolves #9985

The walkfn is virtually identical to config.LoadDirectory() - I chose duplication over abstraction, as I think the scope for the latter is limited and undesirable.

@telegraf-tiger
Copy link
Copy Markdown
Contributor

telegraf-tiger bot commented Jan 5, 2022

Thanks so much for the pull request!
🤝 ✒️ Just a reminder that the CLA has not yet been signed, and we'll need it before merging. Please sign the CLA when you get a chance, then post a comment here saying !signed-cla

Signed-off-by: Conor Evans <coevans@tcd.ie>
@conorevans
Copy link
Copy Markdown
Contributor Author

!signed-cla


// configDirFiles returns a slice made up of filepaths correspoding
// to config files defined in any of the config directories.
func configDirFiles() sliceFlags {
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think it would probably make more sense for this to return []string rather than sliceFlags as in this context it's not to do with the flags.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think so too. I don't see any place where the existing sliceFlags methods (String and Set) are called. It looks like they may be left over from a previous refactor. Let's get rid of them.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

sliceFlags is used because of flag.Var so I think it will break stuff when removed..

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right @Hipska, good catch! I will fix this in the upcoming week (have got a new laptop without env fully set up)

}

if info.IsDir() {
// skip Kubernetes mounts, preventing loading the same config twice
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I didn't remove this check. I don't immediately know if it also applies in this context, so I have to look a bit more. If it is necessary, the comment should probably be updated anyway. Happy for you to weigh in.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think in this case since we're looking only for regular files, we should skip all directories (not just "..").

If there's a directory called "xyz.conf" we wouldn't want the function to append it to configs and return it.

@sjwang90
Copy link
Copy Markdown
Contributor

@conorevans This PR is still named draft. Is this ready for review?

@conorevans
Copy link
Copy Markdown
Contributor Author

Hi @sjwang90 as an external contributor, I marked it as draft to show it needed some early feedback (in case the approach I took was terrible). I left some comments as to the things I though maintainers might like to comment on, but it's remained inactive. I can remove the draft label if that would make it more obvious that it needed feedback?

@sjwang90
Copy link
Copy Markdown
Contributor

Yeah - go ahead and remove the draft title and I'll mention it to the maintainers to do a review.

@Hipska Hipska added area/agent fix pr to fix corresponding bug labels Apr 12, 2022
@Hipska Hipska changed the title draft: watch configs that are defined in config directories fix(agent): watch configs that are defined in config directories Apr 12, 2022
@iarkhanhelsky
Copy link
Copy Markdown
Contributor

@sjwang90 @Hipska @conorevans Hi all! Any chance to make this PR merged? I'll be glad to help if any help is needed.

@conorevans
Copy link
Copy Markdown
Contributor Author

Hi @iarkhanhelsky thanks to @sjwang90 and @reimda pushing this forward I will look to make their requested changes and update this.

Signed-off-by: Conor Evans <coevans@tcd.ie>
Copy link
Copy Markdown
Contributor Author

@conorevans conorevans left a comment

Choose a reason for hiding this comment

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

hi sorry for delay my laptop is pretty terrible atm and was not having a fun time trying to fix git state (i wanted to squash my commits, hence the lazy last commit message)

Signed-off-by: Conor Evans <coevans@tcd.ie>
@shruthikudlur
Copy link
Copy Markdown

Does this code also watch for new files added into the dierctory?

@reimda reimda 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 Aug 11, 2022
@reimda
Copy link
Copy Markdown
Contributor

reimda commented Aug 11, 2022

Does this code also watch for new files added into the dierctory?

It only watches the files that were in the directory when telegraf loads its config. You would need to restart or sighup telegraf to watch new files.

@sspaink
Copy link
Copy Markdown
Contributor

sspaink commented Aug 15, 2022

@conorevans If you fix the failing tests we can get this merged, thank you for your hard work!

It is failing with: cmd/telegraf/telegraf.go:388:11: cannot use &fConfigs (value of type *[]string) as flag.Value value in argument to flag.Var: *[]string does not implement flag.Value (missing method Set)

Looks like we need to keep the fConfigs to be sliceFlags and not []strings

@conorevans conorevans requested review from Hipska and reimda August 15, 2022 19:32
@conorevans
Copy link
Copy Markdown
Contributor Author

Hi @reimda @sspaink @Hipska this should be good now 🤞

@telegraf-tiger
Copy link
Copy Markdown
Contributor

@reimda reimda changed the title fix(agent): watch configs that are defined in config directories fix(agent): watch for changes in configuration files in config directories Aug 17, 2022
return nil
}

if err := filepath.Walk(dir, walkfn); err != nil {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@srebhan and I reviewed this and had the thought that walking the config dir here might not get the same list of configuration files as the code in config/config.go that initially loaded the files.

Instead of walking the path, could we change Config.LoadConfig to save the list of files? Then we know exactly which files to watch and there's no chance for difference or for a race condition. (Like if a file is added to the dir right after Config.LoadDirectory completes but before this code runs)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's a good point. I don't think I quite have the bandwidth to implement that change - would a Telegraf maintainer be able to take over?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@conorevans thank you for working on this, it seems another community member has helped take this over: #12127 if you have time could you check if this change works for you by using the artifacts posted by the bot? thanks!

@MyaLongmire MyaLongmire removed 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 Aug 22, 2022
@srebhan srebhan added the help wanted Request for community participation, code, contribution label Sep 20, 2022
@reimda reimda added the size/s 1 day effort, great beginniner issue label Sep 22, 2022
@sspaink
Copy link
Copy Markdown
Contributor

sspaink commented Nov 3, 2022

Closing as this PR has been replaced by: #12127

@sspaink sspaink closed this Nov 3, 2022
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 help wanted Request for community participation, code, contribution size/s 1 day effort, great beginniner issue

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Telegraf --watch-config doesn't support directories

9 participants