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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
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
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
Expand Up @@ -26,6 +26,12 @@
"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 @@
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
}

Check warning on line 74 in sdk/metric/meter.go

View check run for this annotation

Codecov / codecov/patch

sdk/metric/meter.go#L73-L74

Added lines #L73 - L74 were not covered by tests

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) 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
}

Check warning on line 88 in sdk/metric/meter.go

View check run for this annotation

Codecov / codecov/patch

sdk/metric/meter.go#L87-L88

Added lines #L87 - L88 were not covered by tests

return i, validateInstrumentName(name)
}

// Int64Histogram returns a new instrument identified by name and configured
Expand All @@ -80,7 +96,12 @@
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
}

Check warning on line 102 in sdk/metric/meter.go

View check run for this annotation

Codecov / codecov/patch

sdk/metric/meter.go#L101-L102

Added lines #L101 - L102 were not covered by tests

return i, validateInstrumentName(name)
}

// Int64ObservableCounter returns a new instrument identified by name and
Expand All @@ -95,7 +116,7 @@
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 @@
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 @@
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) 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
}

Check warning on line 161 in sdk/metric/meter.go

View check run for this annotation

Codecov / codecov/patch

sdk/metric/meter.go#L160-L161

Added lines #L160 - L161 were not covered by tests

return i, validateInstrumentName(name)
}

// Float64UpDownCounter returns a new instrument identified by name and
Expand All @@ -143,7 +169,12 @@
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
}

Check warning on line 175 in sdk/metric/meter.go

View check run for this annotation

Codecov / codecov/patch

sdk/metric/meter.go#L174-L175

Added lines #L174 - L175 were not covered by tests

return i, validateInstrumentName(name)
}

// Float64Histogram returns a new instrument identified by name and configured
Expand All @@ -152,7 +183,12 @@
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
}

Check warning on line 189 in sdk/metric/meter.go

View check run for this annotation

Codecov / codecov/patch

sdk/metric/meter.go#L188-L189

Added lines #L188 - L189 were not covered by tests

return i, validateInstrumentName(name)
}

// Float64ObservableCounter returns a new instrument identified by name and
Expand All @@ -167,7 +203,7 @@
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 @@
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 @@
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