metrics

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 22, 2025 License: MIT Imports: 25 Imported by: 0

README

Build Status GoDoc Go Report codecov

metrics - lightweight package for exporting metrics in Prometheus format

Features
  • Lightweight. Has minimal number of third-party dependencies and all these deps are small. See this article for details.
  • Easy to use. See the API docs.
  • Fast.
  • Allows exporting distinct metric sets via distinct endpoints. See Set.
  • Supports easy-to-use histograms, which just work without any tuning. Read more about VictoriaMetrics histograms at this article.
  • Can push metrics to VictoriaMetrics or to any other remote storage, which accepts metrics in Prometheus text exposition format. See these docs.
Limitations
Usage
import "github.com/VictoriaMetrics/metrics"

// Register various metrics.
// Metric name may contain labels in Prometheus format - see below.
var (
	// Register counter without labels.
	requestsTotal = metrics.NewCounter("requests_total")

	// Register summary with a single label.
	requestDuration = metrics.NewSummary(`requests_duration_seconds{path="/foobar/baz"}`)

	// Register gauge with two labels.
	queueSize = metrics.NewGauge(`queue_size{queue="foobar",topic="baz"}`, func() float64 {
		return float64(foobarQueue.Len())
	})

	// Register histogram with a single label.
	responseSize = metrics.NewHistogram(`response_size{path="/foo/bar"}`)
)

// ...
func requestHandler() {
	// Increment requestTotal counter.
	requestsTotal.Inc()

	startTime := time.Now()
	processRequest()
	// Update requestDuration summary.
	requestDuration.UpdateDuration(startTime)

	// Update responseSize histogram.
	responseSize.Update(responseSize)
}

// Expose the registered metrics at `/metrics` path.
http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
	metrics.WritePrometheus(w, true)
})

// ... or push registered metrics every 10 seconds to http://victoria-metrics:8428/api/v1/import/prometheus
// with the added `instance="foobar"` label to all the pushed metrics.
metrics.InitPush("http://victoria-metrics:8428/api/v1/import/prometheus", 10*time.Second, `instance="foobar"`, true)

By default, exposed metrics do not have TYPE or HELP meta information. Call ExposeMetadata(true) in order to generate TYPE and HELP meta information per each metric.

See docs for more info.

Users
FAQ
Why the metrics API isn't compatible with github.com/prometheus/client_golang?

Because the github.com/prometheus/client_golang is too complex and is hard to use.

Why the metrics.WritePrometheus doesn't expose documentation for each metric?

Because this documentation is ignored by Prometheus. The documentation is for users. Just give meaningful names to the exported metrics or add comments in the source code or in other suitable place explaining each metric exposed from your application.

How to implement CounterVec in metrics?

Just use GetOrCreateCounter instead of CounterVec.With. See this example for details.

Why Histogram buckets contain vmrange labels instead of le labels like in Prometheus histograms?

Buckets with vmrange labels occupy less disk space compared to Promethes-style buckets with le labels, because vmrange buckets don't include counters for the previous ranges. VictoriaMetrics provides prometheus_buckets function, which converts vmrange buckets to Prometheus-style buckets with le labels. This is useful for building heatmaps in Grafana. Additionally, its' histogram_quantile function transparently handles histogram buckets with vmrange labels.

Documentation

Overview

Package metrics implements Prometheus-compatible metrics for applications.

This package is lightweight alternative to https://github.com/prometheus/client_golang with simpler API and smaller dependencies.

Usage:

  1. Register the required metrics via New* functions.
  2. Expose them to `/metrics` page via WritePrometheus.
  3. Update the registered metrics during application lifetime.

The package has been extracted from https://victoriametrics.com/

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExposeMetadata

func ExposeMetadata(v bool)

ExposeMetadata allows enabling adding TYPE and HELP metadata to the exposed metrics globally.

It is safe to call this method multiple times. It is allowed to change it in runtime. ExposeMetadata is set to false by default.

Example
metrics.ExposeMetadata(true)
defer metrics.ExposeMetadata(false)

s := metrics.NewSet()

sc := s.NewCounter("set_counter")
sc.Inc()

s.NewGauge(`unused_bytes{foo="bar"}`, func() float64 { return 58 })
s.NewGauge(`used_bytes{foo="bar"}`, func() float64 { return 42 })
s.NewGauge(`used_bytes{foo="baz"}`, func() float64 { return 43 })

h := s.NewHistogram(`request_duration_seconds{path="/foo/bar"}`)
h.Update(1)
h.Update(2)

s.NewSummary("response_size_bytes").Update(1)

// Dump metrics from s.
var bb bytes.Buffer
s.WritePrometheus(&bb)
fmt.Printf("set metrics:\n%s\n", bb.String())
Output:
set metrics:
# HELP request_duration_seconds
# TYPE request_duration_seconds histogram
request_duration_seconds_bucket{path="/foo/bar",vmrange="8.799e-01...1.000e+00"} 1
request_duration_seconds_bucket{path="/foo/bar",vmrange="1.896e+00...2.154e+00"} 1
request_duration_seconds_sum{path="/foo/bar"} 3
request_duration_seconds_count{path="/foo/bar"} 2
# HELP response_size_bytes
# TYPE response_size_bytes summary
response_size_bytes_sum 1
response_size_bytes_count 1
response_size_bytes{quantile="0.5"} 1
response_size_bytes{quantile="0.9"} 1
response_size_bytes{quantile="0.97"} 1
response_size_bytes{quantile="0.99"} 1
response_size_bytes{quantile="1"} 1
# HELP set_counter
# TYPE set_counter counter
set_counter 1
# HELP unused_bytes
# TYPE unused_bytes gauge
unused_bytes{foo="bar"} 58
# HELP used_bytes
# TYPE used_bytes gauge
used_bytes{foo="bar"} 42
used_bytes{foo="baz"} 43

func InitPush

func InitPush(pushURL string, interval time.Duration, extraLabels string, pushProcessMetrics bool) error

