Skip to content

Commit

Permalink
adding NewNoopMeterProvider to follow trace api
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Boten committed Sep 10, 2021
1 parent 360d130 commit fc3f099
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 7 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
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)
}
}

0 comments on commit fc3f099

Please sign in to comment.