2017-12-21 20:23:45 +00:00
|
|
|
// Copyright 2014 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package expfmt
|
|
|
|
|
|
|
|
import (
|
2019-07-31 06:09:38 +00:00
|
|
|
"bytes"
|
2017-12-21 20:23:45 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"math"
|
2019-07-31 06:09:38 +00:00
|
|
|
"strconv"
|
2017-12-21 20:23:45 +00:00
|
|
|
"strings"
|
2019-07-31 06:09:38 +00:00
|
|
|
"sync"
|
2017-12-21 20:23:45 +00:00
|
|
|
|
|
|
|
"github.com/prometheus/common/model"
|
2019-07-31 06:09:38 +00:00
|
|
|
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
)
|
|
|
|
|
|
|
|
// enhancedWriter has all the enhanced write functions needed here. bytes.Buffer
|
|
|
|
// implements it.
|
|
|
|
type enhancedWriter interface {
|
|
|
|
io.Writer
|
|
|
|
WriteRune(r rune) (n int, err error)
|
|
|
|
WriteString(s string) (n int, err error)
|
|
|
|
WriteByte(c byte) error
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
initialBufSize = 512
|
|
|
|
initialNumBufSize = 24
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
bufPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
return bytes.NewBuffer(make([]byte, 0, initialBufSize))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
numBufPool = sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
b := make([]byte, 0, initialNumBufSize)
|
|
|
|
return &b
|
|
|
|
},
|
|
|
|
}
|
2017-12-21 20:23:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
// This method fulfills the type 'prometheus.encoder'.
|
2019-07-31 06:09:38 +00:00
|
|
|
func MetricFamilyToText(out io.Writer, in *dto.MetricFamily) (written int, err error) {
|
2017-12-21 20:23:45 +00:00
|
|
|
// Fail-fast checks.
|
|
|
|
if len(in.Metric) == 0 {
|
2019-07-31 06:09:38 +00:00
|
|
|
return 0, fmt.Errorf("MetricFamily has no metrics: %s", in)
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|
|
|
|
name := in.GetName()
|
|
|
|
if name == "" {
|
2019-07-31 06:09:38 +00:00
|
|
|
return 0, fmt.Errorf("MetricFamily has no name: %s", in)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try the interface upgrade. If it doesn't work, we'll use a
|
|
|
|
// bytes.Buffer from the sync.Pool and write out its content to out in a
|
|
|
|
// single go in the end.
|
|
|
|
w, ok := out.(enhancedWriter)
|
|
|
|
if !ok {
|
|
|
|
b := bufPool.Get().(*bytes.Buffer)
|
|
|
|
b.Reset()
|
|
|
|
w = b
|
|
|
|
defer func() {
|
|
|
|
bWritten, bErr := out.Write(b.Bytes())
|
|
|
|
written = bWritten
|
|
|
|
if err == nil {
|
|
|
|
err = bErr
|
|
|
|
}
|
|
|
|
bufPool.Put(b)
|
|
|
|
}()
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 06:09:38 +00:00
|
|
|
var n int
|
|
|
|
|
2017-12-21 20:23:45 +00:00
|
|
|
// Comments, first HELP, then TYPE.
|
|
|
|
if in.Help != nil {
|
2019-07-31 06:09:38 +00:00
|
|
|
n, err = w.WriteString("# HELP ")
|
2017-12-21 20:23:45 +00:00
|
|
|
written += n
|
|
|
|
if err != nil {
|
2019-07-31 06:09:38 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
n, err = w.WriteString(name)
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = w.WriteByte(' ')
|
|
|
|
written++
|
|
|
|
if err != nil {
|
|
|
|
return
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|
2019-07-31 06:09:38 +00:00
|
|
|
n, err = writeEscapedString(w, *in.Help, false)
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = w.WriteByte('\n')
|
|
|
|
written++
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n, err = w.WriteString("# TYPE ")
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n, err = w.WriteString(name)
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|
|
|
|
metricType := in.GetType()
|
2019-07-31 06:09:38 +00:00
|
|
|
switch metricType {
|
|
|
|
case dto.MetricType_COUNTER:
|
|
|
|
n, err = w.WriteString(" counter\n")
|
|
|
|
case dto.MetricType_GAUGE:
|
|
|
|
n, err = w.WriteString(" gauge\n")
|
|
|
|
case dto.MetricType_SUMMARY:
|
|
|
|
n, err = w.WriteString(" summary\n")
|
|
|
|
case dto.MetricType_UNTYPED:
|
|
|
|
n, err = w.WriteString(" untyped\n")
|
|
|
|
case dto.MetricType_HISTOGRAM:
|
|
|
|
n, err = w.WriteString(" histogram\n")
|
|
|
|
default:
|
|
|
|
return written, fmt.Errorf("unknown metric type %s", metricType.String())
|
|
|
|
}
|
2017-12-21 20:23:45 +00:00
|
|
|
written += n
|
|
|
|
if err != nil {
|
2019-07-31 06:09:38 +00:00
|
|
|
return
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally the samples, one line for each.
|
|
|
|
for _, metric := range in.Metric {
|
|
|
|
switch metricType {
|
|
|
|
case dto.MetricType_COUNTER:
|
|
|
|
if metric.Counter == nil {
|
|
|
|
return written, fmt.Errorf(
|
|
|
|
"expected counter in metric %s %s", name, metric,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
n, err = writeSample(
|
2019-07-31 06:09:38 +00:00
|
|
|
w, name, "", metric, "", 0,
|
2017-12-21 20:23:45 +00:00
|
|
|
metric.Counter.GetValue(),
|
|
|
|
)
|
|
|
|
case dto.MetricType_GAUGE:
|
|
|
|
if metric.Gauge == nil {
|
|
|
|
return written, fmt.Errorf(
|
|
|
|
"expected gauge in metric %s %s", name, metric,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
n, err = writeSample(
|
2019-07-31 06:09:38 +00:00
|
|
|
w, name, "", metric, "", 0,
|
2017-12-21 20:23:45 +00:00
|
|
|
metric.Gauge.GetValue(),
|
|
|
|
)
|
|
|
|
case dto.MetricType_UNTYPED:
|
|
|
|
if metric.Untyped == nil {
|
|
|
|
return written, fmt.Errorf(
|
|
|
|
"expected untyped in metric %s %s", name, metric,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
n, err = writeSample(
|
2019-07-31 06:09:38 +00:00
|
|
|
w, name, "", metric, "", 0,
|
2017-12-21 20:23:45 +00:00
|
|
|
metric.Untyped.GetValue(),
|
|
|
|
)
|
|
|
|
case dto.MetricType_SUMMARY:
|
|
|
|
if metric.Summary == nil {
|
|
|
|
return written, fmt.Errorf(
|
|
|
|
"expected summary in metric %s %s", name, metric,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
for _, q := range metric.Summary.Quantile {
|
|
|
|
n, err = writeSample(
|
2019-07-31 06:09:38 +00:00
|
|
|
w, name, "", metric,
|
|
|
|
model.QuantileLabel, q.GetQuantile(),
|
2017-12-21 20:23:45 +00:00
|
|
|
q.GetValue(),
|
|
|
|
)
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
2019-07-31 06:09:38 +00:00
|
|
|
return
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
n, err = writeSample(
|
2019-07-31 06:09:38 +00:00
|
|
|
w, name, "_sum", metric, "", 0,
|
2017-12-21 20:23:45 +00:00
|
|
|
metric.Summary.GetSampleSum(),
|
|
|
|
)
|
2019-07-31 06:09:38 +00:00
|
|
|
written += n
|
2017-12-21 20:23:45 +00:00
|
|
|
if err != nil {
|
2019-07-31 06:09:38 +00:00
|
|
|
return
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|
|
|
|
n, err = writeSample(
|
2019-07-31 06:09:38 +00:00
|
|
|
w, name, "_count", metric, "", 0,
|
2017-12-21 20:23:45 +00:00
|
|
|
float64(metric.Summary.GetSampleCount()),
|
|
|
|
)
|
|
|
|
case dto.MetricType_HISTOGRAM:
|
|
|
|
if metric.Histogram == nil {
|
|
|
|
return written, fmt.Errorf(
|
|
|
|
"expected histogram in metric %s %s", name, metric,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
infSeen := false
|
2019-07-31 06:09:38 +00:00
|
|
|
for _, b := range metric.Histogram.Bucket {
|
2017-12-21 20:23:45 +00:00
|
|
|
n, err = writeSample(
|
2019-07-31 06:09:38 +00:00
|
|
|
w, name, "_bucket", metric,
|
|
|
|
model.BucketLabel, b.GetUpperBound(),
|
|
|
|
float64(b.GetCumulativeCount()),
|
2017-12-21 20:23:45 +00:00
|
|
|
)
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
2019-07-31 06:09:38 +00:00
|
|
|
return
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|
2019-07-31 06:09:38 +00:00
|
|
|
if math.IsInf(b.GetUpperBound(), +1) {
|
2017-12-21 20:23:45 +00:00
|
|
|
infSeen = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !infSeen {
|
|
|
|
n, err = writeSample(
|
2019-07-31 06:09:38 +00:00
|
|
|
w, name, "_bucket", metric,
|
|
|
|
model.BucketLabel, math.Inf(+1),
|
2017-12-21 20:23:45 +00:00
|
|
|
float64(metric.Histogram.GetSampleCount()),
|
|
|
|
)
|
2019-07-31 06:09:38 +00:00
|
|
|
written += n
|
2017-12-21 20:23:45 +00:00
|
|
|
if err != nil {
|
2019-07-31 06:09:38 +00:00
|
|
|
return
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
n, err = writeSample(
|
2019-07-31 06:09:38 +00:00
|
|
|
w, name, "_sum", metric, "", 0,
|
2017-12-21 20:23:45 +00:00
|
|
|
metric.Histogram.GetSampleSum(),
|
|
|
|
)
|
2019-07-31 06:09:38 +00:00
|
|
|
written += n
|
2017-12-21 20:23:45 +00:00
|
|
|
if err != nil {
|
2019-07-31 06:09:38 +00:00
|
|
|
return
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|
|
|
|
n, err = writeSample(
|
2019-07-31 06:09:38 +00:00
|
|
|
w, name, "_count", metric, "", 0,
|
2017-12-21 20:23:45 +00:00
|
|
|
float64(metric.Histogram.GetSampleCount()),
|
|
|
|
)
|
|
|
|
default:
|
|
|
|
return written, fmt.Errorf(
|
|
|
|
"unexpected type in metric %s %s", name, metric,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
2019-07-31 06:09:38 +00:00
|
|
|
return
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-31 06:09:38 +00:00
|
|
|
return
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|
|
|
|
|
2019-07-31 06:09:38 +00:00
|
|
|
// writeSample writes a single sample in text format to w, given the metric
|
2017-12-21 20:23:45 +00:00
|
|
|
// name, the metric proto message itself, optionally an additional label name
|
2019-07-31 06:09:38 +00:00
|
|
|
// 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.
|
2017-12-21 20:23:45 +00:00
|
|
|
func writeSample(
|
2019-07-31 06:09:38 +00:00
|
|
|
w enhancedWriter,
|
|
|
|
name, suffix string,
|
2017-12-21 20:23:45 +00:00
|
|
|
metric *dto.Metric,
|
2019-07-31 06:09:38 +00:00
|
|
|
additionalLabelName string, additionalLabelValue float64,
|
2017-12-21 20:23:45 +00:00
|
|
|
value float64,
|
|
|
|
) (int, error) {
|
|
|
|
var written int
|
2019-07-31 06:09:38 +00:00
|
|
|
n, err := w.WriteString(name)
|
2017-12-21 20:23:45 +00:00
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
2019-07-31 06:09:38 +00:00
|
|
|
if suffix != "" {
|
|
|
|
n, err = w.WriteString(suffix)
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
n, err = writeLabelPairs(
|
|
|
|
w, metric.Label, additionalLabelName, additionalLabelValue,
|
2017-12-21 20:23:45 +00:00
|
|
|
)
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
2019-07-31 06:09:38 +00:00
|
|
|
err = w.WriteByte(' ')
|
|
|
|
written++
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
n, err = writeFloat(w, value)
|
2017-12-21 20:23:45 +00:00
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
if metric.TimestampMs != nil {
|
2019-07-31 06:09:38 +00:00
|
|
|
err = w.WriteByte(' ')
|
|
|
|
written++
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
n, err = writeInt(w, *metric.TimestampMs)
|
2017-12-21 20:23:45 +00:00
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
}
|
2019-07-31 06:09:38 +00:00
|
|
|
err = w.WriteByte('\n')
|
|
|
|
written++
|
2017-12-21 20:23:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
return written, nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 06:09:38 +00:00
|
|
|
// writeLabelPairs converts a slice of LabelPair proto messages plus the
|
2017-12-21 20:23:45 +00:00
|
|
|
// explicitly given additional label pair into text formatted as required by the
|
2019-07-31 06:09:38 +00:00
|
|
|
// 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.
|
|
|
|
func writeLabelPairs(
|
|
|
|
w enhancedWriter,
|
2017-12-21 20:23:45 +00:00
|
|
|
in []*dto.LabelPair,
|
2019-07-31 06:09:38 +00:00
|
|
|
additionalLabelName string, additionalLabelValue float64,
|
2017-12-21 20:23:45 +00:00
|
|
|
) (int, error) {
|
|
|
|
if len(in) == 0 && additionalLabelName == "" {
|
|
|
|
return 0, nil
|
|
|
|
}
|
2019-07-31 06:09:38 +00:00
|
|
|
var (
|
|
|
|
written int
|
|
|
|
separator byte = '{'
|
|
|
|
)
|
2017-12-21 20:23:45 +00:00
|
|
|
for _, lp := range in {
|
2019-07-31 06:09:38 +00:00
|
|
|
err := w.WriteByte(separator)
|
|
|
|
written++
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
n, err := w.WriteString(lp.GetName())
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
n, err = w.WriteString(`="`)
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
n, err = writeEscapedString(w, lp.GetValue(), true)
|
2017-12-21 20:23:45 +00:00
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
2019-07-31 06:09:38 +00:00
|
|
|
err = w.WriteByte('"')
|
|
|
|
written++
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
2017-12-21 20:23:45 +00:00
|
|
|
separator = ','
|
|
|
|
}
|
|
|
|
if additionalLabelName != "" {
|
2019-07-31 06:09:38 +00:00
|
|
|
err := w.WriteByte(separator)
|
|
|
|
written++
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
n, err := w.WriteString(additionalLabelName)
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
n, err = w.WriteString(`="`)
|
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
n, err = writeFloat(w, additionalLabelValue)
|
2017-12-21 20:23:45 +00:00
|
|
|
written += n
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
2019-07-31 06:09:38 +00:00
|
|
|
err = w.WriteByte('"')
|
|
|
|
written++
|
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|
2019-07-31 06:09:38 +00:00
|
|
|
err := w.WriteByte('}')
|
|
|
|
written++
|
2017-12-21 20:23:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return written, err
|
|
|
|
}
|
|
|
|
return written, nil
|
|
|
|
}
|
|
|
|
|
2019-07-31 06:09:38 +00:00
|
|
|
// writeEscapedString replaces '\' by '\\', new line character by '\n', and - if
|
|
|
|
// includeDoubleQuote is true - '"' by '\"'.
|
2017-12-21 20:23:45 +00:00
|
|
|
var (
|
2019-07-31 06:09:38 +00:00
|
|
|
escaper = strings.NewReplacer("\\", `\\`, "\n", `\n`)
|
|
|
|
quotedEscaper = strings.NewReplacer("\\", `\\`, "\n", `\n`, "\"", `\"`)
|
2017-12-21 20:23:45 +00:00
|
|
|
)
|
|
|
|
|
2019-07-31 06:09:38 +00:00
|
|
|
func writeEscapedString(w enhancedWriter, v string, includeDoubleQuote bool) (int, error) {
|
2017-12-21 20:23:45 +00:00
|
|
|
if includeDoubleQuote {
|
2019-07-31 06:09:38 +00:00
|
|
|
return quotedEscaper.WriteString(w, v)
|
|
|
|
} else {
|
|
|
|
return escaper.WriteString(w, v)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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) {
|
|
|
|
switch {
|
|
|
|
case f == 1:
|
|
|
|
return 1, w.WriteByte('1')
|
|
|
|
case f == 0:
|
|
|
|
return 1, w.WriteByte('0')
|
|
|
|
case f == -1:
|
|
|
|
return w.WriteString("-1")
|
|
|
|
case math.IsNaN(f):
|
|
|
|
return w.WriteString("NaN")
|
|
|
|
case math.IsInf(f, +1):
|
|
|
|
return w.WriteString("+Inf")
|
|
|
|
case math.IsInf(f, -1):
|
|
|
|
return w.WriteString("-Inf")
|
|
|
|
default:
|
|
|
|
bp := numBufPool.Get().(*[]byte)
|
|
|
|
*bp = strconv.AppendFloat((*bp)[:0], f, 'g', -1, 64)
|
|
|
|
written, err := w.Write(*bp)
|
|
|
|
numBufPool.Put(bp)
|
|
|
|
return written, err
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|
2019-07-31 06:09:38 +00:00
|
|
|
}
|
2017-12-21 20:23:45 +00:00
|
|
|
|
2019-07-31 06:09:38 +00:00
|
|
|
// 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) {
|
|
|
|
bp := numBufPool.Get().(*[]byte)
|
|
|
|
*bp = strconv.AppendInt((*bp)[:0], i, 10)
|
|
|
|
written, err := w.Write(*bp)
|
|
|
|
numBufPool.Put(bp)
|
|
|
|
return written, err
|
2017-12-21 20:23:45 +00:00
|
|
|
}
|