InitPush sets up periodic push for globally registered metrics to the given pushURL with the given interval.

extraLabels may contain comma-separated list of `label="value"` labels, which will be added to all the metrics before pushing them to pushURL.

If pushProcessMetrics is set to true, then 'process_*' and `go_*` metrics are also pushed to pushURL.

The metrics are pushed to pushURL in Prometheus text exposition format. See https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md#text-based-format

It is recommended pushing metrics to /api/v1/import/prometheus endpoint according to https://docs.victoriametrics.com/#how-to-import-data-in-prometheus-exposition-format

It is OK calling InitPush multiple times with different pushURL - in this case metrics are pushed to all the provided pushURL urls.

func InitPushExt

func InitPushExt(pushURL string, interval time.Duration, extraLabels string, writeMetrics func(w io.Writer)) error

InitPushExt sets up periodic push for metrics obtained by calling writeMetrics with the given interval.

extraLabels may contain comma-separated list of `label="value"` labels, which will be added to all the metrics before pushing them to pushURL.

The writeMetrics callback must write metrics to w in Prometheus text exposition format without timestamps and trailing comments. See https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md#text-based-format

It is recommended pushing metrics to /api/v1/import/prometheus endpoint according to https://docs.victoriametrics.com/#how-to-import-data-in-prometheus-exposition-format

It is OK calling InitPushExt multiple times with different pushURL - in this case metrics are pushed to all the provided pushURL urls.

It is OK calling InitPushExt multiple times with different writeMetrics - in this case all the metrics generated by writeMetrics callbacks are written to pushURL.

func InitPushExtWithOptions

func InitPushExtWithOptions(ctx context.Context, pushURL string, interval time.Duration, writeMetrics func(w io.Writer), opts *PushOptions) error

InitPushExtWithOptions sets up periodic push for metrics obtained by calling writeMetrics with the given interval.

The writeMetrics callback must write metrics to w in Prometheus text exposition format without timestamps and trailing comments. See https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md#text-based-format

The periodic push is stopped when the ctx is canceled. It is possible to wait until the background metrics push worker is stopped on a WaitGroup passed via opts.WaitGroup.

opts may contain additional configuration options if non-nil.

It is recommended pushing metrics to /api/v1/import/prometheus endpoint according to https://docs.victoriametrics.com/#how-to-import-data-in-prometheus-exposition-format

It is OK calling InitPushExtWithOptions multiple times with different pushURL - in this case metrics are pushed to all the provided pushURL urls.

It is OK calling InitPushExtWithOptions multiple times with different writeMetrics - in this case all the metrics generated by writeMetrics callbacks are written to pushURL.

func InitPushProcessMetrics

func InitPushProcessMetrics(pushURL string, interval time.Duration, extraLabels string) error

InitPushProcessMetrics sets up periodic push for 'process_*' metrics to the given pushURL with the given interval.

extraLabels may contain comma-separated list of `label="value"` labels, which will be added to all the metrics before pushing them to pushURL.

The metrics are pushed to pushURL in Prometheus text exposition format. See https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md#text-based-format

It is recommended pushing metrics to /api/v1/import/prometheus endpoint according to https://docs.victoriametrics.com/#how-to-import-data-in-prometheus-exposition-format

It is OK calling InitPushProcessMetrics multiple times with different pushURL - in this case metrics are pushed to all the provided pushURL urls.

func InitPushWithOptions

func InitPushWithOptions(ctx context.Context, pushURL string, interval time.Duration, pushProcessMetrics bool, opts *PushOptions) error

InitPushWithOptions sets up periodic push for globally registered metrics to the given pushURL with the given interval.

The periodic push is stopped when ctx is canceled. It is possible to wait until the background metrics push worker is stopped on a WaitGroup passed via opts.WaitGroup.

If pushProcessMetrics is set to true, then 'process_*' and `go_*` metrics are also pushed to pushURL.

opts may contain additional configuration options if non-nil.

The metrics are pushed to pushURL in Prometheus text exposition format. See https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md#text-based-format

It is recommended pushing metrics to /api/v1/import/prometheus endpoint according to https://docs.victoriametrics.com/#how-to-import-data-in-prometheus-exposition-format

It is OK calling InitPushWithOptions multiple times with different pushURL - in this case metrics are pushed to all the provided pushURL urls.

func ListMetricNames

func ListMetricNames() []string

ListMetricNames returns sorted list of all the metric names from default set.

func PushMetrics

func PushMetrics(ctx context.Context, pushURL string, pushProcessMetrics bool, opts *PushOptions) error

PushMetrics pushes globally registered metrics to pushURL.

If pushProcessMetrics is set to true, then 'process_*' and `go_*` metrics are also pushed to pushURL.

opts may contain additional configuration options if non-nil.

It is recommended pushing metrics to /api/v1/import/prometheus endpoint according to https://docs.victoriametrics.com/#how-to-import-data-in-prometheus-exposition-format

func PushMetricsExt

func PushMetricsExt(ctx context.Context, pushURL string, writeMetrics func(w io.Writer), opts *PushOptions) error

PushMetricsExt pushes metrics generated by wirteMetrics to pushURL.

The writeMetrics callback must write metrics to w in Prometheus text exposition format without timestamps and trailing comments. See https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md#text-based-format

opts may contain additional configuration options if non-nil.

It is recommended pushing metrics to /api/v1/import/prometheus endpoint according to https://docs.victoriametrics.com/#how-to-import-data-in-prometheus-exposition-format

func RegisterMetricsWriter

func RegisterMetricsWriter(writeMetrics func(w io.Writer))

RegisterMetricsWriter registers writeMetrics callback for including metrics in the output generated by WritePrometheus.

The writeMetrics callback must write metrics to w in Prometheus text exposition format without timestamps and trailing comments. The last line generated by writeMetrics must end with \n. See https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md#text-based-format

