meter

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2026 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package meter provides a simple way to create a new Otel Meter provider. The package also provides an easy way to publish metrics to the Otel collector.

Index

Constants

This section is empty.

Variables

View Source
var (
	// LATENCY_EXPLICIT_BUCKET_BOUNDARIES_IN_MS is the default bucket boundaries for latency histograms (in milliseconds).
	// For measuring latency (e.g., HTTP request durations, database query times), you typically want finer
	// granularity at the lower end (where most values are expected) and coarser granularity at the higher
	// end (to capture outliers).
	LATENCY_EXPLICIT_BUCKET_BOUNDARIES_IN_MS = []float64{0, 5, 10, 25, 50, 75, 100, 250, 500, 750, 1000, 2500, 5000, 10000}
	// LATENCY_EXPLICIT_BUCKET_BOUNDARIES_IN_SECONDS is the default bucket boundaries for latency histograms (in seconds).
	// For measuring latency (e.g., HTTP request durations, database query times), you typically want finer
	// granularity at the lower end (where most values are expected) and coarser granularity at the higher
	// end (to capture outliers).
	//
	// This bucket is heavily influenced by the default buckets of Prometheus clients.
	// e.g.
	//  - Java: https://github.com/prometheus/client_java/blob/6730f3e32199d6bf0e963b306ff69ef08ac5b178/simpleclient/src/main/java/io/prometheus/client/Histogram.java#L88
	//  - Go: https://github.com/prometheus/client_golang/blob/83d56b1144a0c2eb10d399e7abbae3333bebc463/prometheus/histogram.go#L68
	LATENCY_EXPLICIT_BUCKET_BOUNDARIES_IN_SECONDS = []float64{.005, .01, .025, .05, .1, .25, .5, 1, 2.5, 5, 10}
	// REQUEST_RESPONSE_EXPLICIT_BUCKET_BOUNDARIES is the default bucket boundaries for request/response
	// size histograms (in bytes).
	// For measuring the size of requests or responses (e.g., in bytes), you typically want exponential
	// or logarithmic bucket boundaries
	// to cover a wide range of sizes.
	REQUEST_RESPONSE_EXPLICIT_BUCKET_BOUNDARIES = []float64{0, 1024, 2048, 5120, 10240, 20480, 51200, 102400, 204800, 512000, 1048576}
	// ERROR_RATE_EXPLICIT_BUCKET_BOUNDARIES is the default bucket boundaries for error rate histograms(in %).
	// For measuring error rates, you most likely want linear bucket boundaries.
	ERROR_RATE_EXPLICIT_BUCKET_BOUNDARIES = []float64{0, 0.01, 0.1, 1, 5, 10, 20, 50, 100}
	// COUNT_METRIC_EXPLICIT_BUCKET_BOUNDARIES is the default bucket boundaries for count metrics.
	// For measuring counts (e.g., number of requests, number of items in a queue), you most likely want
	// linear bucket boundaries.
	COUNT_METRIC_EXPLICIT_BUCKET_BOUNDARIES = []float64{0, 1, 2, 5, 10, 25, 50, 100, 250, 500, 1000}
	// GENERIC_EXPLICIT_BUCKET_BOUNDARIES is the default bucket boundaries for generic histograms.
	// For generic histograms, you may want to use a mix of linear and exponential bucket boundaries.
	GENERIC_EXPLICIT_BUCKET_BOUNDARIES = []float64{0, 1, 2, 5, 10, 20, 50, 100, 200, 500, 1000, 2000, 5000, 10000}
)
View Source
var (
	// ErrMeterNotInitialized is returned when a metric is created before the meter is initialized.
	ErrMeterNotInitialized = errors.New("meter not initialized")
	// ErrInvalidMetricName is returned when the metric name does not conform to the OpenTelemetry instrument name syntax.
	ErrInvalidMetricName = errors.New("invalid metric name. Must match the regex: " + nameRegex.String())
)

Errors

View Source
var ErrExporterProtocolNotSupported = errors.New("exporter protocol not supported")

