Skip to content

Commit

Permalink
Export the Instrument IsEmpty method (#5431)
Browse files Browse the repository at this point in the history
From
open-telemetry/opentelemetry-go-contrib#5654 (comment)

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.
  • Loading branch information
MrAlias authored May 30, 2024
1 parent 8d4d3c8 commit c3569d8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions sdk/metric/instrument.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 &&
Expand Down
2 changes: 1 addition & 1 deletion sdk/metric/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit c3569d8

Please sign in to comment.