metrics

package
v1.28.0-rc2 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2025 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Overview

Package metrics enables to expose a set of metrics and collectors on a given postgres instance

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ColumnMapping

type ColumnMapping struct {
	Usage       ColumnUsage `yaml:"usage"`
	Description string      `yaml:"description"`

	// Mapping is an optional column mapping for MAPPEDMETRIC
	Mapping map[string]float64 `yaml:"metric_mapping"`

	// SupportedVersions are the semantic version ranges which are supported.
	SupportedVersions string `yaml:"pg_version"`

	// Name allows overriding the key name when naming the column
	Name string `yaml:"name"`
}

ColumnMapping is a representation of a prometheus descriptor map

func (ColumnMapping) ToMetricMap

func (columnMapping ColumnMapping) ToMetricMap(
	columnName, namespace string, variableLabels []string,
) MetricMapSet

ToMetricMap transform this query mapping in the metadata for a Prometheus metric. Since a query from the user can result in multiple metrics being generated (histograms are an example of this behavior), we are returning a mapping, which therefore should be collected together

type ColumnUsage

type ColumnUsage string

ColumnUsage represent how a certain column should be used

const (
	// DISCARD means that this column should be ignored
	DISCARD ColumnUsage = "DISCARD"

	// LABEL means use this column as a label
	LABEL ColumnUsage = "LABEL"

	// COUNTER means use this column as a counter
	COUNTER ColumnUsage = "COUNTER"

	// GAUGE means use this column as a gauge
	GAUGE ColumnUsage = "GAUGE"

	// MAPPEDMETRIC means use this column with the supplied mapping of text values
	MAPPEDMETRIC ColumnUsage = "MAPPEDMETRIC" // Use this column with the supplied mapping of text values

	// DURATION means use this column as a text duration (in milliseconds)
	DURATION ColumnUsage = "DURATION"

	// HISTOGRAM means use this column as an histogram
	HISTOGRAM ColumnUsage = "HISTOGRAM"
)

type Mapping

type Mapping map[string]ColumnMapping

Mapping decide how a certain field, extracted from the query's result, should be used

type MetricMap

type MetricMap struct {
	// The name of this metric
	Name string

	// Is this metric a label?
	Label bool

	// Should metric be discarded during metrics generation?
	// Labels should be discarded because they are used while generating
	// names and not values
	Discard bool

	// Should metric be treated as a histogram?
	Histogram bool

	// Vtype is the prometheus valueType
	Vtype prometheus.ValueType

	// Desc is the prometheus descriptor
	Desc *prometheus.Desc

	// Conversion is the function to convert a dynamic type into a value suitable
	// for the exporter
	Conversion func(interface{}) (float64, bool) `json:"-"`
}

MetricMap stores the prometheus metric description which a given column will be mapped to by the collector

type MetricMapSet

type MetricMapSet map[string]MetricMap

MetricMapSet is a set of MetricMap, usually associated to a UserQuery

type PluginCollector added in v1.27.0

type PluginCollector interface {
	// Collect collects the metrics from the plugins
	Collect(ctx context.Context, ch chan<- prometheus.Metric, cluster *apiv1.Cluster) error
	// Describe describes the metrics from the plugins
	Describe(ctx context.Context, ch chan<- *prometheus.Desc, cluster *apiv1.Cluster)
}

PluginCollector is the interface for collecting metrics from plugins

func NewPluginCollector added in v1.27.0

func NewPluginCollector(
	pluginRepository repository.Interface,
) PluginCollector

NewPluginCollector creates a new PluginCollector that collects metrics from plugins

type QueriesCollector

type QueriesCollector struct {
	// contains filtered or unexported fields
}

QueriesCollector is the implementation of PgCollector for a certain collection of custom queries supplied by the user

func NewQueriesCollector

func NewQueriesCollector(
	name string,
	instance *postgres.Instance,
	defaultDBName string,
) *QueriesCollector

NewQueriesCollector creates a new PgCollector working over a set of custom queries supplied by the user

func (*QueriesCollector) Collect

func (q *QueriesCollector) Collect(ch chan<- prometheus.Metric)

Collect sends the pre-computed metrics to the output channel. These metrics were cached during the last Update() call and are not fetched from the database during collection.

func (*QueriesCollector) Describe

func (q *QueriesCollector) Describe(ch chan<- *prometheus.Desc)

Describe implements the prometheus.Collector and defines the metrics with return

func (*QueriesCollector) InjectUserQueries

func (q *QueriesCollector) InjectUserQueries(defaultQueries UserQueries)

InjectUserQueries injects the passed queries

func (*QueriesCollector) Name

func (q *QueriesCollector) Name() string

Name returns the name of this collector, as supplied by the user in the configMap

func (*QueriesCollector) ParseQueries

func (q *QueriesCollector) ParseQueries(customQueries []byte) error

ParseQueries parses a YAML file containing custom queries and add it to the set of gathered one

func (*QueriesCollector) RecordCacheHit added in v1.28.0

func (q *QueriesCollector) RecordCacheHit()

RecordCacheHit increments the cache hit counter for the current cache period

func (*QueriesCollector) ShouldUpdate added in v1.28.0

func (q *QueriesCollector) ShouldUpdate(ttl time.Duration) bool

ShouldUpdate finds if the metrics from queries need to be rerun, or the cached values can be used

func (*QueriesCollector) Update added in v1.28.0

func (q *QueriesCollector) Update() error

Update recomputes the metrics from the user queries

type QueryRunner added in v1.28.0

type QueryRunner struct {
	// contains filtered or unexported fields
}

QueryRunner computes a custom user query and generates Prometheus metrics from the results

type UserQueries

type UserQueries map[string]UserQuery

UserQueries is a collection of custom queries

func ParseQueries

func ParseQueries(content []byte) (UserQueries, error)

ParseQueries parse a YAML file containing custom queries

type UserQuery

type UserQuery struct {
	Query           string    `yaml:"query"`
	PredicateQuery  string    `yaml:"predicate_query"`
	Metrics         []Mapping `yaml:"metrics"`
	Master          bool      `yaml:"master"` // wokeignore:rule=master
	Primary         bool      `yaml:"primary"`
	CacheSeconds    uint64    `yaml:"cache_seconds"`
	RunOnServer     string    `yaml:"runonserver"`
	TargetDatabases []string  `yaml:"target_databases"`
	// Name allows overriding the key name in the metric namespace
	Name string `yaml:"name"`
}

UserQuery represent a query created by the user

func (UserQuery) ToMetricMap

func (userQuery UserQuery) ToMetricMap(namespace string) (result MetricMapSet, variableLabels VariableSet)

ToMetricMap transform this user query in the metadata for a collection of Prometheus metrics, returning the metrics map and the list of variable labels

type VariableSet

type VariableSet []string

VariableSet is a set of strings used as a collection of prometheus labels

Directories

Path Synopsis
Package histogram contain histogram-metrics related functions
Package histogram contain histogram-metrics related functions

Jump to

Keyboard shortcuts

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