Skip to content

Commit 46398d5

Browse files
authored
Merge pull request #5 from fly-apps/add-pg-exporter
Add PG exporter to dockerfile
2 parents 0f4ddf0 + 733a579 commit 46398d5

File tree

4 files changed

+36
-0
lines changed

4 files changed

+36
-0
lines changed

Dockerfile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ RUN CGO_ENABLED=0 GOOS=linux go build -v -o /fly/bin/flyadmin ./cmd/flyadmin
1010
RUN CGO_ENABLED=0 GOOS=linux go build -v -o /fly/bin/start ./cmd/start
1111
COPY ./bin/* /fly/bin/
1212

13+
FROM wrouesnel/postgres_exporter:latest AS postgres_exporter
1314
FROM postgres:${PG_VERSION}
1415
ENV PGDATA=/data/pg_data
1516
ARG VERSION
@@ -22,8 +23,11 @@ RUN apt-get update && apt-get install --no-install-recommends -y \
2223
ca-certificates iproute2 consul curl bash dnsutils vim procps jq \
2324
&& apt autoremove -y
2425

26+
COPY --from=postgres_exporter /postgres_exporter /usr/local/bin/
2527
COPY --from=0 /fly/bin/* /usr/local/bin
2628

29+
ADD /config/* /fly/
30+
2731
EXPOSE 5432
2832

2933

cmd/start/main.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,26 @@ func main() {
5656
}
5757
}()
5858

59+
node, err := flypg.NewNode()
60+
if err != nil {
61+
PanicHandler(err)
62+
}
63+
5964
svisor := supervisor.New("flypg", 5*time.Minute)
6065
svisor.AddProcess("flypg", "gosu postgres postgres -D /data/postgres/")
6166

67+
exporterEnv := map[string]string{
68+
"DATA_SOURCE_URI": fmt.Sprintf("[%s]:%d/postgres?sslmode=disable", node.PrivateIP, node.PGPort),
69+
"DATA_SOURCE_USER": node.SUCredentials.Username,
70+
"DATA_SOURCE_PASS": node.SUCredentials.Password,
71+
"PG_EXPORTER_EXCLUDE_DATABASE": "template0,template1",
72+
"PG_EXPORTER_DISABLE_SETTINGS_METRICS": "true",
73+
"PG_EXPORTER_AUTO_DISCOVER_DATABASES": "true",
74+
"PG_EXPORTER_EXTEND_QUERY_PATH": "/fly/queries.yaml",
75+
}
76+
77+
svisor.AddProcess("exporter", "postgres_exporter", supervisor.WithEnv(exporterEnv), supervisor.WithRestart(0, 1*time.Second))
78+
6279
svisor.StopOnSignal(syscall.SIGINT, syscall.SIGTERM)
6380
svisor.StartHttpListener()
6481

config/queries.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
pg_database:
2+
query: "SELECT pg_database.datname, pg_database_size(pg_database.datname) as size_bytes FROM pg_database"
3+
master: true
4+
cache_seconds: 30
5+
metrics:
6+
- datname:
7+
usage: "LABEL"
8+
description: "Name of the database"
9+
- size_bytes:
10+
usage: "GAUGE"
11+
description: "Disk space used by the database"

fly.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,7 @@ processes = []
3131
[[mounts]]
3232
destination = "/data"
3333
source = "pg_data"
34+
35+
[metrics]
36+
path = "/metrics"
37+
port = 9187

0 commit comments

Comments
 (0)