New returns a new parser of the byte slice. This function no longer guarantees to return a valid parser. It only returns a valid parser if the supplied contentType and fallbackType allow. An error may also be returned if fallbackType had to be used or there was some other error parsing the supplie
(b []byte, contentType string, st *labels.SymbolTable, opts ParserOptions)
| 164 | // other error parsing the supplied Content-Type. |
| 165 | // If the returned parser is nil then the scrape must fail. |
| 166 | func New(b []byte, contentType string, st *labels.SymbolTable, opts ParserOptions) (Parser, error) { |
| 167 | if st == nil { |
| 168 | st = labels.NewSymbolTable() |
| 169 | } |
| 170 | |
| 171 | mediaType, err := extractMediaType(contentType, opts.FallbackContentType) |
| 172 | // err may be nil or something we want to warn about. |
| 173 | |
| 174 | var baseParser Parser |
| 175 | switch mediaType { |
| 176 | case "application/openmetrics-text": |
| 177 | baseParser = NewOpenMetricsParser(b, st, func(o *openMetricsParserOptions) { |
| 178 | o.skipSTSeries = opts.OpenMetricsSkipSTSeries |
| 179 | o.enableTypeAndUnitLabels = opts.EnableTypeAndUnitLabels |
| 180 | }) |
| 181 | case "application/vnd.google.protobuf": |
| 182 | return NewProtobufParser( |
| 183 | b, |
| 184 | opts.IgnoreNativeHistograms, |
| 185 | opts.KeepClassicOnClassicAndNativeHistograms, |
| 186 | opts.ConvertClassicHistogramsToNHCB, |
| 187 | opts.EnableTypeAndUnitLabels, |
| 188 | st, |
| 189 | ), err |
| 190 | case "text/plain": |
| 191 | baseParser = NewPromParser(b, st, opts.EnableTypeAndUnitLabels) |
| 192 | default: |
| 193 | return nil, err |
| 194 | } |
| 195 | |
| 196 | if baseParser != nil && opts.ConvertClassicHistogramsToNHCB { |
| 197 | baseParser = NewNHCBParser(baseParser, st, opts.KeepClassicOnClassicAndNativeHistograms) |
| 198 | } |
| 199 | |
| 200 | return baseParser, err |
| 201 | } |
| 202 | |
| 203 | // Entry represents the type of a parsed entry. |
| 204 | type Entry int |