* separate hello and authenticate functions, force connection close at end of write cycle so we don't hold open idle connections, which has the benefit of mostly removing the chance of getting hopelessly connection lost * update changelog, though this will need to be updated again to merge into telegraf master * bump instrumental agent version * fix test to deal with better better connect/reconnect logic and changed ident & auth handshake * Update CHANGELOG.md correct URL from instrumental fork to origin and put the change in the correct part of the file * go fmt * Split out Instrumental tests for invalid metric and value. * Ensure nothing remains on the wire after final test. * Force valid metric names by replacing invalid parts with underscores. * Multiple invalid characters being joined into a single udnerscore. * Adjust comment to what happens. * undo split hello and auth commands, to reduce roundtrips * Add ignored_databases option to postgresql configuration files, to enable easy filtering of system databases without needing to whitelist all the databases on the server. Add tests for database whitelist and blacklist. * run go fmt on new postgresql database whitelist/blacklist code * add postgresql database blacklist option to changelog * remove a bad merge from the changelog
225 lines
4.5 KiB
Go
225 lines
4.5 KiB
Go
package postgresql
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestPostgresqlGeneratesMetrics(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("Skipping integration test in short mode")
|
|
}
|
|
|
|
p := &Postgresql{
|
|
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
|
|
testutil.GetLocalHost()),
|
|
Databases: []string{"postgres"},
|
|
}
|
|
|
|
var acc testutil.Accumulator
|
|
err := p.Gather(&acc)
|
|
require.NoError(t, err)
|
|
|
|
availableColumns := make(map[string]bool)
|
|
for _, col := range p.AllColumns {
|
|
availableColumns[col] = true
|
|
}
|
|
intMetrics := []string{
|
|
"xact_commit",
|
|
"xact_rollback",
|
|
"blks_read",
|
|
"blks_hit",
|
|
"tup_returned",
|
|
"tup_fetched",
|
|
"tup_inserted",
|
|
"tup_updated",
|
|
"tup_deleted",
|
|
"conflicts",
|
|
"temp_files",
|
|
"temp_bytes",
|
|
"deadlocks",
|
|
"numbackends",
|
|
"buffers_alloc",
|
|
"buffers_backend",
|
|
"buffers_backend_fsync",
|
|
"buffers_checkpoint",
|
|
"buffers_clean",
|
|
"checkpoints_req",
|
|
"checkpoints_timed",
|
|
"maxwritten_clean",
|
|
}
|
|
|
|
floatMetrics := []string{
|
|
"blk_read_time",
|
|
"blk_write_time",
|
|
}
|
|
|
|
metricsCounted := 0
|
|
|
|
for _, metric := range intMetrics {
|
|
_, ok := availableColumns[metric]
|
|
if ok {
|
|
assert.True(t, acc.HasIntField("postgresql", metric))
|
|
metricsCounted++
|
|
}
|
|
}
|
|
|
|
for _, metric := range floatMetrics {
|
|
_, ok := availableColumns[metric]
|
|
if ok {
|
|
assert.True(t, acc.HasFloatField("postgresql", metric))
|
|
metricsCounted++
|
|
}
|
|
}
|
|
|
|
assert.True(t, metricsCounted > 0)
|
|
//assert.Equal(t, len(availableColumns)-len(p.IgnoredColumns()), metricsCounted)
|
|
}
|
|
|
|
func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("Skipping integration test in short mode")
|
|
}
|
|
|
|
p := &Postgresql{
|
|
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
|
|
testutil.GetLocalHost()),
|
|
Databases: []string{"postgres"},
|
|
}
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := p.Gather(&acc)
|
|
require.NoError(t, err)
|
|
|
|
point, ok := acc.Get("postgresql")
|
|
require.True(t, ok)
|
|
|
|
assert.Equal(t, "postgres", point.Tags["db"])
|
|
}
|
|
|
|
func TestPostgresqlDefaultsToAllDatabases(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("Skipping integration test in short mode")
|
|
}
|
|
|
|
p := &Postgresql{
|
|
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
|
|
testutil.GetLocalHost()),
|
|
}
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := p.Gather(&acc)
|
|
require.NoError(t, err)
|
|
|
|
var found bool
|
|
|
|
for _, pnt := range acc.Metrics {
|
|
if pnt.Measurement == "postgresql" {
|
|
if pnt.Tags["db"] == "postgres" {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
}
|
|
|
|
assert.True(t, found)
|
|
}
|
|
|
|
func TestPostgresqlIgnoresUnwantedColumns(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("Skipping integration test in short mode")
|
|
}
|
|
|
|
p := &Postgresql{
|
|
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
|
|
testutil.GetLocalHost()),
|
|
}
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := p.Gather(&acc)
|
|
require.NoError(t, err)
|
|
|
|
for col := range p.IgnoredColumns() {
|
|
assert.False(t, acc.HasMeasurement(col))
|
|
}
|
|
}
|
|
|
|
func TestPostgresqlDatabaseWhitelistTest(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("Skipping integration test in short mode")
|
|
}
|
|
|
|
p := &Postgresql{
|
|
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
|
|
testutil.GetLocalHost()),
|
|
Databases: []string{"template0"},
|
|
}
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := p.Gather(&acc)
|
|
require.NoError(t, err)
|
|
|
|
var foundTemplate0 = false
|
|
var foundTemplate1 = false
|
|
|
|
for _, pnt := range acc.Metrics {
|
|
if pnt.Measurement == "postgresql" {
|
|
if pnt.Tags["db"] == "template0" {
|
|
foundTemplate0 = true
|
|
}
|
|
}
|
|
if pnt.Measurement == "postgresql" {
|
|
if pnt.Tags["db"] == "template1" {
|
|
foundTemplate1 = true
|
|
}
|
|
}
|
|
}
|
|
|
|
assert.True(t, foundTemplate0)
|
|
assert.False(t, foundTemplate1)
|
|
}
|
|
|
|
func TestPostgresqlDatabaseBlacklistTest(t *testing.T) {
|
|
if testing.Short() {
|
|
t.Skip("Skipping integration test in short mode")
|
|
}
|
|
|
|
p := &Postgresql{
|
|
Address: fmt.Sprintf("host=%s user=postgres sslmode=disable",
|
|
testutil.GetLocalHost()),
|
|
IgnoredDatabases: []string{"template0"},
|
|
}
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err := p.Gather(&acc)
|
|
require.NoError(t, err)
|
|
|
|
var foundTemplate0 = false
|
|
var foundTemplate1 = false
|
|
|
|
for _, pnt := range acc.Metrics {
|
|
if pnt.Measurement == "postgresql" {
|
|
if pnt.Tags["db"] == "template0" {
|
|
foundTemplate0 = true
|
|
}
|
|
}
|
|
if pnt.Measurement == "postgresql" {
|
|
if pnt.Tags["db"] == "template1" {
|
|
foundTemplate1 = true
|
|
}
|
|
}
|
|
}
|
|
|
|
assert.False(t, foundTemplate0)
|
|
assert.True(t, foundTemplate1)
|
|
}
|