mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-01-14 02:33:21 +02:00
Add WithExplicitBucketBoundaries Histogram option to the metric api (#4603)
* Add WithExplicitBucketBoundaries Histogram option to the metric api * Add note that the option is advisory --------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com>
This commit is contained in:
parent
cdd9353641
commit
0f5565af4f
@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
||||
- Add the `go.opentelemetry.io/otel/trace/noop` package as a default no-op implementation of the trace API. (#4620)
|
||||
- Add context propagation in `go.opentelemetry.io/otel/example/dice`. (#4644)
|
||||
- Add view configuration to `go.opentelemetry.io/otel/example/prometheus`. (#4649)
|
||||
- Add `go.opentelemetry.io/otel/metric.WithExplicitBucketBoundaries`, which allows defining default explicit bucket boundaries when creating histogram instruments. (#4603)
|
||||
- Add `Version` function in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc`. (#4660)
|
||||
- Add `Version` function in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`. (#4660)
|
||||
- Add Summary, SummaryDataPoint, and QuantileValue to `go.opentelemetry.io/sdk/metric/metricdata`. (#4622)
|
||||
|
@ -161,6 +161,7 @@ func ExampleMeter_histogram() {
|
||||
"task.duration",
|
||||
metric.WithDescription("The duration of task execution."),
|
||||
metric.WithUnit("s"),
|
||||
metric.WithExplicitBucketBoundaries(.005, .01, .025, .05, .075, .1, .25, .5, .75, 1, 2.5, 5, 7.5, 10),
|
||||
)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
|
@ -39,6 +39,12 @@ type InstrumentOption interface {
|
||||
Float64ObservableGaugeOption
|
||||
}
|
||||
|
||||
// HistogramOption applies options to histogram instruments.
|
||||
type HistogramOption interface {
|
||||
Int64HistogramOption
|
||||
Float64HistogramOption
|
||||
}
|
||||
|
||||
type descOpt string
|
||||
|
||||
func (o descOpt) applyFloat64Counter(c Float64CounterConfig) Float64CounterConfig {
|
||||
@ -171,6 +177,23 @@ func (o unitOpt) applyInt64ObservableGauge(c Int64ObservableGaugeConfig) Int64Ob
|
||||
// The unit u should be defined using the appropriate [UCUM](https://ucum.org) case-sensitive code.
|
||||
func WithUnit(u string) InstrumentOption { return unitOpt(u) }
|
||||
|
||||
// WithExplicitBucketBoundaries sets the instrument explicit bucket boundaries.
|
||||
//
|
||||
// This option is considered "advisory", and may be ignored by API implementations.
|
||||
func WithExplicitBucketBoundaries(bounds ...float64) HistogramOption { return bucketOpt(bounds) }
|
||||
|
||||
type bucketOpt []float64
|
||||
|
||||
func (o bucketOpt) applyFloat64Histogram(c Float64HistogramConfig) Float64HistogramConfig {
|
||||
c.explicitBucketBoundaries = o
|
||||
return c
|
||||
}
|
||||
|
||||
func (o bucketOpt) applyInt64Histogram(c Int64HistogramConfig) Int64HistogramConfig {
|
||||
c.explicitBucketBoundaries = o
|
||||
return c
|
||||
}
|
||||
|
||||
// AddOption applies options to an addition measurement. See
|
||||
// [MeasurementOption] for other options that can be used as an AddOption.
|
||||
type AddOption interface {
|
||||
|
@ -147,8 +147,9 @@ type Float64Histogram interface {
|
||||
// Float64HistogramConfig contains options for synchronous counter instruments
|
||||
// that record int64 values.
|
||||
type Float64HistogramConfig struct {
|
||||
description string
|
||||
unit string
|
||||
description string
|
||||
unit string
|
||||
explicitBucketBoundaries []float64
|
||||
}
|
||||
|
||||
// NewFloat64HistogramConfig returns a new [Float64HistogramConfig] with all
|
||||
@ -171,6 +172,11 @@ func (c Float64HistogramConfig) Unit() string {
|
||||
return c.unit
|
||||
}
|
||||
|
||||
// ExplicitBucketBoundaries returns the configured explicit bucket boundaries.
|
||||
func (c Float64HistogramConfig) ExplicitBucketBoundaries() []float64 {
|
||||
return c.explicitBucketBoundaries
|
||||
}
|
||||
|
||||
// Float64HistogramOption applies options to a [Float64HistogramConfig]. See
|
||||
// [InstrumentOption] for other options that can be used as a
|
||||
// Float64HistogramOption.
|
||||
|
@ -51,3 +51,9 @@ type float64Config interface {
|
||||
Description() string
|
||||
Unit() string
|
||||
}
|
||||
|
||||
func TestFloat64ExplicitBucketHistogramConfiguration(t *testing.T) {
|
||||
bounds := []float64{0.1, 0.5, 1.0}
|
||||
got := NewFloat64HistogramConfig(WithExplicitBucketBoundaries(bounds...))
|
||||
assert.Equal(t, bounds, got.ExplicitBucketBoundaries(), "boundaries")
|
||||
}
|
||||
|
@ -147,8 +147,9 @@ type Int64Histogram interface {
|
||||
// Int64HistogramConfig contains options for synchronous counter instruments
|
||||
// that record int64 values.
|
||||
type Int64HistogramConfig struct {
|
||||
description string
|
||||
unit string
|
||||
description string
|
||||
unit string
|
||||
explicitBucketBoundaries []float64
|
||||
}
|
||||
|
||||
// NewInt64HistogramConfig returns a new [Int64HistogramConfig] with all opts
|
||||
@ -171,6 +172,11 @@ func (c Int64HistogramConfig) Unit() string {
|
||||
return c.unit
|
||||
}
|
||||
|
||||
// ExplicitBucketBoundaries returns the configured explicit bucket boundaries.
|
||||
func (c Int64HistogramConfig) ExplicitBucketBoundaries() []float64 {
|
||||
return c.explicitBucketBoundaries
|
||||
}
|
||||
|
||||
// Int64HistogramOption applies options to a [Int64HistogramConfig]. See
|
||||
// [InstrumentOption] for other options that can be used as an
|
||||
// Int64HistogramOption.
|
||||
|
@ -51,3 +51,9 @@ type int64Config interface {
|
||||
Description() string
|
||||
Unit() string
|
||||
}
|
||||
|
||||
func TestInt64ExplicitBucketHistogramConfiguration(t *testing.T) {
|
||||
bounds := []float64{0.1, 0.5, 1.0}
|
||||
got := NewInt64HistogramConfig(WithExplicitBucketBoundaries(bounds...))
|
||||
assert.Equal(t, bounds, got.ExplicitBucketBoundaries(), "boundaries")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user