Skip to content

Commit b3bb440

Browse files
committed
merge with upstream
2 parents 64f5d9a + 39bd5d3 commit b3bb440

File tree

14 files changed

+555
-232
lines changed

14 files changed

+555
-232
lines changed

.travis.yml

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ go:
77
script:
88
- make all
99
- make docker
10+
- make test-integration
1011
after_success:
1112
- if [ "$TRAVIS_BRANCH" == "master" ]; then docker login -e $DOCKER_EMAIL -u $DOCKER_USER
1213
-p $DOCKER_PASS ; docker push wrouesnel/postgres_exporter ; fi

Makefile

+3
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ vet:
2121
test:
2222
go test -v .
2323

24+
test-integration:
25+
tests/test-smoke
26+
2427
# Do a self-contained docker build - we pull the official upstream container,
2528
# then template out a dockerfile which builds the real image.
2629
docker-build: postgres_exporter

postgres_exporter.go

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,24 @@
11
package main
22

33
import (
4-
//"bytes"
54
"database/sql"
65
"flag"
76
"fmt"
8-
"net/http"
9-
"os"
10-
//"regexp"
11-
//"strconv"
12-
//"strings"
13-
"gopkg.in/yaml.v2"
147
"io/ioutil"
158
"math"
9+
"net/http"
10+
"os"
11+
"strconv"
1612
"time"
1713

14+
"gopkg.in/yaml.v2"
15+
1816
_ "github.com/lib/pq"
1917
"github.com/prometheus/client_golang/prometheus"
20-
"github.com/prometheus/log"
21-
"strconv"
18+
"github.com/prometheus/common/log"
2219
)
2320

24-
var Version string = "0.0.0-dev"
21+
var Version string = "0.0.1"
2522

2623
var (
2724
listenAddress = flag.String(
@@ -375,6 +372,10 @@ func makeDescMap(metricMaps map[string]map[string]ColumnMapping) map[string]Metr
375372
return math.NaN(), false
376373
}
377374

375+
if durationString == "-1" {
376+
return math.NaN(), false
377+
}
378+
378379
d, err := time.ParseDuration(durationString)
379380
if err != nil {
380381
log.Errorln("Failed converting result to metric:", columnName, in, err)
@@ -438,6 +439,13 @@ func dbToFloat64(t interface{}) (float64, bool) {
438439
return math.NaN(), false
439440
}
440441
return result, true
442+
case string:
443+
result, err := strconv.ParseFloat(v, 64)
444+
if err != nil {
445+
log.Infoln("Could not parse string:", err)
446+
return math.NaN(), false
447+
}
448+
return result, true
441449
case nil:
442450
return math.NaN(), true
443451
default:
@@ -556,7 +564,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
556564

557565
db, err := sql.Open("postgres", e.dsn)
558566
if err != nil {
559-
log.Println("Error opening connection to database:", err)
567+
log.Infoln("Error opening connection to database:", err)
560568
e.error.Set(1)
561569
return
562570
}
@@ -602,7 +610,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
602610
// Don't fail on a bad scrape of one metric
603611
rows, err := db.Query(query)
604612
if err != nil {
605-
log.Println("Error running query on database: ", namespace, err)
613+
log.Infoln("Error running query on database: ", namespace, err)
606614
e.error.Set(1)
607615
return
608616
}
@@ -611,7 +619,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
611619
var columnNames []string
612620
columnNames, err = rows.Columns()
613621
if err != nil {
614-
log.Println("Error retrieving column list for: ", namespace, err)
622+
log.Infoln("Error retrieving column list for: ", namespace, err)
615623
e.error.Set(1)
616624
return
617625
}
@@ -631,7 +639,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
631639
for rows.Next() {
632640
err = rows.Scan(scanArgs...)
633641
if err != nil {
634-
log.Println("Error retrieving rows:", namespace, err)
642+
log.Infoln("Error retrieving rows:", namespace, err)
635643
e.error.Set(1)
636644
return
637645
}

tests/test-smoke

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/bin/bash
2+
# Basic integration tests with postgres. Requires docker to work.
3+
4+
VERSIONS=( \
5+
9.1 \
6+
9.2 \
7+
9.3 \
8+
9.4 \
9+
9.5 \
10+
)
11+
12+
smoketest_postgres() {
13+
local version=$1
14+
local CONTAINER_NAME=postgres_exporter-test-smoke
15+
local TIMEOUT=30
16+
local IMAGE_NAME=postgres
17+
18+
local CUR_IMAGE=$IMAGE_NAME:$version
19+
20+
docker run -d --name=$CONTAINER_NAME -e POSTGRES_PASSWORD=password -p 127.0.0.1:55432:5432 $CUR_IMAGE
21+
22+
local WAIT_START=$(date +%s)
23+
while ! docker exec $CONTAINER_NAME bash -c "psql -U postgres -c \"select 'running'\" > /dev/null 2>&1 " ; do
24+
echo "Waiting for postgres to start..."
25+
if [ $(( $(date +%s) - $WAIT_START )) -gt $TIMEOUT ]; then
26+
echo "Timed out waiting for postgres!" 1>&2
27+
docker logs $CONTAINER_NAME
28+
docker kill $CONTAINER_NAME
29+
docker rm $CONTAINER_NAME
30+
exit 1
31+
fi
32+
sleep 1
33+
done
34+
35+
DATA_SOURCE_NAME="postgresql://postgres:password@localhost:55432/?sslmode=disable" ./postgres_exporter &
36+
exporter_pid=$!
37+
local DAEMON_WAIT_START=$(date +%s)
38+
while ! nc -z localhost 9113 ; do
39+
echo "Waiting for exporter to start..."
40+
if [ $(( $(date +%s) - $WAIT_START )) -gt $TIMEOUT ]; then
41+
echo "Timed out waiting for exporter!" 1>&2
42+
docker logs $CONTAINER_NAME
43+
docker kill $CONTAINER_NAME
44+
docker rm $CONTAINER_NAME
45+
exit 1
46+
fi
47+
sleep 1
48+
done
49+
50+
wget -q -O - http://localhost:9113/metrics 1> /dev/null
51+
if [ "$?" != "0" ]; then
52+
echo "Failed on postgres $version ($DOCKER_IMAGE)" 1>&2
53+
kill $exporter_pid
54+
docker logs $CONTAINER_NAME
55+
docker kill $CONTAINER_NAME
56+
docker rm $CONTAINER_NAME
57+
exit 1
58+
fi
59+
60+
kill $exporter_pid
61+
docker kill $CONTAINER_NAME
62+
docker rm $CONTAINER_NAME
63+
}
64+
65+
# Start pulling the docker images in advance
66+
for version in ${VERSIONS[@]}; do
67+
docker pull postgres:$version > /dev/null &
68+
done
69+
70+
for version in ${VERSIONS[@]}; do
71+
echo "Testing postgres version $version"
72+
smoketest_postgres $version
73+
done

vendor/github.com/prometheus/common/NOTICE

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)