Skip to content

Commit

Permalink
Add options to measurement methods (open-telemetry#3971)
Browse files Browse the repository at this point in the history
* Add options to measurement methods

* Update noop

* Update global impl

* Update SDK impl

* Fix metric API example

* Update prometheus exporter tests

* Update examples

* WithAttributes and WithAttributeSet

* Add changes to changelog

* Accept slice instead of variadic to new conf funcs

* Clarify WithAttributes performance in docs

* Address feedback about WithAttributes comment

* Add changelog entry for WithAttribute{s,Set}

* Remove number scope from measure opts

* Update changelog

* Remove left-over test cases

---------

Co-authored-by: Robert Pająk <pellared@hotmail.com>
  • Loading branch information
2 people authored and Kaushal28 committed Apr 21, 2023
1 parent 98407b4 commit 8761749
Show file tree
Hide file tree
Showing 24 changed files with 742 additions and 364 deletions.
24 changes: 24 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,18 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The `go.opentelemetry.io/otel/metric/embedded` package. (#3916)
- The `Version` function to `go.opentelemetry.io/otel/sdk` to return the SDK version. (#3949)
- Add a `WithNamespace` option to `go.opentelemetry.io/otel/exporters/prometheus` to allow users to prefix metrics with a namespace. (#3970)
- The following configuration types were added to `go.opentelemetry.io/otel/metric/instrument` to be used in the configuration of measurement methods. (#3971)
- The `AddConfig` used to hold configuration for addition measurements
- `NewAddConfig` used to create a new `AddConfig`
- `AddOption` used to configure an `AddConfig`
- The `RecordConfig` used to hold configuration for recorded measurements
- `NewRecordConfig` used to create a new `RecordConfig`
- `RecordOption` used to configure a `RecordConfig`
- The `ObserveConfig` used to hold configuration for observed measurements
- `NewObserveConfig` used to create a new `ObserveConfig`
- `ObserveOption` used to configure an `ObserveConfig`
- `WithAttributeSet` and `WithAttributes` are added to `go.opentelemetry.io/otel/metric/instrument`.
They return an option used during a measurement that defines the attribute Set associated with the measurement. (#3971)
- The `Version` function to `go.opentelemetry.io/otel/exporters/otlp/otlpmetric` to return the OTLP metrics client version. (#3956)
- The `Version` function to `go.opentelemetry.io/otel/exporters/otlp/otlptrace` to return the OTLP trace client version. (#3956)

Expand All @@ -24,6 +36,18 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- Move No-Op implementation from `go.opentelemetry.io/otel/metric` into its own package `go.opentelemetry.io/otel/metric/noop`. (#3941)
- `metric.NewNoopMeterProvider` is replaced with `noop.NewMeterProvider`
- Wrap `UploadMetrics` error in `go.opentelemetry.io/otel/exporters/otlp/otlpmetric/` to improve error message when encountering generic grpc errors. (#3974)
- The measurement methods for all instruments in `go.opentelemetry.io/otel/metric/instrument` accept an option instead of the variadic `"go.opentelemetry.io/otel/attribute".KeyValue`. (#3971)
- The `Int64Counter.Add` method now accepts `...AddOption`
- The `Float64Counter.Add` method now accepts `...AddOption`
- The `Int64UpDownCounter.Add` method now accepts `...AddOption`
- The `Float64UpDownCounter.Add` method now accepts `...AddOption`
- The `Int64Histogram.Record` method now accepts `...RecordOption`
- The `Float64Histogram.Record` method now accepts `...RecordOption`
- The `Int64Observer.Observe` method now accepts `...ObserveOption`
- The `Float64Observer.Observe` method now accepts `...ObserveOption`
- The `Observer` methods in `go.opentelemetry.io/otel/metric` accept an option instead of the variadic `"go.opentelemetry.io/otel/attribute".KeyValue`. (#3971)
- The `Observer.ObserveInt64` method now accepts `...ObserveOption`
- The `Observer.ObserveFloat64` method now accepts `...ObserveOption`
- Move global metric back to `go.opentelemetry.io/otel/metric/global` from `go.opentelemetry.io/otel`. (#3986)

### Fixed
Expand Down
16 changes: 8 additions & 8 deletions example/prometheus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,25 +50,25 @@ func main() {
// Start the prometheus HTTP server and pass the exporter Collector to it
go serveMetrics()

attrs := []attribute.KeyValue{
opt := instrument.WithAttributes(
attribute.Key("A").String("B"),
attribute.Key("C").String("D"),
}
)

// This is the equivalent of prometheus.NewCounterVec
counter, err := meter.Float64Counter("foo", instrument.WithDescription("a simple counter"))
if err != nil {
log.Fatal(err)
}
counter.Add(ctx, 5, attrs...)
counter.Add(ctx, 5, opt)

gauge, err := meter.Float64ObservableGauge("bar", instrument.WithDescription("a fun little gauge"))
if err != nil {
log.Fatal(err)
}
_, err = meter.RegisterCallback(func(_ context.Context, o api.Observer) error {
n := -10. + rng.Float64()*(90.) // [-10, 100)
o.ObserveFloat64(gauge, n, attrs...)
o.ObserveFloat64(gauge, n, opt)
return nil
}, gauge)
if err != nil {
Expand All @@ -80,10 +80,10 @@ func main() {
if err != nil {
log.Fatal(err)
}
histogram.Record(ctx, 23, attrs...)
histogram.Record(ctx, 7, attrs...)
histogram.Record(ctx, 101, attrs...)
histogram.Record(ctx, 105, attrs...)
histogram.Record(ctx, 23, opt)
histogram.Record(ctx, 7, opt)
histogram.Record(ctx, 101, opt)
histogram.Record(ctx, 105, opt)

ctx, _ = signal.NotifyContext(ctx, os.Interrupt)
<-ctx.Done()
Expand Down
14 changes: 7 additions & 7 deletions example/view/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,25 @@ func main() {
// Start the prometheus HTTP server and pass the exporter Collector to it
go serveMetrics()

attrs := []attribute.KeyValue{
opt := instrument.WithAttributes(
attribute.Key("A").String("B"),
attribute.Key("C").String("D"),
}
)

counter, err := meter.Float64Counter("foo", instrument.WithDescription("a simple counter"))
if err != nil {
log.Fatal(err)
}
counter.Add(ctx, 5, attrs...)
counter.Add(ctx, 5, opt)

histogram, err := meter.Float64Histogram("custom_histogram", instrument.WithDescription("a histogram with custom buckets and rename"))
if err != nil {
log.Fatal(err)
}
histogram.Record(ctx, 136, attrs...)
histogram.Record(ctx, 64, attrs...)
histogram.Record(ctx, 701, attrs...)
histogram.Record(ctx, 830, attrs...)
histogram.Record(ctx, 136, opt)
histogram.Record(ctx, 64, opt)
histogram.Record(ctx, 701, opt)
histogram.Record(ctx, 830, opt)

ctx, _ = signal.NotifyContext(ctx, os.Interrupt)
<-ctx.Done()
Expand Down

0 comments on commit 8761749

Please sign in to comment.