Skip to content

Commit

Permalink
validate instrument names when creating them
Browse files Browse the repository at this point in the history
  • Loading branch information
dmathieu committed Jun 7, 2023
1 parent ce46eb5 commit 214228d
Show file tree
Hide file tree
Showing 2 changed files with 337 additions and 0 deletions.
59 changes: 59 additions & 0 deletions sdk/metric/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"errors"
"fmt"
"regexp"

"go.opentelemetry.io/otel/internal/global"
"go.opentelemetry.io/otel/metric"
Expand All @@ -26,6 +27,8 @@ import (
"go.opentelemetry.io/otel/sdk/metric/internal"
)

var instrumentNameRe = regexp.MustCompile(`^([A-Za-z]){1}([A-Za-z0-9\_\-\.]){0,62}$`)

// meter handles the creation and coordination of all metric instruments. A
// meter represents a single instrumentation scope; all metric telemetry
// produced by an instrumentation scope will use metric instruments from a
Expand Down Expand Up @@ -60,6 +63,10 @@ var _ metric.Meter = (*meter)(nil)
// options. The instrument is used to synchronously record increasing int64
// measurements during a computational operation.
func (m *meter) Int64Counter(name string, options ...metric.Int64CounterOption) (metric.Int64Counter, error) {
if err := validateInstrumentName(name); err != nil {
return nil, err
}

cfg := metric.NewInt64CounterConfig(options...)
const kind = InstrumentKindCounter
return m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
Expand All @@ -69,6 +76,10 @@ func (m *meter) Int64Counter(name string, options ...metric.Int64CounterOption)
// configured with options. The instrument is used to synchronously record
// int64 measurements during a computational operation.
func (m *meter) Int64UpDownCounter(name string, options ...metric.Int64UpDownCounterOption) (metric.Int64UpDownCounter, error) {
if err := validateInstrumentName(name); err != nil {
return nil, err
}

cfg := metric.NewInt64UpDownCounterConfig(options...)
const kind = InstrumentKindUpDownCounter
return m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
Expand All @@ -78,6 +89,10 @@ func (m *meter) Int64UpDownCounter(name string, options ...metric.Int64UpDownCou
// with options. The instrument is used to synchronously record the
// distribution of int64 measurements during a computational operation.
func (m *meter) Int64Histogram(name string, options ...metric.Int64HistogramOption) (metric.Int64Histogram, error) {
if err := validateInstrumentName(name); err != nil {
return nil, err
}

cfg := metric.NewInt64HistogramConfig(options...)
const kind = InstrumentKindHistogram
return m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
Expand All @@ -87,6 +102,10 @@ func (m *meter) Int64Histogram(name string, options ...metric.Int64HistogramOpti
// configured with options. The instrument is used to asynchronously record
// increasing int64 measurements once per a measurement collection cycle.
func (m *meter) Int64ObservableCounter(name string, options ...metric.Int64ObservableCounterOption) (metric.Int64ObservableCounter, error) {
if err := validateInstrumentName(name); err != nil {
return nil, err
}

cfg := metric.NewInt64ObservableCounterConfig(options...)
const kind = InstrumentKindObservableCounter
p := int64ObservProvider{m.int64IP}
Expand All @@ -102,6 +121,10 @@ func (m *meter) Int64ObservableCounter(name string, options ...metric.Int64Obser
// configured with options. The instrument is used to asynchronously record
// int64 measurements once per a measurement collection cycle.
func (m *meter) Int64ObservableUpDownCounter(name string, options ...metric.Int64ObservableUpDownCounterOption) (metric.Int64ObservableUpDownCounter, error) {
if err := validateInstrumentName(name); err != nil {
return nil, err
}

cfg := metric.NewInt64ObservableUpDownCounterConfig(options...)
const kind = InstrumentKindObservableUpDownCounter
p := int64ObservProvider{m.int64IP}
Expand All @@ -117,6 +140,10 @@ func (m *meter) Int64ObservableUpDownCounter(name string, options ...metric.Int6
// configured with options. The instrument is used to asynchronously record
// instantaneous int64 measurements once per a measurement collection cycle.
func (m *meter) Int64ObservableGauge(name string, options ...metric.Int64ObservableGaugeOption) (metric.Int64ObservableGauge, error) {
if err := validateInstrumentName(name); err != nil {
return nil, err
}

cfg := metric.NewInt64ObservableGaugeConfig(options...)
const kind = InstrumentKindObservableGauge
p := int64ObservProvider{m.int64IP}
Expand All @@ -132,6 +159,10 @@ func (m *meter) Int64ObservableGauge(name string, options ...metric.Int64Observa
// with options. The instrument is used to synchronously record increasing
// float64 measurements during a computational operation.
func (m *meter) Float64Counter(name string, options ...metric.Float64CounterOption) (metric.Float64Counter, error) {
if err := validateInstrumentName(name); err != nil {
return nil, err
}

cfg := metric.NewFloat64CounterConfig(options...)
const kind = InstrumentKindCounter
return m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
Expand All @@ -141,6 +172,10 @@ func (m *meter) Float64Counter(name string, options ...metric.Float64CounterOpti
// configured with options. The instrument is used to synchronously record
// float64 measurements during a computational operation.
func (m *meter) Float64UpDownCounter(name string, options ...metric.Float64UpDownCounterOption) (metric.Float64UpDownCounter, error) {
if err := validateInstrumentName(name); err != nil {
return nil, err
}

cfg := metric.NewFloat64UpDownCounterConfig(options...)
const kind = InstrumentKindUpDownCounter
return m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
Expand All @@ -150,6 +185,10 @@ func (m *meter) Float64UpDownCounter(name string, options ...metric.Float64UpDow
// with options. The instrument is used to synchronously record the
// distribution of float64 measurements during a computational operation.
func (m *meter) Float64Histogram(name string, options ...metric.Float64HistogramOption) (metric.Float64Histogram, error) {
if err := validateInstrumentName(name); err != nil {
return nil, err
}

cfg := metric.NewFloat64HistogramConfig(options...)
const kind = InstrumentKindHistogram
return m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
Expand All @@ -159,6 +198,10 @@ func (m *meter) Float64Histogram(name string, options ...metric.Float64Histogram
// configured with options. The instrument is used to asynchronously record
// increasing float64 measurements once per a measurement collection cycle.
func (m *meter) Float64ObservableCounter(name string, options ...metric.Float64ObservableCounterOption) (metric.Float64ObservableCounter, error) {
if err := validateInstrumentName(name); err != nil {
return nil, err
}

cfg := metric.NewFloat64ObservableCounterConfig(options...)
const kind = InstrumentKindObservableCounter
p := float64ObservProvider{m.float64IP}
Expand All @@ -174,6 +217,10 @@ func (m *meter) Float64ObservableCounter(name string, options ...metric.Float64O
// and configured with options. The instrument is used to asynchronously record
// float64 measurements once per a measurement collection cycle.
func (m *meter) Float64ObservableUpDownCounter(name string, options ...metric.Float64ObservableUpDownCounterOption) (metric.Float64ObservableUpDownCounter, error) {
if err := validateInstrumentName(name); err != nil {
return nil, err
}

cfg := metric.NewFloat64ObservableUpDownCounterConfig(options...)
const kind = InstrumentKindObservableUpDownCounter
p := float64ObservProvider{m.float64IP}
Expand All @@ -189,6 +236,10 @@ func (m *meter) Float64ObservableUpDownCounter(name string, options ...metric.Fl
// configured with options. The instrument is used to asynchronously record
// instantaneous float64 measurements once per a measurement collection cycle.
func (m *meter) Float64ObservableGauge(name string, options ...metric.Float64ObservableGaugeOption) (metric.Float64ObservableGauge, error) {
if err := validateInstrumentName(name); err != nil {
return nil, err
}

cfg := metric.NewFloat64ObservableGaugeConfig(options...)
const kind = InstrumentKindObservableGauge
p := float64ObservProvider{m.float64IP}
Expand All @@ -200,6 +251,14 @@ func (m *meter) Float64ObservableGauge(name string, options ...metric.Float64Obs
return inst, nil
}

func validateInstrumentName(name string) error {
if !instrumentNameRe.MatchString(name) {
global.Warn("Invalid Instrument name. Instrument names must consist of 63 or fewer characters including alphanumeric, _, ., -, and start with a letter")
return errors.New("invalid instrument name")
}
return nil
}

// RegisterCallback registers f to be called each collection cycle so it will
// make observations for insts during those cycles.
//
Expand Down

0 comments on commit 214228d

Please sign in to comment.