Skip to content

Commit

Permalink
move name validation into the lookup method
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu committed Jun 7, 2023
1 parent 09c1104 commit ec8ccc5
Showing 1 changed file with 42 additions and 52 deletions.
94 changes: 42 additions & 52 deletions sdk/metric/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,7 @@ func (m *meter) Int64Counter(name string, options ...metric.Int64CounterOption)

cfg := metric.NewInt64CounterConfig(options...)
const kind = InstrumentKindCounter
i, err := m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
return i, err
}

return i, validateInstrumentName(name)
return m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
}

// Int64UpDownCounter returns a new instrument identified by name and
Expand All @@ -86,12 +81,7 @@ func (m *meter) Int64Counter(name string, options ...metric.Int64CounterOption)
func (m *meter) Int64UpDownCounter(name string, options ...metric.Int64UpDownCounterOption) (metric.Int64UpDownCounter, error) {
cfg := metric.NewInt64UpDownCounterConfig(options...)
const kind = InstrumentKindUpDownCounter
i, err := m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
return i, err
}

return i, validateInstrumentName(name)
return m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
}

// Int64Histogram returns a new instrument identified by name and configured
Expand All @@ -100,12 +90,7 @@ func (m *meter) Int64UpDownCounter(name string, options ...metric.Int64UpDownCou
func (m *meter) Int64Histogram(name string, options ...metric.Int64HistogramOption) (metric.Int64Histogram, error) {
cfg := metric.NewInt64HistogramConfig(options...)
const kind = InstrumentKindHistogram
i, err := m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
return i, err
}

return i, validateInstrumentName(name)
return m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
}

// Int64ObservableCounter returns a new instrument identified by name and
Expand All @@ -116,11 +101,11 @@ func (m *meter) Int64ObservableCounter(name string, options ...metric.Int64Obser
const kind = InstrumentKindObservableCounter
p := int64ObservProvider{m.int64IP}
inst, err := p.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
if err != nil && err != ErrInvalidInstrumentName {
return nil, err
}
p.registerCallbacks(inst, cfg.Callbacks())
return inst, validateInstrumentName(name)
return inst, err
}

// Int64ObservableUpDownCounter returns a new instrument identified by name and
Expand All @@ -131,11 +116,11 @@ func (m *meter) Int64ObservableUpDownCounter(name string, options ...metric.Int6
const kind = InstrumentKindObservableUpDownCounter
p := int64ObservProvider{m.int64IP}
inst, err := p.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
if err != nil && err != ErrInvalidInstrumentName {
return nil, err
}
p.registerCallbacks(inst, cfg.Callbacks())
return inst, validateInstrumentName(name)
return inst, err
}

// Int64ObservableGauge returns a new instrument identified by name and
Expand All @@ -146,11 +131,11 @@ func (m *meter) Int64ObservableGauge(name string, options ...metric.Int64Observa
const kind = InstrumentKindObservableGauge
p := int64ObservProvider{m.int64IP}
inst, err := p.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
if err != nil && err != ErrInvalidInstrumentName {
return nil, err
}
p.registerCallbacks(inst, cfg.Callbacks())
return inst, validateInstrumentName(name)
return inst, err
}

// Float64Counter returns a new instrument identified by name and configured
Expand All @@ -159,12 +144,7 @@ func (m *meter) Int64ObservableGauge(name string, options ...metric.Int64Observa
func (m *meter) Float64Counter(name string, options ...metric.Float64CounterOption) (metric.Float64Counter, error) {
cfg := metric.NewFloat64CounterConfig(options...)
const kind = InstrumentKindCounter
i, err := m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
return i, err
}

return i, validateInstrumentName(name)
return m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
}

// Float64UpDownCounter returns a new instrument identified by name and
Expand All @@ -173,12 +153,7 @@ func (m *meter) Float64Counter(name string, options ...metric.Float64CounterOpti
func (m *meter) Float64UpDownCounter(name string, options ...metric.Float64UpDownCounterOption) (metric.Float64UpDownCounter, error) {
cfg := metric.NewFloat64UpDownCounterConfig(options...)
const kind = InstrumentKindUpDownCounter
i, err := m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
return i, err
}

return i, validateInstrumentName(name)
return m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
}

