metrics

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: May 14, 2026 License: GPL-3.0 Imports: 7 Imported by: 0

README

metrics

Import path: github.com/InsideGallery/core/metrics

metrics provides backend-agnostic service instrumentation. Services record counts, gauges, and distributions through a Client; processor packages register concrete exporters by name.

Main APIs

  • Config selects processors.
  • GetEnvConfig(prefix ...string) reads metrics config, defaulting to the METRICS prefix.
  • PrometheusOnly(cfg Config) collapses any enabled config to Prometheus.
  • Processor is the exporter interface: Close, Count, Gauge, and Distribution.
  • Register, RegisteredProcessors, and Factory manage processor registration.
  • New(cfg Config, service string) builds a fanout client.
  • Default, SetDefault, and InstallDefault manage the process-wide client.
  • NormalizeTags returns a sorted copy of tags; TagSet joins sorted tags with commas.

Usage

package example

import (
	"errors"

	_ "github.com/InsideGallery/core/metrics/all"

	"github.com/InsideGallery/core/metrics"
)

func recordMetric() (err error) {
	cfg, err := metrics.GetEnvConfig()
	if err != nil {
		return err
	}

	client, err := metrics.New(cfg, "api")
	if err != nil {
		return err
	}
	if client == nil {
		return nil
	}

	handle := metrics.InstallDefault(client)
	defer func() {
		err = errors.Join(err, handle.Close())
	}()

	return client.Count("requests_total", 1, []string{"status:ok"})
}

Configuration

GetEnvConfig reads:

  • METRICS_PROCESSORS: comma-separated processor names, default prometheus.

Processor names are trimmed, lowercased, de-duplicated, and may be split across comma-separated entries. The values none, off, and disabled disable metrics. Processor-specific environment variables do not select processors; they only configure a processor after it has been selected and registered.

Operational Notes

New returns nil, nil when metrics are disabled. A nil *Client is safe to call and returns nil for Close, Count, Gauge, and Distribution.

Import metrics/all or the specific processor packages before selecting processor names in METRICS_PROCESSORS. Processor call errors are joined and wrapped with the metric operation and name.

Documentation

Overview

Package metrics provides backend-agnostic service instrumentation.

Services record metrics through Client or the Processor interface. Concrete exporters live in pkg/metrics/processors/* and register themselves at init, following the same plugin pattern used by pkg/fastlog.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NormalizeTags

func NormalizeTags(tags []string) []string

NormalizeTags returns a stable copy of tags suitable for processors.

func Register

func Register(kind string, factory Factory)

Register makes a metrics processor available by kind.

func RegisteredProcessors

func RegisteredProcessors() []string

RegisteredProcessors returns all registered processor names.

func SetDefault

func SetDefault(c *Client)

SetDefault stores the process-wide metrics client for service-specific instrumentation.

func TagSet

func TagSet(tags []string) string

TagSet returns a stable tag-set strings for processors that cannot model arbitrary labels.

Types

type Client

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

Client fans metric calls out to configured processors.

func Default

func Default() *Client

Default returns the process-wide metrics client, or nil when metrics are disabled.

func New

func New(cfg Config, service string) (*Client, error)

New creates a metrics client from configured processors. Returns nil if cfg is not enabled.

func (*Client) Close

func (c *Client) Close() error

Close flushes pending metrics and closes all processors.

func (*Client) Count

func (c *Client) Count(name string, value int64, tags []string) error

Count records a count metric.

func (*Client) Distribution

func (c *Client) Distribution(name string, value float64, tags []string) error

Distribution records a distribution metric.

func (*Client) Gauge

func (c *Client) Gauge(name string, value float64, tags []string) error

Gauge records a gauge metric.

type Config

type Config struct {
	Processors []string `env:"_PROCESSORS" envDefault:"prometheus"`
}

Config holds backend-agnostic metrics configuration.

Environment:

  • METRICS_PROCESSORS defaults to prometheus.

func GetEnvConfig

func GetEnvConfig(prefix ...string) (Config, error)

GetEnvConfig reads metrics configuration from environment variables. Default prefix is METRICS. Processor-specific packages own their own env config.

func PrometheusOnly added in v1.2.1

func PrometheusOnly(cfg Config) Config

PrometheusOnly returns a config that uses Prometheus for every enabled metrics setup.

func (Config) Enabled

func (c Config) Enabled() bool

Enabled reports whether any processor is configured.

func (Config) EnabledProcessors

func (c Config) EnabledProcessors() []string

EnabledProcessors returns the configured processors.

type DefaultHandle

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

DefaultHandle restores a package-level metrics default and closes its client.

func InstallDefault

func InstallDefault(c *Client) *DefaultHandle

InstallDefault installs a process-wide metrics default with an explicit close path.

func (*DefaultHandle) Client

func (h *DefaultHandle) Client() *Client

Client returns the installed default client.

func (*DefaultHandle) Close

func (h *DefaultHandle) Close() error

Close restores the previous default client and closes the installed client.

type Factory

type Factory func(Config, string) (Processor, error)

Factory creates a concrete metrics processor for a service.

type Processor

type Processor interface {
	Close() error
	Count(name string, value int64, tags []string) error
	Gauge(name string, value float64, tags []string) error
	Distribution(name string, value float64, tags []string) error
}

Processor records metrics for one concrete backend.

Directories

Path Synopsis
Package all imports every in-tree metrics processor so each processor registers with the default metrics registry through its init hook.
Package all imports every in-tree metrics processor so each processor registers with the default metrics registry through its init hook.
processors

Jump to

Keyboard shortcuts

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