It is OK to register multiple writeMetrics callbacks - all of them will be called sequentially for gererating the output at WritePrometheus.

func RegisterSet

func RegisterSet(s *Set)

RegisterSet registers the given set s for metrics export via global WritePrometheus() call.

See also UnregisterSet.

func UnregisterAllMetrics

func UnregisterAllMetrics()

UnregisterAllMetrics unregisters all the metrics from default set.

It also unregisters writeMetrics callbacks passed to RegisterMetricsWriter.

func UnregisterMetric

func UnregisterMetric(name string) bool

UnregisterMetric removes metric with the given name from default set.

See also UnregisterAllMetrics.

func UnregisterSet

func UnregisterSet(s *Set, destroySet bool)

UnregisterSet stops exporting metrics for the given s via global WritePrometheus() call.

If destroySet is set to true, then s.UnregisterAllMetrics() is called on s after unregistering it, so s becomes destroyed. Otherwise the s can be registered again in the set by passing it to RegisterSet().

func WithTLSConfigClient

func WithTLSConfigClient(certName string, keyName string, insecureSkipVerify bool, region string) (*http.Client, error)

WithTLSConfigClient creates an HTTP client with TLS configuration.

The certificate and key are retrieved from AWS Secrets Manager using the provided names and region. The returned client can be used with WithHTTPClient.

func WriteCounterFloat64

func WriteCounterFloat64(w io.Writer, name string, value float64)

WriteCounterFloat64 writes counter metric with the given name and value to w in Prometheus text exposition format.

func WriteCounterUint64

func WriteCounterUint64(w io.Writer, name string, value uint64)

WriteCounterUint64 writes counter metric with the given name and value to w in Prometheus text exposition format.

func WriteFDMetrics

func WriteFDMetrics(w io.Writer)

WriteFDMetrics writes `process_max_fds` and `process_open_fds` metrics to w.

func WriteGaugeFloat64

func WriteGaugeFloat64(w io.Writer, name string, value float64)

WriteGaugeFloat64 writes gauge metric with the given name and value to w in Prometheus text exposition format.

func WriteGaugeUint64

func WriteGaugeUint64(w io.Writer, name string, value uint64)

WriteGaugeUint64 writes gauge metric with the given name and value to w in Prometheus text exposition format.

func WriteMetadataIfNeeded

func WriteMetadataIfNeeded(w io.Writer, metricName, metricType string)

WriteMetadataIfNeeded writes HELP and TYPE metadata for the given metricName and metricType if this is globally enabled via ExposeMetadata().

If the metadata exposition isn't enabled, then this function is no-op.

func WriteProcessMetrics

func WriteProcessMetrics(w io.Writer)

WriteProcessMetrics writes additional process metrics in Prometheus format to w.

The following `go_*` and `process_*` metrics are exposed for the currently running process. Below is a short description for the exposed `process_*` metrics:

  • process_cpu_seconds_system_total - CPU time spent in syscalls

  • process_cpu_seconds_user_total - CPU time spent in userspace

  • process_cpu_seconds_total - CPU time spent by the process

  • process_major_pagefaults_total - page faults resulted in disk IO

  • process_minor_pagefaults_total - page faults resolved without disk IO

  • process_resident_memory_bytes - recently accessed memory (aka RSS or resident memory)

  • process_resident_memory_peak_bytes - the maximum RSS memory usage

  • process_resident_memory_anon_bytes - RSS for memory-mapped files

  • process_resident_memory_file_bytes - RSS for memory allocated by the process

  • process_resident_memory_shared_bytes - RSS for memory shared between multiple processes

  • process_virtual_memory_bytes - virtual memory usage

  • process_virtual_memory_peak_bytes - the maximum virtual memory usage

  • process_num_threads - the number of threads

  • process_start_time_seconds - process start time as unix timestamp

  • process_io_read_bytes_total - the number of bytes read via syscalls

  • process_io_written_bytes_total - the number of bytes written via syscalls

  • process_io_read_syscalls_total - the number of read syscalls

  • process_io_write_syscalls_total - the number of write syscalls

  • process_io_storage_read_bytes_total - the number of bytes actually read from disk

  • process_io_storage_written_bytes_total - the number of bytes actually written to disk

  • go_sched_latencies_seconds - time spent by goroutines in ready state before they start execution

  • go_mutex_wait_seconds_total - summary time spent by all the goroutines while waiting for locked mutex

  • go_gc_mark_assist_cpu_seconds_total - summary CPU time spent by goroutines in GC mark assist state

  • go_gc_cpu_seconds_total - summary time spent in GC

  • go_gc_pauses_seconds - duration of GC pauses

  • go_scavenge_cpu_seconds_total - CPU time spent on returning the memory to OS

  • go_memlimit_bytes - the GOMEMLIMIT env var value

  • go_memstats_alloc_bytes - memory usage for Go objects in the heap

  • go_memstats_alloc_bytes_total - the cumulative counter for total size of allocated Go objects

  • go_memstats_buck_hash_sys_bytes - bytes of memory in profiling bucket hash tables

  • go_memstats_frees_total - the cumulative counter for number of freed Go objects

  • go_memstats_gc_cpu_fraction - the fraction of CPU spent in Go garbage collector

  • go_memstats_gc_sys_bytes - the size of Go garbage collector metadata

  • go_memstats_heap_alloc_bytes - the same as go_memstats_alloc_bytes

  • go_memstats_heap_idle_bytes - idle memory ready for new Go object allocations

  • go_memstats_heap_inuse_bytes - bytes in in-use spans

  • go_memstats_heap_objects - the number of Go objects in the heap

  • go_memstats_heap_released_bytes - bytes of physical memory returned to the OS

  • go_memstats_heap_sys_bytes - memory requested for Go objects from the OS

  • go_memstats_last_gc_time_seconds - unix timestamp the last garbage collection finished

  • go_memstats_lookups_total - the number of pointer lookups performed by the runtime

  • go_memstats_mallocs_total - the number of allocations for Go objects

  • go_memstats_mcache_inuse_bytes - bytes of allocated mcache structures

  • go_memstats_mcache_sys_bytes - bytes of memory obtained from the OS for mcache structures

  • go_memstats_mspan_inuse_bytes - bytes of allocated mspan structures

  • go_memstats_mspan_sys_bytes - bytes of memory obtained from the OS for mspan structures

  • go_memstats_next_gc_bytes - the target heap size when the next garbage collection should start

  • go_memstats_other_sys_bytes - bytes of memory in miscellaneous off-heap runtime allocations

  • go_memstats_stack_inuse_bytes - memory used for goroutine stacks

  • go_memstats_stack_sys_bytes - memory requested fromthe OS for goroutine stacks

  • go_memstats_sys_bytes - memory requested by Go runtime from the OS

  • go_cgo_calls_count - the total number of CGO calls

  • go_cpu_count - the number of CPU cores on the host where the app runs

