Skip to content

Commit ebddab4

Browse files
authored
collector/textfile: Avoid inconsistent help-texts (#2962)
Avoid metrics with inconsistent help-texts. The earlier behaviour has been preserved in the sense that the first encountered instance is still used to generate metrics, whereas the subsequent inconsistent ones are ignored along with a few peripheral changes. ``` # HELP node_scrape_collector_duration_seconds node_exporter: Duration of a collector scrape. #TYPE node_scrape_collector_duration_seconds gauge node_scrape_collector_duration_seconds{collector="textfile"} 0.0004005 # HELP node_scrape_collector_success node_exporter: Whether a collector succeeded. # TYPE node_scrape_collector_success gauge node_scrape_collector_success{collector="textfile"} 1 # HELP node_textfile_mtime_seconds Unixtime mtime of textfiles successfully read. # TYPE node_textfile_mtime_seconds gauge node_textfile_mtime_seconds{file="/Users/rexagod/repositories/misc/node_exporter/ne-bar.prom"} 1.710812009e+09 node_textfile_mtime_seconds{file="/Users/rexagod/repositories/misc/node_exporter/ne-foo.prom"} 1.710811982e+09 # HELP node_textfile_scrape_error 1 if there was an error opening or reading a file, 0 otherwise # TYPE node_textfile_scrape_error gauge node_textfile_scrape_error 1 # HELP promhttp_metric_handler_errors_total Total number of internal errors encountered by the promhttp metric handler. # TYPE promhttp_metric_handler_errors_total counter promhttp_metric_handler_errors_total{cause="encoding"} 0 promhttp_metric_handler_errors_total{cause="gathering"} 0 # HELP promhttp_metric_handler_requests_in_flight Current number of scrapes being served. # TYPE promhttp_metric_handler_requests_in_flight gauge promhttp_metric_handler_requests_in_flight 1 # HELP promhttp_metric_handler_requests_total Total number of scrapes by HTTP status code. # TYPE promhttp_metric_handler_requests_total counter promhttp_metric_handler_requests_total{code="200"} 0 promhttp_metric_handler_requests_total{code="500"} 0 promhttp_metric_handler_requests_total{code="503"} 0 # HELP tau_infrastructure_performing_maintenance_task At what timestamp a given task started or stopped, the last time it was run. # TYPE tau_infrastructure_performing_maintenance_task gauge tau_infrastructure_performing_maintenance_task{main_task="nightly",start_or_stop="start",sub_task="main"} 1.64728080198446e+09 ``` Fixes: #2317 Signed-off-by: Pranshu Srivastava <[email protected]>
1 parent 641cf2c commit ebddab4

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

collector/fixtures/textfile/metrics_merge_different_help.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ node_textfile_mtime_seconds{file="fixtures/textfile/metrics_merge_different_help
88
node_textfile_mtime_seconds{file="fixtures/textfile/metrics_merge_different_help/b.prom"} 1
99
# HELP node_textfile_scrape_error 1 if there was an error opening or reading a file, 0 otherwise
1010
# TYPE node_textfile_scrape_error gauge
11-
node_textfile_scrape_error 0
11+
node_textfile_scrape_error 1

collector/textfile.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,7 @@ func (c *textFileCollector) Update(ch chan<- prometheus.Metric) error {
193193
var errored bool
194194
var parsedFamilies []*dto.MetricFamily
195195
metricsNamesToFiles := map[string][]string{}
196+
metricsNamesToHelpTexts := map[string][2]string{}
196197

197198
paths, err := filepath.Glob(c.path)
198199
if err != nil || len(paths) == 0 {
@@ -218,6 +219,23 @@ func (c *textFileCollector) Update(ch chan<- prometheus.Metric) error {
218219
mtime, families, err := c.processFile(path, f.Name(), ch)
219220

220221
for _, mf := range families {
222+
// Check for metrics with inconsistent help texts and take the first help text occurrence.
223+
if helpTexts, seen := metricsNamesToHelpTexts[*mf.Name]; seen {
224+
if mf.Help != nil && helpTexts[0] != *mf.Help || helpTexts[1] != "" {
225+
metricsNamesToHelpTexts[*mf.Name] = [2]string{helpTexts[0], *mf.Help}
226+
errored = true
227+
level.Error(c.logger).Log("msg", "inconsistent metric help text",
228+
"metric", *mf.Name,
229+
"original_help_text", helpTexts[0],
230+
"new_help_text", *mf.Help,
231+
// Only the first file path will be recorded in case of two or more inconsistent help texts.
232+
"file", metricsNamesToFiles[*mf.Name][0])
233+
continue
234+
}
235+
}
236+
if mf.Help != nil {
237+
metricsNamesToHelpTexts[*mf.Name] = [2]string{*mf.Help}
238+
}
221239
metricsNamesToFiles[*mf.Name] = append(metricsNamesToFiles[*mf.Name], metricsFilePath)
222240
parsedFamilies = append(parsedFamilies, mf)
223241
}

0 commit comments

Comments
 (0)