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

Function MetricFamilyToText

expfmt/text_create.go:66–269  ·  view source on GitHub ↗

MetricFamilyToText converts a MetricFamily proto message into text format and writes the resulting lines to 'out'. It returns the number of bytes written and any error encountered. The output will have the same order as the input, no further sorting is performed. Furthermore, this function assumes t

(out io.Writer, in *dto.MetricFamily)

Source from the content-addressed store, hash-verified

64//
65// This method fulfills the type 'prometheus.encoder'.
66func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (written int, err error) {
67 // Fail-fast checks.
68 if len(in.Metric) == 0 {
69 return 0, fmt.Errorf("MetricFamily has no metrics: %s", in)
70 }
71 name := in.GetName()
72 if name == "" {
73 return 0, fmt.Errorf("MetricFamily has no name: %s", in)
74 }
75
76 // Try the interface upgrade. If it doesn't work, we'll use a
77 // bufio.Writer from the sync.Pool.
78 w, ok := out.(enhancedWriter)
79 if !ok {
80 b := bufPool.Get().(*bufio.Writer)
81 b.Reset(out)
82 w = b
83 defer func() {
84 bErr := b.Flush()
85 if err == nil {
86 err = bErr
87 }
88 bufPool.Put(b)
89 }()
90 }
91
92 var n int
93
94 // Comments, first HELP, then TYPE.
95 if in.Help != nil {
96 n, err = w.WriteString("# HELP ")
97 written += n
98 if err != nil {
99 return
100 }
101 n, err = w.WriteString(name)
102 written += n
103 if err != nil {
104 return
105 }
106 err = w.WriteByte(' ')
107 written++
108 if err != nil {
109 return
110 }
111 n, err = writeEscapedString(w, *in.Help, false)
112 written += n
113 if err != nil {
114 return
115 }
116 err = w.WriteByte('\n')
117 written++
118 if err != nil {
119 return
120 }
121 }
122 n, err = w.WriteString("# TYPE ")
123 written += n

Callers 5

TestCreateFunction · 0.85
BenchmarkCreateFunction · 0.85
BenchmarkCreateBuildInfoFunction · 0.85
TestCreateErrorFunction · 0.85
NewEncoderFunction · 0.85

Calls 7

writeEscapedStringFunction · 0.85
writeSampleFunction · 0.85
GetMethod · 0.80
PutMethod · 0.80
WriteStringMethod · 0.80
WriteByteMethod · 0.80
StringMethod · 0.65

Tested by 4

TestCreateFunction · 0.68
BenchmarkCreateFunction · 0.68
BenchmarkCreateBuildInfoFunction · 0.68
TestCreateErrorFunction · 0.68