The WriteProcessMetrics func is usually called in combination with writing Set metrics inside "/metrics" handler:

http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
    mySet.WritePrometheus(w)
    metrics.WriteProcessMetrics(w)
})

See also WriteFDMetrics.

func WritePrometheus

func WritePrometheus(w io.Writer, exposeProcessMetrics bool)

WritePrometheus writes all the metrics in Prometheus format from the default set, all the added sets and metrics writers to w.

Additional sets can be registered via RegisterSet() call. Additional metric writers can be registered via RegisterMetricsWriter() call.

If exposeProcessMetrics is true, then various `go_*` and `process_*` metrics are exposed for the current process.

The WritePrometheus func is usually called inside "/metrics" handler:

http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
    metrics.WritePrometheus(w, true)
})
Example
// Export all the registered metrics in Prometheus format at `/metrics` http path.
http.HandleFunc("/metrics", func(w http.ResponseWriter, req *http.Request) {
	metrics.WritePrometheus(w, true)
})

Types

type Counter

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

Counter is a counter.

It may be used as a gauge if Dec and Set are called.

Example
// Define a counter in global scope.
var c = metrics.NewCounter(`metric_total{label1="value1", label2="value2"}`)

// Increment the counter when needed.
for i := 0; i < 10; i++ {
	c.Inc()
}
n := c.Get()
fmt.Println(n)
Output:
10
Example (Vec)
for i := 0; i < 3; i++ {
	// Dynamically construct metric name and pass it to GetOrCreateCounter.
	name := fmt.Sprintf(`metric_total{label1=%q, label2="%d"}`, "value1", i)
	metrics.GetOrCreateCounter(name).Add(i + 1)
}

// Read counter values.
for i := 0; i < 3; i++ {
	name := fmt.Sprintf(`metric_total{label1=%q, label2="%d"}`, "value1", i)
	n := metrics.GetOrCreateCounter(name).Get()
	fmt.Println(n)
}
Output:
1
2
3

func GetOrCreateCounter

func GetOrCreateCounter(name string) *Counter

GetOrCreateCounter returns registered counter with the given name or creates new counter if the registry doesn't contain counter with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned counter is safe to use from concurrent goroutines.

Performance tip: prefer NewCounter instead of GetOrCreateCounter.

func NewCounter

func NewCounter(name string) *Counter

NewCounter registers and returns new counter with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned counter is safe to use from concurrent goroutines.

func (*Counter) Add

func (c *Counter) Add(n int)

Add adds n to c.

func (*Counter) AddInt64

func (c *Counter) AddInt64(n int64)

AddInt64 adds n to c.

func (*Counter) Dec

func (c *Counter) Dec()

Dec decrements c.

func (*Counter) Get

func (c *Counter) Get() uint64

Get returns the current value for c.

func (*Counter) Inc

func (c *Counter) Inc()

Inc increments c.

func (*Counter) Set

func (c *Counter) Set(n uint64)

Set sets c value to n.

type FloatCounter

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

FloatCounter is a float64 counter guarded by RWmutex.

It may be used as a gauge if Add and Sub are called.

Example
// Define a float64 counter in global scope.
var fc = metrics.NewFloatCounter(`float_metric_total{label1="value1", label2="value2"}`)

// Add to the counter when needed.
for i := 0; i < 10; i++ {
	fc.Add(1.01)
}
n := fc.Get()
fmt.Println(n)
Output:
10.1
Example (Vec)
for i := 0; i < 3; i++ {
	// Dynamically construct metric name and pass it to GetOrCreateFloatCounter.
	name := fmt.Sprintf(`float_metric_total{label1=%q, label2="%d"}`, "value1", i)
	metrics.GetOrCreateFloatCounter(name).Add(float64(i) + 1.01)
}

// Read counter values.
for i := 0; i < 3; i++ {
	name := fmt.Sprintf(`float_metric_total{label1=%q, label2="%d"}`, "value1", i)
	n := metrics.GetOrCreateFloatCounter(name).Get()
	fmt.Println(n)
}
Output:
1.01
2.01
3.01

func GetOrCreateFloatCounter

func GetOrCreateFloatCounter(name string) *FloatCounter

GetOrCreateFloatCounter returns registered FloatCounter with the given name or creates new FloatCounter if the registry doesn't contain FloatCounter with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned FloatCounter is safe to use from concurrent goroutines.

Performance tip: prefer NewFloatCounter instead of GetOrCreateFloatCounter.

func NewFloatCounter

func NewFloatCounter(name string) *FloatCounter

NewFloatCounter registers and returns new counter of float64 type with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned counter is safe to use from concurrent goroutines.

func (*FloatCounter) Add

func (fc *FloatCounter) Add(n float64)

Add adds n to fc.

func (*FloatCounter) Get

func (fc *FloatCounter) Get() float64

Get returns the current value for fc.

func (*FloatCounter) Set

func (fc *FloatCounter) Set(n float64)

Set sets fc value to n.

func (*FloatCounter) Sub

