mmetric

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2025 License: MIT Imports: 5 Imported by: 1

Documentation

Index

Constants

View Source
const (
	// HTTP attributes.
	AttrHTTPRoute              = "http.route"
	AttrHTTPRequestMethod      = "http.request.method"
	AttrHTTPResponseStatusCode = "http.response.status_code"

	// Network attributes.
	AttrNetworkProtocolVersion = "network.protocol.version"
	AttrServerAddress          = "server.address"
	AttrServerPort             = "server.port"
	AttrClientAddress          = "client.address"

	// URL attributes.
	AttrURLScheme = "url.scheme"
	AttrURLPath   = "url.path"
	AttrURLHost   = "url.host"

	// Error attributes.
	AttrErrorCode = "error.code"
)

Semantic conventions for metric attributes. These keys are based on the OpenTelemetry specification for semantic conventions. See: https://opentelemetry.io/docs/specs/semconv/

Variables

This section is empty.

Functions

func NewProvider added in v0.1.3

func NewProvider(opts ...sdkmetric.Option) *sdkmetric.MeterProvider

NewProvider is a wrapper around sdkmetric.NewMeterProvider. It is a convenience function for creating a new OTel MeterProvider.

func SetProvider

func SetProvider(p metric.MeterProvider)

SetProvider sets the global metric provider. It is a convenience function that wraps otel.SetMeterProvider. The parameter `p` should be a concrete provider that implements the metric.MeterProvider interface, often created by a specific SDK (e.g., `*sdkmetric.MeterProvider`).

func Shutdown

func Shutdown(ctx context.Context) error

Shutdown gracefully shuts down the global metric provider.

Types

type Attributes

type Attributes []attribute.KeyValue

Attributes is a slice of attribute.KeyValue. It is used to add metadata to metrics. Using attribute.KeyValue allows for strongly-typed attributes, which is recommended by OpenTelemetry for better performance and correctness.

Example:

attrs := mmetric.Attributes{
	attribute.String("request.method", "GET"),
	attribute.Int("response.status_code", 200),
}

type Counter

type Counter interface {
	// Add adds a value to the counter. The value must be non-negative.
	Add(ctx context.Context, value float64, opts ...Option)
	// Inc increments the counter by 1.
	Inc(ctx context.Context, opts ...Option)
}

Counter is an interface for a counter metric.

func NewCounter

func NewCounter(name string, option MetricOption) (Counter, error)

NewCounter creates a new Counter metric. It uses the global default provider.

func NewMustCounter

func NewMustCounter(name string, option MetricOption) Counter

NewMustCounter creates a new Counter metric and panics if an error occurs.

type Histogram

type Histogram interface {
	// Record records a value in the histogram.
	Record(value float64, opts ...Option)
}

Histogram is an interface for a histogram metric.

func NewHistogram

func NewHistogram(name string, option MetricOption) (Histogram, error)

NewHistogram creates a new Histogram metric.

func NewMustHistogram

func NewMustHistogram(name string, option MetricOption) Histogram

NewMustHistogram creates a new Histogram metric and panics if an error occurs.

type Meter

type Meter interface {
	// Counter creates a new counter metric. A counter is a metric that only goes up.
	Counter(name string, option MetricOption) (Counter, error)
	// MustCounter is like Counter but panics on error.
	MustCounter(name string, option MetricOption) Counter
	// UpDownCounter creates a new up-down counter. This metric can go up and down.
	UpDownCounter(name string, option MetricOption) (UpDownCounter, error)
	// MustUpDownCounter is like UpDownCounter but panics on error.
	MustUpDownCounter(name string, option MetricOption) UpDownCounter
	// Histogram creates a new histogram metric. Histograms are used to measure the
	// distribution of a set of values.
	Histogram(name string, option MetricOption) (Histogram, error)
	// MustHistogram is like Histogram but panics on error.
	MustHistogram(name string, option MetricOption) Histogram
}

Meter is the interface for a metric meter. It is responsible for creating instruments. This is an abstraction over OpenTelemetry's Meter.

func GetMeter

func GetMeter(name string) Meter

GetMeter creates a Meter with the specified instrument name. It uses the global default provider.