// Float64Histogram returns a new instrument identified by name and configured
Expand All @@ -187,12 +162,7 @@ func (m *meter) Float64UpDownCounter(name string, options ...metric.Float64UpDow
func (m *meter) Float64Histogram(name string, options ...metric.Float64HistogramOption) (metric.Float64Histogram, error) {
cfg := metric.NewFloat64HistogramConfig(options...)
const kind = InstrumentKindHistogram
i, err := m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
return i, err
}

return i, validateInstrumentName(name)
return m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
}

// Float64ObservableCounter returns a new instrument identified by name and
Expand All @@ -203,11 +173,11 @@ func (m *meter) Float64ObservableCounter(name string, options ...metric.Float64O
const kind = InstrumentKindObservableCounter
p := float64ObservProvider{m.float64IP}
inst, err := p.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
if err != nil && err != ErrInvalidInstrumentName {
return nil, err
}
p.registerCallbacks(inst, cfg.Callbacks())
return inst, validateInstrumentName(name)
return inst, err
}

// Float64ObservableUpDownCounter returns a new instrument identified by name
Expand All @@ -218,11 +188,11 @@ func (m *meter) Float64ObservableUpDownCounter(name string, options ...metric.Fl
const kind = InstrumentKindObservableUpDownCounter
p := float64ObservProvider{m.float64IP}
inst, err := p.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
if err != nil && err != ErrInvalidInstrumentName {
return nil, err
}
p.registerCallbacks(inst, cfg.Callbacks())
return inst, validateInstrumentName(name)
return inst, err
}

// Float64ObservableGauge returns a new instrument identified by name and
Expand All @@ -233,11 +203,11 @@ func (m *meter) Float64ObservableGauge(name string, options ...metric.Float64Obs
const kind = InstrumentKindObservableGauge
p := float64ObservProvider{m.float64IP}
inst, err := p.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
if err != nil && err != ErrInvalidInstrumentName {
return nil, err
}
p.registerCallbacks(inst, cfg.Callbacks())
return inst, validateInstrumentName(name)
return inst, err
}

func validateInstrumentName(name string) error {
Expand Down Expand Up @@ -439,7 +409,12 @@ func (p *int64InstProvider) aggs(kind InstrumentKind, name, desc, u string) ([]i
// lookup returns the resolved instrumentImpl.
func (p *int64InstProvider) lookup(kind InstrumentKind, name, desc, u string) (*int64Inst, error) {
aggs, err := p.aggs(kind, name, desc, u)
return &int64Inst{aggregators: aggs}, err
inst := &int64Inst{aggregators: aggs}
if err != nil {
return inst, err
}

return inst, validateInstrumentName(name)
}

// float64InstProvider provides float64 OpenTelemetry instruments.
Expand Down Expand Up @@ -467,14 +442,24 @@ func (p *float64InstProvider) aggs(kind InstrumentKind, name, desc, u string) ([
// lookup returns the resolved instrumentImpl.
func (p *float64InstProvider) lookup(kind InstrumentKind, name, desc, u string) (*float64Inst, error) {
aggs, err := p.aggs(kind, name, desc, u)
return &float64Inst{aggregators: aggs}, err
inst := &float64Inst{aggregators: aggs}
if err != nil {
return inst, err
}

return inst, validateInstrumentName(name)
}

type int64ObservProvider struct{ *int64InstProvider }

func (p int64ObservProvider) lookup(kind InstrumentKind, name, desc, u string) (int64Observable, error) {
aggs, err := p.aggs(kind, name, desc, u)
return newInt64Observable(p.scope, kind, name, desc, u, aggs), err
inst := newInt64Observable(p.scope, kind, name, desc, u, aggs)
if err != nil {
return inst, err
}

return inst, validateInstrumentName(name)
}

func (p int64ObservProvider) registerCallbacks(inst int64Observable, cBacks []metric.Int64Callback) {
Expand Down Expand Up @@ -507,7 +492,12 @@ type float64ObservProvider struct{ *float64InstProvider }

func (p float64ObservProvider) lookup(kind InstrumentKind, name, desc, u string) (float64Observable, error) {
aggs, err := p.aggs(kind, name, desc, u)
return newFloat64Observable(p.scope, kind, name, desc, u, aggs), err
inst := newFloat64Observable(p.scope, kind, name, desc, u, aggs)
if err != nil {
return inst, err
}

return inst, validateInstrumentName(name)
}

func (p float64ObservProvider) registerCallbacks(inst float64Observable, cBacks []metric.Float64Callback) {
Expand Down

0 comments on commit ec8ccc5

Please sign in to comment.