func (fc *FloatCounter) Sub(n float64)

Sub substracts n from fc.

type Gauge

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

Gauge is a float64 gauge.

Example
// Define a gauge exporting the number of goroutines.
var g = metrics.NewGauge(`goroutines_count`, func() float64 {
	return float64(runtime.NumGoroutine())
})

// Obtain gauge value.
fmt.Println(g.Get())
Example (Vec)
for i := 0; i < 3; i++ {
	// Dynamically construct metric name and pass it to GetOrCreateGauge.
	name := fmt.Sprintf(`metric{label1=%q, label2="%d"}`, "value1", i)
	iLocal := i
	metrics.GetOrCreateGauge(name, func() float64 {
		return float64(iLocal + 1)
	})
}

// Read counter values.
for i := 0; i < 3; i++ {
	name := fmt.Sprintf(`metric{label1=%q, label2="%d"}`, "value1", i)
	n := metrics.GetOrCreateGauge(name, func() float64 { return 0 }).Get()
	fmt.Println(n)
}
Output:
1
2
3

func GetOrCreateGauge

func GetOrCreateGauge(name string, f func() float64) *Gauge

GetOrCreateGauge returns registered gauge with the given name or creates new gauge if the registry doesn't contain gauge with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned gauge is safe to use from concurrent goroutines.

Performance tip: prefer NewGauge instead of GetOrCreateGauge.

See also FloatCounter for working with floating-point values.

func NewGauge

func NewGauge(name string, f func() float64) *Gauge

NewGauge registers and returns gauge with the given name, which calls f to obtain gauge value.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

f must be safe for concurrent calls. if f is nil, then it is expected that the gauge value is changed via Set(), Inc(), Dec() and Add() calls.

The returned gauge is safe to use from concurrent goroutines.

See also FloatCounter for working with floating-point values.

func (*Gauge) Add

func (g *Gauge) Add(fAdd float64)

Add adds fAdd to g. fAdd may be positive and negative.

The g must be created with nil callback in order to be able to call this function.

func (*Gauge) Dec

func (g *Gauge) Dec()

Dec decrements g by 1.

The g must be created with nil callback in order to be able to call this function.

func (*Gauge) Get

func (g *Gauge) Get() float64

Get returns the current value for g.

func (*Gauge) Inc

func (g *Gauge) Inc()

Inc increments g by 1.

The g must be created with nil callback in order to be able to call this function.

func (*Gauge) Set

func (g *Gauge) Set(v float64)

Set sets g value to v.

The g must be created with nil callback in order to be able to call this function.

type Histogram

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

Histogram is a histogram for non-negative values with automatically created buckets.

See https://medium.com/@valyala/improving-histogram-usability-for-prometheus-and-grafana-bc7e5df0e350

Each bucket contains a counter for values in the given range. Each non-empty bucket is exposed via the following metric:

<metric_name>_bucket{<optional_tags>,vmrange="<start>...<end>"} <counter>

Where:

  • <metric_name> is the metric name passed to NewHistogram
  • <optional_tags> is optional tags for the <metric_name>, which are passed to NewHistogram
  • <start> and <end> - start and end values for the given bucket
  • <counter> - the number of hits to the given bucket during Update* calls

