Skip to content

Commit fe7058d

Browse files
alrexAneurysm9MrAlias
authored
adding NewNoopMeterProvider to follow trace api (#2237)
* adding NewNoopMeterProvider to follow trace api * Update CHANGELOG.md Co-authored-by: Anthony Mirabella <[email protected]> * fix lint Co-authored-by: Anthony Mirabella <[email protected]> Co-authored-by: Tyler Yahn <[email protected]>
1 parent c338a5e commit fe7058d

4 files changed

Lines changed: 51 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
88

99
## [Unreleased]
1010

11+
### Changed
12+
13+
- NoopMeterProvider is now private and NewNoopMeterProvider must be used to obtain a noopMeterProvider. (#2237)
14+
1115
### Fixed
1216

1317
- Slice-valued attributes can correctly be used as map keys. (#2223)

metric/global/metric_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,12 @@ func (*testMeterProvider) Meter(_ string, _ ...metric.MeterOption) metric.Meter
3030

3131
func TestMultipleGlobalMeterProvider(t *testing.T) {
3232
p1 := testMeterProvider{}
33-
p2 := metric.NoopMeterProvider{}
33+
p2 := metric.NewNoopMeterProvider()
3434
SetMeterProvider(&p1)
35-
SetMeterProvider(&p2)
35+
SetMeterProvider(p2)
3636

3737
got := GetMeterProvider()
38-
want := &p2
38+
want := p2
3939
if got != want {
4040
t.Fatalf("MeterProvider: got %p, want %p\n", got, want)
4141
}
Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,26 @@ import (
2121
"go.opentelemetry.io/otel/metric/number"
2222
)
2323

24-
type NoopMeterProvider struct{}
24+
// NewNoopMeterProvider returns an implementation of MeterProvider that
25+
// performs no operations. The Meter and Instrument created from the returned
26+
// MeterProvider also perform no operations.
27+
func NewNoopMeterProvider() MeterProvider {
28+
return noopMeterProvider{}
29+
}
30+
31+
type noopMeterProvider struct{}
2532

2633
type noopInstrument struct{}
2734
type noopBoundInstrument struct{}
2835
type NoopSync struct{ noopInstrument }
2936
type NoopAsync struct{ noopInstrument }
3037

31-
var _ MeterProvider = NoopMeterProvider{}
38+
var _ MeterProvider = noopMeterProvider{}
3239
var _ SyncImpl = NoopSync{}
3340
var _ BoundSyncImpl = noopBoundInstrument{}
3441
var _ AsyncImpl = NoopAsync{}
3542

36-
func (NoopMeterProvider) Meter(_ string, _ ...MeterOption) Meter {
43+
func (noopMeterProvider) Meter(instrumentationName string, opts ...MeterOption) Meter {
3744
return Meter{}
3845
}
3946

metric/noop_test.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright The OpenTelemetry Authors
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package metric
16+
17+
import (
18+
"testing"
19+
)
20+
21+
func TestNewNoopMeterProvider(t *testing.T) {
22+
got, want := NewNoopMeterProvider(), noopMeterProvider{}
23+
if got != want {
24+
t.Errorf("NewNoopMeterProvider() returned %#v, want %#v", got, want)
25+
}
26+
}
27+
28+
func TestNoopMeterProviderMeter(t *testing.T) {
29+
mp := NewNoopMeterProvider()
30+
got, want := mp.Meter(""), Meter{}
31+
if got != want {
32+
t.Errorf("noopMeterProvider.Meter() returned %#v, want %#v", got, want)
33+
}
34+
}

0 commit comments

Comments
 (0)