advmetrics

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package advmetrics implements github.com/VictoriaMetrics/metrics library initialization using settings provided in the github.com/onlineconf/onlineconf.

To collect metrics, you can use:

This library provides some high-level metrics collectors:

Settings and defaults

  • /mode - "pull", "push", or "hybrid". Default: "push"
  • /push_url - Metrics server URL in push or hybrid mode. Mandatory.
  • /push_interval - The interval between periodic pushes. 0 is used to turn off periodic pushes, only the final push when the [Metrics.Stop] method is called is performed. Use 0 for short-lived jobs or in hybrid mode. Default: 30s in push mode and 0 in hybrid mode.
  • /push_timeout - Affects the final push only (performed on the app shutdown in push or hybrid modes). Default: 1m
  • /namespace_env - Push only: add the "namespace" label with the value of this env var. Set to "" to disable the "namespace" label. Default: "NAMESPACE"
  • /extra_labels - Push only: comma-separated list of extra labels added to each metric (e.g., the standard env label, which is redundant when dev and prod environments are fully separated, including the metrics receivers). Format: label=value.
  • /pull_endpoint - Metrics endpoint in pull or hybrid mode. Default: /metrics
  • /pull_bind_addr - Used in pull or hybrid mode. Default: localhost:6000
  • /pull_timeout - Maximum duration for reading the pull request (http.Server.ReadTimeout). Default: 30s
  • /write_system_metrics - Perform metrics.WriteProcessMetrics and metrics.WriteFDMetrics during each push or pull. Affects all modes. Default: false.

Currently, a restart is required to apply any setting except for /write_system_metrics.

Example

// on server startup
ms := advmetricsset.New()
m, err := advmetrics.New(conf.Subtree("/victoria"), ms)
if err != nil {
	log.Fatal(err)
}

err = m.Start()
if err != nil {
	log.Fatal(err)
}

// on server shutdown
err = m.Stop()
if err != nil {
	log.Fatal(err)
}

Index

Constants

View Source
const (
	DefaultPushInterval = 30 * time.Second
	DefaultPushTimeout  = 1 * time.Minute // final push only
	DefaultPullEndpoint = "/metrics"
	DefaultPullBindAddr = "localhost:6000"
	DefaultPullTimeout  = 30 * time.Second
	DefaultNamespaceEnv = "NAMESPACE"
)

Default settings

View Source
const (
	ModePull    = "pull"
	ModePush    = "push"
	ModeHybrid  = "hybrid"
	DefaultMode = ModePush
)

Modes supported by New.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hybrid

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

Hybrid mode combines both Pull and Push modes. It acts in Pull mode, and provides the final Push to prevent metric losses during the service shutdown. Implements Metrics.

func NewHybrid

func NewHybrid(conf OnlineConf, job string, writer PrometheusWriter) *Hybrid

NewHybrid creates a Hybrid instance.

func (*Hybrid) Start

func (h *Hybrid) Start() error

Start starts both Pull and Push. The difference with plain Push mode is that the periodic push is disabled by default (if the /push_interval setting is omitted), and only the final push is performed when the application is shut down.

func (*Hybrid) StartCtx

func (h *Hybrid) StartCtx(ctx context.Context) error

StartCtx starts both Pull and Push. The difference with plain Push mode is that the periodic push is disabled by default (if the /push_interval setting is omitted), and only the final push is performed when the application is shut down.

When ctx is cancelled, the metrics service is stopped.

func (*Hybrid) Stop

func (h *Hybrid) Stop() error

Stop stops both Pull and Push. The final push is performed.

type Metrics

type Metrics interface {
	// Start starts the metrics service.
	Start() error

	// StartCtx starts the metrics service. When ctx is cancelled, the metrics
	// service is stopped.
	StartCtx(context.Context) error

	// Stop stops the metrics service. In push or hybrid mode, the final push
	// is performed.
	//
	// Application should not exit before the Stop method returns
	// to prevent metric data losses.
	Stop() error
}