Histogram buckets can be converted to Prometheus-like buckets with `le` labels with `prometheus_buckets(<metric_name>_bucket)` function from PromQL extensions in VictoriaMetrics. (see https://docs.victoriametrics.com/metricsql/ ):

prometheus_buckets(request_duration_bucket)

Time series produced by the Histogram have better compression ratio comparing to Prometheus histogram buckets with `le` labels, since they don't include counters for all the previous buckets.

Zero histogram is usable.

Example
// Define a histogram in global scope.
var h = metrics.NewHistogram(`request_duration_seconds{path="/foo/bar"}`)

// Update the histogram with the duration of processRequest call.
startTime := time.Now()
processRequest()
h.UpdateDuration(startTime)
Example (Vec)
for i := 0; i < 3; i++ {
	// Dynamically construct metric name and pass it to GetOrCreateHistogram.
	name := fmt.Sprintf(`response_size_bytes{path=%q}`, "/foo/bar")
	response := processRequest()
	metrics.GetOrCreateHistogram(name).Update(float64(len(response)))
}

func GetOrCreateHistogram

func GetOrCreateHistogram(name string) *Histogram

GetOrCreateHistogram returns registered histogram with the given name or creates new histogram if the registry doesn't contain histogram with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned histogram is safe to use from concurrent goroutines.

Performance tip: prefer NewHistogram instead of GetOrCreateHistogram.

func NewHistogram

func NewHistogram(name string) *Histogram

NewHistogram creates and returns new histogram with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned histogram is safe to use from concurrent goroutines.

func (*Histogram) Merge

func (h *Histogram) Merge(src *Histogram)

Merge merges src to h

func (*Histogram) Reset

func (h *Histogram) Reset()

Reset resets the given histogram.

func (*Histogram) Update

func (h *Histogram) Update(v float64)

Update updates h with v.

Negative values and NaNs are ignored.

func (*Histogram) UpdateDuration

func (h *Histogram) UpdateDuration(startTime time.Time)

UpdateDuration updates request duration based on the given startTime.

func (*Histogram) VisitNonZeroBuckets

func (h *Histogram) VisitNonZeroBuckets(f func(vmrange string, count uint64))

VisitNonZeroBuckets calls f for all buckets with non-zero counters.

vmrange contains "<start>...<end>" string with bucket bounds. The lower bound isn't included in the bucket, while the upper bound is included. This is required to be compatible with Prometheus-style histogram buckets with `le` (less or equal) labels.

type Monitor

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

Monitor represents the VictoriaMetrics monitoring component.

Monitor implements the VMMetricsMonitor interface and provides functionality for pushing metrics to a VictoriaMetrics server at specified intervals. It supports various configuration options including custom HTTP clients, TLS configurations, and metric labels.

func NewVMMetricsMonitor

func NewVMMetricsMonitor(opts ...MonitorOption) (*Monitor, error)

NewVMMetricsMonitor creates a new instance of the VictoriaMetrics Monitor.

The function accepts a variadic list of MonitorOption functions which configure the monitor. At minimum, the following options must be provided:

  • A storage URL (using WithStorageURL)
  • An HTTP client (using either WithHTTPClient or WithTLSConfigClient)
  • Service name, environment, and region labels (via WithServiceAndEnvAndRegion or WithExtraLabel)

If the push interval is not specified, it defaults to 15 seconds.

func (*Monitor) Shutdown

func (m *Monitor) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the VictoriaMetrics Monitor.

It stops pushing when the provided context is canceled.

func (*Monitor) Start

func (m *Monitor) Start(ctx context.Context) error

Start starts the VictoriaMetrics monitoring and pushing.

It begins pushing metrics to the configured storage URL at the configured interval. The pushing continues until the provided context is canceled or until Shutdown is called.

func (*Monitor) ValidateMonitor

func (m *Monitor) ValidateMonitor() error

ValidateMonitor validates that the Monitor has all required fields.

It checks that: - A storage URL is provided - An HTTP client is provided - Service name, environment, and region labels are provided - The push interval has a valid value, or sets the default if not provided

type MonitorOption

type MonitorOption func(*Monitor)

MonitorOption defines a function that can configure a Monitor.

This follows the functional options pattern for configuring the Monitor instance.

func WithDisableCompression

func WithDisableCompression(disable bool) MonitorOption

WithDisableCompression disables HTTP request body compression when pushing metrics.

When compression is enabled, it can reduce network bandwidth usage.

func WithDisablePush added in v0.0.2

func WithDisablePush(disable bool) MonitorOption

WithDisablePush disables the push functionality of the Monitor.

When disabled, metrics will not be pushed to the storage URL.

func WithExtraLabel

func WithExtraLabel(key, value string) MonitorOption

WithExtraLabel adds a custom label to the metrics.

Labels are key-value pairs that are added to all metrics pushed by the Monitor.

func WithHTTPClient

func WithHTTPClient(client *http.Client) MonitorOption

WithHTTPClient allows setting a custom HTTP client for the Monitor.

The provided client will be used for pushing metrics to the storage URL.

func WithProcessMetrics

func WithProcessMetrics(enable bool) MonitorOption

WithProcessMetrics enables or disables collection of process metrics.

When enabled, Go runtime metrics and process metrics will be collected and pushed.

func WithPushInterval

func WithPushInterval(interval time.Duration) MonitorOption

WithPushInterval sets the interval at which metrics are pushed to the storage URL.

If not specified, the default interval is 15 seconds.

func WithServiceAndEnvAndRegion

func WithServiceAndEnvAndRegion(serviceName, env, region string) MonitorOption

WithServiceAndEnvAndRegion sets the service name, environment, and region labels.

This is a convenience function for adding the three most common labels that are required by the validator.

func WithStorageURL

func WithStorageURL(url string) MonitorOption

WithStorageURL sets the VictoriaMetrics storage URL for the Monitor.

The URL should point to a VictoriaMetrics server endpoint that accepts metrics in Prometheus format

type PushOptions

type PushOptions struct {
	// ExtraLabels is an optional comma-separated list of `label="value"` labels, which must be added to all the metrics before pushing them to pushURL.
	ExtraLabels string

	// Headers is an optional list of HTTP headers to add to every push request to pushURL.
	//
	// Every item in the list must have the form `Header: value`. For example, `Authorization: Custom my-top-secret`.
	Headers []string

	// Whether to disable HTTP request body compression before sending the metrics to pushURL.
	//
	// By default the compression is enabled.
	DisableCompression bool

	// Method is HTTP request method to use when pushing metrics to pushURL.
	//
	// By default the Method is GET.
	Method string

	// Optional WaitGroup for waiting until all the push workers created with this WaitGroup are stopped.
	WaitGroup *sync.WaitGroup

	// CustomClient allows specifying a custom HTTP client for making the push requests.
	//
	// If nil, the default http.Client will be created.
	CustomClient *http.Client
}

PushOptions is the list of options, which may be applied to InitPushWithOptions().

type Set

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

Set is a set of metrics.

Metrics belonging to a set are exported separately from global metrics.

Set.WritePrometheus must be called for exporting metrics from the set.

Example
// Create a set with a counter
s := metrics.NewSet()
sc := s.NewCounter("set_counter")
sc.Inc()
s.NewGauge(`set_gauge{foo="bar"}`, func() float64 { return 42 })

// Dump metrics from s.
var bb bytes.Buffer
s.WritePrometheus(&bb)
fmt.Printf("set metrics:\n%s\n", bb.String())
Output:
set metrics:
set_counter 1
set_gauge{foo="bar"} 42

func GetDefaultSet

func GetDefaultSet() *Set

GetDefaultSet returns the default metrics set.

func NewSet

func NewSet() *Set

NewSet creates new set of metrics.

Pass the set to RegisterSet() function in order to export its metrics via global WritePrometheus() call.

func (*Set) GetOrCreateCounter

func (s *Set) GetOrCreateCounter(name string) *Counter

GetOrCreateCounter returns registered counter in s with the given name or creates new counter if s doesn't contain counter with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned counter is safe to use from concurrent goroutines.

Performance tip: prefer NewCounter instead of GetOrCreateCounter.

func (*Set) GetOrCreateFloatCounter

func (s *Set) GetOrCreateFloatCounter(name string) *FloatCounter

GetOrCreateFloatCounter returns registered FloatCounter in s with the given name or creates new FloatCounter if s doesn't contain FloatCounter with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned FloatCounter is safe to use from concurrent goroutines.

Performance tip: prefer NewFloatCounter instead of GetOrCreateFloatCounter.

func (*Set) GetOrCreateGauge

func (s *Set) GetOrCreateGauge(name string, f func() float64) *Gauge

GetOrCreateGauge returns registered gauge with the given name in s or creates new gauge if s doesn't contain gauge with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned gauge is safe to use from concurrent goroutines.

Performance tip: prefer NewGauge instead of GetOrCreateGauge.

func (*Set) GetOrCreateHistogram

func (s *Set) GetOrCreateHistogram(name string) *Histogram

GetOrCreateHistogram returns registered histogram in s with the given name or creates new histogram if s doesn't contain histogram with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned histogram is safe to use from concurrent goroutines.

Performance tip: prefer NewHistogram instead of GetOrCreateHistogram.

func (*Set) GetOrCreateSummary

func (s *Set) GetOrCreateSummary(name string) *Summary

GetOrCreateSummary returns registered summary with the given name in s or creates new summary if s doesn't contain summary with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned summary is safe to use from concurrent goroutines.

Performance tip: prefer NewSummary instead of GetOrCreateSummary.

func (*Set) GetOrCreateSummaryExt

func (s *Set) GetOrCreateSummaryExt(name string, window time.Duration, quantiles []float64) *Summary

GetOrCreateSummaryExt returns registered summary with the given name, window and quantiles in s or creates new summary if s doesn't contain summary with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned summary is safe to use from concurrent goroutines.

Performance tip: prefer NewSummaryExt instead of GetOrCreateSummaryExt.

func (*Set) InitPush

func (s *Set) InitPush(pushURL string, interval time.Duration, extraLabels string) error

InitPush sets up periodic push for metrics from s to the given pushURL with the given interval.

extraLabels may contain comma-separated list of `label="value"` labels, which will be added to all the metrics before pushing them to pushURL.

The metrics are pushed to pushURL in Prometheus text exposition format. See https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md#text-based-format

It is recommended pushing metrics to /api/v1/import/prometheus endpoint according to https://docs.victoriametrics.com/#how-to-import-data-in-prometheus-exposition-format

It is OK calling InitPush multiple times with different pushURL - in this case metrics are pushed to all the provided pushURL urls.

func (*Set) InitPushWithOptions

func (s *Set) InitPushWithOptions(ctx context.Context, pushURL string, interval time.Duration, opts *PushOptions) error

InitPushWithOptions sets up periodic push for metrics from s to the given pushURL with the given interval.

The periodic push is stopped when the ctx is canceled. It is possible to wait until the background metrics push worker is stopped on a WaitGroup passed via opts.WaitGroup.

opts may contain additional configuration options if non-nil.

The metrics are pushed to pushURL in Prometheus text exposition format. See https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md#text-based-format

It is recommended pushing metrics to /api/v1/import/prometheus endpoint according to https://docs.victoriametrics.com/#how-to-import-data-in-prometheus-exposition-format

It is OK calling InitPushWithOptions multiple times with different pushURL - in this case metrics are pushed to all the provided pushURL urls.

func (*Set) ListMetricNames

func (s *Set) ListMetricNames() []string

ListMetricNames returns sorted list of all the metrics in s.

The returned list doesn't include metrics generated by metricsWriter passed to RegisterMetricsWriter.

func (*Set) NewCounter

func (s *Set) NewCounter(name string) *Counter

NewCounter registers and returns new counter with the given name in the s.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned counter is safe to use from concurrent goroutines.

func (*Set) NewFloatCounter

func (s *Set) NewFloatCounter(name string) *FloatCounter

NewFloatCounter registers and returns new FloatCounter with the given name in the s.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned FloatCounter is safe to use from concurrent goroutines.

func (*Set) NewGauge

func (s *Set) NewGauge(name string, f func() float64) *Gauge

NewGauge registers and returns gauge with the given name in s, which calls f to obtain gauge value.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

f must be safe for concurrent calls.

The returned gauge is safe to use from concurrent goroutines.

func (*Set) NewHistogram

func (s *Set) NewHistogram(name string) *Histogram

NewHistogram creates and returns new histogram in s with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned histogram is safe to use from concurrent goroutines.

func (*Set) NewSummary

func (s *Set) NewSummary(name string) *Summary

NewSummary creates and returns new summary with the given name in s.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned summary is safe to use from concurrent goroutines.

func (*Set) NewSummaryExt

func (s *Set) NewSummaryExt(name string, window time.Duration, quantiles []float64) *Summary

NewSummaryExt creates and returns new summary in s with the given name, window and quantiles.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned summary is safe to use from concurrent goroutines.

func (*Set) PushMetrics

func (s *Set) PushMetrics(ctx context.Context, pushURL string, opts *PushOptions) error

PushMetrics pushes s metrics to pushURL.

opts may contain additional configuration options if non-nil.

It is recommended pushing metrics to /api/v1/import/prometheus endpoint according to https://docs.victoriametrics.com/#how-to-import-data-in-prometheus-exposition-format

func (*Set) RegisterMetricsWriter

func (s *Set) RegisterMetricsWriter(writeMetrics func(w io.Writer))

RegisterMetricsWriter registers writeMetrics callback for including metrics in the output generated by s.WritePrometheus.

The writeMetrics callback must write metrics to w in Prometheus text exposition format without timestamps and trailing comments. The last line generated by writeMetrics must end with \n. See https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md#text-based-format

It is OK to reguster multiple writeMetrics callbacks - all of them will be called sequentially for gererating the output at s.WritePrometheus.

func (*Set) UnregisterAllMetrics

func (s *Set) UnregisterAllMetrics()

UnregisterAllMetrics de-registers all metrics registered in s.

It also de-registers writeMetrics callbacks passed to RegisterMetricsWriter.

func (*Set) UnregisterMetric

func (s *Set) UnregisterMetric(name string) bool

UnregisterMetric removes metric with the given name from s.

True is returned if the metric has been removed. False is returned if the given metric is missing in s.

func (*Set) WritePrometheus

func (s *Set) WritePrometheus(w io.Writer)

WritePrometheus writes all the metrics from s to w in Prometheus format.

type Summary

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

Summary implements summary.

Example
// Define a summary in global scope.
var s = metrics.NewSummary(`request_duration_seconds{path="/foo/bar"}`)

// Update the summary with the duration of processRequest call.
startTime := time.Now()
processRequest()
s.UpdateDuration(startTime)
Example (Vec)
for i := 0; i < 3; i++ {
	// Dynamically construct metric name and pass it to GetOrCreateSummary.
	name := fmt.Sprintf(`response_size_bytes{path=%q}`, "/foo/bar")
	response := processRequest()
	metrics.GetOrCreateSummary(name).Update(float64(len(response)))
}

func GetOrCreateSummary

func GetOrCreateSummary(name string) *Summary

GetOrCreateSummary returns registered summary with the given name or creates new summary if the registry doesn't contain summary with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned summary is safe to use from concurrent goroutines.

Performance tip: prefer NewSummary instead of GetOrCreateSummary.

func GetOrCreateSummaryExt

func GetOrCreateSummaryExt(name string, window time.Duration, quantiles []float64) *Summary

GetOrCreateSummaryExt returns registered summary with the given name, window and quantiles or creates new summary if the registry doesn't contain summary with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned summary is safe to use from concurrent goroutines.

Performance tip: prefer NewSummaryExt instead of GetOrCreateSummaryExt.

func NewSummary

func NewSummary(name string) *Summary

NewSummary creates and returns new summary with the given name.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned summary is safe to use from concurrent goroutines.

func NewSummaryExt

func NewSummaryExt(name string, window time.Duration, quantiles []float64) *Summary

NewSummaryExt creates and returns new summary with the given name, window and quantiles.

name must be valid Prometheus-compatible metric with possible labels. For instance,

  • foo
  • foo{bar="baz"}
  • foo{bar="baz",aaa="b"}

The returned summary is safe to use from concurrent goroutines.

func (*Summary) Update

func (sm *Summary) Update(v float64)

Update updates the summary.

func (*Summary) UpdateDuration

func (sm *Summary) UpdateDuration(startTime time.Time)

UpdateDuration updates request duration based on the given startTime.

type VMMetricsMonitor

type VMMetricsMonitor interface {
	Start(ctx context.Context) error
	Shutdown(ctx context.Context) error
}

VMMetricsMonitor is the interface for monitoring VM metrics.

Implementations of this interface provide functionality for starting and shutting down metrics monitoring services that push metrics to VictoriaMetrics.

Example

This example demonstrates how to set up a VMMetricsMonitor for monitoring application metrics.

// Create an HTTP client
httpClient := &http.Client{
	Timeout: 10 * time.Second,
}

// Create a new VMMetricsMonitor with required options
monitor, err := metrics.NewVMMetricsMonitor(
	metrics.WithStorageURL("http://victoria-metrics:8428/api/v1/import/prometheus"),
	metrics.WithHTTPClient(httpClient),
	metrics.WithServiceAndEnvAndRegion("my-service", "production", "us-west-2"),
	metrics.WithPushInterval(30*time.Second),
	metrics.WithProcessMetrics(true),
)
if err != nil {
	fmt.Printf("Failed to create metrics monitor: %v\n", err)
	return
}

// Create context that will be canceled on SIGINT or SIGTERM
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Set up signal handling
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
	<-sigCh
	fmt.Println("Received shutdown signal")
	cancel()
}()

