Skip to content

Commit 0f5565a

Browse files
dashpolepellaredMadVikingGod
authored
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 <[email protected]> Co-authored-by: Aaron Clawson <[email protected]>
1 parent cdd9353 commit 0f5565a

7 files changed

+53
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1616
- Add the `go.opentelemetry.io/otel/trace/noop` package as a default no-op implementation of the trace API. (#4620)
1717
- Add context propagation in `go.opentelemetry.io/otel/example/dice`. (#4644)
1818
- Add view configuration to `go.opentelemetry.io/otel/example/prometheus`. (#4649)
19+
- Add `go.opentelemetry.io/otel/metric.WithExplicitBucketBoundaries`, which allows defining default explicit bucket boundaries when creating histogram instruments. (#4603)
1920
- Add `Version` function in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetricgrpc`. (#4660)
2021
- Add `Version` function in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/otlpmetrichttp`. (#4660)
2122
- Add Summary, SummaryDataPoint, and QuantileValue to `go.opentelemetry.io/sdk/metric/metricdata`. (#4622)

metric/example_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ func ExampleMeter_histogram() {
161161
"task.duration",
162162
metric.WithDescription("The duration of task execution."),
163163
metric.WithUnit("s"),
164+
metric.WithExplicitBucketBoundaries(.005, .01, .025, .05, .075, .1, .25, .5, .75, 1, 2.5, 5, 7.5, 10),
164165
)
165166
if err != nil {
166167
panic(err)

metric/instrument.go

+23
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ type InstrumentOption interface {
3939
Float64ObservableGaugeOption
4040
}
4141

42+
// HistogramOption applies options to histogram instruments.
43+
type HistogramOption interface {
44+
Int64HistogramOption
45+
Float64HistogramOption
46+
}
47+
4248
type descOpt string
4349

4450
func (o descOpt) applyFloat64Counter(c Float64CounterConfig) Float64CounterConfig {
@@ -171,6 +177,23 @@ func (o unitOpt) applyInt64ObservableGauge(c Int64ObservableGaugeConfig) Int64Ob
171177
// The unit u should be defined using the appropriate [UCUM](https://ucum.org) case-sensitive code.
172178
func WithUnit(u string) InstrumentOption { return unitOpt(u) }
173179

180+
// WithExplicitBucketBoundaries sets the instrument explicit bucket boundaries.
181+
//
182+
// This option is considered "advisory", and may be ignored by API implementations.
183+
func WithExplicitBucketBoundaries(bounds ...float64) HistogramOption { return bucketOpt(bounds) }
184+
185+
type bucketOpt []float64
186+
187+
func (o bucketOpt) applyFloat64Histogram(c Float64HistogramConfig) Float64HistogramConfig {
188+
c.explicitBucketBoundaries = o
189+
return c
190+
}
191+
192+
func (o bucketOpt) applyInt64Histogram(c Int64HistogramConfig) Int64HistogramConfig {
193+
c.explicitBucketBoundaries = o
194+
return c
195+
}
196+
174197
// AddOption applies options to an addition measurement. See
175198
// [MeasurementOption] for other options that can be used as an AddOption.
176199
type AddOption interface {

metric/syncfloat64.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ type Float64Histogram interface {
147147
// Float64HistogramConfig contains options for synchronous counter instruments
148148
// that record int64 values.
149149
type Float64HistogramConfig struct {
150-
description string
151-
unit string
150+
description string
151+
unit string
152+
explicitBucketBoundaries []float64
152153
}
153154

154155
// NewFloat64HistogramConfig returns a new [Float64HistogramConfig] with all
@@ -171,6 +172,11 @@ func (c Float64HistogramConfig) Unit() string {
171172
return c.unit
172173
}
173174

175+
// ExplicitBucketBoundaries returns the configured explicit bucket boundaries.
176+
func (c Float64HistogramConfig) ExplicitBucketBoundaries() []float64 {
177+
return c.explicitBucketBoundaries
178+
}
179+
174180
// Float64HistogramOption applies options to a [Float64HistogramConfig]. See
175181
// [InstrumentOption] for other options that can be used as a
176182
// Float64HistogramOption.

metric/syncfloat64_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ type float64Config interface {
5151
Description() string
5252
Unit() string
5353
}
54+
55+
func TestFloat64ExplicitBucketHistogramConfiguration(t *testing.T) {
56+
bounds := []float64{0.1, 0.5, 1.0}
57+
got := NewFloat64HistogramConfig(WithExplicitBucketBoundaries(bounds...))
58+
assert.Equal(t, bounds, got.ExplicitBucketBoundaries(), "boundaries")
59+
}

metric/syncint64.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,9 @@ type Int64Histogram interface {
147147
// Int64HistogramConfig contains options for synchronous counter instruments
148148
// that record int64 values.
149149
type Int64HistogramConfig struct {
150-
description string
151-
unit string
150+
description string
151+
unit string
152+
explicitBucketBoundaries []float64
152153
}
153154

154155
// NewInt64HistogramConfig returns a new [Int64HistogramConfig] with all opts
@@ -171,6 +172,11 @@ func (c Int64HistogramConfig) Unit() string {
171172
return c.unit
172173
}
173174

175+
// ExplicitBucketBoundaries returns the configured explicit bucket boundaries.
176+
func (c Int64HistogramConfig) ExplicitBucketBoundaries() []float64 {
177+
return c.explicitBucketBoundaries
178+
}
179+
174180
// Int64HistogramOption applies options to a [Int64HistogramConfig]. See
175181
// [InstrumentOption] for other options that can be used as an
176182
// Int64HistogramOption.

metric/syncint64_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@ type int64Config interface {
5151
Description() string
5252
Unit() string
5353
}
54+
55+
func TestInt64ExplicitBucketHistogramConfiguration(t *testing.T) {
56+
bounds := []float64{0.1, 0.5, 1.0}
57+
got := NewInt64HistogramConfig(WithExplicitBucketBoundaries(bounds...))
58+
assert.Equal(t, bounds, got.ExplicitBucketBoundaries(), "boundaries")
59+
}

0 commit comments

Comments
 (0)