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

Log an error for an empty view criteria #4307

Merged
merged 3 commits into from
Jul 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
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
11 changes: 11 additions & 0 deletions sdk/metric/view_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

"github.com/go-logr/logr"
"github.com/go-logr/logr/funcr"
"github.com/go-logr/logr/testr"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -466,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 ExampleNewView() {
// Create a view that renames the "latency" instrument from the v0.34.0
// version of the "http" instrumentation library as "request.latency".
Expand Down