Skip to content

Commit

Permalink
Add WithExplicitBucketBoundaries Histogram option to the metric api
Browse files Browse the repository at this point in the history
  • Loading branch information
dashpole committed Oct 10, 2023
1 parent 3f17bcb commit 3972b9f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Added

- Add `go.opentelemetry.io/otel/bridge/opencensus.InstallTraceBridge`, which installs the OpenCensus trace bridge, and replaces `opencensus.NewTracer`. (#4567)
- Add `go.opentelemetry.io/otel/metric.WithExplicitBucketBoundaries`, which allows defining default explicit bucket boundaries when creating histogram instruments. (#4603)

### Deprecated

Expand Down
21 changes: 21 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,21 @@ 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.
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 {
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
}

// 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")
}

0 comments on commit 3972b9f

Please sign in to comment.