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 ¶
- Variables
- func NormalizeTags(tags []string) []string
- func Register(kind string, factory Factory)
- func RegisteredProcessors() []string
- func SetDefault(c *Client)
- func TagSet(tags []string) string
- type Client
- func (c *Client) Close() error
- func (c *Client) Count(name string, value int64, tags []string) error
- func (c *Client) CounterHandle(name string, tags []string) (Counter, error)
- func (c *Client) DeleteSeriesMatching(name string, tags []string) error
- func (c *Client) Distribution(name string, value float64, tags []string) error
- func (c *Client) DistributionHandle(name string, tags []string) (Observer, error)
- func (c *Client) Gauge(name string, value float64, tags []string) error
- func (c *Client) GaugeHandle(name string, tags []string) (Gauge, error)
- type Config
- type Counter
- type DefaultHandle
- type Factory
- type Gauge
- type HandleProvider
- type Observer
- type Processor
- type SeriesDeleter
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSeriesDeleteUnsupported reports that none of the configured processors can // retire a series, so the series a caller asked to delete is still published. // It is returned rather than absorbed because the caller cannot tell otherwise: // a successful delete and a delete nobody could perform both leave it with no // error and no way to know the label it wanted gone is still being exported. ErrSeriesDeleteUnsupported = errors.New("no configured processor can delete a series") // ErrEmptySeriesMatch reports a delete whose tags named no label. An empty match // selects every child of the metric, so it is refused instead of being obeyed: // wiping a whole metric family is not what a caller who passed no tags — or // passed one assembled from an empty subject — meant to ask for. ErrEmptySeriesMatch = errors.New("series delete requires at least one key:value tag") )
Functions ¶
func NormalizeTags ¶
NormalizeTags returns a stable copy of tags suitable for processors.
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.
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 ¶
New creates a metrics client from configured processors. Returns nil if cfg is not enabled.
func (*Client) CounterHandle ¶ added in v1.2.5
CounterHandle resolves one counter handle for the metric, covering every configured processor. See HandleProvider.
func (*Client) DeleteSeriesMatching ¶ added in v1.2.6
DeleteSeriesMatching retires the series in every configured processor that can retire one. See SeriesDeleter for the match semantics and for when retiring is the right thing to do at all.
A processor without the capability is skipped rather than reported: it holds no resident series, so there is nothing there to retire and its presence in the client is not a failure. Only a client where no processor at all can delete reports ErrSeriesDeleteUnsupported.
func (*Client) Distribution ¶
Distribution records a distribution metric.
func (*Client) DistributionHandle ¶ added in v1.2.5
DistributionHandle resolves one distribution handle for the metric, covering every configured processor. See HandleProvider.
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 ¶
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
PrometheusOnly returns a config that uses Prometheus for every enabled metrics setup.
func (Config) EnabledProcessors ¶
EnabledProcessors returns the configured processors.
type Counter ¶ added in v1.2.5
type Counter = interface{ Add(value int64) }
Counter, Gauge and Observer are resolved metric handles: a caller resolves one through HandleProvider at wiring time and records through it afterwards, so turning a (name, tags) tuple back into a backend child metric happens once instead of on every record. On a data path that records the same bounded set of tuples forever, that is the difference between a hashed cache lookup per record and an increment.
They are aliases to interface literals rather than defined types, and that is load-bearing. A consumer pinned to a release of this module that predates HandleProvider cannot name metrics.Counter, but it can declare the identical literal locally and assert the capability structurally, because Go matches method signatures on type identity and a defined type is never identical to any other type. The aliases are therefore what let a consumer use handles when the linked version provides them and keep using Count/Gauge/Distribution when it does not, without bumping its pin in lockstep with this addition. Do not turn them into defined types.
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 Gauge ¶ added in v1.2.5
type Gauge = interface{ Set(value float64) }
Counter, Gauge and Observer are resolved metric handles: a caller resolves one through HandleProvider at wiring time and records through it afterwards, so turning a (name, tags) tuple back into a backend child metric happens once instead of on every record. On a data path that records the same bounded set of tuples forever, that is the difference between a hashed cache lookup per record and an increment.
They are aliases to interface literals rather than defined types, and that is load-bearing. A consumer pinned to a release of this module that predates HandleProvider cannot name metrics.Counter, but it can declare the identical literal locally and assert the capability structurally, because Go matches method signatures on type identity and a defined type is never identical to any other type. The aliases are therefore what let a consumer use handles when the linked version provides them and keep using Count/Gauge/Distribution when it does not, without bumping its pin in lockstep with this addition. Do not turn them into defined types.
type HandleProvider ¶ added in v1.2.5
type HandleProvider interface {
CounterHandle(name string, tags []string) (Counter, error)
GaugeHandle(name string, tags []string) (Gauge, error)
DistributionHandle(name string, tags []string) (Observer, error)
}
HandleProvider is the optional capability of resolving a metric handle before recording. It is deliberately not part of Processor: a processor that does not implement it keeps recording through Count, Gauge and Distribution, so implementing it stays opt-in per backend and callers detect support with a type assertion. A returned handle must be safe for concurrent use and stays valid for the life of the processor.
type Observer ¶ added in v1.2.5
type Observer = interface{ Observe(value float64) }
Counter, Gauge and Observer are resolved metric handles: a caller resolves one through HandleProvider at wiring time and records through it afterwards, so turning a (name, tags) tuple back into a backend child metric happens once instead of on every record. On a data path that records the same bounded set of tuples forever, that is the difference between a hashed cache lookup per record and an increment.
They are aliases to interface literals rather than defined types, and that is load-bearing. A consumer pinned to a release of this module that predates HandleProvider cannot name metrics.Counter, but it can declare the identical literal locally and assert the capability structurally, because Go matches method signatures on type identity and a defined type is never identical to any other type. The aliases are therefore what let a consumer use handles when the linked version provides them and keep using Count/Gauge/Distribution when it does not, without bumping its pin in lockstep with this addition. Do not turn them into defined types.
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.
type SeriesDeleter ¶ added in v1.2.6
SeriesDeleter is the optional capability of retiring a published series: delete every child of name whose labels include all of tags. Like HandleProvider it is deliberately not part of Processor — only a backend that holds its series in-process has anything to retire, so a push backend needs no change and callers detect support with a type assertion. *Client implements it and fans the delete out to whichever processors do.
The match is partial rather than exact because the labels that identify a subject are usually not all of its labels: a series split by an open-ended label (an error reason, a status class) has children a caller cannot enumerate, and retiring the subject has to retire all of them. An empty match is refused with ErrEmptySeriesMatch instead of matching everything.
Two rules for callers:
- Retire only what has genuinely gone away. A counter for a subject that is merely unreachable is exactly the signal an operator needs during an incident, and deleting it destroys that signal at the moment it matters.
- Retire series recorded through Count, Gauge and Distribution, which resolve their backend child per record and therefore recreate it. A handle resolved through HandleProvider holds the child directly, so deleting that child orphans the handle: records through it are accepted and exported nowhere. Give a retirable series the recording path, or re-resolve the handle afterwards.
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
|
|