Skip to content

Commit 105a5de

Browse files
committed
Move pg_post_master query to collector
Signed-off-by: Felix Yuan <felix.yuan@reddit.com>
1 parent 6e671f2 commit 105a5de

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

collector/pg_postmaster.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
// Copyright 2023 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package collector
15+
16+
import (
17+
"context"
18+
"database/sql"
19+
20+
"github.com/prometheus/client_golang/prometheus"
21+
)
22+
23+
func init() {
24+
registerCollector("postmaster", defaultEnabled, NewPGPostmasterCollector)
25+
}
26+
27+
type PGPostmasterCollector struct {
28+
}
29+
30+
func NewPGPostmasterCollector(collectorConfig) (Collector, error) {
31+
return &PGPostmasterCollector{}, nil
32+
}
33+
34+
var pgPostmaster = map[string]*prometheus.Desc{
35+
"start_time_seconds": prometheus.NewDesc(
36+
"pg_postmaster_start_time_seconds",
37+
"Time at which postmaster started",
38+
[]string{"process_name"}, nil,
39+
),
40+
}
41+
42+
func (c *PGPostmasterCollector) Update(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
43+
row := db.QueryRowContext(ctx,
44+
`SELECT
45+
pg_postmaster_start_time
46+
from pg_postmaster_start_time();`)
47+
48+
var startTimeSeconds float64
49+
err := row.Scan(&startTimeSeconds)
50+
if err != nil {
51+
return err
52+
}
53+
ch <- prometheus.MustNewConstMetric(
54+
pgPostmaster["start_time_seconds"],
55+
prometheus.GaugeValue, startTimeSeconds, "postmaster",
56+
)
57+
return nil
58+
}

0 commit comments

Comments
 (0)