metrics

package
v5.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2026 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Overview

Package metrics is responsible for reading and writing metric definitions.

At the moment, metric definitions support two storages:

  • PostgreSQL database
  • YAML file

Content

  • `postgres*.go` files cover the functionality for the PostgreSQL database.
  • `yaml*.go` files cover the functionality for the YAML file.
  • `metrics.yaml` holds all default metrics and presets.
  • `default.go` provides access to default metrics.

Index

Constants

View Source
const (
	EpochColumnName string = "epoch_ns" // this column (epoch in nanoseconds) is expected in every metric query
	TagPrefix       string = "tag_"
)
View Source
const MigrationsCount = 2

MigrationsCount is the total number of migrations in pgwatch.migration table

Variables

View Source
var (
	ErrNeedsMigration = errors.New("config database schema is outdated, please run migrations using `pgwatch config upgrade` command")
	ErrMetricNotFound = errors.New("metric not found")
	ErrPresetNotFound = errors.New("preset not found")
	ErrInvalidMetric  = errors.New("invalid metric")
	ErrInvalidPreset  = errors.New("invalid preset")
	ErrMetricExists   = errors.New("metric already exists")
	ErrPresetExists   = errors.New("preset already exists")
)

Functions

func GetDefaultBuiltInMetrics

func GetDefaultBuiltInMetrics() []string

func RowToMeasurement

func RowToMeasurement(row pgx.CollectableRow) (map[string]any, error)

RowToMap returns a map scanned from row.

Types

type CmdOpts

type CmdOpts struct {
	Metrics                      string `` /* 137-byte string literal not displayed */
	DirectOSStats                bool   `` /* 191-byte string literal not displayed */
	InstanceLevelCacheMaxSeconds int64  `` /* 264-byte string literal not displayed */
	EmergencyPauseTriggerfile    string `` /* 227-byte string literal not displayed */
}

CmdOpts specifies metric command-line options

func (CmdOpts) CacheAge

func (c CmdOpts) CacheAge() time.Duration

type ExtensionInfo

type ExtensionInfo struct {
	ExtName       string `yaml:"ext_name"`
	ExtMinVersion string `yaml:"ext_min_version"`
}

type ExtensionOverrides

type ExtensionOverrides struct {
	TargetMetric              string          `yaml:"target_metric"`
	ExpectedExtensionVersions []ExtensionInfo `yaml:"expected_extension_versions"`
}

type Measurement

type Measurement map[string]any

func NewMeasurement

func NewMeasurement(epoch int64) Measurement

func (Measurement) GetEpoch

func (m Measurement) GetEpoch() int64

func (*Measurement) ScanRow

func (m *Measurement) ScanRow(rows pgx.Rows) error

type MeasurementEnvelope

type MeasurementEnvelope struct {
	DBName     string
	MetricName string
	CustomTags map[string]string
	Data       Measurements
}

type Measurements

type Measurements []map[string]any

func (Measurements) DeepCopy

func (m Measurements) DeepCopy() Measurements

func (Measurements) GetEpoch

func (m Measurements) GetEpoch() int64

func (Measurements) IsEpochSet

func (m Measurements) IsEpochSet() bool

func (Measurements) Touch

func (m Measurements) Touch()

Touch updates the last modified time of the metric definitions

type Metric

type Metric struct {
	SQLs            SQLs
	InitSQL         string   `yaml:"init_sql,omitempty"`
	NodeStatus      string   `yaml:"node_status,omitempty"`
	Gauges          []string `yaml:",omitempty"`
	IsInstanceLevel bool     `yaml:"is_instance_level,omitempty"`
	StorageName     string   `yaml:"storage_name,omitempty"`
	Description     string   `yaml:"description,omitempty"`
}

func (Metric) GetSQL

func (m Metric) GetSQL(version int) string

func (Metric) PrimaryOnly

func (m Metric) PrimaryOnly() bool

func (Metric) StandbyOnly

func (m Metric) StandbyOnly() bool

type MetricAttrs

type MetricAttrs struct {
	ExtensionVersionOverrides []ExtensionOverrides `yaml:"extension_version_based_overrides,omitempty"`
	IsPrivate                 bool                 `yaml:"is_private,omitempty"`                // used only for extension overrides currently and ignored otherwise
	DisabledDays              string               `yaml:"disabled_days,omitempty"`             // Cron style, 0 = Sunday. Ranges allowed: 0,2-4
	DisableTimes              []string             `yaml:"disabled_times,omitempty"`            // "11:00-13:00"
	StatementTimeoutSeconds   int64                `yaml:"statement_timeout_seconds,omitempty"` // overrides per monitored DB settings
}

type MetricDefs

type MetricDefs map[string]Metric

type Metrics

type Metrics struct {
	MetricDefs MetricDefs `yaml:"metrics,omitempty"`
	PresetDefs PresetDefs `yaml:"presets,omitempty"`
}

func GetDefaultMetrics

func GetDefaultMetrics() (metrics *Metrics)

func (*Metrics) FilterByNames

func (m *Metrics) FilterByNames(names []string) (*Metrics, error)

FilterByNames returns a new Metrics struct containing only the specified metrics and/or presets. When a preset is requested, it includes both the preset definition and all its metrics. If names is empty, returns a full copy of all metrics and presets. Returns an error if any name is not found.

type Preset

type Preset struct {
	Description string
	Metrics     map[string]float64
}

type PresetDefs

type PresetDefs map[string]Preset

type Reader

type Reader interface {
	GetMetrics() (*Metrics, error)
}

type ReaderWriter

type ReaderWriter interface {
	Reader
	Writer
}

func NewDefaultMetricReader

func NewDefaultMetricReader(context.Context) (ReaderWriter, error)

NewDefaultMetricReader creates a new default metric reader with an empty path.

func NewPostgresMetricReaderWriter

func NewPostgresMetricReaderWriter(ctx context.Context, connstr string) (ReaderWriter, error)

func NewPostgresMetricReaderWriterConn

func NewPostgresMetricReaderWriterConn(ctx context.Context, conn db.PgxPoolIface) (ReaderWriter, error)

func NewYAMLMetricReaderWriter

func NewYAMLMetricReaderWriter(ctx context.Context, path string) (ReaderWriter, error)

type SQLs

type SQLs map[int]string

type Writer

type Writer interface {
	WriteMetrics(metricDefs *Metrics) error
	DeleteMetric(metricName string) error
	DeletePreset(presetName string) error
	UpdateMetric(metricName string, metric Metric) error
	UpdatePreset(presetName string, preset Preset) error
	CreateMetric(metricName string, metric Metric) error
	CreatePreset(presetName string, preset Preset) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL