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

Method AddSubmetric

metrics/metric.go:40–82  ·  view source on GitHub ↗

AddSubmetric creates a new submetric from the key:value threshold definition and adds it to the metric's submetrics list.

(keyValues string)

Source from the content-addressed store, hash-verified

38// AddSubmetric creates a new submetric from the key:value threshold definition
39// and adds it to the metric's submetrics list.
40func (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
85var ErrMetricNameParsing = errors.New("parsing metric name failed")

Callers 7

TestThresholdsValidateFunction · 0.95
TestAddSubmetricFunction · 0.80
toSnapshotMethod · 0.80
addMethod · 0.80

Calls 5

ErrorfMethod · 0.80
SplitMethod · 0.80
RootTagSetMethod · 0.80
WithMethod · 0.80
newMetricMethod · 0.45

Tested by 4

TestThresholdsValidateFunction · 0.76
TestAddSubmetricFunction · 0.64