prometheus

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HistogramOpts

type HistogramOpts struct {
	Buckets []float64
}

type Registry

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

Registry describes a set of metrics. It implements metrics.Registry as well as prometheus.Collector and can thus be used as an adapter to collect Conduit metrics and deliver them to the prometheus client.

func NewRegistry

func NewRegistry(labels map[string]string) *Registry

NewRegistry returns a registry that is responsible for managing a collection of metrics.

Labels allows constant labels to be added to all metrics created in this registry, although this parameter should be used responsibly. See also https://prometheus.io/docs/instrumenting/writing_exporters/#target-labels,-not-static-scraped-labels

Example
package main

import (
	"os"
	"strings"
	"time"

	"github.com/conduitio/conduit/pkg/foundation/metrics"
	"github.com/conduitio/conduit/pkg/foundation/metrics/prometheus"
	promclient "github.com/prometheus/client_golang/prometheus"
	"github.com/prometheus/common/expfmt"
)

var (
	testCounter = metrics.NewCounter("prom_example_counter", "example")
	testTimer   = metrics.NewTimer("prom_example_timer", "example")
	//nolint:unused // the whole point of this is to show that even unused gauges show up in prometheus
	testGauge = metrics.NewGauge("prom_example_gauge", "example")
)

func main() {
	// create a new registry and register it in the metrics package and as a
	// prometheus collector
	reg := prometheus.NewRegistry(map[string]string{"static_label": "example"})
	metrics.Register(reg)
	promclient.MustRegister(reg)

	// add a metric dynamically
	labeledCounter := metrics.NewLabeledCounter("prom_example_dynamic_labeled", "example", []string{"test_label"})

	// observe some metrics
	testCounter.Inc()
	testTimer.Update(time.Second)
	labeledCounter.WithValues("val1").Inc(100)
	labeledCounter.WithValues("val2")

	// gather and print metrics
	gatheredMetrics, err := promclient.DefaultGatherer.Gather()
	if err != nil {
		panic(err)
	}

	enc := expfmt.NewEncoder(os.Stdout, expfmt.NewFormat(expfmt.TypeTextPlain))
	for _, m := range gatheredMetrics {
		if strings.HasPrefix(m.GetName(), "prom_example_") {
			err := enc.Encode(m)
			if err != nil {
				panic(err)
			}
		}
	}

}
Output:

# HELP prom_example_counter example
# TYPE prom_example_counter counter
prom_example_counter{static_label="example"} 1
# HELP prom_example_dynamic_labeled example
# TYPE prom_example_dynamic_labeled counter
prom_example_dynamic_labeled{static_label="example",test_label="val1"} 100
prom_example_dynamic_labeled{static_label="example",test_label="val2"} 0
# HELP prom_example_gauge example
# TYPE prom_example_gauge gauge
prom_example_gauge{static_label="example"} 0
# HELP prom_example_timer example
# TYPE prom_example_timer histogram
prom_example_timer_bucket{static_label="example",le="0.005"} 0
prom_example_timer_bucket{static_label="example",le="0.01"} 0
prom_example_timer_bucket{static_label="example",le="0.025"} 0
prom_example_timer_bucket{static_label="example",le="0.05"} 0
prom_example_timer_bucket{static_label="example",le="0.1"} 0
prom_example_timer_bucket{static_label="example",le="0.25"} 0
prom_example_timer_bucket{static_label="example",le="0.5"} 0
prom_example_timer_bucket{static_label="example",le="1"} 1
prom_example_timer_bucket{static_label="example",le="2.5"} 1
prom_example_timer_bucket{static_label="example",le="5"} 1
prom_example_timer_bucket{static_label="example",le="10"} 1
prom_example_timer_bucket{static_label="example",le="+Inf"} 1
prom_example_timer_sum{static_label="example"} 1
prom_example_timer_count{static_label="example"} 1

func (*Registry) Collect

func (r *Registry) Collect(ch chan<- prometheus.Metric)

func (*Registry) Describe

func (r *Registry) Describe(ch chan<- *prometheus.Desc)

func (*Registry) NewCounter

func (r *Registry) NewCounter(name, help string, opts ...metrics.Option) metrics.Counter

func (*Registry) NewGauge

func (r *Registry) NewGauge(name, help string, opts ...metrics.Option) metrics.Gauge

func (*Registry) NewHistogram

func (r *Registry) NewHistogram(name, help string, opts ...metrics.Option) metrics.Histogram

func (*Registry) NewLabeledCounter

func (r *Registry) NewLabeledCounter(name, help string, labels []string, opts ...metrics.Option) metrics.LabeledCounter

func (*Registry) NewLabeledGauge

func (r *Registry) NewLabeledGauge(name, help string, labels []string, opts ...metrics.Option) metrics.LabeledGauge

func (*Registry) NewLabeledHistogram

func (r *Registry) NewLabeledHistogram(name, help string, labels []string, opts ...metrics.Option) metrics.LabeledHistogram

func (*Registry) NewLabeledTimer

func (r *Registry) NewLabeledTimer(name, help string, labels []string, opts ...metrics.Option) metrics.LabeledTimer

func (*Registry) NewTimer

func (r *Registry) NewTimer(name, help string, opts ...metrics.Option) metrics.Timer

Jump to

Keyboard shortcuts

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