Metrics is implemented by Push, Pull and Hybrid types.

func New

func New(conf OnlineConf, job string, writer PrometheusWriter) (Metrics, error)

New returns Push, Pull or Hybrid instance depending on the /mode setting. By default, Push is returned.

The job parameter is used in push and hybrid modes only to specify the `job` label (an application name).

type OnlineConf

type OnlineConf interface {
	Path(string) string
	GetBool(string, bool) bool
	GetString(string, string) string
	GetStringIfExists(string) (string, bool)
	GetDuration(string, time.Duration) time.Duration
	GetDurationIfExists(string) (time.Duration, bool)
}

type PrometheusWriter

type PrometheusWriter interface {
	WritePrometheus(w io.Writer)
}

PrometheusWriter is any metrics.Set-like type.

type Pull

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

Pull implements Metrics.

func NewPull

func NewPull(conf OnlineConf, writer PrometheusWriter) *Pull

NewPull creates a Pull instance.

Note that no labels are added to metrics automatically, unlike the push mode, since it's the scrape process (vmagent) responsibility.

func (*Pull) Start

func (p *Pull) Start() error

Start starts an http server on /pull_bind_addr and handles the /pull_endpoint path. Any http method is supported.

func (*Pull) StartCtx

func (p *Pull) StartCtx(ctx context.Context) error

StartCtx starts an http server on /pull_bind_addr and handles the /pull_endpoint path. Any http method is supported.

When ctx is cancelled, the metrics service is stopped.

func (*Pull) Stop

func (p *Pull) Stop() error

Stop stops an http server started by Pull.Start or Pull.StartCtx.

type Push

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

Push implements Metrics.

These labels are added to each metric before pushing:

  • job: the value of the job parameter (i.e. an application name)
  • instance: the hostname (e.g. the dynamic pod name of a k8s deployment)
  • namespace: the value of $NAMESPACE env var (use /namespace_env setting to override)
  • a custom list of labels from the /extra_labels setting

func NewPush

func NewPush(conf OnlineConf, job string, writer PrometheusWriter) *Push

NewPush creates a Push instance.

func (*Push) Start

func (p *Push) Start() error

Start starts the periodic push to /push_url if /push_interval is non-zero.

func (*Push) StartCtx

func (p *Push) StartCtx(ctx context.Context) error

StartCtx starts the periodic push if /push_interval is non-zero. When ctx is cancelled, periodic push is stopped.

If /push_interval is zero (i.e., periodic pushes are disabled), a final push isn't performed on context cancelation. Moreover, ctx isn't used at all, so there's no reason to use this method if you will never use periodic pushes. Use Push.Stop to send data when periodic pushes are disabled.

func (*Push) Stop

func (p *Push) Stop() error

Stop stops the periodic push if /push_interval is non-zero. All pending data are pushed to the metrics server regardless of whether periodic pushes are enabled or not.

Application should not exit before the Stop method returns to prevent metric data losses.

Directories

Path Synopsis
Package httpmetrics provides net/http-compatible Middleware for collecting the http server request metrics.
Package httpmetrics provides net/http-compatible Middleware for collecting the http server request metrics.
internal
test/mock/onlineconf
Package mockonlineconf is a generated GoMock package.
Package mockonlineconf is a generated GoMock package.
Package pgxmetrics provides Tracer type for collecting the database metrics.
Package pgxmetrics provides Tracer type for collecting the database metrics.
Package advmetricsset provides the metrics.Set wrappers Set and SetWithPrefix with metric name prefix support and high-level labels API, which is less error-prone and 2-3x faster than making label names with fmt.Sprintf, which is commonly used along with the metrics package.
Package advmetricsset provides the metrics.Set wrappers Set and SetWithPrefix with metric name prefix support and high-level labels API, which is less error-prone and 2-3x faster than making label names with fmt.Sprintf, which is commonly used along with the metrics package.

Jump to

Keyboard shortcuts

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