-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Thomas Berger <[email protected]>
- Loading branch information
0 parents
commit bde8b12
Showing
1,111 changed files
with
328,548 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.idea | ||
bin/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Hacking on pg-exporter | ||
|
||
## adding a new scraper | ||
|
||
### define your model in collector/models | ||
|
||
Models are defined by creating a struct like in the most ORM's. As pg-exporter uses [go-pg/v9][1], | ||
the same `sql` tags are used. | ||
|
||
In addition, the following pg-exporter specific tags are available and required for code-generation: | ||
|
||
- **help**: defines the help text for the metric | ||
- **metric**: defines metric name and type. The first, optional paramter is the name. Type could be: | ||
- **counter**: for counter values | ||
- **gauge**: for gauge values | ||
- **label**: fo labels | ||
|
||
### generate the full code for the model | ||
|
||
run `go generate .` in the root folder of the project | ||
|
||
### define the scraper | ||
|
||
- create a new, empty, struct in the `collector` package, that implements the `Scraper` interface. | ||
- Inside the `Scrape` function, fetch your data from the database with the given database connection. | ||
- see [go-pg/v9][1] for details how to do that | ||
- use the (generated) function `ToMetrics` on your result to provide the metrics to the prometheus handler | ||
|
||
|
||
[1]: https://github.com/go-pg/pg/tree/v9 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2020 1&1 Telecommunication SE | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# PostgreSQL Server Exporter | ||
|
||
Prometheus exporter for PostgreSQL server metrics. | ||
|
||
## Project state | ||
|
||
This project is in early beta. There is missing documentation, | ||
things may still change and the default scrapers are not final, and could change | ||
at any time. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#!/bin/bash | ||
|
||
mkdir -p bin | ||
|
||
REVISION=$(git rev-parse HEAD) | ||
VERSION=$(git describe --exact-match ${REVISION} 2>/dev/null) | ||
BRANCH=$(git rev-parse --abbrev-ref HEAD) | ||
|
||
|
||
set -x | ||
CGO_ENABLED=0 go build \ | ||
-mod=vendor -a -tags netgo \ | ||
-ldflags "-X github.com/prometheus/common/version.Version=${VERSION} | ||
-X github.com/prometheus/common/version.Revision=${REVISION} | ||
-X github.com/prometheus/common/version.Branch=${BRANCH} | ||
-X github.com/prometheus/common/version.BuildUser=${USER}@$(hostname) | ||
-X github.com/prometheus/common/version.BuildDate=$(date +%Y%m%d-%X)" \ | ||
-o bin/pg_exporter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package collector | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/go-pg/pg/v9" | ||
"github.com/prometheus/client_golang/prometheus" | ||
"gopkg.in/alecthomas/kingpin.v2" | ||
|
||
"github.com/1and1/pg-exporter/collector/models" | ||
) | ||
|
||
const ( | ||
// subsystem | ||
activity = "sessions" | ||
) | ||
|
||
// additional command line options | ||
var ( | ||
withUsername = kingpin.Flag("collect.pg_stat_activity.with_username", | ||
"Include username in session statistics"). | ||
Default("false").Bool() | ||
withApplicationName = kingpin.Flag("collect.pg_stat_activity.with_appname", | ||
"Include application name in session statistics"). | ||
Default("false").Bool() | ||
withClientAddr = kingpin.Flag("collect.pg_stat_activity.with_clientaddr", | ||
"Include application name in session statistics"). | ||
Default("false").Bool() | ||
withState = kingpin.Flag("collect.pg_stat_activity.with_state", | ||
"Include session state in session statistics"). | ||
Default("true").Bool() | ||
) | ||
|
||
// ScrapeActivity scrapes from pg_stat_bgwriter | ||
type ScrapeActivity struct{} | ||
|
||
// Name of the Scraper | ||
func (ScrapeActivity) Name() string { | ||
return "pg_stat_activity" | ||
} | ||
|
||
// Help describes the role of the Scraper | ||
func (ScrapeActivity) Help() string { | ||
return "Collect from pg_stat_activity" | ||
} | ||
|
||
// minimum PostgreSQL version | ||
func (ScrapeActivity) Version() int { | ||
return 0 | ||
} | ||
|
||
// scrape type | ||
func (ScrapeActivity) Type() ScrapeType { | ||
return SCRAPEGLOBAL | ||
} | ||
|
||
// Scrape collects data from database connection and sends it over channel as prometheus metric. | ||
func (ScrapeActivity) Scrape(ctx context.Context, db *pg.DB, ch chan<- prometheus.Metric) error { | ||
// we create a query based on the given commandline flags | ||
columns := "" | ||
if *withUsername { | ||
columns += ", usename" | ||
} | ||
|
||
if *withApplicationName { | ||
columns += ", application_name" | ||
} | ||
|
||
if *withClientAddr { | ||
columns += ", client_addr" | ||
} | ||
|
||
if *withState { | ||
columns += ", state" | ||
} | ||
|
||
qs := fmt.Sprintf(`SELECT datid, datname %s, count(1) as connections FROM`+ | ||
` pg_stat_activity WHERE datname IN (?) AND state IS NOT NULL and state != '' GROUP BY datid, datname %s`, | ||
columns, columns) | ||
|
||
var statActivity models.PgStatActivitySlice | ||
if _, err := db.QueryContext(ctx, &statActivity, qs, pg.In(collectDatabases)); err != nil { | ||
return err | ||
} | ||
return statActivity.ToMetrics(namespace, activity, ch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package collector | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-pg/pg/v9" | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"github.com/1and1/pg-exporter/collector/models" | ||
) | ||
|
||
const ( | ||
// subsystem | ||
archiver = "archiver" | ||
) | ||
|
||
// ScrapeArchiver scrapes from pg_stat_archiver | ||
type ScrapeArchiver struct{} | ||
|
||
// Name of the Scraper | ||
func (ScrapeArchiver) Name() string { | ||
return "pg_stat_archiver" | ||
} | ||
|
||
// Help describes the role of the Scraper | ||
func (ScrapeArchiver) Help() string { | ||
return "Collect from pg_stat_archiver" | ||
} | ||
|
||
// minimum PostgreSQL version | ||
func (ScrapeArchiver) Version() int { | ||
return 0 | ||
} | ||
|
||
// scrape type | ||
func (ScrapeArchiver) Type() ScrapeType { | ||
return SCRAPEGLOBAL | ||
} | ||
|
||
// Scrape collects data from database connection and sends it over channel as prometheus metric. | ||
func (ScrapeArchiver) Scrape(ctx context.Context, db *pg.DB, ch chan<- prometheus.Metric) error { | ||
statArchiver := &models.PgStatArchiver{} | ||
if err := db.ModelContext(ctx, statArchiver).Select(); err != nil { | ||
return err | ||
} | ||
|
||
return statArchiver.ToMetrics(namespace, archiver, ch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package collector | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-pg/pg/v9" | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"github.com/1and1/pg-exporter/collector/models" | ||
) | ||
|
||
const ( | ||
// subsystem | ||
bgwriter = "bgwriter" | ||
) | ||
|
||
// ScrapeBgWriter scrapes from pg_stat_bgwriter | ||
type ScrapeBgWriter struct{} | ||
|
||
// Name of the Scraper | ||
func (ScrapeBgWriter) Name() string { | ||
return "pg_stat_bgwriter" | ||
} | ||
|
||
// Help describes the role of the Scraper | ||
func (ScrapeBgWriter) Help() string { | ||
return "Collect from pg_stat_bgwriter" | ||
} | ||
|
||
// minimum PostgreSQL version | ||
func (ScrapeBgWriter) Version() int { | ||
return 0 | ||
} | ||
|
||
// scrape type | ||
func (ScrapeBgWriter) Type() ScrapeType { | ||
return SCRAPEGLOBAL | ||
} | ||
|
||
// Scrape collects data from database connection and sends it over channel as prometheus metric. | ||
func (ScrapeBgWriter) Scrape(ctx context.Context, db *pg.DB, ch chan<- prometheus.Metric) error { | ||
statBgwriter := &models.PgStatBgWriter{} | ||
if err := db.ModelContext(ctx, statBgwriter).Select(); err != nil { | ||
return err | ||
} | ||
|
||
return statBgwriter.ToMetrics(namespace, bgwriter, ch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package collector | ||
|
||
import ( | ||
_ "context" | ||
) | ||
|
||
const ( | ||
namespace = "postgresql" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package collector | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-pg/pg/v9" | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"github.com/1and1/pg-exporter/collector/models" | ||
) | ||
|
||
const ( | ||
// subsystem | ||
txid = "txid" | ||
) | ||
|
||
// ScrapeTXID scrapes from txid_current() | ||
type ScrapeTXID struct{} | ||
|
||
// Name of the Scraper | ||
func (ScrapeTXID) Name() string { | ||
return "txid_current" | ||
} | ||
|
||
// Help describes the role of the Scraper | ||
func (ScrapeTXID) Help() string { | ||
return "Collect from txid_current()" | ||
} | ||
|
||
// minimum PostgreSQL version | ||
func (ScrapeTXID) Version() int { | ||
return 0 | ||
} | ||
|
||
// scrape type | ||
func (ScrapeTXID) Type() ScrapeType { | ||
return SCRAPEGLOBAL | ||
} | ||
|
||
// Scrape collects data from database connection and sends it over channel as prometheus metric. | ||
func (ScrapeTXID) Scrape(ctx context.Context, db *pg.DB, ch chan<- prometheus.Metric) error { | ||
qs := `SELECT CASE WHEN pg_is_in_recovery() THEN NULL ELSE txid_current() END AS current` | ||
ctxid := &models.PgTxid{} | ||
if _, err := db.QueryContext(ctx, ctxid, qs); err != nil { | ||
return err | ||
} | ||
return ctxid.ToMetrics(namespace, txid, ch) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package collector | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/go-pg/pg/v9" | ||
"github.com/prometheus/client_golang/prometheus" | ||
|
||
"github.com/1and1/pg-exporter/collector/models" | ||
) | ||
|
||
const ( | ||
// subsystem | ||
databaseconflicts = "database_conflicts" | ||
) | ||
|
||
// ScrapeBgWriter scrapes from pg_stat_database_conflicts | ||
type ScrapeDatabaseConflicts struct{} | ||
|
||
// Name of the Scraper | ||
func (ScrapeDatabaseConflicts) Name() string { | ||
return "pg_stat_database_conflicts" | ||
} | ||
|
||
// Help describes the role of the Scraper | ||
func (ScrapeDatabaseConflicts) Help() string { | ||
return "Collect from pg_stat_database_conflicts" | ||
} | ||
|
||
// minimum PostgreSQL version | ||
func (ScrapeDatabaseConflicts) Version() int { | ||
return 0 | ||
} | ||
|
||
// scrape type | ||
func (ScrapeDatabaseConflicts) Type() ScrapeType { | ||
return SCRAPEGLOBAL | ||
} | ||
|
||
// Scrape collects data from database connection and sends it over channel as prometheus metric. | ||
func (ScrapeDatabaseConflicts) Scrape(ctx context.Context, db *pg.DB, ch chan<- prometheus.Metric) error { | ||
var databaseConflict models.PgStatDatabaseConflictsSlice | ||
if err := db.ModelContext(ctx, &databaseConflict).Where("datname IN (?)", pg.In(collectDatabases)). | ||
Select(); err != nil { | ||
return err | ||
} | ||
|
||
return databaseConflict.ToMetrics(namespace, databaseconflicts, ch) | ||
} |
Oops, something went wrong.