Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate instrument names when creating them #4210

Merged
merged 19 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

- Starting from `v1.21.0` of semantic conventions, `go.opentelemetry.io/otel/semconv/{version}/httpconv` and `go.opentelemetry.io/otel/semconv/{version}/netconv` packages will no longer be published. (#4145)
- Log duplicate instrument conflict at a warning level instead of info in `go.opentelemetry.io/otel/sdk/metric`. (#4202)
- Return an error on the creation of new instruments if their name doesn't pass regexp validation. (#4210)

## [1.16.0/0.39.0] 2023-05-18

Expand Down
2 changes: 1 addition & 1 deletion exporters/prometheus/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestPrometheusExporter(t *testing.T) {
gauge.Add(ctx, 100, opt)

counter, err := meter.Float64Counter("0invalid.counter.name", otelmetric.WithDescription("a counter with an invalid name"))
require.NoError(t, err)
require.ErrorIs(t, err, metric.ErrInstrumentName)
counter.Add(ctx, 100, opt)

histogram, err := meter.Float64Histogram("invalid.hist.name", otelmetric.WithDescription("a histogram with an invalid name"))
Expand Down
87 changes: 75 additions & 12 deletions sdk/metric/meter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ import (
"go.opentelemetry.io/otel/sdk/metric/internal"
)

var (
// ErrInstrumentName indicates the created instrument has an invalid name.
// Valid names must consist of 63 or fewer characters including alphanumeric, _, ., -, and start with a letter.
ErrInstrumentName = errors.New("invalid instrument name")
)

// 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 @@ -62,7 +68,12 @@ var _ metric.Meter = (*meter)(nil)
func (m *meter) Int64Counter(name string, options ...metric.Int64CounterOption) (metric.Int64Counter, error) {
cfg := metric.NewInt64CounterConfig(options...)
const kind = InstrumentKindCounter
return m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
i, err := m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
return i, err
}

return i, validateInstrumentName(name)
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
}

// Int64UpDownCounter returns a new instrument identified by name and
Expand All @@ -71,7 +82,12 @@ 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
return m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
i, err := m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
return i, err
}

return i, validateInstrumentName(name)
}

// Int64Histogram returns a new instrument identified by name and configured
Expand All @@ -80,7 +96,12 @@ 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
return m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
i, err := m.int64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
return i, err
}

return i, validateInstrumentName(name)
}

// Int64ObservableCounter returns a new instrument identified by name and
Expand All @@ -95,7 +116,7 @@ func (m *meter) Int64ObservableCounter(name string, options ...metric.Int64Obser
return nil, err
}
p.registerCallbacks(inst, cfg.Callbacks())
return inst, nil
return inst, validateInstrumentName(name)
}

// Int64ObservableUpDownCounter returns a new instrument identified by name and
Expand All @@ -110,7 +131,7 @@ func (m *meter) Int64ObservableUpDownCounter(name string, options ...metric.Int6
return nil, err
}
p.registerCallbacks(inst, cfg.Callbacks())
return inst, nil
return inst, validateInstrumentName(name)
}

// Int64ObservableGauge returns a new instrument identified by name and
Expand All @@ -125,7 +146,7 @@ func (m *meter) Int64ObservableGauge(name string, options ...metric.Int64Observa
return nil, err
}
p.registerCallbacks(inst, cfg.Callbacks())
return inst, nil
return inst, validateInstrumentName(name)
}

// Float64Counter returns a new instrument identified by name and configured
Expand All @@ -134,7 +155,12 @@ 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
return m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
i, err := m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
return i, err
}

return i, validateInstrumentName(name)
}

// Float64UpDownCounter returns a new instrument identified by name and
Expand All @@ -143,7 +169,12 @@ 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
return m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
i, err := m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
return i, err
}

return i, validateInstrumentName(name)
}

// Float64Histogram returns a new instrument identified by name and configured
Expand All @@ -152,7 +183,12 @@ 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
return m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
i, err := m.float64IP.lookup(kind, name, cfg.Description(), cfg.Unit())
if err != nil {
return i, err
}

return i, validateInstrumentName(name)
}

// Float64ObservableCounter returns a new instrument identified by name and
Expand All @@ -167,7 +203,7 @@ func (m *meter) Float64ObservableCounter(name string, options ...metric.Float64O
return nil, err
}
p.registerCallbacks(inst, cfg.Callbacks())
return inst, nil
return inst, validateInstrumentName(name)
}

// Float64ObservableUpDownCounter returns a new instrument identified by name
Expand All @@ -182,7 +218,7 @@ func (m *meter) Float64ObservableUpDownCounter(name string, options ...metric.Fl
return nil, err
}
p.registerCallbacks(inst, cfg.Callbacks())
return inst, nil
return inst, validateInstrumentName(name)
}

// Float64ObservableGauge returns a new instrument identified by name and
Expand All @@ -197,7 +233,34 @@ func (m *meter) Float64ObservableGauge(name string, options ...metric.Float64Obs
return nil, err
}
p.registerCallbacks(inst, cfg.Callbacks())
return inst, nil
return inst, validateInstrumentName(name)
}

func validateInstrumentName(name string) error {
if len(name) == 0 {
return fmt.Errorf("%w: %s: is empty", ErrInstrumentName, name)
}
if len(name) > 63 {
return fmt.Errorf("%w: %s: longer than 63 characters", ErrInstrumentName, name)
}
if !isAlpha([]rune(name)[0]) {
return fmt.Errorf("%w: %s: must start with a letter", ErrInstrumentName, name)
}
if len(name) == 1 {
return nil
}
for _, c := range name[1:] {
if !isAlphanumeric(c) && c != '_' && c != '.' && c != '-' {
return fmt.Errorf("%w: %s: must only contain [A-Za-z0-9_.-]", ErrInstrumentName, name)
}
}
return nil
}
func isAlpha(c rune) bool {
return ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')
}
func isAlphanumeric(c rune) bool {
return isAlpha(c) || ('0' <= c && c <= '9')
}

// RegisterCallback registers f to be called each collection cycle so it will
Expand Down
Loading