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)
| 83 | // applications. |
| 84 | // See: https://github.com/prometheus/common/issues/812 |
| 85 | func NewDecoder(r io.Reader, format Format) Decoder { |
| 86 | scheme := model.LegacyValidation |
| 87 | if format.ToEscapingScheme() == model.NoEscaping { |
| 88 | scheme = model.UTF8Validation |
| 89 | } |
| 90 | switch format.FormatType() { |
| 91 | case TypeProtoDelim: |
| 92 | return &protoDecoder{r: bufio.NewReader(r), s: scheme} |
| 93 | case TypeProtoText, TypeProtoCompact: |
| 94 | return &errDecoder{err: fmt.Errorf("format %s not supported for decoding", format)} |
| 95 | case TypeOpenMetrics: |
| 96 | _, params, err := mime.ParseMediaType(string(format)) |
| 97 | if err == nil && params["version"] == OpenMetricsVersion_2_0_0 { |
| 98 | return &errDecoder{err: fmt.Errorf("format %s not supported for decoding", format)} |
| 99 | } |
| 100 | } |
| 101 | return &textDecoder{r: r, s: scheme} |
| 102 | } |
| 103 | |
| 104 | // protoDecoder implements the Decoder interface for protocol buffers. |
| 105 | type protoDecoder struct { |