MCPcopy
hub / github.com/prometheus/client_golang / newExemplar

Function newExemplar

prometheus/value.go:245–274  ·  view source on GitHub ↗

newExemplar creates a new dto.Exemplar from the provided values. An error is returned if any of the label names or values are invalid or if the total number of runes in the label names and values exceeds ExemplarMaxRunes.

(value float64, ts time.Time, l Labels)

Source from the content-addressed store, hash-verified

243// returned if any of the label names or values are invalid or if the total
244// number of runes in the label names and values exceeds ExemplarMaxRunes.
245func newExemplar(value float64, ts time.Time, l Labels) (*dto.Exemplar, error) {
246 e := &dto.Exemplar{}
247 e.Value = proto.Float64(value)
248 tsProto := timestamppb.New(ts)
249 if err := tsProto.CheckValid(); err != nil {
250 return nil, err
251 }
252 e.Timestamp = tsProto
253 labelPairs := make([]*dto.LabelPair, 0, len(l))
254 var runes int
255 for name, value := range l {
256 if !checkLabelName(name) {
257 return nil, fmt.Errorf("exemplar label name %q is invalid", name)
258 }
259 runes += utf8.RuneCountInString(name)
260 if !utf8.ValidString(value) {
261 return nil, fmt.Errorf("exemplar label value %q is not valid UTF-8", value)
262 }
263 runes += utf8.RuneCountInString(value)
264 labelPairs = append(labelPairs, &dto.LabelPair{
265 Name: proto.String(name),
266 Value: proto.String(value),
267 })
268 }
269 if runes > ExemplarMaxRunes {
270 return nil, fmt.Errorf("exemplar labels have %d runes, exceeding the limit of %d", runes, ExemplarMaxRunes)
271 }
272 e.Label = labelPairs
273 return e, nil
274}

Callers 3

updateExemplarMethod · 0.85
NewMetricWithExemplarsFunction · 0.85
updateExemplarMethod · 0.85

Calls 2

checkLabelNameFunction · 0.85
StringMethod · 0.45

Tested by

no test coverage detected