Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WithExplicitBucketBoundaries Histogram option to the metric api #4603

Merged
merged 9 commits into from
Oct 26, 2023
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions metric/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
23 changes: 23 additions & 0 deletions metric/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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.
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
//
// 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
MadVikingGod marked this conversation as resolved.
Show resolved Hide resolved
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 {
Expand Down
10 changes: 8 additions & 2 deletions metric/syncfloat64.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand 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.
Expand Down
6 changes: 6 additions & 0 deletions metric/syncfloat64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
10 changes: 8 additions & 2 deletions metric/syncint64.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
pellared marked this conversation as resolved.
Show resolved Hide resolved
}

// NewInt64HistogramConfig returns a new [Int64HistogramConfig] with all opts
Expand All @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions metric/syncint64_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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