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)
| 101 | // and is not intended for high-performance applications. |
| 102 | // See: https://github.com/prometheus/common/issues/812 |
| 103 | func 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. |
| 138 | type protoDecoder struct { |