prom

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Dec 29, 2021 License: MIT Imports: 11 Imported by: 3

README

Prometheus Adapter for Contextualized Stats Tracker

This library provides context-driven stats tracker implementation.

Build Status Coverage Status GoDevDoc time tracker Code lines Comments

Features

  • Context-driven labels control.
  • Zero allocation.
  • A simple interface with variadic number of key-value pairs for labels.
  • Easily mockable interface free from 3rd party dependencies.

Example

// Bring your own Prometheus registry.
registry := prometheus.NewRegistry()
tr := prom.Tracker{
    Registry: registry,
}

// Add custom Prometheus configuration where necessary.
tr.DeclareHistogram("my_latency_seconds", prometheus.HistogramOpts{
    Buckets: []float64{1e-4, 1e-3, 1e-2, 1e-1, 1, 10, 100},
})

ctx := context.Background()

// Add labels to context.
ctx = stats.AddKeysAndValues(ctx, "ctx-label", "ctx-value0")

// Override label values.
ctx = stats.AddKeysAndValues(ctx, "ctx-label", "ctx-value1")

// Collect stats with last mile labels.
tr.Add(ctx, "my_count", 1,
    "some-label", "some-value",
)

tr.Add(ctx, "my_latency_seconds", 1.23)

tr.Set(ctx, "temperature", 33.3)

Performance

Sample benchmark result with go1.16.

goos: darwin
goarch: amd64
pkg: github.com/bool64/stats/prom-stats
cpu: Intel(R) Core(TM) i7-8559U CPU @ 2.70GHz
name             time/op
Tracker_Add-8    635ns ± 3%
RawPrometheus-8  472ns ± 2%

name             alloc/op
Tracker_Add-8    0.00B     
RawPrometheus-8   336B ± 0%

name             allocs/op
Tracker_Add-8     0.00     
RawPrometheus-8   2.00 ± 0%

Caveats

Prometheus client does not support metrics with same name and different label sets. If you add a label to context, make sure you have it in all cases, at least with an empty value "".

Versioning

This project adheres to Semantic Versioning.

Before version 1.0.0, breaking changes are tagged with MINOR bump, features and fixes are tagged with PATCH bump. After version 1.0.0, breaking changes are tagged with MAJOR bump.

Documentation

Overview

Package prom provides stats tracker implementation based on prometheus client.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type PrometheusRegistryProvider added in v0.1.3

type PrometheusRegistryProvider interface {
	PrometheusRegistry() *prometheus.Registry
}

PrometheusRegistryProvider provides a prometheus.Registry.

type Tracker

type Tracker struct {

	// Deprecated: Use PrometheusRegistry() instead.
	Registry  *prometheus.Registry
	ErrLogger func(ctx context.Context, err error, labels []string)
	// contains filtered or unexported fields
}

Tracker implements stats tracker with prometheus registry.

Please use NewStatsTracker to create new instance.

Example
// Bring your own Prometheus registry.
registry := prometheus.NewRegistry()
tr := prom.Tracker{
	Registry: registry,
}

// Add custom Prometheus configuration where necessary.
tr.DeclareHistogram("my_latency_seconds", prometheus.HistogramOpts{
	Buckets: []float64{1e-4, 1e-3, 1e-2, 1e-1, 1, 10, 100},
})

ctx := context.Background()

// Add labels to context.
ctx = stats.AddKeysAndValues(ctx, "ctx-label", "ctx-value0")

// Override label values.
ctx = stats.AddKeysAndValues(ctx, "ctx-label", "ctx-value1")

// Collect stats with last mile labels.
tr.Add(ctx, "my_count", 1,
	"some-label", "some-value",
)

tr.Add(ctx, "my_latency_seconds", 1.23)

tr.Set(ctx, "temperature", 33.3)
Output:

func NewStatsTracker

func NewStatsTracker(registry *prometheus.Registry) (*Tracker, error)

NewStatsTracker creates prometheus stats tracker.

func (*Tracker) Add

func (t *Tracker) Add(ctx context.Context, name string, value float64, labelsAndValues ...string)

Add collects value to Counter, Summary or Histogram.

func (*Tracker) DeclareCounter

func (t *Tracker) DeclareCounter(name string, opts prometheus.CounterOpts)

DeclareCounter registers counter metric for given name.

func (*Tracker) DeclareGauge

func (t *Tracker) DeclareGauge(name string, opts prometheus.GaugeOpts)

DeclareGauge registers gauge metric for given name.

func (*Tracker) DeclareHistogram

func (t *Tracker) DeclareHistogram(name string, opts prometheus.HistogramOpts)

DeclareHistogram registers histogram metric for given name.

func (*Tracker) DeclareSummary

func (t *Tracker) DeclareSummary(name string, opts prometheus.SummaryOpts)

DeclareSummary registers summary metric for given name.

func (*Tracker) PrometheusRegistry added in v0.1.3

func (t *Tracker) PrometheusRegistry() *prometheus.Registry

PrometheusRegistry provides a prometheus.Registry.

func (*Tracker) Reset

func (t *Tracker) Reset()

Reset unregisters and removes all registered collectors.

func (*Tracker) Set

func (t *Tracker) Set(ctx context.Context, name string, absolute float64, labelsAndValues ...string)

Set collects absolute value to Gauge.

func (*Tracker) StatsTracker

func (t *Tracker) StatsTracker() stats.Tracker

StatsTracker is a service locator provider.

Jump to

Keyboard shortcuts

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