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 ¶
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 ¶
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 ¶
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.