Categorygithub.com/zeim839/go-metrics-plus
modulepackage
0.0.0-20240514141728-2113ccc707c7
Repository: https://github.com/zeim839/go-metrics-plus.git
Documentation: pkg.go.dev

# README

go-metrics-plus

Go Reference JavaScript Style Guide GitHub Workflow Status (with event) GitHub go.mod Go version (subdirectory of monorepo)

Go Metrics Library with support for Graphite, InfluxDB, Prometheus, StatsD, and AppOptics. This is a lively fork of RCrowley's go-metrics including updated backend drivers, support for labels/tags, and various optimizations.

Install

Download as a Go module dependency:

go get github.com/zeim839/go-metrics-plus

Import into your project:

import "github.com/zeim839/go-metrics-plus"

Usage

c := metrics.NewCounter(nil)
metrics.Register("foo", c)
c.Inc(47)

g := metrics.NewGauge(metrics.Labels{"key":"value"})
metrics.Register("bar", g)
g.Update(47)

r := NewRegistry()
g := metrics.NewRegisteredFunctionalGauge("cache-evictions", r, func() int64 {
	return cache.getEvictionsCount()
}, metrics.Labels{"key":"value"})

s := metrics.NewExpDecaySample(1028, 0.015) // or metrics.NewUniformSample(1028)
h := metrics.NewHistogram(s, nil)
metrics.Register("baz", h)
h.Update(47)

m := metrics.NewMeter(nil)
metrics.Register("quux", m)
m.Mark(47)

t := metrics.NewTimer(nil)
metrics.Register("bang", t)
t.Time(func() {})
t.Update(47)

Register() is not threadsafe. For threadsafe metric registration use GetOrRegister:

t := metrics.GetOrRegisterTimer("account.create.latency", nil, nil)
t.Time(func() {})
t.Update(47)

Periodically log every metric in human-readable form to standard error:

import (
	"github.com/zeim839/go-metrics-plus"
	"github.com/zeim839/go-metrics-plus/logging"
	"time"
)

