MCPcopy
hub / github.com/grafana/k6 / NewMetric

Method NewMetric

metrics/registry.go:43–67  ·  view source on GitHub ↗

NewMetric returns new metric registered to this registry TODO have multiple versions returning specific metric types when we have such things

(name string, typ MetricType, t ...ValueType)

Source from the content-addressed store, hash-verified

41// NewMetric returns new metric registered to this registry
42// TODO have multiple versions returning specific metric types when we have such things
43func (r *Registry) NewMetric(name string, typ MetricType, t ...ValueType) (*Metric, error) {
44 r.l.Lock()
45 defer r.l.Unlock()
46
47 if !checkName(name) {
48 return nil, fmt.Errorf("Invalid metric name: '%s'. %s", name, badNameWarning) //nolint:staticcheck
49 }
50 oldMetric, ok := r.metrics[name]
51
52 if !ok {
53 m := r.newMetric(name, typ, t...)
54 r.metrics[name] = m
55 return m, nil
56 }
57 if oldMetric.Type != typ {
58 return nil, fmt.Errorf("metric '%s' already exists but with type %s, instead of %s", name, oldMetric.Type, typ)
59 }
60 if len(t) > 0 {
61 if t[0] != oldMetric.Contains {
62 return nil, fmt.Errorf("metric '%s' already exists but with a value type %s, instead of %s",
63 name, oldMetric.Contains, t[0])
64 }
65 }
66 return oldMetric, nil
67}
68
69// MustNewMetric is like NewMetric, but will panic if there is an error
70func (r *Registry) MustNewMetric(name string, typ MetricType, t ...ValueType) *Metric {

Callers 15

TestRegistryNewMetricFunction · 0.95
TestRegistryAllFunction · 0.95
MustNewMetricMethod · 0.95
TestThresholdsValidateFunction · 0.95
TestSampleBufferBasicsFunction · 0.95
benchmarkInfluxdbFunction · 0.95

Calls 5

newMetricMethod · 0.95
checkNameFunction · 0.85
UnlockMethod · 0.80
ErrorfMethod · 0.80
LockMethod · 0.45

Tested by 15

TestRegistryNewMetricFunction · 0.76
TestRegistryAllFunction · 0.76
TestThresholdsValidateFunction · 0.76
TestSampleBufferBasicsFunction · 0.76
benchmarkInfluxdbFunction · 0.76
TestOutputFunction · 0.76