Skip to content

Commit

Permalink
Merge branch 'main' into agg-builder
Browse files Browse the repository at this point in the history
  • Loading branch information
pellared authored Jul 14, 2023
2 parents 7d5bbae + 55fb2bb commit 7f72e72
Show file tree
Hide file tree
Showing 3 changed files with 31 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
25 changes: 25 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,30 @@ 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) {
got = args
}, funcr.Options{Verbosity: 6}))

_ = NewView(Instrument{
Name: "*", // Wildcard match name (multiple instruments).
}, Stream{
Name: "non-empty",
})
assert.Contains(t, got, errMultiInst.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

0 comments on commit 7f72e72

Please sign in to comment.