Source File
decode.go
Belonging Package
github.com/prometheus/common/expfmt
package expfmt
import (
dto
)
type Decoder interface {
Decode(*dto.MetricFamily) error
}
func ( http.Header) Format {
:= .Get(hdrContentType)
, , := mime.ParseMediaType()
if != nil {
return FmtUnknown
}
const = "text/plain"
switch {
case ProtoType:
if , := ["proto"]; && != ProtoProtocol {
return FmtUnknown
}
if , := ["encoding"]; && != "delimited" {
return FmtUnknown
}
return FmtProtoDelim
case :
if , := ["version"]; && != TextVersion {
return FmtUnknown
}
return FmtText
}
return FmtUnknown
}
func ( io.Reader, Format) Decoder {
switch {
case FmtProtoDelim:
return &protoDecoder{r: }
}
return &textDecoder{r: }
}
type protoDecoder struct {
r io.Reader
}
func ( *protoDecoder) ( *dto.MetricFamily) error {
, := pbutil.ReadDelimited(.r, )
if != nil {
return
}
if !model.IsValidMetricName(model.LabelValue(.GetName())) {
return fmt.Errorf("invalid metric name %q", .GetName())
}
for , := range .GetMetric() {
if == nil {
continue
}
for , := range .GetLabel() {
if == nil {
continue
}
if !model.LabelValue(.GetValue()).IsValid() {
return fmt.Errorf("invalid label value %q", .GetValue())
}
if !model.LabelName(.GetName()).IsValid() {
return fmt.Errorf("invalid label name %q", .GetName())
}
}
}
return nil
}
type textDecoder struct {
r io.Reader
p TextParser
fams []*dto.MetricFamily
}
type SampleDecoder struct {
Dec Decoder
Opts *DecodeOptions
f dto.MetricFamily
}
func ( *DecodeOptions, ...*dto.MetricFamily) (model.Vector, error) {
var (
model.Vector
error
)
for , := range {
, := extractSamples(, )
if != nil {
=
continue
}
= append(, ...)
}
return ,
}
func ( *dto.MetricFamily, *DecodeOptions) (model.Vector, error) {
switch .GetType() {
case dto.MetricType_COUNTER:
return extractCounter(, ), nil
case dto.MetricType_GAUGE:
return extractGauge(, ), nil
case dto.MetricType_SUMMARY:
return extractSummary(, ), nil
case dto.MetricType_UNTYPED:
return extractUntyped(, ), nil
case dto.MetricType_HISTOGRAM:
return extractHistogram(, ), nil
}
return nil, fmt.Errorf("expfmt.extractSamples: unknown metric family type %v", .GetType())
}
func ( *DecodeOptions, *dto.MetricFamily) model.Vector {
:= make(model.Vector, 0, len(.Metric))
for , := range .Metric {
if .Counter == nil {
continue
}
:= make(model.LabelSet, len(.Label)+1)
for , := range .Label {
[model.LabelName(.GetName())] = model.LabelValue(.GetValue())
}
[model.MetricNameLabel] = model.LabelValue(.GetName())
:= &model.Sample{
Metric: model.Metric(),
Value: model.SampleValue(.Counter.GetValue()),
}
if .TimestampMs != nil {
.Timestamp = model.TimeFromUnixNano(*.TimestampMs * 1000000)
} else {
.Timestamp = .Timestamp
}
= append(, )
}
return
}
func ( *DecodeOptions, *dto.MetricFamily) model.Vector {
:= make(model.Vector, 0, len(.Metric))
for , := range .Metric {
if .Gauge == nil {
continue
}
:= make(model.LabelSet, len(.Label)+1)
for , := range .Label {
[model.LabelName(.GetName())] = model.LabelValue(.GetValue())
}
[model.MetricNameLabel] = model.LabelValue(.GetName())
:= &model.Sample{
Metric: model.Metric(),
Value: model.SampleValue(.Gauge.GetValue()),
}
if .TimestampMs != nil {
.Timestamp = model.TimeFromUnixNano(*.TimestampMs * 1000000)
} else {
.Timestamp = .Timestamp
}
= append(, )
}
return
}
func ( *DecodeOptions, *dto.MetricFamily) model.Vector {
:= make(model.Vector, 0, len(.Metric))
for , := range .Metric {
if .Untyped == nil {
continue
}
:= make(model.LabelSet, len(.Label)+1)
for , := range .Label {
[model.LabelName(.GetName())] = model.LabelValue(.GetValue())
}
[model.MetricNameLabel] = model.LabelValue(.GetName())
:= &model.Sample{
Metric: model.Metric(),
Value: model.SampleValue(.Untyped.GetValue()),
}
if .TimestampMs != nil {
.Timestamp = model.TimeFromUnixNano(*.TimestampMs * 1000000)
} else {
.Timestamp = .Timestamp
}
= append(, )
}
return
}
func ( *DecodeOptions, *dto.MetricFamily) model.Vector {
:= make(model.Vector, 0, len(.Metric))
for , := range .Metric {
if .Summary == nil {
continue
}
:= .Timestamp
if .TimestampMs != nil {
= model.TimeFromUnixNano(*.TimestampMs * 1000000)
}
for , := range .Summary.Quantile {
:= make(model.LabelSet, len(.Label)+2)
for , := range .Label {
[model.LabelName(.GetName())] = model.LabelValue(.GetValue())
[model.LabelName(model.QuantileLabel)] = model.LabelValue(fmt.Sprint(.GetQuantile()))
[model.MetricNameLabel] = model.LabelValue(.GetName())
= append(, &model.Sample{
Metric: model.Metric(),
Value: model.SampleValue(.GetValue()),
Timestamp: ,
})
}
:= make(model.LabelSet, len(.Label)+1)
for , := range .Label {
[model.LabelName(.GetName())] = model.LabelValue(.GetValue())
}
[model.MetricNameLabel] = model.LabelValue(.GetName() + "_sum")
= append(, &model.Sample{
Metric: model.Metric(),
Value: model.SampleValue(.Summary.GetSampleSum()),
Timestamp: ,
})
= make(model.LabelSet, len(.Label)+1)
for , := range .Label {
[model.LabelName(.GetName())] = model.LabelValue(.GetValue())
}
[model.MetricNameLabel] = model.LabelValue(.GetName() + "_count")
= append(, &model.Sample{
Metric: model.Metric(),
Value: model.SampleValue(.Summary.GetSampleCount()),
Timestamp: ,
})
}
return
}
func ( *DecodeOptions, *dto.MetricFamily) model.Vector {
:= make(model.Vector, 0, len(.Metric))
for , := range .Metric {
if .Histogram == nil {
continue
}
:= .Timestamp
if .TimestampMs != nil {
= model.TimeFromUnixNano(*.TimestampMs * 1000000)
}
:= false
for , := range .Histogram.Bucket {
:= make(model.LabelSet, len(.Label)+2)
for , := range .Label {
[model.LabelName(.GetName())] = model.LabelValue(.GetValue())
}
[model.LabelName(model.BucketLabel)] = model.LabelValue(fmt.Sprint(.GetUpperBound()))
[model.MetricNameLabel] = model.LabelValue(.GetName() + "_bucket")
if math.IsInf(.GetUpperBound(), +1) {
= true
}
= append(, &model.Sample{
Metric: model.Metric(),
Value: model.SampleValue(.GetCumulativeCount()),
Timestamp: ,
})
}
:= make(model.LabelSet, len(.Label)+1)
for , := range .Label {
[model.LabelName(.GetName())] = model.LabelValue(.GetValue())
}
[model.MetricNameLabel] = model.LabelValue(.GetName() + "_sum")
= append(, &model.Sample{
Metric: model.Metric(),
Value: model.SampleValue(.Histogram.GetSampleSum()),
Timestamp: ,
})
= make(model.LabelSet, len(.Label)+1)
for , := range .Label {
[model.LabelName(.GetName())] = model.LabelValue(.GetValue())
}
[model.MetricNameLabel] = model.LabelValue(.GetName() + "_count")
:= &model.Sample{
Metric: model.Metric(),
Value: model.SampleValue(.Histogram.GetSampleCount()),
Timestamp: ,
}
= append(, )
:= make(model.LabelSet, len(.Label)+2)
for , := range .Label {
[model.LabelName(.GetName())] = model.LabelValue(.GetValue())
}
[model.LabelName(model.BucketLabel)] = model.LabelValue("+Inf")
[model.MetricNameLabel] = model.LabelValue(.GetName() + "_bucket")
= append(, &model.Sample{
Metric: model.Metric(),
Value: .Value,
Timestamp: ,
})
}
}
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. |