2015-05-18 17:46:44 +00:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
2015-07-07 01:18:31 +00:00
|
|
|
"fmt"
|
2015-05-18 17:46:44 +00:00
|
|
|
"testing"
|
|
|
|
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-05-18 17:46:44 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestPostgresqlGeneratesMetrics(t *testing.T) {
|
2015-08-04 20:52:44 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
|
|
|
|
2015-05-18 17:46:44 +00:00
|
|
|
p := &Postgresql{
|
2018-01-06 00:03:09 +00:00
|
|
|
Service: Service{
|
|
|
|
Address: fmt.Sprintf(
|
|
|
|
"host=%s user=postgres sslmode=disable",
|
|
|
|
testutil.GetLocalHost(),
|
|
|
|
),
|
2018-08-01 22:44:10 +00:00
|
|
|
IsPgBouncer: false,
|
2018-01-06 00:03:09 +00:00
|
|
|
},
|
2016-01-07 00:37:56 +00:00
|
|
|
Databases: []string{"postgres"},
|
2015-05-18 17:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2018-01-06 00:03:09 +00:00
|
|
|
require.NoError(t, p.Start(&acc))
|
|
|
|
require.NoError(t, p.Gather(&acc))
|
2017-01-24 20:36:36 +00:00
|
|
|
|
2015-05-18 17:46:44 +00:00
|
|
|
intMetrics := []string{
|
2015-05-18 19:15:15 +00:00
|
|
|
"xact_commit",
|
|
|
|
"xact_rollback",
|
|
|
|
"blks_read",
|
|
|
|
"blks_hit",
|
|
|
|
"tup_returned",
|
|
|
|
"tup_fetched",
|
|
|
|
"tup_inserted",
|
|
|
|
"tup_updated",
|
|
|
|
"tup_deleted",
|
|
|
|
"conflicts",
|
|
|
|
"temp_files",
|
|
|
|
"temp_bytes",
|
|
|
|
"deadlocks",
|
2016-02-10 09:40:48 +00:00
|
|
|
"buffers_alloc",
|
|
|
|
"buffers_backend",
|
|
|
|
"buffers_backend_fsync",
|
|
|
|
"buffers_checkpoint",
|
|
|
|
"buffers_clean",
|
|
|
|
"checkpoints_req",
|
|
|
|
"checkpoints_timed",
|
|
|
|
"maxwritten_clean",
|
2017-11-09 22:03:16 +00:00
|
|
|
"datid",
|
2017-01-24 20:36:36 +00:00
|
|
|
"numbackends",
|
|
|
|
}
|
|
|
|
|
2017-11-09 22:03:16 +00:00
|
|
|
int32Metrics := []string{}
|
|
|
|
|
2015-05-18 17:46:44 +00:00
|
|
|
floatMetrics := []string{
|
2015-05-18 19:15:15 +00:00
|
|
|
"blk_read_time",
|
|
|
|
"blk_write_time",
|
2017-01-24 20:36:36 +00:00
|
|
|
"checkpoint_write_time",
|
|
|
|
"checkpoint_sync_time",
|
|
|
|
}
|
|
|
|
|
|
|
|
stringMetrics := []string{
|
|
|
|
"datname",
|
2015-05-18 17:46:44 +00:00
|
|
|
}
|
|
|
|
|
2015-09-14 00:11:49 +00:00
|
|
|
metricsCounted := 0
|
|
|
|
|
2015-05-18 17:46:44 +00:00
|
|
|
for _, metric := range intMetrics {
|
2018-01-06 00:03:09 +00:00
|
|
|
assert.True(t, acc.HasInt64Field("postgresql", metric))
|
|
|
|
metricsCounted++
|
2015-05-18 17:46:44 +00:00
|
|
|
}
|
|
|
|
|
2017-01-24 20:36:36 +00:00
|
|
|
for _, metric := range int32Metrics {
|
2018-01-06 00:03:09 +00:00
|
|
|
assert.True(t, acc.HasInt32Field("postgresql", metric))
|
|
|
|
metricsCounted++
|
2017-01-24 20:36:36 +00:00
|
|
|
}
|
|
|
|
|
2015-05-18 17:46:44 +00:00
|
|
|
for _, metric := range floatMetrics {
|
2018-01-06 00:03:09 +00:00
|
|
|
assert.True(t, acc.HasFloatField("postgresql", metric))
|
|
|
|
metricsCounted++
|
2015-05-18 17:46:44 +00:00
|
|
|
}
|
2015-09-14 00:11:49 +00:00
|
|
|
|
2017-01-24 20:36:36 +00:00
|
|
|
for _, metric := range stringMetrics {
|
2018-01-06 00:03:09 +00:00
|
|
|
assert.True(t, acc.HasStringField("postgresql", metric))
|
|
|
|
metricsCounted++
|
2017-01-24 20:36:36 +00:00
|
|
|
}
|
|
|
|
|
2015-09-14 00:11:49 +00:00
|
|
|
assert.True(t, metricsCounted > 0)
|
2018-01-06 00:03:09 +00:00
|
|
|
assert.Equal(t, len(floatMetrics)+len(intMetrics)+len(int32Metrics)+len(stringMetrics), metricsCounted)
|
2015-05-18 17:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) {
|
2015-08-04 20:52:44 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
|
|
|
|
2015-05-18 17:46:44 +00:00
|
|
|
p := &Postgresql{
|
2018-01-06 00:03:09 +00:00
|
|
|
Service: Service{
|
|
|
|
Address: fmt.Sprintf(
|
|
|
|
"host=%s user=postgres sslmode=disable",
|
|
|
|
testutil.GetLocalHost(),
|
|
|
|
),
|
|
|
|
},
|
2016-01-07 00:37:56 +00:00
|
|
|
Databases: []string{"postgres"},
|
2015-05-18 17:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
2018-01-06 00:03:09 +00:00
|
|
|
require.NoError(t, p.Start(&acc))
|
|
|
|
require.NoError(t, p.Gather(&acc))
|
2015-05-18 17:46:44 +00:00
|
|
|
|
2016-01-07 00:37:56 +00:00
|
|
|
point, ok := acc.Get("postgresql")
|
2015-05-18 17:46:44 +00:00
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
assert.Equal(t, "postgres", point.Tags["db"])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestPostgresqlDefaultsToAllDatabases(t *testing.T) {
|
2015-08-04 20:52:44 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
|
|
|
|
2015-05-18 17:46:44 +00:00
|
|
|
p := &Postgresql{
|
2018-01-06 00:03:09 +00:00
|
|
|
Service: Service{
|
|
|
|
Address: fmt.Sprintf(
|
|
|
|
"host=%s user=postgres sslmode=disable",
|
|
|
|
testutil.GetLocalHost(),
|
|
|
|
),
|
|
|
|
},
|
2015-05-18 17:46:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
2018-01-06 00:03:09 +00:00
|
|
|
require.NoError(t, p.Start(&acc))
|
|
|
|
require.NoError(t, p.Gather(&acc))
|
2015-05-18 17:46:44 +00:00
|
|
|
|
|
|
|
var found bool
|
|
|
|
|
2016-01-27 23:15:14 +00:00
|
|
|
for _, pnt := range acc.Metrics {
|
2016-01-07 00:37:56 +00:00
|
|
|
if pnt.Measurement == "postgresql" {
|
2015-05-18 17:46:44 +00:00
|
|
|
if pnt.Tags["db"] == "postgres" {
|
|
|
|
found = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.True(t, found)
|
|
|
|
}
|
2015-09-13 08:30:38 +00:00
|
|
|
|
|
|
|
func TestPostgresqlIgnoresUnwantedColumns(t *testing.T) {
|
2015-09-13 08:34:54 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
2015-09-13 08:30:38 +00:00
|
|
|
|
|
|
|
p := &Postgresql{
|
2018-01-06 00:03:09 +00:00
|
|
|
Service: Service{
|
|
|
|
Address: fmt.Sprintf(
|
|
|
|
"host=%s user=postgres sslmode=disable",
|
|
|
|
testutil.GetLocalHost(),
|
|
|
|
),
|
|
|
|
},
|
2015-09-13 08:30:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2018-01-06 00:03:09 +00:00
|
|
|
require.NoError(t, p.Start(&acc))
|
|
|
|
require.NoError(t, p.Gather(&acc))
|
2015-09-13 08:30:38 +00:00
|
|
|
|
2015-09-14 00:22:19 +00:00
|
|
|
for col := range p.IgnoredColumns() {
|
|
|
|
assert.False(t, acc.HasMeasurement(col))
|
2015-09-13 08:30:38 +00:00
|
|
|
}
|
|
|
|
}
|
2016-09-07 08:39:55 +00:00
|
|
|
|
|
|
|
func TestPostgresqlDatabaseWhitelistTest(t *testing.T) {
|
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &Postgresql{
|
2018-01-06 00:03:09 +00:00
|
|
|
Service: Service{
|
|
|
|
Address: fmt.Sprintf(
|
|
|
|
"host=%s user=postgres sslmode=disable",
|
|
|
|
testutil.GetLocalHost(),
|
|
|
|
),
|
|
|
|
},
|
2016-09-07 08:39:55 +00:00
|
|
|
Databases: []string{"template0"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
2018-01-06 00:03:09 +00:00
|
|
|
require.NoError(t, p.Start(&acc))
|
|
|
|
require.NoError(t, p.Gather(&acc))
|
2016-09-07 08:39:55 +00:00
|
|
|
|
|
|
|
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{
|
2018-01-06 00:03:09 +00:00
|
|
|
Service: Service{
|
|
|
|
Address: fmt.Sprintf(
|
|
|
|
"host=%s user=postgres sslmode=disable",
|
|
|
|
testutil.GetLocalHost(),
|
|
|
|
),
|
|
|
|
},
|
2016-09-07 08:39:55 +00:00
|
|
|
IgnoredDatabases: []string{"template0"},
|
|
|
|
}
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
2018-01-06 00:03:09 +00:00
|
|
|
require.NoError(t, p.Start(&acc))
|
|
|
|
require.NoError(t, p.Gather(&acc))
|
2016-09-07 08:39:55 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|