metrics

package
v0.6.0 Latest Latest
Warning

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

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

README

Metrics

Usage

This package provides a set of interfaces and implementations for collecting and exposing metrics in your application. The main interfaces are MetricCollector, Observable, and Counter which are defined in metrics.go. The prometheus.go file provides an implementation of these interfaces using the Prometheus monitoring system.

This package is easy to test as it is based on interfaces. You can create mock implementations of the MetricCollector, Observable, and Counter interfaces for testing purposes.

For New Packages

To use this metrics package in your application, you need to do the following:

  1. Import the metrics package in your code.
  2. Create a new instance of the MetricCollector using the NewPrometheus() function.
  3. Use the Register* methods to register new metrics. You can register gauges, counters, and histograms.
  4. Use the SetGauge, AddCounter, and ObserveHistogram methods to update the metrics.
  5. Use the MetricsHandler method to get an http.Handler for serving the metrics.
  6. Expose the http.Handler along with your HTTP server. Note that Prometheus does not start a server by itself, so you need to integrate it with your existing server or start a new one.
To Scrape the Metrics

To scrape the metrics from Prometheus, you can use the following example:

curl http://localhost:8080/metrics

This will return a plain text page with a series of lines like this:

# TYPE go_memstats_stack_sys_bytes gauge
go_memstats_stack_sys_bytes 884736
# HELP go_memstats_sys_bytes Number of bytes obtained from system.
# TYPE go_memstats_sys_bytes gauge
go_memstats_sys_bytes 2.25188e+07
# HELP go_threads Number of OS threads created.
# TYPE go_threads gauge
go_threads 10
# HELP guac_http_deps_dev_version_errors Counter for http_deps_dev_version_errors
# TYPE guac_http_deps_dev_version_errors counter
guac_http_deps_dev_version_errors{name="antlr",namespace="github.com/antlr/antlr4/runtime/go",pkgtype="golang"} 2
guac_http_deps_dev_version_errors{name="api",namespace="github.com/hashicorp/vault",pkgtype="golang"} 1
guac_http_deps_dev_version_errors{name="consul",namespace="github.com/hashicorp",pkgtype="golang"} 1
guac_http_deps_dev_version_errors{name="name",namespace="namespace",pkgtype="pkgtype"} 0
guac_http_deps_dev_version_errors{name="readline",namespace="github.com/chzyer",pkgtype="golang"} 1
guac_http_deps_dev_version_errors{name="sdk",namespace="github.com/hashicorp/vault",pkgtype="golang"} 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func WithMetrics added in v0.4.0

func WithMetrics(ctx context.Context, name string) context.Context

WithMetrics returns a new context with a prometheusCollector.

Types

type Counter added in v0.4.0

type Counter interface {
	Inc()
	Add(float64)
}

Counter is an interface that allows for incrementing and adding a float64 value.

type MetricCollector added in v0.4.0

type MetricCollector interface {
	// RegisterHistogram registers a histogram metric with the given name and labels.
	RegisterHistogram(ctx context.Context, name string, labels ...string) (Observable, error)
	// RegisterGauge registers a gauge metric with the given name and labels.
	RegisterGauge(ctx context.Context, name string, labels ...string) (Counter, error)
	// RegisterCounter registers a counter metric with the given name and labels.
	RegisterCounter(ctx context.Context, name string, labels ...string) (Counter, error)
	// ObserveHistogram observes a value for the histogram metric with the given name and labels.
	ObserveHistogram(ctx context.Context, name string, value float64, labels ...string) error
	// SetGauge sets a value for the gauge metric with the given name and labels.
	SetGauge(ctx context.Context, name string, value float64, labels ...string) error
	// AddCounter adds a value to the counter metric with the given name and labels.
	AddCounter(ctx context.Context, name string, value float64, labels ...string) error
	// MetricsHandler returns an http.Handler for serving the metrics.
	MetricsHandler() http.Handler
	// MeasureFunctionExecutionTime measures the execution time of a function with the given name.
	MeasureFunctionExecutionTime(ctx context.Context, name string) (func(), error)
	// UnregisterCounter unregisters the counter metric with the given name and labels.
	UnregisterCounter(ctx context.Context, name string, labels ...string) error
	// UnregisterHistogram unregisters the histogram metric with the given name and labels.
	UnregisterHistogram(ctx context.Context, name string, labels ...string) error
	// UnregisterGauge unregisters the gauge metric with the given name and labels.
	UnregisterGauge(ctx context.Context, name string, labels ...string) error
	// MeasureGraphQLResponseDuration returns a http.Handler that measures the response duration of the given handler.
	MeasureGraphQLResponseDuration(next http.Handler) http.Handler
}

MetricCollector is an interface that provides methods for registering and manipulating metrics.

func FromContext added in v0.4.0

func FromContext(ctx context.Context, name string) MetricCollector

FromContext returns the MetricCollector from the context.

func NewPrometheus

func NewPrometheus(name string) MetricCollector

NewPrometheus creates a new prometheusCollector with empty sync.Maps for histograms, gauges, and counters.

type Observable added in v0.4.0

type Observable interface {
	Observe(float64)
}

Observable is an interface that allows for observing a float64 value.

Jump to

Keyboard shortcuts

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