ErrExporterProtocolNotSupported is returned when the exporter protocol is not supported

View Source
var ErrMetricsProviderNotInitialized = errors.New("metrics provider not initialized")

ErrMetricsProviderNotInitialized is returned when the metrics provider is not initialized

Functions

func FloatCounter

func FloatCounter(name string, input MetricInput) (metric.Float64Counter, error)

FloatCounter returns a new go.opentelemetry.io/otel/metric.Float64Counter instrument identified by name and configured with options. The instrument is used to synchronously record increasing float64 measurements during a computational operation.

The name needs to conform to the OpenTelemetry instrument name syntax. See the Instrument Name section of the package documentation for more information (https://opentelemetry.io/docs/specs/semconv/general/metrics/).

The description is optional, but recommended. It is used to describe the instrument in human-readable terms. In the description, the unit will be appended in parentheses.

The unit is optional, but recommended. It is used to describe the unit of the measurements. The default unit is "1" (Ratio). However, for better documentation, the unit should be set to a more meaningful value.

func FloatGauge

func FloatGauge(name string, input MetricInput) (metric.Float64Gauge, error)

FloatGauge returns a new go.opentelemetry.io/otel/metric.Float64Gauge instrument identified by name and configured with options. The instrument is used to synchronously record instantaneous float64 measurements during a computational operation.

The name needs to conform to the OpenTelemetry instrument name syntax. See the Instrument Name section of the package documentation for more information (https://opentelemetry.io/docs/specs/semconv/general/metrics/).

The description is optional, but recommended. It is used to describe the instrument in human-readable terms.

The unit is optional, but recommended. It is used to describe the unit of the measurements. The default unit is "By" (Bytes). However, for better documentation, the unit should be set to a more meaningful value.

func FloatHistogram

func FloatHistogram(name string, input HistogramMetricInput) (metric.Float64Histogram, error)

FloatHistogram returns a new go.opentelemetry.io/otel/metric.Float64Histogram instrument identified by name and configured with options. The instrument is used to synchronously record the distribution of float64 measurements during a computational operation.

The name needs to conform to the OpenTelemetry instrument name syntax. See the Instrument Name section of the package documentation for more information (https://opentelemetry.io/docs/specs/semconv/general/metrics/).

The description is optional, but recommended. It is used to describe the instrument in human-readable terms.

The unit is optional, but recommended. It is used to describe the unit of the measurements. The default unit is "ms" (Milliseconds). However, for better documentation, the unit should be set to a more meaningful value.

The explicitBucketBoundaries are the bucket boundaries for the histogram. Make sure to set the bucket boundaries according to the metric you are measuring. You can use the default bucket boundaries provided in this package or set your own.

func FloatUpDownCounter

func FloatUpDownCounter(name string, input MetricInput) (metric.Float64UpDownCounter, error)

FloatUpDownCounter returns a new go.opentelemetry.io/otel/metric.Float64UpDownCounter instrument identified by name and configured with options. The instrument is used to synchronously record float64 measurements during a computational operation.

The name needs to conform to the OpenTelemetry instrument name syntax. See the Instrument Name section of the package documentation for more information (https://opentelemetry.io/docs/specs/semconv/general/metrics/).

The description is optional, but recommended. It is used to describe the instrument in human-readable terms. In the description, the unit will be appended in parentheses.

The unit is optional, but recommended. It is used to describe the unit of the measurements. The default unit is "1" (Ratio). However, for better documentation, the unit should be set to a more meaningful value.

func IntCounter

func IntCounter(name string, input MetricInput) (metric.Int64Counter, error)

IntCounter returns a new go.opentelemetry.io/otel/metric.Int64Counter instrument identified by name and configured with options. The instrument is used to synchronously record increasing int64 measurements during a computational operation.

The name needs to conform to the OpenTelemetry instrument name syntax. See the Instrument Name section of the package documentation for more information (https://opentelemetry.io/docs/specs/semconv/general/metrics/).

The description is optional, but recommended. It is used to describe the instrument in human-readable terms.

The unit is optional, but recommended. It is used to describe the unit of the measurements. The default unit is "1" (Ratio). However, for better documentation, the unit should be set to a more meaningful value.

func IntGauge

func IntGauge(name string, input MetricInput) (metric.Int64Gauge, error)

IntGauge returns a new go.opentelemetry.io/otel/metric.Int64Gauge instrument identified by name and configured with options. The instrument is used to synchronously record instantaneous int64 measurements during a computational operation.

The name needs to conform to the OpenTelemetry instrument name syntax. See the Instrument Name section of the package documentation for more information (https://opentelemetry.io/docs/specs/semconv/general/metrics/).

The description is optional, but recommended. It is used to describe the instrument in human-readable terms.

The unit is optional, but recommended. It is used to describe the unit of the measurements. The default unit is "By" (Bytes). However, for better documentation, the unit should be set to a more meaningful value.

func IntHistogram

func IntHistogram(name string, input HistogramMetricInput) (metric.Int64Histogram, error)

IntHistogram returns a new go.opentelemetry.io/otel/metric.Int64Histogram instrument identified by name and configured with options. The instrument is used to synchronously record the distribution of int64 measurements during a computational operation.

The name needs to conform to the OpenTelemetry instrument name syntax. See the Instrument Name section of the package documentation for more information (https://opentelemetry.io/docs/specs/semconv/general/metrics/).

The description is optional, but recommended. It is used to describe the instrument in human-readable terms.

The unit is optional, but recommended. It is used to describe the unit of the measurements. The default unit is "ms" (Milliseconds).

The explicitBucketBoundaries are the bucket boundaries for the histogram. Make sure to set the bucket boundaries according to the metric you are measuring. You can use the default bucket boundaries provided in this package or set your own.

func IntUpDownCounter

func IntUpDownCounter(name string, input MetricInput) (metric.Int64UpDownCounter, error)

IntUpDownCounter returns a new go.opentelemetry.io/otel/metric.Int64UpDownCounter instrument identified by name and configured with options. The instrument is used to synchronously record int64 measurements during a computational operation.

The name needs to conform to the OpenTelemetry instrument name syntax. See the Instrument Name section of the package documentation for more information (https://opentelemetry.io/docs/specs/semconv/general/metrics/).

The description is optional, but recommended. It is used to describe the instrument in human-readable terms. In the description, the unit will be appended in parentheses.

The unit is optional, but recommended. It is used to describe the unit of the measurements. The default unit is "1" (Ratio). However, for better documentation, the unit should be set to a more meaningful value.

func ShutdownMeterProvider

func ShutdownMeterProvider(ctx context.Context) error

ShutdownMeterProvider gracefully shuts down the global MeterProvider.

func StartDefaultMeter

func StartDefaultMeter(ctx context.Context) (metric.Meter, error)

StartDefaultMeter initializes and starts the default OpenTelemetry Meter.

This function checks if custom metrics are enabled in the provided configuration. If they are enabled, it creates a new Meter by using the default MeterProvider. It also validates that the meter name (the service name in that case) is set in the configuration. If the meter name is not provided, a noop Meter is initialised as a default.

The function also sets the global default Meter to be used for custom metrics in the application. If custom metrics are not enabled, it returns a noop Meter.

It returns the created Meter.

For learning more about the Otel Metrics Data Model, please reference to https://opentelemetry.io/docs/specs/otel/metrics/data-model

Types

type HistogramMetricInput

type HistogramMetricInput struct {
	MetricInput

	// ExplicitBucketBoundaries are the bucket boundaries for the histogram.
	ExplicitBucketBoundaries []float64
}

MetricInput is the input metadata for creating a histogram metric.

type MetricInput

type MetricInput struct {
	// Description is the human-readable description of the metric.
	Description string
	// Unit is the unit of the metric.
	Unit units.Unit
}

MetricInput is the input metadata for creating a metric.

Directories

Path Synopsis
Package units provides a set of predefined units that can be used in the custom metrics.
Package units provides a set of predefined units that can be used in the custom metrics.

Jump to

Keyboard shortcuts

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