postgres_exporter/collector/pg_postmaster.go
Joe Adams ab33346cbd
Add the instance struct to handle connections
The intent is to use the instance struct to hold the connection to the database as well as metadata about the instance. Currently this metadata only includes the version of postgres for the instance which can be used in the collectors to decide what query to run. In the future this could hold more metadata but for now it keeps the Collector interface arguments to a reasonable number.

Signed-off-by: Joe Adams <github@joeadams.io>
2023-06-21 20:36:59 -04:00

65 lines
1.7 KiB
Go

// 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"
"github.com/prometheus/client_golang/prometheus"
)
const postmasterSubsystem = "postmaster"
func init() {
registerCollector(postmasterSubsystem, defaultEnabled, NewPGPostmasterCollector)
}
type PGPostmasterCollector struct {
}
func NewPGPostmasterCollector(collectorConfig) (Collector, error) {
return &PGPostmasterCollector{}, nil
}
var (
pgPostMasterStartTimeSeconds = prometheus.NewDesc(
prometheus.BuildFQName(
namespace,
postmasterSubsystem,
"start_time_seconds",
),
"Time at which postmaster started",
[]string{}, nil,
)
pgPostmasterQuery = "SELECT pg_postmaster_start_time from pg_postmaster_start_time();"
)
func (c *PGPostmasterCollector) Update(ctx context.Context, instance *instance, ch chan<- prometheus.Metric) error {
db := instance.getDB()
row := db.QueryRowContext(ctx,
pgPostmasterQuery)
var startTimeSeconds float64
err := row.Scan(&startTimeSeconds)
if err != nil {
return err
}
ch <- prometheus.MustNewConstMetric(
pgPostMasterStartTimeSeconds,
prometheus.GaugeValue, startTimeSeconds,
)
return nil
}