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

Function MetricFamilyToText

expfmt/text_create.go:78–293  ·  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

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

Callers 5

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

Calls 8

writeNameFunction · 0.85
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

Used in the wild real call sites across dependent graphs

searching dependent graphs…