Source File
text_create.go
Belonging Package
github.com/prometheus/common/expfmt
package expfmt
import (
dto
)
type enhancedWriter interface {
io.Writer
WriteRune(r rune) (n int, err error)
WriteString(s string) (n int, err error)
WriteByte(c byte) error
}
const (
initialNumBufSize = 24
)
var (
bufPool = sync.Pool{
New: func() interface{} {
return bufio.NewWriter(ioutil.Discard)
},
}
numBufPool = sync.Pool{
New: func() interface{} {
:= make([]byte, 0, initialNumBufSize)
return &
},
}
)
if .Help != nil {
, = .WriteString("# HELP ")
+=
if != nil {
return
}
, = .WriteString()
+=
if != nil {
return
}
= .WriteByte(' ')
++
if != nil {
return
}
, = writeEscapedString(, *.Help, false)
+=
if != nil {
return
}
= .WriteByte('\n')
++
if != nil {
return
}
}
, = .WriteString("# TYPE ")
+=
if != nil {
return
}
, = .WriteString()
+=
if != nil {
return
}
:= .GetType()
switch {
case dto.MetricType_COUNTER:
, = .WriteString(" counter\n")
case dto.MetricType_GAUGE:
, = .WriteString(" gauge\n")
case dto.MetricType_SUMMARY:
, = .WriteString(" summary\n")
case dto.MetricType_UNTYPED:
, = .WriteString(" untyped\n")
case dto.MetricType_HISTOGRAM:
, = .WriteString(" histogram\n")
default:
return , fmt.Errorf("unknown metric type %s", .String())
}
+=
if != nil {
return
}
for , := range .Metric {
switch {
case dto.MetricType_COUNTER:
if .Counter == nil {
return , fmt.Errorf(
"expected counter in metric %s %s", , ,
)
}
, = writeSample(
, , "", , "", 0,
.Counter.GetValue(),
)
case dto.MetricType_GAUGE:
if .Gauge == nil {
return , fmt.Errorf(
"expected gauge in metric %s %s", , ,
)
}
, = writeSample(
, , "", , "", 0,
.Gauge.GetValue(),
)
case dto.MetricType_UNTYPED:
if .Untyped == nil {
return , fmt.Errorf(
"expected untyped in metric %s %s", , ,
)
}
, = writeSample(
, , "", , "", 0,
.Untyped.GetValue(),
)
case dto.MetricType_SUMMARY:
if .Summary == nil {
return , fmt.Errorf(
"expected summary in metric %s %s", , ,
)
}
for , := range .Summary.Quantile {
, = writeSample(
, , "", ,
model.QuantileLabel, .GetQuantile(),
.GetValue(),
)
+=
if != nil {
return
}
}
, = writeSample(
, , "_sum", , "", 0,
.Summary.GetSampleSum(),
)
+=
if != nil {
return
}
, = writeSample(
, , "_count", , "", 0,
float64(.Summary.GetSampleCount()),
)
case dto.MetricType_HISTOGRAM:
if .Histogram == nil {
return , fmt.Errorf(
"expected histogram in metric %s %s", , ,
)
}
:= false
for , := range .Histogram.Bucket {
, = writeSample(
, , "_bucket", ,
model.BucketLabel, .GetUpperBound(),
float64(.GetCumulativeCount()),
)
+=
if != nil {
return
}
if math.IsInf(.GetUpperBound(), +1) {
= true
}
}
if ! {
, = writeSample(
, , "_bucket", ,
model.BucketLabel, math.Inf(+1),
float64(.Histogram.GetSampleCount()),
)
+=
if != nil {
return
}
}
, = writeSample(
, , "_sum", , "", 0,
.Histogram.GetSampleSum(),
)
+=
if != nil {
return
}
, = writeSample(
, , "_count", , "", 0,
float64(.Histogram.GetSampleCount()),
)
default:
return , fmt.Errorf(
"unexpected type in metric %s %s", , ,
)
}
+=
if != nil {
return
}
}
return
}
func (
enhancedWriter,
, string,
*dto.Metric,
string, float64,
float64,
) (int, error) {
var int
, := .WriteString()
+=
if != nil {
return ,
}
if != "" {
, = .WriteString()
+=
if != nil {
return ,
}
}
, = writeLabelPairs(
, .Label, , ,
)
+=
if != nil {
return ,
}
= .WriteByte(' ')
++
if != nil {
return ,
}
, = writeFloat(, )
+=
if != nil {
return ,
}
if .TimestampMs != nil {
= .WriteByte(' ')
++
if != nil {
return ,
}
, = writeInt(, *.TimestampMs)
+=
if != nil {
return ,
}
}
= .WriteByte('\n')
++
if != nil {
return ,
}
return , nil
}
func (
enhancedWriter,
[]*dto.LabelPair,
string, float64,
) (int, error) {
if len() == 0 && == "" {
return 0, nil
}
var (
int
byte = '{'
)
for , := range {
:= .WriteByte()
++
if != nil {
return ,
}
, := .WriteString(.GetName())
+=
if != nil {
return ,
}
, = .WriteString(`="`)
+=
if != nil {
return ,
}
, = writeEscapedString(, .GetValue(), true)
+=
if != nil {
return ,
}
= .WriteByte('"')
++
if != nil {
return ,
}
= ','
}
if != "" {
:= .WriteByte()
++
if != nil {
return ,
}
, := .WriteString()
+=
if != nil {
return ,
}
, = .WriteString(`="`)
+=
if != nil {
return ,
}
, = writeFloat(, )
+=
if != nil {
return ,
}
= .WriteByte('"')
++
if != nil {
return ,
}
}
:= .WriteByte('}')
++
if != nil {
return ,
}
return , nil
}
var (
escaper = strings.NewReplacer("\\", `\\`, "\n", `\n`)
quotedEscaper = strings.NewReplacer("\\", `\\`, "\n", `\n`, "\"", `\"`)
)
func ( enhancedWriter, string, bool) (int, error) {
if {
return quotedEscaper.WriteString(, )
} else {
return escaper.WriteString(, )
}
}
func ( enhancedWriter, float64) (int, error) {
switch {
case == 1:
return 1, .WriteByte('1')
case == 0:
return 1, .WriteByte('0')
case == -1:
return .WriteString("-1")
case math.IsNaN():
return .WriteString("NaN")
case math.IsInf(, +1):
return .WriteString("+Inf")
case math.IsInf(, -1):
return .WriteString("-Inf")
default:
:= numBufPool.Get().(*[]byte)
* = strconv.AppendFloat((*)[:0], , 'g', -1, 64)
, := .Write(*)
numBufPool.Put()
return ,
}
}
func ( enhancedWriter, int64) (int, error) {
:= numBufPool.Get().(*[]byte)
* = strconv.AppendInt((*)[:0], , 10)
, := .Write(*)
numBufPool.Put()
return ,
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |