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

adding NewNoopMeterProvider to follow trace api #2237

Merged
Show file tree
Hide file tree
Changes from 3 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ 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)
Expand Down
6 changes: 3 additions & 3 deletions metric/global/metric_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
13 changes: 10 additions & 3 deletions metric/metric_noop.go → metric/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
}

Expand Down
34 changes: 34 additions & 0 deletions metric/noop_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}