AddSubmetric creates a new submetric from the key:value threshold definition and adds it to the metric's submetrics list.
(keyValues string)
| 38 | // AddSubmetric creates a new submetric from the key:value threshold definition |
| 39 | // and adds it to the metric's submetrics list. |
| 40 | func (m *Metric) AddSubmetric(keyValues string) (*Submetric, error) { |
| 41 | keyValues = strings.TrimSpace(keyValues) |
| 42 | if len(keyValues) == 0 { |
| 43 | return nil, fmt.Errorf("submetric criteria for metric '%s' cannot be empty", m.Name) |
| 44 | } |
| 45 | kvs := strings.Split(keyValues, ",") |
| 46 | tags := m.registry.RootTagSet() |
| 47 | for _, kv := range kvs { |
| 48 | if kv == "" { |
| 49 | continue |
| 50 | } |
| 51 | k, v, _ := strings.Cut(kv, ":") |
| 52 | |
| 53 | key := strings.Trim(strings.TrimSpace(k), `"'`) |
| 54 | if v == "" { |
| 55 | tags = tags.With(key, "") |
| 56 | continue |
| 57 | } |
| 58 | |
| 59 | value := strings.Trim(strings.TrimSpace(v), `"'`) |
| 60 | tags = tags.With(key, value) |
| 61 | } |
| 62 | |
| 63 | for _, sm := range m.Submetrics { |
| 64 | if tags == sm.Tags { |
| 65 | return sm, nil |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | subMetric := &Submetric{ |
| 70 | Name: m.Name + "{" + keyValues + "}", |
| 71 | Suffix: keyValues, |
| 72 | Tags: tags, |
| 73 | Parent: m, |
| 74 | } |
| 75 | subMetricMetric := m.registry.newMetric(subMetric.Name, m.Type, m.Contains) |
| 76 | subMetricMetric.Sub = subMetric // sigh |
| 77 | subMetric.Metric = subMetricMetric |
| 78 | |
| 79 | m.Submetrics = append(m.Submetrics, subMetric) |
| 80 | |
| 81 | return subMetric, nil |
| 82 | } |
| 83 | |
| 84 | // ErrMetricNameParsing indicates parsing a metric name failed |
| 85 | var ErrMetricNameParsing = errors.New("parsing metric name failed") |