MCPcopy Create free account
hub / github.com/prometheus/common / writeSample

Function writeSample

expfmt/text_create.go:276–331  ·  view source on GitHub ↗

writeSample writes a single sample in text format to w, given the metric name, the metric proto message itself, optionally an additional label name with a float64 value (use empty string as label name if not required), and the value. The function returns the number of bytes written and any error enc

(
	w enhancedWriter,
	name, suffix string,
	metric *dto.Metric,
	additionalLabelName string, additionalLabelValue float64,
	value float64,
)

Source from the content-addressed store, hash-verified

274// the value. The function returns the number of bytes written and any error
275// encountered.
276func writeSample(
277 w enhancedWriter,
278 name, suffix string,
279 metric *dto.Metric,
280 additionalLabelName string, additionalLabelValue float64,
281 value float64,
282) (int, error) {
283 var written int
284 n, err := w.WriteString(name)
285 written += n
286 if err != nil {
287 return written, err
288 }
289 if suffix != "" {
290 n, err = w.WriteString(suffix)
291 written += n
292 if err != nil {
293 return written, err
294 }
295 }
296 n, err = writeLabelPairs(
297 w, metric.Label, additionalLabelName, additionalLabelValue,
298 )
299 written += n
300 if err != nil {
301 return written, err
302 }
303 err = w.WriteByte(' ')
304 written++
305 if err != nil {
306 return written, err
307 }
308 n, err = writeFloat(w, value)
309 written += n
310 if err != nil {
311 return written, err
312 }
313 if metric.TimestampMs != nil {
314 err = w.WriteByte(' ')
315 written++
316 if err != nil {
317 return written, err
318 }
319 n, err = writeInt(w, *metric.TimestampMs)
320 written += n
321 if err != nil {
322 return written, err
323 }
324 }
325 err = w.WriteByte('\n')
326 written++
327 if err != nil {
328 return written, err
329 }
330 return written, nil
331}
332
333// writeLabelPairs converts a slice of LabelPair proto messages plus the

Callers 1

MetricFamilyToTextFunction · 0.85

Calls 5

writeLabelPairsFunction · 0.85
writeFloatFunction · 0.85
writeIntFunction · 0.85
WriteStringMethod · 0.80
WriteByteMethod · 0.80

Tested by

no test coverage detected