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)
| 41 | // NewMetric returns new metric registered to this registry |
| 42 | // TODO have multiple versions returning specific metric types when we have such things |
| 43 | func (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 |
| 70 | func (r *Registry) MustNewMetric(name string, typ MetricType, t ...ValueType) *Metric { |