2023-06-08 21:28:04 +00:00
|
|
|
// 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"
|
2023-06-27 06:07:59 +00:00
|
|
|
"database/sql"
|
2023-06-08 21:28:04 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
)
|
|
|
|
|
2023-06-13 15:28:11 +00:00
|
|
|
const postmasterSubsystem = "postmaster"
|
|
|
|
|
2023-06-08 21:28:04 +00:00
|
|
|
func init() {
|
2023-06-13 15:28:11 +00:00
|
|
|
registerCollector(postmasterSubsystem, defaultEnabled, NewPGPostmasterCollector)
|
2023-06-08 21:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type PGPostmasterCollector struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewPGPostmasterCollector(collectorConfig) (Collector, error) {
|
|
|
|
return &PGPostmasterCollector{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
pgPostMasterStartTimeSeconds = prometheus.NewDesc(
|
2023-06-13 15:28:11 +00:00
|
|
|
prometheus.BuildFQName(
|
|
|
|
namespace,
|
|
|
|
postmasterSubsystem,
|
|
|
|
"start_time_seconds",
|
|
|
|
),
|
2023-06-08 21:28:04 +00:00
|
|
|
"Time at which postmaster started",
|
|
|
|
[]string{}, nil,
|
|
|
|
)
|
|
|
|
|
|
|
|
pgPostmasterQuery = "SELECT pg_postmaster_start_time from pg_postmaster_start_time();"
|
|
|
|
)
|
|
|
|
|
2023-03-30 01:25:27 +00:00
|
|
|
func (c *PGPostmasterCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
|
|
|
|
db := instance.getDB()
|
2023-06-08 21:28:04 +00:00
|
|
|
row := db.QueryRowContext(ctx,
|
|
|
|
pgPostmasterQuery)
|
|
|
|
|
2023-06-27 06:07:59 +00:00
|
|
|
var startTimeSeconds sql.NullFloat64
|
2023-06-08 21:28:04 +00:00
|
|
|
err := row.Scan(&startTimeSeconds)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-06-27 06:07:59 +00:00
|
|
|
startTimeSecondsMetric := 0.0
|
|
|
|
if startTimeSeconds.Valid {
|
|
|
|
startTimeSecondsMetric = startTimeSeconds.Float64
|
|
|
|
}
|
2023-06-08 21:28:04 +00:00
|
|
|
ch <- prometheus.MustNewConstMetric(
|
|
|
|
pgPostMasterStartTimeSeconds,
|
2023-06-27 06:07:59 +00:00
|
|
|
prometheus.GaugeValue, startTimeSecondsMetric,
|
2023-06-08 21:28:04 +00:00
|
|
|
)
|
|
|
|
return nil
|
|
|
|
}
|