0.3.0 unit tests: agent and prometheus
This commit is contained in:
parent
4fdcb136bc
commit
64b98a9b61
|
@ -16,31 +16,36 @@ import (
|
||||||
func TestAgent_LoadPlugin(t *testing.T) {
|
func TestAgent_LoadPlugin(t *testing.T) {
|
||||||
c := config.NewConfig()
|
c := config.NewConfig()
|
||||||
c.PluginFilters = []string{"mysql"}
|
c.PluginFilters = []string{"mysql"}
|
||||||
c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
err := c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
||||||
|
assert.NoError(t, err)
|
||||||
a, _ := NewAgent(c)
|
a, _ := NewAgent(c)
|
||||||
assert.Equal(t, 1, len(a.Config.Plugins))
|
assert.Equal(t, 1, len(a.Config.Plugins))
|
||||||
|
|
||||||
c = config.NewConfig()
|
c = config.NewConfig()
|
||||||
c.PluginFilters = []string{"foo"}
|
c.PluginFilters = []string{"foo"}
|
||||||
c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
||||||
|
assert.NoError(t, err)
|
||||||
a, _ = NewAgent(c)
|
a, _ = NewAgent(c)
|
||||||
assert.Equal(t, 0, len(a.Config.Plugins))
|
assert.Equal(t, 0, len(a.Config.Plugins))
|
||||||
|
|
||||||
c = config.NewConfig()
|
c = config.NewConfig()
|
||||||
c.PluginFilters = []string{"mysql", "foo"}
|
c.PluginFilters = []string{"mysql", "foo"}
|
||||||
c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
||||||
|
assert.NoError(t, err)
|
||||||
a, _ = NewAgent(c)
|
a, _ = NewAgent(c)
|
||||||
assert.Equal(t, 1, len(a.Config.Plugins))
|
assert.Equal(t, 1, len(a.Config.Plugins))
|
||||||
|
|
||||||
c = config.NewConfig()
|
c = config.NewConfig()
|
||||||
c.PluginFilters = []string{"mysql", "redis"}
|
c.PluginFilters = []string{"mysql", "redis"}
|
||||||
c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
||||||
|
assert.NoError(t, err)
|
||||||
a, _ = NewAgent(c)
|
a, _ = NewAgent(c)
|
||||||
assert.Equal(t, 2, len(a.Config.Plugins))
|
assert.Equal(t, 2, len(a.Config.Plugins))
|
||||||
|
|
||||||
c = config.NewConfig()
|
c = config.NewConfig()
|
||||||
c.PluginFilters = []string{"mysql", "foo", "redis", "bar"}
|
c.PluginFilters = []string{"mysql", "foo", "redis", "bar"}
|
||||||
c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
||||||
|
assert.NoError(t, err)
|
||||||
a, _ = NewAgent(c)
|
a, _ = NewAgent(c)
|
||||||
assert.Equal(t, 2, len(a.Config.Plugins))
|
assert.Equal(t, 2, len(a.Config.Plugins))
|
||||||
}
|
}
|
||||||
|
@ -48,37 +53,51 @@ func TestAgent_LoadPlugin(t *testing.T) {
|
||||||
func TestAgent_LoadOutput(t *testing.T) {
|
func TestAgent_LoadOutput(t *testing.T) {
|
||||||
c := config.NewConfig()
|
c := config.NewConfig()
|
||||||
c.OutputFilters = []string{"influxdb"}
|
c.OutputFilters = []string{"influxdb"}
|
||||||
c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
err := c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
||||||
|
assert.NoError(t, err)
|
||||||
a, _ := NewAgent(c)
|
a, _ := NewAgent(c)
|
||||||
assert.Equal(t, 2, len(a.Config.Outputs))
|
assert.Equal(t, 2, len(a.Config.Outputs))
|
||||||
|
|
||||||
|
c = config.NewConfig()
|
||||||
|
c.OutputFilters = []string{"kafka"}
|
||||||
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
a, _ = NewAgent(c)
|
||||||
|
assert.Equal(t, 1, len(a.Config.Outputs))
|
||||||
|
|
||||||
c = config.NewConfig()
|
c = config.NewConfig()
|
||||||
c.OutputFilters = []string{}
|
c.OutputFilters = []string{}
|
||||||
c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
||||||
|
assert.NoError(t, err)
|
||||||
a, _ = NewAgent(c)
|
a, _ = NewAgent(c)
|
||||||
assert.Equal(t, 3, len(a.Config.Outputs))
|
assert.Equal(t, 3, len(a.Config.Outputs))
|
||||||
|
|
||||||
c = config.NewConfig()
|
c = config.NewConfig()
|
||||||
c.OutputFilters = []string{"foo"}
|
c.OutputFilters = []string{"foo"}
|
||||||
c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
||||||
|
assert.NoError(t, err)
|
||||||
a, _ = NewAgent(c)
|
a, _ = NewAgent(c)
|
||||||
assert.Equal(t, 0, len(a.Config.Outputs))
|
assert.Equal(t, 0, len(a.Config.Outputs))
|
||||||
|
|
||||||
c = config.NewConfig()
|
c = config.NewConfig()
|
||||||
c.OutputFilters = []string{"influxdb", "foo"}
|
c.OutputFilters = []string{"influxdb", "foo"}
|
||||||
c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
||||||
|
assert.NoError(t, err)
|
||||||
a, _ = NewAgent(c)
|
a, _ = NewAgent(c)
|
||||||
assert.Equal(t, 2, len(a.Config.Outputs))
|
assert.Equal(t, 2, len(a.Config.Outputs))
|
||||||
|
|
||||||
c = config.NewConfig()
|
c = config.NewConfig()
|
||||||
c.OutputFilters = []string{"influxdb", "kafka"}
|
c.OutputFilters = []string{"influxdb", "kafka"}
|
||||||
c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
||||||
|
assert.NoError(t, err)
|
||||||
|
assert.Equal(t, 3, len(c.Outputs))
|
||||||
a, _ = NewAgent(c)
|
a, _ = NewAgent(c)
|
||||||
assert.Equal(t, 3, len(a.Config.Outputs))
|
assert.Equal(t, 3, len(a.Config.Outputs))
|
||||||
|
|
||||||
c = config.NewConfig()
|
c = config.NewConfig()
|
||||||
c.OutputFilters = []string{"influxdb", "foo", "kafka", "bar"}
|
c.OutputFilters = []string{"influxdb", "foo", "kafka", "bar"}
|
||||||
c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
err = c.LoadConfig("./internal/config/testdata/telegraf-agent.toml")
|
||||||
|
assert.NoError(t, err)
|
||||||
a, _ = NewAgent(c)
|
a, _ = NewAgent(c)
|
||||||
assert.Equal(t, 3, len(a.Config.Outputs))
|
assert.Equal(t, 3, len(a.Config.Outputs))
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,20 +21,13 @@
|
||||||
|
|
||||||
# Tags can also be specified via a normal map, but only one form at a time:
|
# Tags can also be specified via a normal map, but only one form at a time:
|
||||||
[tags]
|
[tags]
|
||||||
# dc = "us-east-1"
|
dc = "us-east-1"
|
||||||
|
|
||||||
# Configuration for telegraf agent
|
# Configuration for telegraf agent
|
||||||
[agent]
|
[agent]
|
||||||
# Default data collection interval for all plugins
|
# Default data collection interval for all plugins
|
||||||
interval = "10s"
|
interval = "10s"
|
||||||
|
|
||||||
# If utc = false, uses local time (utc is highly recommended)
|
|
||||||
utc = true
|
|
||||||
|
|
||||||
# Precision of writes, valid values are n, u, ms, s, m, and h
|
|
||||||
# note: using second precision greatly helps InfluxDB compression
|
|
||||||
precision = "s"
|
|
||||||
|
|
||||||
# run telegraf in debug mode
|
# run telegraf in debug mode
|
||||||
debug = false
|
debug = false
|
||||||
|
|
||||||
|
@ -58,17 +51,6 @@
|
||||||
# The target database for metrics. This database must already exist
|
# The target database for metrics. This database must already exist
|
||||||
database = "telegraf" # required.
|
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"
|
|
||||||
|
|
||||||
[[outputs.influxdb]]
|
[[outputs.influxdb]]
|
||||||
urls = ["udp://localhost:8089"]
|
urls = ["udp://localhost:8089"]
|
||||||
database = "udp-telegraf"
|
database = "udp-telegraf"
|
||||||
|
@ -92,8 +74,8 @@
|
||||||
|
|
||||||
# Read Apache status information (mod_status)
|
# Read Apache status information (mod_status)
|
||||||
[[plugins.apache]]
|
[[plugins.apache]]
|
||||||
# An array of Apache status URI to gather stats.
|
# An array of Apache status URI to gather stats.
|
||||||
urls = ["http://localhost/server-status?auto"]
|
urls = ["http://localhost/server-status?auto"]
|
||||||
|
|
||||||
# Read metrics about cpu usage
|
# Read metrics about cpu usage
|
||||||
[[plugins.cpu]]
|
[[plugins.cpu]]
|
||||||
|
@ -128,8 +110,6 @@ urls = ["http://localhost/server-status?auto"]
|
||||||
|
|
||||||
# Read flattened metrics from one or more commands that output JSON to stdout
|
# Read flattened metrics from one or more commands that output JSON to stdout
|
||||||
[[plugins.exec]]
|
[[plugins.exec]]
|
||||||
# specify commands via an array of tables
|
|
||||||
[[exec.commands]]
|
|
||||||
# the command to run
|
# the command to run
|
||||||
command = "/usr/bin/mycollector --foo=bar"
|
command = "/usr/bin/mycollector --foo=bar"
|
||||||
|
|
||||||
|
@ -148,28 +128,25 @@ urls = ["http://localhost/server-status?auto"]
|
||||||
|
|
||||||
# Read flattened metrics from one or more JSON HTTP endpoints
|
# Read flattened metrics from one or more JSON HTTP endpoints
|
||||||
[[plugins.httpjson]]
|
[[plugins.httpjson]]
|
||||||
# Specify services via an array of tables
|
# a name for the service being polled
|
||||||
[[httpjson.services]]
|
name = "webserver_stats"
|
||||||
|
|
||||||
# a name for the service being polled
|
# URL of each server in the service's cluster
|
||||||
name = "webserver_stats"
|
servers = [
|
||||||
|
"http://localhost:9999/stats/",
|
||||||
|
"http://localhost:9998/stats/",
|
||||||
|
]
|
||||||
|
|
||||||
# URL of each server in the service's cluster
|
# HTTP method to use (case-sensitive)
|
||||||
servers = [
|
method = "GET"
|
||||||
"http://localhost:9999/stats/",
|
|
||||||
"http://localhost:9998/stats/",
|
|
||||||
]
|
|
||||||
|
|
||||||
# HTTP method to use (case-sensitive)
|
# HTTP parameters (all values must be strings)
|
||||||
method = "GET"
|
[httpjson.parameters]
|
||||||
|
event_type = "cpu_spike"
|
||||||
# HTTP parameters (all values must be strings)
|
threshold = "0.75"
|
||||||
[httpjson.services.parameters]
|
|
||||||
event_type = "cpu_spike"
|
|
||||||
threshold = "0.75"
|
|
||||||
|
|
||||||
# Read metrics about disk IO by device
|
# Read metrics about disk IO by device
|
||||||
[[plugins.io]]
|
[[plugins.diskio]]
|
||||||
# no configuration
|
# no configuration
|
||||||
|
|
||||||
# read metrics from a Kafka topic
|
# read metrics from a Kafka topic
|
||||||
|
@ -261,9 +238,6 @@ urls = ["http://localhost/server-status?auto"]
|
||||||
|
|
||||||
# Read metrics from one or many postgresql servers
|
# Read metrics from one or many postgresql servers
|
||||||
[[plugins.postgresql]]
|
[[plugins.postgresql]]
|
||||||
# specify servers via an array of tables
|
|
||||||
[[postgresql.servers]]
|
|
||||||
|
|
||||||
# specify address via a url matching:
|
# specify address via a url matching:
|
||||||
# postgres://[pqgotest[:password]]@localhost[/dbname]?sslmode=[disable|verify-ca|verify-full]
|
# postgres://[pqgotest[:password]]@localhost[/dbname]?sslmode=[disable|verify-ca|verify-full]
|
||||||
# or a simple string:
|
# or a simple string:
|
||||||
|
@ -297,7 +271,6 @@ urls = ["http://localhost/server-status?auto"]
|
||||||
# Read metrics from one or many RabbitMQ servers via the management API
|
# Read metrics from one or many RabbitMQ servers via the management API
|
||||||
[[plugins.rabbitmq]]
|
[[plugins.rabbitmq]]
|
||||||
# Specify servers via an array of tables
|
# Specify servers via an array of tables
|
||||||
[[rabbitmq.servers]]
|
|
||||||
# name = "rmq-server-1" # optional tag
|
# name = "rmq-server-1" # optional tag
|
||||||
# url = "http://localhost:15672"
|
# url = "http://localhost:15672"
|
||||||
# username = "guest"
|
# username = "guest"
|
||||||
|
|
|
@ -3,11 +3,11 @@ package prometheus_client
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/influxdb/influxdb/client/v2"
|
"github.com/influxdb/influxdb/client/v2"
|
||||||
"github.com/influxdb/telegraf/plugins/prometheus"
|
"github.com/influxdb/telegraf/plugins/prometheus"
|
||||||
"github.com/influxdb/telegraf/testutil"
|
"github.com/influxdb/telegraf/testutil"
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var pTesting *PrometheusClient
|
var pTesting *PrometheusClient
|
||||||
|
@ -48,7 +48,8 @@ func TestPrometheusWritePointEmptyTag(t *testing.T) {
|
||||||
|
|
||||||
require.NoError(t, p.Gather(&acc))
|
require.NoError(t, p.Gather(&acc))
|
||||||
for _, e := range expected {
|
for _, e := range expected {
|
||||||
assert.NoError(t, acc.ValidateValue(e.name, e.value))
|
acc.AssertContainsFields(t, "prometheus_"+e.name,
|
||||||
|
map[string]interface{}{"value": e.value})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,7 +89,8 @@ func TestPrometheusWritePointTag(t *testing.T) {
|
||||||
|
|
||||||
require.NoError(t, p.Gather(&acc))
|
require.NoError(t, p.Gather(&acc))
|
||||||
for _, e := range expected {
|
for _, e := range expected {
|
||||||
assert.True(t, acc.CheckTaggedValue(e.name, e.value, tags))
|
acc.AssertContainsFields(t, "prometheus_"+e.name,
|
||||||
|
map[string]interface{}{"value": e.value})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue