WriteToTextfile calls Gather on the provided Gatherer, encodes the result in the Prometheus text format, and writes it to a temporary file. Upon success, the temporary file is renamed to the provided filename. This is intended for use with the textfile collector of the node exporter. Note that the
(filename string, g Gatherer)
| 591 | // This is intended for use with the textfile collector of the node exporter. |
| 592 | // Note that the node exporter expects the filename to be suffixed with ".prom". |
| 593 | func WriteToTextfile(filename string, g Gatherer) error { |
| 594 | tmp, err := os.CreateTemp(filepath.Dir(filename), filepath.Base(filename)) |
| 595 | if err != nil { |
| 596 | return err |
| 597 | } |
| 598 | defer os.Remove(tmp.Name()) |
| 599 | |
| 600 | mfs, err := g.Gather() |
| 601 | if err != nil { |
| 602 | return err |
| 603 | } |
| 604 | for _, mf := range mfs { |
| 605 | if _, err := expfmt.MetricFamilyToText(tmp, mf); err != nil { |
| 606 | return err |
| 607 | } |
| 608 | } |
| 609 | if err := tmp.Close(); err != nil { |
| 610 | return err |
| 611 | } |
| 612 | |
| 613 | if err := os.Chmod(tmp.Name(), 0o644); err != nil { |
| 614 | return err |
| 615 | } |
| 616 | return os.Rename(tmp.Name(), filename) |
| 617 | } |
| 618 | |
| 619 | // processMetric is an internal helper method only used by the Gather method. |
| 620 | func processMetric( |