type enhancedWriter … const initialNumBufSize … var bufPool … var numBufPool … // 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 the input // is already sanitized and does not perform any sanity checks. If the input // contains duplicate metrics or invalid metric or label names, the conversion // will result in invalid text format output. // // If metric names conform to the legacy validation pattern, they will be placed // outside the brackets in the traditional way, like `foo{}`. If the metric name // fails the legacy validation check, it will be placed quoted inside the // brackets: `{"foo"}`. As stated above, the input is assumed to be santized and // no error will be thrown in this case. // // Similar to metric names, if label names conform to the legacy validation // pattern, they will be unquoted as normal, like `foo{bar="baz"}`. If the label // name fails the legacy validation check, it will be quoted: // `foo{"bar"="baz"}`. As stated above, the input is assumed to be santized and // no error will be thrown in this case. // // This method fulfills the type 'prometheus.encoder'. func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (written int, err error) { … } // 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 // encountered. func writeSample( w enhancedWriter, name, suffix string, metric *dto.Metric, additionalLabelName string, additionalLabelValue float64, value float64, ) (int, error) { … } // writeNameAndLabelPairs converts a slice of LabelPair proto messages plus the // explicitly given metric name and additional label pair into text formatted as // required by the text format and writes it to 'w'. An empty slice in // combination with an empty string 'additionalLabelName' results in nothing // being written. Otherwise, the label pairs are written, escaped as required by // the text format, and enclosed in '{...}'. The function returns the number of // bytes written and any error encountered. If the metric name is not // legacy-valid, it will be put inside the brackets as well. Legacy-invalid // label names will also be quoted. func writeNameAndLabelPairs( w enhancedWriter, name string, in []*dto.LabelPair, additionalLabelName string, additionalLabelValue float64, ) (int, error) { … } var escaper … var quotedEscaper … func writeEscapedString(w enhancedWriter, v string, includeDoubleQuote bool) (int, error) { … } // writeFloat is equivalent to fmt.Fprint with a float64 argument but hardcodes // a few common cases for increased efficiency. For non-hardcoded cases, it uses // strconv.AppendFloat to avoid allocations, similar to writeInt. func writeFloat(w enhancedWriter, f float64) (int, error) { … } // writeInt is equivalent to fmt.Fprint with an int64 argument but uses // strconv.AppendInt with a byte slice taken from a sync.Pool to avoid // allocations. func writeInt(w enhancedWriter, i int64) (int, error) { … } // writeName writes a string as-is if it complies with the legacy naming // scheme, or escapes it in double quotes if not. func writeName(w enhancedWriter, name string) (int, error) { … }