From fc3f099eb974c6d0c0ccaa199d3e3971cfd898a4 Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Fri, 10 Sep 2021 09:02:04 -0700 Subject: [PATCH 1/3] adding NewNoopMeterProvider to follow trace api --- CHANGELOG.md | 5 ++++- metric/global/metric_test.go | 6 +++--- metric/{metric_noop.go => noop.go} | 13 +++++++++--- metric/noop_test.go | 34 ++++++++++++++++++++++++++++++ 4 files changed, 51 insertions(+), 7 deletions(-) rename metric/{metric_noop.go => noop.go} (77%) create mode 100644 metric/noop_test.go diff --git a/CHANGELOG.md b/CHANGELOG.md index b5a9e446313..e2073431bb6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +### Changed +- NoopMeterProvider is now private and NewNoopMeterProvider must be used to obtain a noopMeterProvider. (#2237) + ### Fixed - Slice-valued attributes can correctly be used as map keys. (#2223) @@ -710,7 +713,7 @@ with major version 0. - OTLP Metric exporter supports LastValue aggregation. (#1165) - Move the `go.opentelemetry.io/otel/api/unit` package to `go.opentelemetry.io/otel/unit`. (#1185) - Rename `Provider` to `MeterProvider` in the `go.opentelemetry.io/otel/api/metric` package. (#1190) -- Rename `NoopProvider` to `NoopMeterProvider` in the `go.opentelemetry.io/otel/api/metric` package. (#1190) +- Rename `NoopProvider` to `noopMeterProvider` in the `go.opentelemetry.io/otel/api/metric` package. (#1190) - Rename `NewProvider` to `NewMeterProvider` in the `go.opentelemetry.io/otel/api/metric/metrictest` package. (#1190) - Rename `Provider` to `MeterProvider` in the `go.opentelemetry.io/otel/api/metric/registry` package. (#1190) - Rename `NewProvider` to `NewMeterProvider` in the `go.opentelemetry.io/otel/api/metri/registryc` package. (#1190) diff --git a/metric/global/metric_test.go b/metric/global/metric_test.go index 8f6000245ad..dc4d9370a92 100644 --- a/metric/global/metric_test.go +++ b/metric/global/metric_test.go @@ -30,12 +30,12 @@ func (*testMeterProvider) Meter(_ string, _ ...metric.MeterOption) metric.Meter func TestMultipleGlobalMeterProvider(t *testing.T) { p1 := testMeterProvider{} - p2 := metric.NoopMeterProvider{} + p2 := metric.NewNoopMeterProvider() SetMeterProvider(&p1) - SetMeterProvider(&p2) + SetMeterProvider(p2) got := GetMeterProvider() - want := &p2 + want := p2 if got != want { t.Fatalf("MeterProvider: got %p, want %p\n", got, want) } diff --git a/metric/metric_noop.go b/metric/noop.go similarity index 77% rename from metric/metric_noop.go rename to metric/noop.go index 30e57b6945b..5b51af759f3 100644 --- a/metric/metric_noop.go +++ b/metric/noop.go @@ -21,19 +21,26 @@ import ( "go.opentelemetry.io/otel/metric/number" ) -type NoopMeterProvider struct{} +// NewNoopMeterProvider returns an implementation of MeterProvider that +// performs no operations. The Meter and Instrument created from the returned +// MeterProvider also perform no operations. +func NewNoopMeterProvider() MeterProvider { + return noopMeterProvider{} +} + +type noopMeterProvider struct{} type noopInstrument struct{} type noopBoundInstrument struct{} type NoopSync struct{ noopInstrument } type NoopAsync struct{ noopInstrument } -var _ MeterProvider = NoopMeterProvider{} +var _ MeterProvider = noopMeterProvider{} var _ SyncImpl = NoopSync{} var _ BoundSyncImpl = noopBoundInstrument{} var _ AsyncImpl = NoopAsync{} -func (NoopMeterProvider) Meter(_ string, _ ...MeterOption) Meter { +func (noopMeterProvider) Meter(instrumentationName string, opts ...MeterOption) Meter { return Meter{} } diff --git a/metric/noop_test.go b/metric/noop_test.go new file mode 100644 index 00000000000..e5a7528a217 --- /dev/null +++ b/metric/noop_test.go @@ -0,0 +1,34 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package metric + +import ( + "testing" +) + +func TestNewNoopMeterProvider(t *testing.T) { + got, want := NewNoopMeterProvider(), noopMeterProvider{} + if got != want { + t.Errorf("NewNoopMeterProvider() returned %#v, want %#v", got, want) + } +} + +func TestNoopMeterProviderMeter(t *testing.T) { + mp := NewNoopMeterProvider() + got, want := mp.Meter(""), Meter{} + if got != want { + t.Errorf("noopMeterProvider.Meter() returned %#v, want %#v", got, want) + } +} From 3cf15992eb3002b09146396d5f3f5dd6a87a2c0e Mon Sep 17 00:00:00 2001 From: alrex Date: Fri, 10 Sep 2021 09:21:44 -0700 Subject: [PATCH 2/3] Update CHANGELOG.md Co-authored-by: Anthony Mirabella --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e2073431bb6..e5bf0977ea7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -713,7 +713,7 @@ with major version 0. - OTLP Metric exporter supports LastValue aggregation. (#1165) - Move the `go.opentelemetry.io/otel/api/unit` package to `go.opentelemetry.io/otel/unit`. (#1185) - Rename `Provider` to `MeterProvider` in the `go.opentelemetry.io/otel/api/metric` package. (#1190) -- Rename `NoopProvider` to `noopMeterProvider` in the `go.opentelemetry.io/otel/api/metric` package. (#1190) +- Rename `NoopProvider` to `NoopMeterProvider` in the `go.opentelemetry.io/otel/api/metric` package. (#1190) - Rename `NewProvider` to `NewMeterProvider` in the `go.opentelemetry.io/otel/api/metric/metrictest` package. (#1190) - Rename `Provider` to `MeterProvider` in the `go.opentelemetry.io/otel/api/metric/registry` package. (#1190) - Rename `NewProvider` to `NewMeterProvider` in the `go.opentelemetry.io/otel/api/metri/registryc` package. (#1190) From 9226d0e1dc44821dc47c804f83031c3a381a05ef Mon Sep 17 00:00:00 2001 From: Alex Boten Date: Fri, 10 Sep 2021 09:32:08 -0700 Subject: [PATCH 3/3] fix lint --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e5bf0977ea7..d3107d56617 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] ### Changed + - NoopMeterProvider is now private and NewNoopMeterProvider must be used to obtain a noopMeterProvider. (#2237) ### Fixed