From 55fb2bb57b47133d0b966e4d55151fba47aae057 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 12 Jul 2023 15:10:40 -0700 Subject: [PATCH] Log an error for an empty view criteria (#4307) * Log an error for an empty view Resolves #4149 * Add changelog entry --- CHANGELOG.md | 1 + sdk/metric/view.go | 5 +++++ sdk/metric/view_test.go | 10 ++++++++++ 3 files changed, 16 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f69a0ddfd6..8759f726a18 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/sdk/metric/view.go b/sdk/metric/view.go index 9dab839f003..f1df24466bc 100644 --- a/sdk/metric/view.go +++ b/sdk/metric/view.go @@ -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 } ) @@ -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 } diff --git a/sdk/metric/view_test.go b/sdk/metric/view_test.go index 439e7819c2d..b8f6c921468 100644 --- a/sdk/metric/view_test.go +++ b/sdk/metric/view_test.go @@ -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) {