Skip to content

Commit 5c1fe87

Browse files
author
James Lawrence
committed
fix tests
1 parent 9f00f38 commit 5c1fe87

File tree

5 files changed

+134
-8
lines changed

5 files changed

+134
-8
lines changed

plugins/inputs/postgresql/postgresql.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import (
1313
"github.com/influxdata/telegraf"
1414
"github.com/influxdata/telegraf/plugins/inputs"
1515

16-
_ "github.com/jackc/pgx/stdlib"
16+
"github.com/jackc/pgx"
17+
"github.com/jackc/pgx/stdlib"
1718
)
1819

1920
type Postgresql struct {
@@ -68,7 +69,7 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
6869
p.Address = localhost
6970
}
7071

71-
db, err := sql.Open("pgx", p.Address)
72+
db, err := connect(p.Address)
7273
if err != nil {
7374
return err
7475
}
@@ -277,3 +278,21 @@ func parseURL(uri string) (string, error) {
277278
sort.Strings(kvs) // Makes testing easier (not a performance concern)
278279
return strings.Join(kvs, " "), nil
279280
}
281+
282+
func connect(address string) (*sql.DB, error) {
283+
if strings.HasPrefix(address, "postgres://") || strings.HasPrefix(address, "postgresql://") {
284+
return sql.Open("pgx", address)
285+
}
286+
287+
config, err := pgx.ParseDSN(address)
288+
if err != nil {
289+
return nil, err
290+
}
291+
292+
pool, err := pgx.NewConnPool(pgx.ConnPoolConfig{ConnConfig: config})
293+
if err != nil {
294+
return nil, err
295+
}
296+
297+
return stdlib.OpenFromConnPool(pool)
298+
}

plugins/inputs/postgresql/postgresql_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
2828
for _, col := range p.AllColumns {
2929
availableColumns[col] = true
3030
}
31+
3132
intMetrics := []string{
3233
"xact_commit",
3334
"xact_rollback",
@@ -42,7 +43,6 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
4243
"temp_files",
4344
"temp_bytes",
4445
"deadlocks",
45-
"numbackends",
4646
"buffers_alloc",
4747
"buffers_backend",
4848
"buffers_backend_fsync",
@@ -53,9 +53,20 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
5353
"maxwritten_clean",
5454
}
5555

56+
int32Metrics := []string{
57+
"numbackends",
58+
}
59+
5660
floatMetrics := []string{
5761
"blk_read_time",
5862
"blk_write_time",
63+
"checkpoint_write_time",
64+
"checkpoint_sync_time",
65+
}
66+
67+
stringMetrics := []string{
68+
"datname",
69+
"datid",
5970
}
6071

6172
metricsCounted := 0
@@ -68,6 +79,14 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
6879
}
6980
}
7081

82+
for _, metric := range int32Metrics {
83+
_, ok := availableColumns[metric]
84+
if ok {
85+
assert.True(t, acc.HasInt32Field("postgresql", metric))
86+
metricsCounted++
87+
}
88+
}
89+
7190
for _, metric := range floatMetrics {
7291
_, ok := availableColumns[metric]
7392
if ok {
@@ -76,8 +95,16 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
7695
}
7796
}
7897

98+
for _, metric := range stringMetrics {
99+
_, ok := availableColumns[metric]
100+
if ok {
101+
assert.True(t, acc.HasStringField("postgresql", metric))
102+
metricsCounted++
103+
}
104+
}
105+
79106
assert.True(t, metricsCounted > 0)
80-
//assert.Equal(t, len(availableColumns)-len(p.IgnoredColumns()), metricsCounted)
107+
assert.Equal(t, len(availableColumns)-len(p.IgnoredColumns()), metricsCounted)
81108
}
82109

