From c3569d85d989c641a019f7727759d7d7df28e4a3 Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Thu, 30 May 2024 07:50:34 -0700 Subject: [PATCH] Export the Instrument IsEmpty method (#5431) From https://github.com/open-telemetry/opentelemetry-go-contrib/pull/5654#discussion_r1617971674 Constructing a view requires an `Instrument` to be constructed. Given `NewView` will not return an error directly when an empty instrument is passed, it may be ideal for users to check this prior to making the call. Instead of having all use-cases copy this code, export it so they can just call the method. --- CHANGELOG.md | 5 +++++ sdk/metric/instrument.go | 4 ++-- sdk/metric/view.go | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f40860d88..25a1b4811 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,11 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased] +### Added + +- The `IsEmpty` method is added to the `Instrument` type in `go.opentelemetry.io/otel/sdk/metric`. + This method is used to check if an `Instrument` instance is a zero-value. (#5431) + ### Fixed - Log a warning to the OpenTelemetry internal logger when a `Record` in `go.opentelemetry.io/otel/sdk/log` drops an attribute due to a limit being reached. (#5376) diff --git a/sdk/metric/instrument.go b/sdk/metric/instrument.go index f9768fd11..bbf560867 100644 --- a/sdk/metric/instrument.go +++ b/sdk/metric/instrument.go @@ -77,8 +77,8 @@ type Instrument struct { nonComparable // nolint: unused } -// empty returns if all fields of i are their zero-value. -func (i Instrument) empty() bool { +// IsEmpty returns if all Instrument fields are their zero-value. +func (i Instrument) IsEmpty() bool { return i.Name == "" && i.Description == "" && i.Kind == zeroInstrumentKind && diff --git a/sdk/metric/view.go b/sdk/metric/view.go index 11e334319..cd08c6732 100644 --- a/sdk/metric/view.go +++ b/sdk/metric/view.go @@ -43,7 +43,7 @@ type View func(Instrument) (Stream, bool) // of the default. If you need to zero out an Stream field returned from a // View, create a View directly. func NewView(criteria Instrument, mask Stream) View { - if criteria.empty() { + if criteria.IsEmpty() { global.Error( errEmptyView, "dropping view", "mask", mask,