feat(inputs.example): Add secret-store sample code (#12550)

This commit is contained in:
Thomas Casteleyn 2023-02-02 14:14:47 +01:00 committed by GitHub
parent 177ce5eea3
commit 0ade9e1a88
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 0 deletions

View File

@ -29,6 +29,10 @@ type Example struct {
// Example of passing a duration option allowing the format of e.g. "100ms", "5m" or "1h"
Timeout config.Duration `toml:"timeout"`
// Example of passing a password/token/username or other sensitive data with the secret-store
UserName config.Secret `toml:"username"`
Password config.Secret `toml:"password"`
// Telegraf logging facility
// The exact name is important to allow automatic initialization by telegraf.
Log telegraf.Logger `toml:"-"`
@ -56,6 +60,19 @@ func (m *Example) Init() error {
m.NumberFields = 2
}
// Check using the secret-store
if m.UserName.Empty() {
// For example, use a default value
m.Log.Debug("using default username")
}
// Retrieve credentials using the secret-store
password, err := m.Password.Get()
if err != nil {
return fmt.Errorf("getting password failed: %w", err)
}
defer config.ReleaseSecret(password)
// Initialze your internal states
m.count = 1