MCPcopy
hub / github.com/prometheus/node_exporter / processFile

Method processFile

collector/textfile.go:288–315  ·  view source on GitHub ↗

processFile processes a single file, returning its modification time on success.

(dir, name string, ch chan<- prometheus.Metric)

Source from the content-addressed store, hash-verified

286
287// processFile processes a single file, returning its modification time on success.
288func (c *textFileCollector) processFile(dir, name string, ch chan<- prometheus.Metric) (*time.Time, map[string]*dto.MetricFamily, error) {
289 path := filepath.Join(dir, name)
290 f, err := os.Open(path)
291 if err != nil {
292 return nil, nil, fmt.Errorf("failed to open textfile data file %q: %w", path, err)
293 }
294 defer f.Close()
295
296 parser := expfmt.NewTextParser(model.LegacyValidation)
297 families, err := parser.TextToMetricFamilies(f)
298 if err != nil {
299 return nil, nil, fmt.Errorf("failed to parse textfile data from %q: %w", path, err)
300 }
301
302 if hasTimestamps(families) {
303 return nil, nil, fmt.Errorf("textfile %q contains unsupported client-side timestamps, skipping entire file", path)
304 }
305
306 // Only stat the file once it has been parsed and validated, so that
307 // a failure does not appear fresh.
308 stat, err := f.Stat()
309 if err != nil {
310 return nil, families, fmt.Errorf("failed to stat %q: %w", path, err)
311 }
312
313 t := stat.ModTime()
314 return &t, families, nil
315}
316
317// hasTimestamps returns true when metrics contain unsupported timestamps.
318func hasTimestamps(parsedFamilies map[string]*dto.MetricFamily) bool {

Callers 1

UpdateMethod · 0.95

Calls 2

hasTimestampsFunction · 0.85
CloseMethod · 0.65

Tested by

no test coverage detected