Skip to content

Commit

Permalink
feat: add custom collector for statement summary pre-release
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio-form3 committed Nov 18, 2024
1 parent cd450cd commit a69bc25
Show file tree
Hide file tree
Showing 2 changed files with 182 additions and 0 deletions.
118 changes: 118 additions & 0 deletions collector/pg_stat_statements_summary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package collector

import (
"context"
"database/sql"

"github.com/go-kit/log"
"github.com/prometheus/client_golang/prometheus"
)

const statStatementsSummarySubsystem = "stat_statements_summary"

func init() {
registerCollector(statStatementsSummarySubsystem, defaultDisabled, NewPGStatStatementsSummaryCollector)
}

type PGStatStatementsSummaryCollector struct {
log log.Logger
}

func NewPGStatStatementsSummaryCollector(config collectorConfig) (Collector, error) {
return &PGStatStatementsSummaryCollector{log: config.logger}, nil
}

var (
statSTatementsSummaryCallsTotal = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statStatementsSubsystem, "calls_total"),
"Number of times executed",
[]string{"datname"},
prometheus.Labels{},
)
statStatementsSummarySecondsTotal = prometheus.NewDesc(
prometheus.BuildFQName(namespace, statStatementsSubsystem, "seconds_total"),
"Total time spent in the statement, in seconds",
[]string{"datname"},
prometheus.Labels{},
)

pgStatStatementsSummaryQuery = `SELECT
pg_database.datname,
SUM(pg_stat_statements.calls) as calls_total,
SUM(pg_stat_statements.total_exec_time) / 1000.0 as seconds_total
FROM pg_stat_statements
JOIN pg_database
ON pg_database.oid = pg_stat_statements.dbid
WHERE
total_exec_time > (
SELECT percentile_cont(0.1)
WITHIN GROUP (ORDER BY total_exec_time)
FROM pg_stat_statements
)
GROUP BY pg_database.datname;`
)

func (PGStatStatementsSummaryCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
query := pgStatStatementsSummaryQuery

db := instance.getDB()
rows, err := db.QueryContext(ctx, query)

if err != nil {
return err
}
defer rows.Close()
for rows.Next() {
var datname sql.NullString
var callsTotal sql.NullInt64
var secondsTotal sql.NullFloat64

if err := rows.Scan(&datname, &callsTotal, &secondsTotal); err != nil {
return err
}

datnameLabel := "unknown"
if datname.Valid {
datnameLabel = datname.String
}

callsTotalMetric := 0.0
if callsTotal.Valid {
callsTotalMetric = float64(callsTotal.Int64)
}
ch <- prometheus.MustNewConstMetric(
statSTatementsSummaryCallsTotal,
prometheus.CounterValue,
callsTotalMetric,
datnameLabel,
)

secondsTotalMetric := 0.0
if secondsTotal.Valid {
secondsTotalMetric = secondsTotal.Float64
}
ch <- prometheus.MustNewConstMetric(
statStatementsSummarySecondsTotal,
prometheus.CounterValue,
secondsTotalMetric,
datnameLabel,
)
}
if err := rows.Err(); err != nil {
return err
}
return nil
}
64 changes: 64 additions & 0 deletions collector/pg_stat_statements_summary_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright 2023 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package collector

import (
"context"
"testing"

"github.com/DATA-DOG/go-sqlmock"
"github.com/blang/semver/v4"
"github.com/prometheus/client_golang/prometheus"
dto "github.com/prometheus/client_model/go"
"github.com/smartystreets/goconvey/convey"
)

func TestPGStateStatementsSummaryCollector(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, version: semver.MustParse("13.3.7")}

columns := []string{"datname", "calls_total", "seconds_total"}
rows := sqlmock.NewRows(columns).
AddRow("postgres", 5, 0.4)
mock.ExpectQuery(sanitizeQuery(pgStatStatementsSummaryQuery)).WillReturnRows(rows)

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

if err := c.Update(context.Background(), inst, ch); err != nil {
t.Errorf("Error calling PGStatStatementsSummaryCollector.Update: %s", err)
}
}()

expected := []MetricResult{
{labels: labelMap{"datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 5},
{labels: labelMap{"datname": "postgres"}, metricType: dto.MetricType_COUNTER, value: 0.4},
}

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)
}
}

0 comments on commit a69bc25

Please sign in to comment.