Skip to content

Cleanup/add gitlab exporters #819

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
6323907
Add wal receiver
Sticksman Jun 16, 2023
8ee74db
Finish pg_stat_walreceiver
Sticksman Jun 20, 2023
0117653
Add pg_archiver
Sticksman Jun 20, 2023
eecb3ba
Missed a label declaration
Sticksman Jun 20, 2023
a6998af
Add stat_user_indexes
Sticksman Jun 20, 2023
dff617a
Add pg statio user queries and fix a test name
Sticksman Jun 22, 2023
4b3e95c
Migrate from db -> instance
Sticksman Jun 22, 2023
29736ce
pg_index_size query
Sticksman Jun 22, 2023
9198ec9
Add total relation size query
Sticksman Jun 22, 2023
c7dad9d
Add pg_blocked query
Sticksman Jun 23, 2023
fb04e3c
Add pg_blocked queries
Sticksman Jun 23, 2023
f76c2ce
Add pg_slow
Sticksman Jun 23, 2023
5d06f64
Add long running transactions query
Sticksman Jun 23, 2023
eb86b4c
Add stuck in transaction query + xid query + fix some init
Sticksman Jun 23, 2023
b235947
Add database wraparound query
Sticksman Jun 23, 2023
eac7650
Fix test name
Sticksman Jun 23, 2023
18ba5bb
xlog location
Sticksman Jun 23, 2023
bb5dba8
Add pg_stat_activity_marginalia sampler
Sticksman Jun 23, 2023
8bbd6d4
Add pg stat activity autovacuum
Sticksman Jun 23, 2023
82a51fe
Add autovacuum active
Sticksman Jun 23, 2023
c9f83ae
Long running transactions marginalia
Sticksman Jun 23, 2023
9b4d845
Lint fixes
Sticksman Jun 23, 2023
49e1a46
Lint
Sticksman Jun 23, 2023
d822ec4
Fix the NaN tests
Sticksman Jun 23, 2023
7651eed
Remove broken tests for now
Sticksman Jun 23, 2023
534af41
Lint
Sticksman Jun 23, 2023
8d6499f
Disable all new queries by default, rename marginalia -> summary
Sticksman Jun 23, 2023
4891644
Update pg_blocked with nulls
Sticksman Jun 26, 2023
4adddb3
Db wraparound test nullable
Sticksman Jun 26, 2023
8b7ffa6
index size nullable
Sticksman Jun 26, 2023
532c04f
LongRunningTransactionsSUmmary nullable
Sticksman Jun 26, 2023
f839778
stat user indexes nullable
Sticksman Jun 26, 2023
fddba48
stat walreceiver nil
Sticksman Jun 26, 2023
6513418
statiouser_indexes nullable
Sticksman Jun 26, 2023
aa910f3
total relation size nullable
Sticksman Jun 26, 2023
2d33e61
Remove linebreak
Sticksman Jun 26, 2023
55034b3
Use labels pattern
Sticksman Jun 26, 2023
57ea9d8
stat activity summary nullable
Sticksman Jun 26, 2023
3718e52
Redo all change requests
Sticksman Jun 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
stat walreceiver nil
Signed-off-by: Felix Yuan <[email protected]>
  • Loading branch information
Sticksman committed Jun 28, 2023
commit fddba48c8c4e7fae533bbcc70a9d71f189975b4c
105 changes: 73 additions & 32 deletions collector/pg_stat_walreceiver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ package collector