83110
func TestPostgresqlTagsMetricsWithDatabaseName(t *testing.T) {

plugins/inputs/postgresql_extensible/postgresql_extensible.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ import (
1414
"github.com/influxdata/telegraf"
1515
"github.com/influxdata/telegraf/plugins/inputs"
1616

17-
_ "github.com/jackc/pgx/stdlib"
17+
"github.com/jackc/pgx"
18+
"github.com/jackc/pgx/stdlib"
1819
)
1920

2021
type Postgresql struct {
@@ -128,7 +129,7 @@ func (p *Postgresql) Gather(acc telegraf.Accumulator) error {
128129
p.Address = localhost
129130
}
130131

131-
db, err := sql.Open("pgx", p.Address)
132+
db, err := connect(p.Address)
132133
if err != nil {
133134
return err
134135
}
@@ -286,7 +287,7 @@ COLUMN:
286287
tags[col] = v
287288
case []byte:
288289
tags[col] = string(v)
289-
case int64:
290+
case int64, int32, int:
290291
tags[col] = fmt.Sprintf("%d", v)
291292
default:
292293
log.Println("failed to add additional tag", col)
@@ -376,3 +377,21 @@ func parseURL(uri string) (string, error) {
376377
sort.Strings(kvs) // Makes testing easier (not a performance concern)
377378
return strings.Join(kvs, " "), nil
378379
}
380+
381+
func connect(address string) (*sql.DB, error) {
382+
if strings.HasPrefix(address, "postgres://") || strings.HasPrefix(address, "postgresql://") {
383+
return sql.Open("pgx", address)
384+
}
385+
386+
config, err := pgx.ParseDSN(address)
387+
if err != nil {
388+
return nil, err
389+
}
390+
391+
pool, err := pgx.NewConnPool(pgx.ConnPoolConfig{ConnConfig: config})
392+
if err != nil {
393+
return nil, err
394+
}
395+
396+
return stdlib.OpenFromConnPool(pool)
397+
}

plugins/inputs/postgresql_extensible/postgresql_extensible_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
3333
for _, col := range p.AllColumns {
3434
availableColumns[col] = true
3535
}
36+
3637
intMetrics := []string{
3738
"xact_commit",
3839
"xact_rollback",
@@ -47,6 +48,9 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
4748
"temp_files",
4849
"temp_bytes",
4950
"deadlocks",
51+
}
52+
53+
int32Metrics := []string{
5054
"numbackends",
5155
}
5256

@@ -55,6 +59,11 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
5559
"blk_write_time",
5660
}
5761

62+
stringMetrics := []string{
63+
"datname",
64+
"datid",
65+
}
66+
5867
metricsCounted := 0
5968

6069
for _, metric := range intMetrics {
@@ -65,6 +74,14 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
6574
}
6675
}
6776

77+
for _, metric := range int32Metrics {
78+
_, ok := availableColumns[metric]
79+
if ok {
80+
assert.True(t, acc.HasInt32Field("postgresql", metric))
81+
metricsCounted++
82+
}
83+
}
84+
6885
for _, metric := range floatMetrics {
6986
_, ok := availableColumns[metric]
7087
if ok {
@@ -73,6 +90,14 @@ func TestPostgresqlGeneratesMetrics(t *testing.T) {
7390
}
7491
}
7592

93+
for _, metric := range stringMetrics {
94+
_, ok := availableColumns[metric]
95+
if ok {
96+
assert.True(t, acc.HasStringField("postgresql", metric))
97+
metricsCounted++
98+
}
99+
}
100+
76101
assert.True(t, metricsCounted > 0)
77102
assert.Equal(t, len(availableColumns)-len(p.IgnoredColumns()), metricsCounted)
78103
}

testutil/accumulator.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ func (a *Accumulator) AssertContainsFields(
188188
assert.Fail(t, msg)
189189
}
190190

191-
// HasIntValue returns true if the measurement has an Int value
191+
// HasIntField returns true if the measurement has an Int value
192192
func (a *Accumulator) HasIntField(measurement string, field string) bool {
193193
a.Lock()
194194
defer a.Unlock()
@@ -206,6 +206,42 @@ func (a *Accumulator) HasIntField(measurement string, field string) bool {
206206
return false
207207
}
208208

209+
// HasInt32Field returns true if the measurement has an Int value
210+
func (a *Accumulator) HasInt32Field(measurement string, field string) bool {
211+
a.Lock()
212+
defer a.Unlock()
213+
for _, p := range a.Metrics {
214+
if p.Measurement == measurement {
215+
for fieldname, value := range p.Fields {
216+
if fieldname == field {
217+
_, ok := value.(int32)
218+
return ok
219+
}
220+
}
221+
}
222+
}
223+
224+
return false
225+
}
226+
227+
// HasStringField returns true if the measurement has an Int value
228+
func (a *Accumulator) HasStringField(measurement string, field string) bool {
229+
a.Lock()
230+
defer a.Unlock()
231+
for _, p := range a.Metrics {
232+
if p.Measurement == measurement {
233+
for fieldname, value := range p.Fields {
234+
if fieldname == field {
235+
_, ok := value.(string)
236+
return ok
237+
}
238+
}
239+
}
240+
}
241+
242+
return false
243+
}
244+
209245
// HasUIntValue returns true if the measurement has a UInt value
210246
func (a *Accumulator) HasUIntField(measurement string, field string) bool {
211247
a.Lock()

0 commit comments

Comments
 (0)