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

[Proposal] Add WithAttribute methods for metric measurement operations with attributes #3955

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
- The `Extrema` in `go.opentelemetry.io/otel/sdk/metric/metricdata` is redefined with a generic argument of `[N int64 | float64]`. (#3870)
- 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`
- The measurement methods for all instruments in `go.opentelemetry.io/otel/metric/instrument` accept a `"go.opentelemetry.io/otel/attribute".Set` instead of the variadic `"go.opentelemetry.io/otel/attribute".KeyValue`. (#3947)
- The `Observer` methods in `go.opentelemetry.io/otel/metric` accept a `"go.opentelemetry.io/otel/attribute".Set` instead of the variadic `"go.opentelemetry.io/otel/attribute".KeyValue`. (#3947)

### 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{
attrs := attribute.NewSet(
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.AddWithAttributes(ctx, 5, attrs)

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.ObserveFloat64WithAttributes(gauge, n, attrs)
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.RecordWithAttributes(ctx, 23, attrs)
histogram.RecordWithAttributes(ctx, 7, attrs)
histogram.RecordWithAttributes(ctx, 101, attrs)
histogram.RecordWithAttributes(ctx, 105, attrs)

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{
attrs := attribute.NewSet(
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.AddWithAttributes(ctx, 5, attrs)

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.RecordWithAttributes(ctx, 136, attrs)
histogram.RecordWithAttributes(ctx, 64, attrs)
histogram.RecordWithAttributes(ctx, 701, attrs)
histogram.RecordWithAttributes(ctx, 830, attrs)

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