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

Method Update

collector/textfile.go:183–285  ·  view source on GitHub ↗

Update implements the Collector interface.

(ch chan<- prometheus.Metric)

Source from the content-addressed store, hash-verified

181
182// Update implements the Collector interface.
183func (c *textFileCollector) Update(ch chan<- prometheus.Metric) error {
184 // Iterate over files and accumulate their metrics, but also track any
185 // parsing errors so an error metric can be reported.
186 var errored bool
187 var parsedFamilies []*dto.MetricFamily
188 metricsNamesToFiles := map[string][]string{}
189 metricsNamesToHelpTexts := map[string][2]string{}
190
191 paths := []string{}
192 for _, glob := range c.paths {
193 ps, err := filepath.Glob(glob)
194 if err != nil || len(ps) == 0 {
195 // not glob or not accessible path either way assume single
196 // directory and let os.ReadDir handle it
197 ps = []string{glob}
198 }
199 paths = append(paths, ps...)
200 }
201
202 mtimes := make(map[string]time.Time)
203 for _, path := range paths {
204 files, err := os.ReadDir(path)
205 if err != nil && path != "" {
206 errored = true
207 c.logger.Error("failed to read textfile collector directory", "path", path, "err", err)
208 }
209
210 for _, f := range files {
211 metricsFilePath := filepath.Join(path, f.Name())
212 if !strings.HasSuffix(f.Name(), ".prom") {
213 continue
214 }
215
216 mtime, families, err := c.processFile(path, f.Name(), ch)
217
218 for _, mf := range families {
219 // Check for metrics with inconsistent help texts and take the first help text occurrence.
220 if helpTexts, seen := metricsNamesToHelpTexts[*mf.Name]; seen {
221 if mf.Help != nil && helpTexts[0] != *mf.Help || helpTexts[1] != "" {
222 metricsNamesToHelpTexts[*mf.Name] = [2]string{helpTexts[0], *mf.Help}
223 errored = true
224 c.logger.Error("inconsistent metric help text",
225 "metric", *mf.Name,
226 "original_help_text", helpTexts[0],
227 "new_help_text", *mf.Help,
228 // Only the first file path will be recorded in case of two or more inconsistent help texts.
229 "file", metricsNamesToFiles[*mf.Name][0])
230 continue
231 }
232 }
233 if mf.Help != nil {
234 metricsNamesToHelpTexts[*mf.Name] = [2]string{*mf.Help}
235 }
236 metricsNamesToFiles[*mf.Name] = append(metricsNamesToFiles[*mf.Name], metricsFilePath)
237 parsedFamilies = append(parsedFamilies, mf)
238 }
239
240 if err != nil {

Callers

nothing calls this directly

Calls 3

processFileMethod · 0.95
exportMTimesMethod · 0.95
convertMetricFamilyFunction · 0.85

Tested by

no test coverage detected