MCPcopy
hub / github.com/prometheus/client_golang / WriteToTextfile

Function WriteToTextfile

prometheus/registry.go:593–617  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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".
593func 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.
620func processMetric(

Callers 1

TestWriteToTextfileFunction · 0.92

Calls 1

GatherMethod · 0.65

Tested by 1

TestWriteToTextfileFunction · 0.74