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 9 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)
- Reject the creation of new instruments if their name doesn't pass regexp validation. (#4210)
dmathieu marked this conversation as resolved.
Show resolved Hide resolved

## [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.Equal(t, err, metric.ErrInvalidInstrumentName)
counter.Add(ctx, 100, opt)

histogram, err := meter.Float64Histogram("invalid.hist.name", otelmetric.WithDescription("a histogram with an invalid name"))
Expand Down
68 changes: 56 additions & 12 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,11 @@ import (
"go.opentelemetry.io/otel/sdk/metric/internal"
)

var (
instrumentNameRe = regexp.MustCompile(`^([A-Za-z]){1}([A-Za-z0-9\_\-\.]){0,62}$`)
ErrInvalidInstrumentName = errors.New("invalid instrument name. Instrument names must consist of 63 or fewer characters including alphanumeric, _, ., -, and start with a letter")
)

// 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,9 +66,15 @@ 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) {

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 +83,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 +97,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 +117,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 +132,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 +147,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 +156,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 +170,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 +184,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 +204,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 +219,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 +234,14 @@ 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 !instrumentNameRe.MatchString(name) {
return ErrInvalidInstrumentName
}
return nil
}
dmathieu marked this conversation as resolved.
Show resolved Hide resolved

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