Skip to content

Commit

Permalink
Outputs enhancement to require Description and SampleConfig functions
Browse files Browse the repository at this point in the history
Closes #142
  • Loading branch information
sparrc committed Aug 26, 2015
1 parent 846fd31 commit a00510a
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 45 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
94 changes: 49 additions & 45 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.
Expand All @@ -348,75 +346,81 @@ 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"
# Tags can also be specified via a normal map, but only one form at a time:
[tags]
# dc = "us-east-1"
# Set the user agent for the POSTs (can be useful for log differentiation)
# user_agent = "telegraf"
# Configuration for telegraf itself
[agent]
# interval = "10s"
# debug = false
# hostname = "prod3241"
# Tags can also be specified via a normal map, but only one form at a time:
###############################################################################
# OUTPUTS #
###############################################################################
# [tags]
# dc = "us-east-1" }
[outputs]
`

# Configuration for telegraf itself
# [agent]
# interval = "10s"
# debug = false
# hostname = "prod3241"
var header2 = `
# PLUGINS
###############################################################################
# PLUGINS #
###############################################################################
`

// PrintSampleConfig prints the sample config!
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)

sort.Strings(names)
for _, oname := range onames {
creator := outputs.Outputs[oname]
output := creator()

for _, name := range names {
creator := plugins.Plugins[name]
fmt.Printf("\n# %s\n[outputs.%s]\n", output.Description(), oname)

plugin := creator()
config := output.SampleConfig()
if config == "" {
fmt.Printf(" # no configuration\n\n")
} else {
fmt.Printf(config)
}
}

fmt.Printf("# %s\n[%s]\n", plugin.Description(), name)
fmt.Printf(header2)

var config string
// Print Plugins
var pnames []string

config = strings.TrimSpace(plugin.SampleConfig())
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(), 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")
}
}
Expand Down
16 changes: 16 additions & 0 deletions outputs/datadog/datadog.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
Expand Down Expand Up @@ -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},
Expand Down
27 changes: 27 additions & 0 deletions outputs/influxdb/influxdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 2 additions & 0 deletions outputs/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
type Output interface {
Connect() error
Close() error
Description() string
SampleConfig() string
Write(client.BatchPoints) error
}

Expand Down

0 comments on commit a00510a

Please sign in to comment.