readMetricsFromFile reads metrics from a single Prometheus text format file.
(path string)
| 63 | |
| 64 | // readMetricsFromFile reads metrics from a single Prometheus text format file. |
| 65 | func readMetricsFromFile(path string) ([]*dto.MetricFamily, error) { |
| 66 | f, err := os.Open(path) |
| 67 | if err != nil { |
| 68 | return nil, xerrors.Errorf("can't open metrics file %s: %w", path, err) |
| 69 | } |
| 70 | defer f.Close() |
| 71 | |
| 72 | var metrics []*dto.MetricFamily |
| 73 | |
| 74 | decoder := expfmt.NewDecoder(f, expfmt.NewFormat(expfmt.TypeTextPlain)) |
| 75 | for { |
| 76 | var m dto.MetricFamily |
| 77 | err = decoder.Decode(&m) |
| 78 | if errors.Is(err, io.EOF) { |
| 79 | break |
| 80 | } else if err != nil { |
| 81 | return nil, xerrors.Errorf("decoding metrics from %s: %w", path, err) |
| 82 | } |
| 83 | metrics = append(metrics, &m) |
| 84 | } |
| 85 | |
| 86 | return metrics, nil |
| 87 | } |
| 88 | |
| 89 | // readAndMergeMetrics reads metrics from both generated and static files, |
| 90 | // merges them, and returns a sorted list. Generated metrics are produced |
no test coverage detected