import (
"context"
"database/sql"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
Expand Down Expand Up @@ -173,18 +174,9 @@ func (c *PGStatWalReceiverCollector) Update(ctx context.Context, instance *insta
}
defer rows.Close()
for rows.Next() {
var upstreamHost string
var slotName string
var status int
var receiveStartLsn int64
var receiveStartTli int64
var flushedLsn int64
var receivedTli int64
var lastMsgSendTime float64
var lastMsgReceiptTime float64
var latestEndLsn int64
var latestEndTime float64
var upstreamNode int
var upstreamHost, slotName sql.NullString
var status, receiveStartLsn, receiveStartTli, flushedLsn, receivedTli, latestEndLsn, upstreamNode sql.NullInt64
var lastMsgSendTime, lastMsgReceiptTime, latestEndTime sql.NullFloat64

if hasFlushedLSN {
if err := rows.Scan(&upstreamHost, &slotName, &status, &receiveStartLsn, &receiveStartTli, &flushedLsn, &receivedTli, &lastMsgSendTime, &lastMsgReceiptTime, &latestEndLsn, &latestEndTime, &upstreamNode); err != nil {
Expand All @@ -195,68 +187,117 @@ func (c *PGStatWalReceiverCollector) Update(ctx context.Context, instance *insta
return err
}
}
upstreamHostLabel := "unknown"
if upstreamHost.Valid {
upstreamHostLabel = upstreamHost.String
}
slotNameLabel := "unknown"
if slotName.Valid {
slotNameLabel = slotName.String
}
labels := []string{upstreamHostLabel, slotNameLabel}

statusMetric := 0.0
if status.Valid {
statusMetric = float64(status.Int64)
}
ch <- prometheus.MustNewConstMetric(
statWalReceiverStatus,
prometheus.GaugeValue,
float64(status),
upstreamHost, slotName)
statusMetric,
labels...)

receiveStartLsnMetric := 0.0
if receiveStartLsn.Valid {
receiveStartLsnMetric = float64(receiveStartLsn.Int64)
}
ch <- prometheus.MustNewConstMetric(
statWalReceiverReceiveStartLsn,
prometheus.CounterValue,
float64(receiveStartLsn),
upstreamHost, slotName)
receiveStartLsnMetric,
labels...)

receiveStartTliMetric := 0.0
if receiveStartTli.Valid {
receiveStartTliMetric = float64(receiveStartTli.Int64)
}
ch <- prometheus.MustNewConstMetric(
statWalReceiverReceiveStartTli,
prometheus.GaugeValue,
float64(receiveStartTli),
upstreamHost, slotName)
receiveStartTliMetric,
labels...)

if hasFlushedLSN {
flushedLsnMetric := 0.0
if flushedLsn.Valid {
flushedLsnMetric = float64(flushedLsn.Int64)
}
ch <- prometheus.MustNewConstMetric(
statWalReceiverFlushedLSN,
prometheus.CounterValue,
float64(flushedLsn),
upstreamHost, slotName)
flushedLsnMetric,
labels...)
}

receivedTliMetric := 0.0
if receivedTli.Valid {
receivedTliMetric = float64(receivedTli.Int64)
}
ch <- prometheus.MustNewConstMetric(
statWalReceiverReceivedTli,
prometheus.GaugeValue,
float64(receivedTli),
upstreamHost, slotName)
receivedTliMetric,
labels...)

lastMsgSendTimeMetric := 0.0
if lastMsgSendTime.Valid {
lastMsgSendTimeMetric = float64(lastMsgSendTime.Float64)
}
ch <- prometheus.MustNewConstMetric(
statWalReceiverLastMsgSendTime,
prometheus.CounterValue,
float64(lastMsgSendTime),
upstreamHost, slotName)
lastMsgSendTimeMetric,
labels...)

lastMsgReceiptTimeMetric := 0.0
if lastMsgReceiptTime.Valid {
lastMsgReceiptTimeMetric = float64(lastMsgReceiptTime.Float64)
}
ch <- prometheus.MustNewConstMetric(
statWalReceiverLastMsgReceiptTime,
prometheus.CounterValue,
float64(lastMsgReceiptTime),
upstreamHost, slotName)
lastMsgReceiptTimeMetric,
labels...)

latestEndLsnMetric := 0.0
if latestEndLsn.Valid {
latestEndLsnMetric = float64(latestEndLsn.Int64)
}
ch <- prometheus.MustNewConstMetric(
statWalReceiverLatestEndLsn,
prometheus.CounterValue,
float64(latestEndLsn),
upstreamHost, slotName)
latestEndLsnMetric,
labels...)

