Next advances the parser to the next sample. It returns (EntryInvalid, io.EOF) if no samples were read.
()
| 298 | // Next advances the parser to the next sample. |
| 299 | // It returns (EntryInvalid, io.EOF) if no samples were read. |
| 300 | func (p *PromParser) Next() (Entry, error) { |
| 301 | var err error |
| 302 | |
| 303 | p.start = p.l.i |
| 304 | p.offsets = p.offsets[:0] |
| 305 | |
| 306 | switch t := p.nextToken(); t { |
| 307 | case tEOF: |
| 308 | return EntryInvalid, io.EOF |
| 309 | case tLinebreak: |
| 310 | // Allow full blank lines. |
| 311 | return p.Next() |
| 312 | |
| 313 | case tHelp, tType: |
| 314 | switch t2 := p.nextToken(); t2 { |
| 315 | case tMName: |
| 316 | mStart := p.l.start |
| 317 | mEnd := p.l.i |
| 318 | if p.l.b[mStart] == '"' && p.l.b[mEnd-1] == '"' { |
| 319 | mStart++ |
| 320 | mEnd-- |
| 321 | } |
| 322 | p.offsets = append(p.offsets, mStart, mEnd) |
| 323 | default: |
| 324 | return EntryInvalid, p.parseError("expected metric name after "+t.String(), t2) |
| 325 | } |
| 326 | switch t2 := p.nextToken(); t2 { |
| 327 | case tText: |
| 328 | if len(p.l.buf()) > 1 { |
| 329 | p.text = p.l.buf()[1:] |
| 330 | } else { |
| 331 | p.text = []byte{} |
| 332 | } |
| 333 | default: |
| 334 | return EntryInvalid, fmt.Errorf("expected text in %s, got %v", t.String(), t2.String()) |
| 335 | } |
| 336 | switch t { |
| 337 | case tType: |
| 338 | switch s := yoloString(p.text); s { |
| 339 | case "counter": |
| 340 | p.mtype = model.MetricTypeCounter |
| 341 | case "gauge": |
| 342 | p.mtype = model.MetricTypeGauge |
| 343 | case "histogram": |
| 344 | p.mtype = model.MetricTypeHistogram |
| 345 | case "summary": |
| 346 | p.mtype = model.MetricTypeSummary |
| 347 | case "untyped": |
| 348 | p.mtype = model.MetricTypeUnknown |
| 349 | default: |
| 350 | return EntryInvalid, fmt.Errorf("invalid metric type %q", s) |
| 351 | } |
| 352 | case tHelp: |
| 353 | if !utf8.Valid(p.text) { |
| 354 | return EntryInvalid, fmt.Errorf("help text %q is not a valid utf8 string", p.text) |
| 355 | } |
| 356 | } |
| 357 | if t := p.nextToken(); t != tLinebreak { |