From 8afbad93c6bfe2a2d52e82a1071da419facb730e Mon Sep 17 00:00:00 2001 From: Cameron Sparr Date: Tue, 25 Aug 2015 17:59:12 -0600 Subject: [PATCH] Outputs enhancement to require Description and SampleConfig functions Closes #142 --- CHANGELOG.md | 1 + config.go | 98 +++++++++++++++++++----------------- outputs/datadog/datadog.go | 16 ++++++ outputs/influxdb/influxdb.go | 27 ++++++++++ outputs/registry.go | 2 + 5 files changed, 97 insertions(+), 47 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 068f65f44..2f669de0e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ - [#136](https://github.com/influxdb/telegraf/issues/136): Add a -usage flag for printing usage of a single plugin. - [#137](https://github.com/influxdb/telegraf/issues/137): Memcached: fix when a value contains a space - [#138](https://github.com/influxdb/telegraf/issues/138): MySQL server address tag. +- [#142](https://github.com/influxdb/telegraf/pull/142): Add Description and SampleConfig funcs to output interface ### Bugfixes - [#128](https://github.com/influxdb/telegraf/issues/128): system_load measurement missing. diff --git a/config.go b/config.go index fe47c6d0d..5eb22ffe6 100644 --- a/config.go +++ b/config.go @@ -8,6 +8,7 @@ import ( "strings" "time" + "github.com/influxdb/telegraf/outputs" "github.com/influxdb/telegraf/plugins" "github.com/naoina/toml" "github.com/naoina/toml/ast" @@ -326,9 +327,6 @@ type hasDescr interface { var header = `# Telegraf configuration -# If this file is missing an [agent] section, you must first generate a -# valid config with 'telegraf -sample-config > telegraf.toml' - # Telegraf is entirely plugin driven. All metrics are gathered from the # declared plugins. @@ -348,40 +346,28 @@ 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 -[outputs.influxdb] -# The full HTTP endpoint URL for your InfluxDB instance -url = "http://localhost:8086" # required. - -# The target database for metrics. This database must already exist -database = "telegraf" # required. - -# Connection timeout (for the connection with InfluxDB), formatted as a string. -# Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". -# If not provided, will default to 0 (no timeout) -# timeout = "5s" - -# username = "telegraf" -# password = "metricsmetricsmetricsmetrics" - -# 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] -# interval = "10s" -# debug = false -# hostname = "prod3241" +[agent] + # interval = "10s" + # debug = false + # hostname = "prod3241" -# PLUGINS +############################################################################### +# OUTPUTS # +############################################################################### + +[outputs] +` + +var header2 = ` + +############################################################################### +# PLUGINS # +############################################################################### ` @@ -389,34 +375,52 @@ database = "telegraf" # required. func PrintSampleConfig() { fmt.Printf(header) - var names []string + // Print Outputs + var onames []string - for name := range plugins.Plugins { - names = append(names, name) + for oname := range outputs.Outputs { + onames = append(onames, oname) + } + sort.Strings(onames) + + for _, oname := range onames { + creator := outputs.Outputs[oname] + output := creator() + + fmt.Printf("\n# %s\n[outputs.%s]\n", output.Description(), oname) + + config := output.SampleConfig() + if config == "" { + fmt.Printf(" # no configuration\n\n") + } else { + fmt.Printf(config) + } } - sort.Strings(names) + fmt.Printf(header2) - for _, name := range names { - creator := plugins.Plugins[name] + // Print Plugins + var pnames []string + for pname := range plugins.Plugins { + pnames = append(pnames, pname) + } + sort.Strings(pnames) + + for _, pname := range pnames { + creator := plugins.Plugins[pname] plugin := creator() - fmt.Printf("# %s\n[%s]\n", plugin.Description(), name) - - var config string - - config = strings.TrimSpace(plugin.SampleConfig()) + fmt.Printf("# %s\n[%s]\n", plugin.Description(), pname) + config := plugin.SampleConfig() if config == "" { - fmt.Printf(" # no configuration\n\n") + fmt.Printf(" # no configuration\n\n") } else { - fmt.Printf("\n") lines := strings.Split(config, "\n") for _, line := range lines { fmt.Printf("%s\n", line) } - fmt.Printf("\n") } } diff --git a/outputs/datadog/datadog.go b/outputs/datadog/datadog.go index b79d1c828..627ba125e 100644 --- a/outputs/datadog/datadog.go +++ b/outputs/datadog/datadog.go @@ -21,6 +21,14 @@ type Datadog struct { client *http.Client } +var sampleConfig = ` + # Datadog API key + apikey = "my-secret-key" # required. + + # Connection timeout. + # timeout = "5s" +` + type TimeSeries struct { Series []*Metric `json:"series"` } @@ -91,6 +99,14 @@ func (d *Datadog) Write(bp client.BatchPoints) error { return nil } +func (d *Datadog) SampleConfig() string { + return sampleConfig +} + +func (d *Datadog) Description() string { + return "Configuration for DataDog API to send metrics to." +} + func (d *Datadog) authenticatedUrl() string { q := url.Values{ "api_key": []string{d.Apikey}, diff --git a/outputs/influxdb/influxdb.go b/outputs/influxdb/influxdb.go index dd6546928..5bb74b4e3 100644 --- a/outputs/influxdb/influxdb.go +++ b/outputs/influxdb/influxdb.go @@ -22,6 +22,25 @@ type InfluxDB struct { conn *client.Client } +var sampleConfig = ` + # The full HTTP endpoint URL for your InfluxDB instance + url = "http://localhost:8086" # required. + + # The target database for metrics. This database must already exist + database = "telegraf" # required. + + # Connection timeout (for the connection with InfluxDB), formatted as a string. + # Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h". + # If not provided, will default to 0 (no timeout) + # timeout = "5s" + + # username = "telegraf" + # password = "metricsmetricsmetricsmetrics" + + # Set the user agent for the POSTs (can be useful for log differentiation) + # user_agent = "telegraf" +` + func (i *InfluxDB) Connect() error { u, err := url.Parse(i.URL) if err != nil { @@ -57,6 +76,14 @@ func (i *InfluxDB) Close() error { return nil } +func (i *InfluxDB) SampleConfig() string { + return sampleConfig +} + +func (i *InfluxDB) Description() string { + return "Configuration for influxdb server to send metrics to" +} + func (i *InfluxDB) Write(bp client.BatchPoints) error { bp.Database = i.Database if _, err := i.conn.Write(bp); err != nil { diff --git a/outputs/registry.go b/outputs/registry.go index a2f22f73b..92ce2b34e 100644 --- a/outputs/registry.go +++ b/outputs/registry.go @@ -7,6 +7,8 @@ import ( type Output interface { Connect() error Close() error + Description() string + SampleConfig() string Write(client.BatchPoints) error }