Skip to content

Commit

Permalink
Allow users to tell telegraf Agent not to include host tag
Browse files Browse the repository at this point in the history
closes #848
  • Loading branch information
sparrc committed Mar 21, 2016
1 parent 5917a42 commit f543dbb
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [#866](https://github.com/influxdata/telegraf/pull/866): couchbase input plugin. Thanks @ljosa!
- [#789](https://github.com/influxdata/telegraf/pull/789): Support multiple field specification and `field*` in graphite templates. Thanks @chrusty!
- [#762](https://github.com/influxdata/telegraf/pull/762): Nagios parser for the exec plugin. Thanks @titilambert!
- [#848](https://github.com/influxdata/telegraf/issues/848): Provide option to omit host tag from telegraf agent.

### Bugfixes
- [#890](https://github.com/influxdata/telegraf/issues/890): Create TLS config even if only ssl_ca is provided.
Expand Down
16 changes: 9 additions & 7 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,19 @@ func NewAgent(config *config.Config) (*Agent, error) {
Config: config,
}

if a.Config.Agent.Hostname == "" {
hostname, err := os.Hostname()
if err != nil {
return nil, err
if !a.Config.Agent.OmitHostname {
if a.Config.Agent.Hostname == "" {
hostname, err := os.Hostname()
if err != nil {
return nil, err
}

a.Config.Agent.Hostname = hostname
}

a.Config.Agent.Hostname = hostname
config.Tags["host"] = a.Config.Agent.Hostname
}

config.Tags["host"] = a.Config.Agent.Hostname

return a, nil
}

Expand Down
11 changes: 10 additions & 1 deletion agent/agent_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package agent

import (
"github.com/stretchr/testify/assert"
"testing"
"time"

Expand All @@ -11,8 +10,18 @@ import (
_ "github.com/influxdata/telegraf/plugins/inputs/all"
// needing to load the outputs
_ "github.com/influxdata/telegraf/plugins/outputs/all"

"github.com/stretchr/testify/assert"
)

func TestAgent_OmitHostname(t *testing.T) {
c := config.NewConfig()
c.Agent.OmitHostname = true
_, err := NewAgent(c)
assert.NoError(t, err)
assert.NotContains(t, c.Tags, "host")
}

func TestAgent_LoadPlugin(t *testing.T) {
c := config.NewConfig()
c.InputFilters = []string{"mysql"}
Expand Down
7 changes: 5 additions & 2 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,9 @@ type AgentConfig struct {
Debug bool

// Quiet is the option for running in quiet mode
Quiet bool
Hostname string
Quiet bool
Hostname string
OmitHostname bool
}

// Inputs returns a list of strings of the configured inputs.
Expand Down Expand Up @@ -183,6 +184,8 @@ var header = `# Telegraf Configuration
quiet = false
## Override default hostname, if empty use os.Hostname()
hostname = ""
## If set to true, do no set the "host" tag in the telegraf agent.
omit_hostname = false
#
Expand Down

0 comments on commit f543dbb

Please sign in to comment.