// Log the DefaultRegistry to stdout every second.
go logging.Logger(logging.Encode, metrics.DefaultRegistry, time.Second, "some.prefix"

Publishing Metrics

Contributing

Thank you for considering to contribute to go-metrics-plus. We accept contributions from anyone on the internet.

If you'd like to propose a new feature or report an issue, please do so in the GitHub issues section. If you'd like to contribute documentation or code, then please fork, fix, commit, and send a pull request. A maintainer will then review and merge your changes to the codebase. Pull requests must be opened on the 'master' branch.

Please make sure your contributions adhere to our coding guidelines:

  • Code must adhere to the official Go formatting guidelines (i.e uses gofmt).
  • Code must be documented adhering to the official Go commentary guidelines.
  • Pull requests need to be based on and opened against the master branch.

License

This project is an amalgamation of various open-source projects. You may assume that each directory in the source tree represents a separate package distribution, with its corresponding license documented in a LICENSE.md file contained within the directory.

The root directory is governed by go-metrics's original project license, reproduced in LICENSE.md.

# Packages

No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author

# Functions

CaptureCPUStats captures new values for the Go process CPU usage statistics exported in cpu.CPUStats.
CaptureCPUStatsOnce captures new values for the Go process CPU usage statistics exported in cpu.CPUStats.
CaptureDebugGCStats captures new values for the Go garbage collector statistics exported in debug.GCStats.
CaptureDebugGCStatsOnce capture new values for the Go garbage collector statistics exported in debug.GCStats.
CaptureDiskStats captures new values for the Go process disk usage statistics exported in disk.DiskStats.
CaptureDiskStatsOnce captures new values for the Go process disk usage statistics exported in disk.DiskStats.
CaptureRuntimeMemStats captures new values for the Go runtime statistics exported in runtime.MemStats.
CaptureRuntimeMemStatsOnce captures new values for the Go runtime statistics exported in runtime.MemStats.
Each calls the given function for each registered metric.
Get the metric by the given name or nil if none is registered.
GetOrRegister gets an existing metric or creates and registers a new one.
GetOrRegisterCounter returns an existing Counter or constructs and registers a new StandardCounter.
GetOrRegisterGauge returns an existing Gauge or constructs and registers a new StandardGauge.
GetOrRegisterGaugeFloat64 returns an existing GaugeFloat64 or constructs and registers a new StandardGaugeFloat64.
GetOrRegisterHistogram returns an existing Histogram or constructs and registers a new StandardHistogram.
GetOrRegisterMeter returns an existing Meter or constructs and registers a new StandardMeter.
GetOrRegisterTimer returns an existing Timer or constructs and registers a new StandardTimer.
MustRegister registers the given metric under the given name.
NewCounter constructs a new StandardCounter.
NewCustomTimer constructs a new StandardTimer from a Histogram and a Meter.
NewEWMA constructs a new EWMA with the given alpha and period.
NewEWMA1 constructs a new EWMA for a one-minute moving average.
NewEWMA15 constructs a new EWMA for a fifteen-minute moving average.
NewEWMA5 constructs a new EWMA for a five-minute moving average.
NewExpDecaySample constructs a new exponentially-decaying sample with the given reservoir size and alpha.
NewFunctionalGauge constructs a new FunctionalGauge.
NewFunctionalGaugeFloat64 constructs a new FunctionalGauge.
NewGauge constructs a new StandardGauge.
NewGaugeFloat64 constructs a new StandardGaugeFloat64.
NewHealthcheck constructs a new Healthcheck which will use the given function to update its status.
NewHistogram constructs a new StandardHistogram from a Sample.
NewMeter constructs a new StandardMeter.
NewPrefixedChildRegistry creates a new PrefixedRegistry from an existing parent registry and assigns the specified prefix.
NewPrefixedRegistry creates a new PrefixedRegistry using the specified prefix.
NewRegisteredCounter constructs and registers a new StandardCounter.
NewRegisteredFunctionalGauge constructs and registers a new StandardGauge.
NewRegisteredFunctionalGaugeFloat64 constructs and registers a new StandardGauge.
NewRegisteredGauge constructs and registers a new StandardGauge.
NewRegisteredGaugeFloat64 constructs and registers a new StandardGaugeFloat64.
NewRegisteredHistogram constructs and registers a new StandardHistogram from a Sample.
NewRegisteredMeter constructs and registers a new StandardMeter.
NewRegisteredTimer constructs and registers a new StandardTimer.
NewRegistry creates a new registry.
NewSampleSnapshot creates a new instance of SampleSnapshot with the given count and values.
NewTimer constructs a new StandardTimer using an exponentially-decaying sample with the same reservoir size and alpha as UNIX load averages.
NewUniformSample constructs a new uniform sample with the given reservoir size.
ReadCPUStats retrieves the current CPU stats.
ReadDiskStats retrieves the disk IO stats belonging to the current process.
Register the given metric under the given name.
RegisterCPUStats registers metrics for the Go process CPU usage statistics exported in cpu.CPUStats.
RegisterDebugGCStats registers metrics for the Go garbage collector statistics exported in debug.GCStats.
RegisterDiskStats registers metrics for the Go process disk usage statistics exported in disk.DiskStats.
RegisterRuntimeMemStats registers runtime metrics for the Go runtime statistics exported in runtime and specifically runtime.MemStats.
RunHealthchecks runs all registered healthchecks.
SampleMax returns the maximum value of the slice of int64.
SampleMean returns the mean value of the slice of int64.
SampleMin returns the minimum value of the slice of int64.
SamplePercentile returns an arbitrary percentile of the slice of int64.
SamplePercentiles returns a slice of arbitrary percentiles of the slice of int64.
SampleStdDev returns the standard deviation of the slice of int64.
SampleSum returns the sum of the slice of int64.
SampleVariance returns the variance of the slice of int64.
SinkOnce enqueues the given metric without registering it, allowing it to be picked up by Each() only once.
Unregister the metric with the given name.
WriteJSON writes metrics from the given registry periodically to the specified io.Writer as JSON.
WriteJSONOnce writes metrics from the given registry to the specified io.Writer as JSON.

# Variables

DefaultRegistry is a globally-scoped registry.
UseNilMetrics is checked by the constructor functions for all of the standard metrics.

# Structs

CounterSnapshot is a read-only copy of another Counter.
CPUStats is the system and process CPU stats.
DiskStats is the per process disk io stats.
ExpDecaySample is an exponentially-decaying sample using a forward-decaying priority reservoir.
FunctionalGauge returns value from given function.
FunctionalGaugeFloat64 returns value from given function.
GaugeFloat64Snapshot is a read-only copy of another GaugeFloat64.
GaugeSnapshot is a read-only copy of another Gauge.
HistogramSnapshot is a read-only copy of another Histogram.
MeterSnapshot is a read-only copy of another Meter.
NilCounter is a no-op Counter.
NilEWMA is a no-op EWMA.
NilGauge is a no-op Gauge.
NilGaugeFloat64 is a no-op Gauge.
NilHealthcheck is a no-op.
NilHistogram is a no-op Histogram.
NilMeter is a no-op Meter.
NilSample is a no-op Sample.
NilTimer is a no-op Timer.
PrefixedRegistry manages an underlying registry but exposes it with some given prefix.
SampleSnapshot is a read-only copy of another Sample.
StandardCounter is the standard implementation of a Counter and uses the sync/atomic package to manage a single int64 value.
StandardEWMA is the standard implementation of an EWMA.
StandardGauge is the standard implementation of a Gauge and uses the sync/atomic package to manage a single int64 value.
StandardGaugeFloat64 is the standard implementation of a GaugeFloat64 and uses atomic uint64 (holds float bytes) to manage a single float64 value.
StandardHealthcheck is the standard implementation of a Healthcheck and stores the pstatus and a function to call to update the status.
StandardHistogram is the standard implementation of a Histogram and uses a Sample to bound its memory use.
StandardMeter is the standard implementation of a Meter.
StandardRegistry is the standard implementation of a Registry is a mutex-protected map of names to metrics.
StandardTimer is the standard implementation of a Timer and uses a Histogram and Meter.
TimerSnapshot is a read-only copy of another Timer.
A UniformSample is a sample using Vitter's Algorithm R.

# Interfaces

Counter holds an int64 value that can be incremented and decremented.
EWMA calculates an exponentially-weighted per-second moving average.
Gauge holds an int64 value that can be set arbitrarily.
GaugeFloat64 holds a float64 value that can be set arbitrarily.
Healthcheck holds an error value describing an arbitrary up/down status.
Histogram calculates distribution statistics from a series of int64 values.
Meter counts events to produce exponentially-weighted moving average rates at one-, five-, and fifteen-minutes and a mean rate.
A Registry holds references to a set of metrics by name and can iterate over them, calling callback functions provided by the user.
Sample maintains a statistically-significant selection of values from a stream.
Stoppable defines the metrics which has to be stopped.
Timer captures the duration and rate of events.

# Type aliases

DuplicateMetric is the error returned by Registry.Register when a metric already exists.
EWMASnapshot is a read-only copy of another EWMA.