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:
- github.com/my-mail-ru/go-adv-metrics/set - prefixed metric names with fast high-level labels API, or
- use the VictoriaMetrics library directly: metrics.Set, or
- any other type that implements PrometheusWriter interface.
This library provides some high-level metrics collectors:
- net/http-compatible middleware for collecting the http server metrics: github.com/my-mail-ru/go-adv-metrics/httpmetrics
- pgx-compatible tracker for collecting the PostgreSQL database query and pool metrics: github.com/my-mail-ru/go-adv-metrics/pgxmetrics
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 ¶
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
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 ¶
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 ¶
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.
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 PrometheusWriter ¶
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 ¶
Start starts an http server on /pull_bind_addr and handles the /pull_endpoint path. Any http method is supported.
func (*Pull) StartCtx ¶
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 ¶
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) StartCtx ¶
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.
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. |