// Start the metrics monitoring
fmt.Println("Starting metrics monitor...")
if err := monitor.Start(ctx); err != nil {
	fmt.Printf("Failed to start metrics monitor: %v\n", err)
	return
}
fmt.Println("Metrics monitor started successfully")

// Create some metrics that will be captured
counter := metrics.NewCounter("api_requests_total")
gauge := metrics.NewGauge("active_connections", nil)
histogram := metrics.NewHistogram("request_duration_seconds")

// Simulate some metrics updates
counter.Inc()
gauge.Set(42)
histogram.Update(0.25)

// Wait for context cancellation in a real application
// This would run until receiving a shutdown signal
fmt.Println("Monitoring metrics (would run until shutdown in a real application)")

// For example purposes, simulate a shutdown after 1 second
time.Sleep(1 * time.Second)
cancel()
fmt.Println("Shutting down metrics monitor...")

// Perform graceful shutdown
shutdownCtx, shutdownCancel := context.WithTimeout(context.Background(), 5*time.Second)
defer shutdownCancel()

if err := monitor.Shutdown(shutdownCtx); err != nil {
	fmt.Printf("Error during metrics monitor shutdown: %v\n", err)
	return
}
fmt.Println("Metrics monitor shut down successfully")
Example (WithTLS)

This example demonstrates how to set up a monitor with TLS client for secure metrics transmission for applications running in AWS.

// Create a TLS-enabled HTTP client
tlsClient, err := metrics.WithTLSConfigClient(
	"cert-secret-name",
	"key-secret-name",
	false,
	"us-west-2",
)
if err != nil {
	fmt.Printf("Failed to create TLS client: %v\n", err)
	return
}

// Create a monitor with the TLS client
monitor, err := metrics.NewVMMetricsMonitor(
	metrics.WithStorageURL("https://secure-victoria-metrics:8428/api/v1/import/prometheus"),
	metrics.WithHTTPClient(tlsClient),
	metrics.WithServiceAndEnvAndRegion("secure-service", "production", "us-west-2"),
)
if err != nil {
	fmt.Printf("Failed to create secure metrics monitor: %v\n", err)
	return
}

// Start the metrics monitoring
fmt.Println("Starting secure metrics monitor...")
ctx := context.Background()
if err := monitor.Start(ctx); err != nil {
	fmt.Printf("Failed to start secure metrics monitor: %v\n", err)
	return
}
fmt.Println("Secure metrics monitor started successfully")

Jump to

Keyboard shortcuts

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