metrics

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2020 License: ISC Imports: 3 Imported by: 0

README

metrics

This is a helper package for creating consistent Prometheus metrics.

Quick Start

For creating new metrics using default buckets and quantiles and register them with default registry:

package main

import (
  "log"
  "net/http"

  "github.com/moorara/observe/metrics"
  "github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
  mf := metrics.NewFactory(metrics.FactoryOptions{})

  // Create a histogram metric
  histogram := mf.Histogram("session_duration_seconds", "duration of user sessions", []string{"environment", "region"})
  histogram.WithLabelValues("prodcution", "us-east-1").Observe(0.0027)

  // Expose metrics via /metrics endpoint and an HTTP server
  http.Handle("/metrics", promhttp.Handler())
  log.Fatal(http.ListenAndServe(":8080", nil))
}

For creating new metrics using custom buckets and quantiles and register them with a new registry:

package main

import (
  "log"
  "net/http"

  "github.com/moorara/observe/metrics"
  "github.com/prometheus/client_golang/prometheus"
  "github.com/prometheus/client_golang/prometheus/promhttp"
)

func main() {
  registry := prometheus.NewRegistry()
  mf := metrics.NewFactory(metrics.FactoryOptions{
    Prefix:     "auth-service",
    Registerer: registry,
    Buckets:    []float64{0.01, 0.10, 0.50, 1.00, 5.00},
    Quantiles:  map[float64]float64{0.5: 0.05, 0.9: 0.01, 0.99: 0.001},
  })

  // Create a histogram metric
  histogram := mf.Histogram("session_duration_seconds", "duration of user sessions", []string{"environment", "region"})
  histogram.WithLabelValues("prodcution", "us-east-1").Observe(0.0027)

  // Expose metrics via /metrics endpoint and an HTTP server
  http.Handle("/metrics", promhttp.HandlerFor(registry, promhttp.HandlerOpts{}))
  log.Fatal(http.ListenAndServe(":8080", nil))
}

Defaults

Default buckets:

[]float64{0.01, 0.10, 0.50, 1.00, 5.00}

Default quantiles:

map[float64]float64{
  0.1:  0.1,
  0.5:  0.05,
  0.95: 0.01,
  0.99: 0.001,
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Factory

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

Factory is used for creating new metrics with consistent settings.

func NewFactory

func NewFactory(opts FactoryOptions) *Factory

NewFactory creates a new instance of Factory.

func (*Factory) Counter

func (f *Factory) Counter(name, description string, labels []string) *prometheus.CounterVec

Counter creates a new counter metrics.

func (*Factory) Gauge

func (f *Factory) Gauge(name, description string, labels []string) *prometheus.GaugeVec

Gauge creates a new gauge metrics.

func (*Factory) Histogram

func (f *Factory) Histogram(name, description string, labels []string) *prometheus.HistogramVec

Histogram creates a new histogram metrics.

func (*Factory) Summary

func (f *Factory) Summary(name, description string, labels []string) *prometheus.SummaryVec

Summary creates a new summary metrics.

type FactoryOptions

type FactoryOptions struct {
	Prefix     string
	Buckets    []float64
	Quantiles  map[float64]float64
	Registerer prometheus.Registerer
}

FactoryOptions contains optional options for creating a Factory.

type OpMetrics

type OpMetrics struct {
	OpLatencyHist *prometheus.HistogramVec
	OpLatencySumm *prometheus.SummaryVec
}

OpMetrics includes metrics for internal operations.

type RequestMetrics

type RequestMetrics struct {
	ReqCounter      *prometheus.CounterVec
	ReqGauge        *prometheus.GaugeVec
	ReqDurationHist *prometheus.HistogramVec
	ReqDurationSumm *prometheus.SummaryVec
}

RequestMetrics includes metrics for service requests.

Jump to

Keyboard shortcuts

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