| 48 | } |
| 49 | |
| 50 | func (p *promTextParser) parseToSeries(text []byte) (Series, error) { |
| 51 | p.series.Reset() |
| 52 | |
| 53 | parser := textparse.NewPromParser(text, labels.NewSymbolTable()) |
| 54 | for { |
| 55 | entry, err := parser.Next() |
| 56 | if err != nil { |
| 57 | if errors.Is(err, io.EOF) { |
| 58 | break |
| 59 | } |
| 60 | if entry == textparse.EntryInvalid && strings.HasPrefix(err.Error(), "invalid metric type") { |
| 61 | continue |
| 62 | } |
| 63 | return nil, fmt.Errorf("failed to parse prometheus metrics: %v", err) |
| 64 | } |
| 65 | |
| 66 | switch entry { |
| 67 | case textparse.EntrySeries: |
| 68 | p.currSeries = p.currSeries[:0] |
| 69 | |
| 70 | parser.Metric(&p.currSeries) |
| 71 | |
| 72 | if p.sr != nil && !p.sr.Matches(p.currSeries) { |
| 73 | continue |
| 74 | } |
| 75 | |
| 76 | _, _, val := parser.Series() |
| 77 | p.series.Add(SeriesSample{Labels: copyLabels(p.currSeries), Value: val}) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | p.series.Sort() |
| 82 | |
| 83 | return p.series, nil |
| 84 | } |
| 85 | |
| 86 | var reSpace = regexp.MustCompile(`\s+`) |
| 87 | |