Skip to content

Commit

Permalink
move tags to influxdb struct, update all sample configs
Browse files Browse the repository at this point in the history
  • Loading branch information
jipperinbham committed Aug 7, 2015
1 parent 48c10f9 commit 91f6c4b
Show file tree
Hide file tree
Showing 9 changed files with 19 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pkg/
tivan
.vagrant
telegraf
11 changes: 1 addition & 10 deletions agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,12 @@ func NewAgent(config *Config) (*Agent, error) {
agent.Hostname = hostname
}

if config.Tags == nil {
config.Tags = map[string]string{}
}

config.Tags["host"] = agent.Hostname

return agent, nil
}

func (a *Agent) Connect() error {
for _, o := range a.outputs {
err := o.output.Connect()
err := o.output.Connect(a.Hostname)
if err != nil {
return err
}
Expand Down Expand Up @@ -157,7 +151,6 @@ func (a *Agent) crankParallel() error {
close(points)

var bp BatchPoints
bp.Tags = a.Config.Tags
bp.Time = time.Now()

for sub := range points {
Expand All @@ -181,7 +174,6 @@ func (a *Agent) crank() error {
}
}

acc.Tags = a.Config.Tags
acc.Time = time.Now()

return a.flush(&acc)
Expand All @@ -202,7 +194,6 @@ func (a *Agent) crankSeparate(shutdown chan struct{}, plugin *runningPlugin) err
return err
}

acc.Tags = a.Config.Tags
acc.Time = time.Now()

err = a.flush(&acc)
Expand Down
6 changes: 1 addition & 5 deletions cmd/telegraf/telegraf.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func main() {
close(shutdown)
}()

log.Print("InfluxDB Agent running")
log.Print("Telegraf Agent running")
log.Printf("Loaded outputs: %s", strings.Join(outputs, " "))
log.Printf("Loaded plugins: %s", strings.Join(plugins, " "))
if ag.Debug {
Expand All @@ -111,10 +111,6 @@ func main() {
ag.Interval, ag.Debug, ag.Hostname)
}

if len(outputs) > 0 {
log.Printf("Tags enabled: %v", config.ListTags())
}

if *fPidfile != "" {
f, err := os.Create(*fPidfile)
if err != nil {
Expand Down
32 changes: 5 additions & 27 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@ func (d *Duration) UnmarshalTOML(b []byte) error {
// will be logging to, as well as all the plugins that the user has
// specified
type Config struct {
Tags map[string]string

agent *ast.Table
plugins map[string]*ast.Table
outputs map[string]*ast.Table
Expand Down Expand Up @@ -200,7 +198,6 @@ func LoadConfig(path string) (*Config, error) {
}

c := &Config{
Tags: make(map[string]string),
plugins: make(map[string]*ast.Table),
outputs: make(map[string]*ast.Table),
}
Expand All @@ -214,10 +211,6 @@ func LoadConfig(path string) (*Config, error) {
switch name {
case "agent":
c.agent = subtbl
case "tags":
if err := toml.UnmarshalTable(subtbl, c.Tags); err != nil {
return nil, errInvalidConfig
}
case "outputs":
for outputName, outputVal := range subtbl.Fields {
outputSubtbl, ok := outputVal.(*ast.Table)
Expand All @@ -234,20 +227,6 @@ func LoadConfig(path string) (*Config, error) {
return c, nil
}

// ListTags returns a string of tags specified in the config,
// line-protocol style
func (c *Config) ListTags() string {
var tags []string

for k, v := range c.Tags {
tags = append(tags, fmt.Sprintf("%s=%s", k, v))
}

sort.Strings(tags)

return strings.Join(tags, " ")
}

type hasConfig interface {
BasicConfig() string
}
Expand Down Expand Up @@ -280,8 +259,11 @@ var header = `# Telegraf configuration
# NOTE: The configuration has a few required parameters. They are marked
# with 'required'. Be sure to edit those to make this configuration work.
# OUTPUTS
[outputs]
# Configuration for influxdb server to send metrics to
[influxdb]
[outputs.influxdb]
# The full HTTP endpoint URL for your InfluxDB instance
url = "http://localhost:8086" # required.
Expand All @@ -298,12 +280,8 @@ database = "telegraf" # required.
# Set the user agent for the POSTs (can be useful for log differentiation)
# user_agent = "telegraf"
# tags = { "dc": "us-east-1" }
# Tags can also be specified via a normal map, but only one form at a time:
# [influxdb.tags]
# dc = "us-east-1"
# tags = { "dc" = "us-east-1" }
# Configuration for telegraf itself
# [agent]
Expand Down
4 changes: 1 addition & 3 deletions etc/config.sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ database = "telegraf" # required.
# Set the user agent for the POSTs (can be useful for log differentiation)
# user_agent = "telegraf"

# Tags can also be specified via a normal map, but only one form at a time:
# [tags]
# dc = "us-east-1"
# tags = { "dc" = "us-east-1" }

# Configuration for telegraf itself
# [agent]
Expand Down
9 changes: 8 additions & 1 deletion outputs/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ type InfluxDB struct {
Password string
Database string
UserAgent string
Tags map[string]string

conn *client.Client
}

func (i *InfluxDB) Connect() error {
func (i *InfluxDB) Connect(host string) error {
u, err := url.Parse(i.URL)
if err != nil {
return err
Expand All @@ -34,12 +35,18 @@ func (i *InfluxDB) Connect() error {
return err
}

if i.Tags == nil {
i.Tags = make(map[string]string)
}
i.Tags["host"] = host

i.conn = c
return nil
}

func (i *InfluxDB) Write(bp client.BatchPoints) error {
bp.Database = i.Database
bp.Tags = i.Tags
if _, err := i.conn.Write(bp); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion outputs/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
)

type Output interface {
Connect() error
Connect(string) error
Write(client.BatchPoints) error
}

Expand Down
Binary file removed telegraf
Binary file not shown.
4 changes: 1 addition & 3 deletions testdata/influx.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ url = "http://localhost:8086"
username = "root"
password = "root"
database = "telegraf"

[tags]
dc = "us-phx-1"
tags = { "dc" = "us-phx-1" }

[redis]
address = ":6379"

0 comments on commit 91f6c4b

Please sign in to comment.