MCPcopy
hub / github.com/google/mtail / GetDatum

Method GetDatum

internal/metrics/metric.go:151–181  ·  view source on GitHub ↗

GetDatum returns the datum named by a sequence of string label values from a Metric. If the sequence of label values does not yet exist, it is created.

(labelvalues ...string)

Source from the content-addressed store, hash-verified

149// GetDatum returns the datum named by a sequence of string label values from a
150// Metric. If the sequence of label values does not yet exist, it is created.
151func (m *Metric) GetDatum(labelvalues ...string) (d datum.Datum, err error) {
152 if len(labelvalues) != len(m.Keys) {
153 return nil, errors.Errorf("Label values requested (%q) not same length as keys for metric %v", labelvalues, m)
154 }
155 m.Lock()
156 defer m.Unlock()
157 if lv := m.FindLabelValueOrNil(labelvalues); lv != nil {
158 d = lv.Value
159 } else {
160 // TODO Check m.Limit and expire old data
161 switch m.Type {
162 case Int:
163 d = datum.NewInt()
164 case Float:
165 d = datum.NewFloat()
166 case String:
167 d = datum.NewString()
168 case Buckets:
169 buckets := m.Buckets
170 if buckets == nil {
171 buckets = make([]datum.Range, 0)
172 }
173 d = datum.NewBuckets(buckets)
174 }
175 lv := &LabelValue{Labels: labelvalues, Value: d}
176 if err := m.AppendLabelValue(lv); err != nil {
177 return nil, err
178 }
179 }
180 return d, nil
181}
182
183// RemoveOldestDatum scans the Metric's LabelValues for the Datum with the oldest timestamp, and removes it.
184func (m *Metric) RemoveOldestDatum() {

Callers 15

TestExpireOldDatumFunction · 0.95
TestExpireOversizeDatumFunction · 0.95
TestExpireManyMetricsFunction · 0.95
TestScalarMetricFunction · 0.95
TestDimensionedMetricFunction · 0.95
TestEmitLabelSetFunction · 0.95
TestFindLabelValueOrNilFunction · 0.95
TestAppendLabelValueFunction · 0.95
TestMetricJSONRoundTripFunction · 0.95
TestTimerFunction · 0.95

Calls 6

FindLabelValueOrNilMethod · 0.95
AppendLabelValueMethod · 0.95
NewIntFunction · 0.92
NewFloatFunction · 0.92
NewStringFunction · 0.92
NewBucketsFunction · 0.92

Tested by 15

TestExpireOldDatumFunction · 0.76
TestExpireOversizeDatumFunction · 0.76
TestExpireManyMetricsFunction · 0.76
TestScalarMetricFunction · 0.76
TestDimensionedMetricFunction · 0.76
TestEmitLabelSetFunction · 0.76
TestFindLabelValueOrNilFunction · 0.76
TestAppendLabelValueFunction · 0.76
TestMetricJSONRoundTripFunction · 0.76
TestTimerFunction · 0.76