Skip to content

Commit

Permalink
Log an error for an empty view criteria (#4307)
Browse files Browse the repository at this point in the history
* Log an error for an empty view

Resolves #4149

* Add changelog entry
  • Loading branch information
MrAlias authored Jul 12, 2023
1 parent fcc6709 commit 55fb2bb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
### Fixed

- Correctly format log messages from the `go.opentelemetry.io/otel/exporters/zipkin` exporter. (#4143)
- Log an error for calls to `NewView` in `go.opentelemetry.io/otel/sdk/metric` that have empty criteria. (#4307)

## [1.16.0/0.39.0] 2023-05-18

Expand Down
5 changes: 5 additions & 0 deletions sdk/metric/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

var (
errMultiInst = errors.New("name replacement for multiple instruments")
errEmptyView = errors.New("no criteria provided for view")

emptyView = func(Instrument) (Stream, bool) { return Stream{}, false }
)
Expand Down Expand Up @@ -55,6 +56,10 @@ type View func(Instrument) (Stream, bool)
// View, create a View directly.
func NewView(criteria Instrument, mask Stream) View {
if criteria.empty() {
global.Error(
errEmptyView, "dropping view",
"mask", mask,
)
return emptyView
}

Expand Down
10 changes: 10 additions & 0 deletions sdk/metric/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -467,6 +467,16 @@ func TestNewViewAggregationErrorLogged(t *testing.T) {
assert.Equal(t, 1, l.ErrorN())
}

func TestNewViewEmptyViewErrorLogged(t *testing.T) {
var got string
otel.SetLogger(funcr.New(func(_, args string) {
got = args
}, funcr.Options{Verbosity: 6}))

_ = NewView(Instrument{}, Stream{})
assert.Contains(t, got, errEmptyView.Error())
}

func TestNewViewMultiInstMatchErrorLogged(t *testing.T) {
var got string
otel.SetLogger(funcr.New(func(_, args string) {
Expand Down

0 comments on commit 55fb2bb

Please sign in to comment.