Categorygithub.com/containerssh/metrics
modulepackage
1.0.0
Repository: https://github.com/containerssh/metrics.git
Documentation: pkg.go.dev

# README

ContainerSSH - Launch Containers on Demand

ContainerSSH Metrics Library

Go Report Card LGTM Alerts

This library provides centralized metrics collection across modules. It also provides a Prometheus / OpenMetrics compatible HTTP server exposing the collected metrics.

⚠⚠⚠ Warning: This is a developer documentation. ⚠⚠⚠
The user documentation for ContainerSSH is located at containerssh.io.

Collecting metrics

The core component of the metrics is the metrics.Collector interface. You can create a new instance of this interface by calling metrics.New() with a GeoIP lookup provider from the geoip library as a parameter. You can then dynamically create metrics:

m := metrics.New(geoip)
testCounter, err := m.CreateCounter(
    "test", // Name of the metric
    "MB", // Unit of the metric
    "This is a test", // Help text of the metric
)

You can then increment the counter:

testCounter.Increment()
testCounter.IncrementBy(5)

Alternatively, you can also create a CounterGeo to make a label automatically based on GeoIP lookup:

testCounter, err := m.CreateCounterGeo(
    "test", // Name of the metric
    "MB", // Unit of the metric
    "This is a test", // Help text of the metric
)
testCounter.Increment(net.ParseIP("127.0.0.1"))

If you need a metric that can be decremented or set directly you can use the Gauge type instead. Each Create* method also has a counterpart named MustCreate*, which panics instead of returning an error.

Custom labels

Each of the metric methods allow adding extra labels:

testCounter.Increment(
    net.ParseIP("127.0.0.1"),
    metrics.Label("foo", "bar"),
    metrics.Label("somelabel","somevalue")
)

The following rules apply and will cause a panic if violated:

  • Label names and values cannot be empty.
  • The country label name is reserved for GeoIP usage.

The metrics also have a WithLabels() method that allow for creating a copy of a metric already primed with a set of labels. This can be used when passing metrics to other modules that need to be scoped.

Using the metrics server

The metrics server exposes the collected metrics on an HTTP webserver in the Prometheus / OpenMetrics format. It requires the service library and a logger from the log library to work properly:

server := metrics.NewServer(
    metrics.Config{
        ServerConfiguration: http.ServerConfiguration{
            Listen:       "127.0.0.1:8080",
        },
        Enable:              true,
        Path:                "/metrics",
    },
    metricsCollector,
    logger,
)

lifecycle := service.NewLifecycle(server)
go func() {
    if err := lifecycle.Run(); err != nil {
        // Handle crash
    } 	
}()

//Later:
lifecycle.Stop(context.Background())

Alternatively, you can skip the full HTTP server and request a handler that you can embed in any Go HTTP server:

handler := metrics.NewHandler(
    "/metrics",
    metricsCollector
)
http.ListenAndServe("0.0.0.0:8080", handler)

# Functions

Label creates a MetricLabel for use with the metric functions.
New creates the metric collector.
NewHandler creates a new HTTP handler that outputs the collected metrics to the requesting client in the Prometheus/OpenMetrics format.
NewServer creates a new metrics server based on the configuration.

# Constants

MetricTypeCounter is a data type that contains ever increasing numbers from the start of the server.
MetricTypeGauge is a metric type that can increase or decrease depending on the current value.
The metrics service is now online and ready for service.

# Variables

CounterCannotBeIncrementedByNegative is an error returned by counters when they are incremented with a negative number.
MetricAlreadyExists is an error that is returned from the Create functions when the metric already exists.

# Structs

No description provided by the author
Metric is a descriptor for metrics.
MetricLabel is a struct that can be used with the metrics to pass additional labels.
MetricValue is a structure that contains a value for a specific metric name and set of values.

# Interfaces

Collector is the main interface for interacting with the metrics collector.
Counter extends the SimpleCounter interface by adding a WithLabels function to create a copy of the counter that is primed with a set of labels.
Gauge extends the SimpleGauge interface by adding a WithLabels function to create a copy of the counter that is primed with a set of labels.
GeoCounter extends the SimpleGeoCounter interface by adding a WithLabels function to create a copy of the counter that is primed with a set of labels.
GeoGauge extends the SimpleGeoGauge interface by adding a WithLabels function to create a copy of the counter that is primed with a set of labels.
SimpleCounter is a simple counter that can only be incremented.
SimpleGauge is a metric that can be incremented and decremented.
SimpleGeoCounter is a simple counter that can only be incremented and is labeled with the country from a GeoIP lookup.
SimpleGeoGauge is a metric that can be incremented and decremented and is labeled by the country from a GeoIP lookup.

# Type aliases

MetricType is the enum for tye types of metrics supported.