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

Function NewDecoder

expfmt/decode.go:103–135  ·  view source on GitHub ↗

NewDecoder returns a new decoder based on the given input format. Metric names are validated based on the provided Format -- if the format requires escaping, traditional Prometheus validity checking is used. Otherwise, names are checked for UTF-8 validity. Supported formats include delimited protobu

(r io.Reader, format Format)

Source from the content-addressed store, hash-verified

101// and is not intended for high-performance applications.
102// See: https://github.com/prometheus/common/issues/812
103func NewDecoder(r io.Reader, format Format) Decoder {
104 scheme := model.LegacyValidation
105 if format.ToEscapingScheme() == model.NoEscaping {
106 scheme = model.UTF8Validation
107 }
108 switch format.FormatType() {
109 case TypeProtoDelim:
110 return &protoDecoder{r: bufio.NewReader(r), s: scheme}
111 case TypeProtoText, TypeProtoCompact:
112 return &errDecoder{err: fmt.Errorf("format %s not supported for decoding", format)}
113 case TypeOpenMetrics:
114 _, params, err := mime.ParseMediaType(string(format))
115 if err != nil {
116 return &errDecoder{err: fmt.Errorf("format %s not supported for decoding", format)}
117 }
118 switch params["version"] {
119 case "", OpenMetricsVersion_0_0_1, OpenMetricsVersion_1_0_0:
120 return &openMetricsDecoder{p: newOpenMetricsParser(r, scheme)}
121 case OpenMetricsVersion_2_0_0_rc, OpenMetricsVersion_2_0_0:
122 // format.ToEscapingScheme() defaults to UnderscoreEscaping when no escaping
123 // parameter is set, whereas OpenMetrics 2.0 supports UTF-8 names by default
124 // unless a non-UTF-8 escaping scheme is explicitly requested.
125 om2Scheme := model.UTF8Validation
126 if _, hasEscaping := params[model.EscapingKey]; hasEscaping && format.ToEscapingScheme() != model.NoEscaping {
127 om2Scheme = model.LegacyValidation
128 }
129 return &openMetrics20Decoder{p: newOpenMetrics20Parser(r, om2Scheme)}
130 default:
131 return &errDecoder{err: fmt.Errorf("format %s not supported for decoding", format)}
132 }
133 }
134 return &textDecoder{r: r, s: scheme}
135}
136
137// protoDecoder implements the Decoder interface for protocol buffers.
138type protoDecoder struct {

Callers 12

TestNewDecoderFunction · 0.85
TestOpenMetricsDecoderFunction · 0.85
TestOpenMetrics20DecoderFunction · 0.85
TestDottedEncodeFunction · 0.85
decodeAllOpenMetricsFunction · 0.85
decodeOpenMetricsErrFunction · 0.85
TestOpenMetricsStreamingFunction · 0.85
decodeOpenMetrics20ToMapFunction · 0.85

Calls 4

newOpenMetricsParserFunction · 0.85
newOpenMetrics20ParserFunction · 0.85
ToEscapingSchemeMethod · 0.80
FormatTypeMethod · 0.80

Tested by 12

TestNewDecoderFunction · 0.68
TestOpenMetricsDecoderFunction · 0.68
TestOpenMetrics20DecoderFunction · 0.68
TestDottedEncodeFunction · 0.68
decodeAllOpenMetricsFunction · 0.68
decodeOpenMetricsErrFunction · 0.68
TestOpenMetricsStreamingFunction · 0.68
decodeOpenMetrics20ToMapFunction · 0.68