type MeterOption

type MeterOption struct {
	// Instrument is the name of the instrumentation library.
	Instrument string
	// InstrumentVersion is the version of the instrumentation library.
	InstrumentVersion string
	// Attributes is a list of attributes that will be attached to all metrics created by this meter.
	Attributes Attributes
}

MeterOption is the option for creating a new meter. A meter is responsible for creating instruments (e.g., counters, histograms).

func NewMeterOption

func NewMeterOption() MeterOption

NewMeterOption creates a new meter option

func (MeterOption) WithInstrument

func (o MeterOption) WithInstrument(instrument string) MeterOption

WithInstrument sets the instrument name

func (MeterOption) WithInstrumentVersion

func (o MeterOption) WithInstrumentVersion(version string) MeterOption

WithInstrumentVersion sets the instrument version

func (MeterOption) WithMeterAttributes

func (o MeterOption) WithMeterAttributes(attrs Attributes) MeterOption

WithMeterAttributes sets the attributes

type MetricOption

type MetricOption struct {
	// Help provides a brief description of the metric. It is used by some backends
	// to display help text in GUIs.
	Help string
	// Unit specifies the unit of the metric. It should follow the UCUM standard.
	// See: https://unitsofmeasure.org/ucum.html
	Unit string
	// Attributes is a list of attributes that will be attached to the metric.
	Attributes Attributes
	// Buckets defines the bucket boundaries for a Histogram. If not set, a default
	// set of buckets will be used by the provider.
	// This is only applicable to Histogram metrics.
	Buckets []float64
}

MetricOption is the option for creating a new metric instrument (e.g., Counter, Histogram).

func NewMetricOption

func NewMetricOption() MetricOption

NewMetricOption creates a new metric option

func (MetricOption) WithBuckets

func (o MetricOption) WithBuckets(buckets []float64) MetricOption

WithBuckets sets the histogram buckets

func (MetricOption) WithHelp

func (o MetricOption) WithHelp(help string) MetricOption

WithHelp sets the help information

func (MetricOption) WithMetricAttributes

func (o MetricOption) WithMetricAttributes(attrs Attributes) MetricOption

WithMetricAttributes sets the attributes

func (MetricOption) WithUnit

func (o MetricOption) WithUnit(unit string) MetricOption

WithUnit sets the unit

type Option

type Option struct {
	// Attributes is a list of attributes that will be attached to this specific metric observation.
	// These attributes are combined with the attributes from the Meter and the Instrument.
	Attributes Attributes
}

Option is the option for a single metric operation, like Add or Inc.

func WithAttributes added in v0.1.3

func WithAttributes(attrs ...attribute.KeyValue) Option

WithAttributes creates an Option with the given attributes. This is a convenience function for creating attributes for a single metric observation.

type Provider

type Provider interface {
	// Meter creates a new meter with the given options.
	Meter(option MeterOption) Meter
	// Shutdown gracefully shuts down the provider, ensuring all buffered metrics are exported.
	Shutdown(ctx context.Context) error
}

Provider is the interface for a metric provider. It is responsible for creating meters. This is an abstraction over OpenTelemetry's MeterProvider.

func GetProvider

func GetProvider() Provider

GetProvider returns the global metric provider, which is a wrapper around the default OpenTelemetry MeterProvider.

type UpDownCounter

type UpDownCounter interface {
	// Add adds a value to the counter. The value can be positive or negative.
	Add(ctx context.Context, value float64, opts ...Option)
	// Inc increments the counter by 1.
	Inc(ctx context.Context, opts ...Option)
	// Dec decrements the counter by 1.
	Dec(ctx context.Context, opts ...Option)
}

UpDownCounter is an interface for an up-down counter metric.

func NewMustUpDownCounter

func NewMustUpDownCounter(name string, option MetricOption) UpDownCounter

NewMustUpDownCounter creates a new UpDownCounter metric and panics if an error occurs.

func NewUpDownCounter

func NewUpDownCounter(name string, option MetricOption) (UpDownCounter, error)

NewUpDownCounter creates a new UpDownCounter metric.

Jump to

Keyboard shortcuts

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