metricw

package
v0.10.1 Latest Latest
Warning

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

Go to latest
Published: May 5, 2025 License: MIT Imports: 20 Imported by: 0

Documentation

Overview

Provides a convenience wrapper around the OpenTelemetry SDK metric (see go.opentelemetry.io/otel/sdk/metric) package.

This wrapper simplifies metric configuration and shutdown.

Usage:

metric, err := metricw.Configure(ctx, config.Logger, attrs, writers...)
if err != nil {
	fmt.Fprintf(os.Stderr, "metric configure: %v", err)
	return osx.ExitFailure
}
defer func() {
	err := errors.Join(err, metric.Shutdown(ctx))
	if err != nil {
		fmt.Fprintf(os.Stderr, "metric shutdown: %v", err)
	}
}()

Index

Constants

View Source
const (
	// DefaultEnable is the default setting for enabling metrics.
	DefaultEnable = false

	// DefaultEnable is the default setting for enablingPrometheus metric mapping.
	DefaultPrometheus = false

	// DefaultInterval holds the default metrics collection interval.
	DefaultInterval = 10 * time.Second
)

Variables

View Source
var (
	// ErrInvalidFormat is returned when config.Format is not qual to slogw.Console or slogw.JSON.
	ErrInvalidProtocol = errors.New("invalid protocol")

	// ErrInvalidMetricType is returned when a not supported Prometheus metric type is requested.
	ErrInvalidMetricType = errors.New("invalid metric type")
)

Functions

func Defaults

func Defaults() map[string]any

Defaults returns a map of default configuration values for the metricw package. It includes default settings for enabling metrics, prometheus metrics mapping, metrics collection interval and defaults for the otlp package.

Types

type Config

type Config struct {
	// Enable indicates whether metrics are enabled.
	Enable bool `json:"enable" yaml:"enable" mapstructure:"enable"`

	// Prometheus indicates whether Prometheus metric mapping is enabled.
	Prometheus bool `json:"prometheus" yaml:"prometheus" mapstructure:"prometheus"`

	// Interval holds metric collection interval.
	Interval time.Duration `json:"interval" yaml:"interval" mapstructure:"interval"`

	// OTLP holds the configuration for the OTEL protocol.
	OTLP otlp.Config `json:"otlp" yaml:"otlp" mapstructure:"OTLP"`
}

Config holds the configuration settings for the package metricw package.

type Metric

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

Metric represents a structured wrapper for OpenTelemetry metrics. It includes metric providers, exporters, and various metric types such as counters, gauges, and histograms.

func Configure

func Configure(
	ctx context.Context,
	config Config,
	attrs []attribute.KeyValue,
	writers ...io.Writer,
) (*Metric, error)

Configure initializes and configures the OpenTelemetry metric provider. It sets up the exporter, resource attributes, and meter provider. Optionally, it registers Prometheus collectors if enabled in the config.

func (*Metric) RegisterPrometheusCollectors

func (m *Metric) RegisterPrometheusCollectors(ctx context.Context, colls ...prometheus.Collector) error

Maps metrics provided by Prometheus collectors to otel metrics. The metrics are sent via otlp http/grpc to Otel Collector.

func (*Metric) Shutdown

func (m *Metric) Shutdown(ctx context.Context) error

Shutdown gracefully shuts down all metric-related components. It unregisters metric registrations, shuts down the provider, and ensures the exporter is properly closed.

Jump to

Keyboard shortcuts

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