latestEndTimeMetric := 0.0
if latestEndTime.Valid {
latestEndTimeMetric = float64(latestEndTime.Float64)
}
ch <- prometheus.MustNewConstMetric(
statWalReceiverLatestEndTime,
prometheus.CounterValue,
float64(latestEndTime),
upstreamHost, slotName)
latestEndTimeMetric,
labels...)

upstreamNodeMetric := 0.0
if upstreamNode.Valid {
upstreamNodeMetric = float64(upstreamNode.Int64)
}
ch <- prometheus.MustNewConstMetric(
statWalReceiverUpstreamNode,
prometheus.GaugeValue,
float64(upstreamNode),
upstreamHost, slotName)
upstreamNodeMetric,
labels...)
}
if err := rows.Err(); err != nil {
return err
Expand Down
83 changes: 83 additions & 0 deletions collector/pg_stat_walreceiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,3 +181,86 @@ func TestPGStatWalReceiverCollectorWithNoFlushedLSN(t *testing.T) {
}

}

func TestPGStatWalReceiverCollectorWithFlushedLSNNull(t *testing.T) {
db, mock, err := sqlmock.New()
if err != nil {
t.Fatalf("Error opening a stub db connection: %s", err)
}
defer db.Close()

inst := &instance{db: db}
infoSchemaColumns := []string{
"column_name",
}

infoSchemaRows := sqlmock.NewRows(infoSchemaColumns).
AddRow(
"flushed_lsn",
)

mock.ExpectQuery(sanitizeQuery(pgStatWalColumnQuery)).WillReturnRows(infoSchemaRows)

columns := []string{
"upstream_host",
"slot_name",
"status",
"receive_start_lsn",
"receive_start_tli",
"flushed_lsn",
"received_tli",
"last_msg_send_time",
"last_msg_receipt_time",
"latest_end_lsn",
"latest_end_time",
"upstream_node",
}
rows := sqlmock.NewRows(columns).
AddRow(
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
nil,
)
mock.ExpectQuery(sanitizeQuery(pgStatWalReceiverQueryWithFlushedLSN)).WillReturnRows(rows)

ch := make(chan prometheus.Metric)
go func() {
defer close(ch)
c := PGStatWalReceiverCollector{}

if err := c.Update(context.Background(), inst, ch); err != nil {
t.Errorf("Error calling PgStatWalReceiverCollector.Update: %s", err)
}
}()
expected := []MetricResult{
{labels: labelMap{"upstream_host": "unknown", "slot_name": "unknown"}, value: 0, metricType: dto.MetricType_GAUGE},
{labels: labelMap{"upstream_host": "unknown", "slot_name": "unknown"}, value: 0, metricType: dto.MetricType_COUNTER},
{labels: labelMap{"upstream_host": "unknown", "slot_name": "unknown"}, value: 0, metricType: dto.MetricType_GAUGE},
{labels: labelMap{"upstream_host": "unknown", "slot_name": "unknown"}, value: 0, metricType: dto.MetricType_COUNTER},
{labels: labelMap{"upstream_host": "unknown", "slot_name": "unknown"}, value: 0, metricType: dto.MetricType_GAUGE},
{labels: labelMap{"upstream_host": "unknown", "slot_name": "unknown"}, value: 0, metricType: dto.MetricType_COUNTER},
{labels: labelMap{"upstream_host": "unknown", "slot_name": "unknown"}, value: 0, metricType: dto.MetricType_COUNTER},
{labels: labelMap{"upstream_host": "unknown", "slot_name": "unknown"}, value: 0, metricType: dto.MetricType_COUNTER},
{labels: labelMap{"upstream_host": "unknown", "slot_name": "unknown"}, value: 0, metricType: dto.MetricType_COUNTER},
{labels: labelMap{"upstream_host": "unknown", "slot_name": "unknown"}, value: 0, metricType: dto.MetricType_GAUGE},
}
convey.Convey("Metrics comparison", t, func() {
for _, expect := range expected {
m := readMetric(<-ch)
convey.So(expect, convey.ShouldResemble, m)
}
})
if err := mock.ExpectationsWereMet(); err != nil {
t.Errorf("there were unfulfilled exceptions: %s", err)
}

}