mailserver: add DB query metrics

Useful for debugging spikes in I/O caused by SQL queries.

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2022-07-26 13:34:11 +02:00
parent 8191f24ef3
commit f2ef922e55
No known key found for this signature in database
GPG Key ID: 09AA5403E54D9931
3 changed files with 16 additions and 0 deletions

View File

@ -128,6 +128,8 @@ func (db *LevelDB) BuildIterator(query CursorQuery) (Iterator, error) {
defer recoverLevelDBPanics("BuildIterator")
i := db.ldb.NewIterator(&util.Range{Start: query.start, Limit: query.end}, nil)
envelopeQueriesCounter.WithLabelValues("unknown", "unknown").Inc()
// seek to the end as we want to return envelopes in a descending order
if len(query.cursor) == CursorLength {
i.Seek(query.cursor)

View File

@ -130,20 +130,26 @@ func (i *PostgresDB) BuildIterator(query CursorQuery) (Iterator, error) {
stmtString := "SELECT id, data FROM envelopes"
var historyRange string
if len(query.cursor) > 0 {
args = append(args, query.start, query.cursor)
// If we have a cursor, we don't want to include that envelope in the result set
stmtString += " " + "WHERE id >= $1 AND id < $2"
historyRange = "partial" //nolint: goconst
} else {
args = append(args, query.start, query.end)
stmtString += " " + "WHERE id >= $1 AND id <= $2"
historyRange = "full" //nolint: goconst
}
var filterRange string
if len(query.topics) > 0 {
args = append(args, pq.Array(query.topics))
stmtString += " " + "AND topic = any($3)"
filterRange = "partial" //nolint: goconst
} else {
stmtString += " " + fmt.Sprintf("AND bloom & b'%s'::bit(512) = bloom", toBitString(query.bloom))
filterRange = "full" //nolint: goconst
}
// Positional argument depends on the fact whether the query uses topics or bloom filter.
@ -156,10 +162,13 @@ func (i *PostgresDB) BuildIterator(query CursorQuery) (Iterator, error) {
if err != nil {
return nil, err
}
envelopeQueriesCounter.WithLabelValues(filterRange, historyRange).Inc()
rows, err := stmt.Query(args...)
if err != nil {
return nil, err
}
return &postgresIterator{rows}, nil
}

View File

@ -60,6 +60,10 @@ var (
Help: "Size of envelopes saved.",
Buckets: prom.ExponentialBuckets(1024, 2, 11),
}, []string{"db"})
envelopeQueriesCounter = prom.NewCounterVec(prom.CounterOpts{
Name: "mailserver_envelope_queries_total",
Help: "Number of queries for envelopes in the DB.",
}, []string{"filter", "history"})
)
func init() {
@ -76,4 +80,5 @@ func init() {
prom.MustRegister(archivedErrorsCounter)
prom.MustRegister(archivedEnvelopesGauge)
prom.MustRegister(archivedEnvelopeSizeMeter)
prom.MustRegister(envelopeQueriesCounter)
}