Skip to content

Commit 4b53adf

Browse files
authoredJan 23, 2025
Log errors when ignoreZeroResult = true (#173)
* Log errors when ignoreZeroResult = true --------- Signed-off-by: Anders Swanson <anders.swanson@oracle.com>
1 parent 72297da commit 4b53adf

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed
 

‎collector/collector.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric, tick *time.Time) {
360360
err = err1
361361
}
362362
errmutex.Unlock()
363-
if !metric.IgnoreZeroResult {
364-
// do not print repetitive error messages for metrics
365-
// with ignoreZeroResult set to true
363+
if shouldLogScrapeError(err, metric.IgnoreZeroResult) {
366364
level.Error(e.logger).Log("msg", "Error scraping metric",
367365
"Context", metric.Context,
368366
"MetricsDesc", fmt.Sprint(metric.MetricsDesc),
@@ -622,7 +620,9 @@ func (e *Exporter) scrapeGenericValues(db *sql.DB, ch chan<- prometheus.Metric,
622620
return err
623621
}
624622
if !ignoreZeroResult && metricsCount == 0 {
625-
return errors.New("no metrics found while parsing, query returned no rows")
623+
// a zero result error is returned for caller error identification.
624+
// https://github.com/oracle/oracle-db-appdev-monitoring/issues/168
625+
return newZeroResultError()
626626
}
627627
return err
628628
}

‎collector/errors.go

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) 2024, Oracle and/or its affiliates.
2+
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
3+
4+
package collector
5+
6+
import "errors"
7+
8+
type zeroResultError struct {
9+
err string
10+
}
11+
12+
func (z *zeroResultError) Error() string {
13+
return z.err
14+
}
15+
16+
func newZeroResultError() error {
17+
return &zeroResultError{
18+
err: "no metrics found while parsing, query returned no rows",
19+
}
20+
}
21+
22+
// shouldLogScrapeError returns false if the error is a zero result error and zero result errors are ignored.
23+
func shouldLogScrapeError(err error, isIgnoreZeroResult bool) bool {
24+
return !isIgnoreZeroResult || !errors.Is(err, newZeroResultError())
25+
}

0 commit comments

Comments
 (0)
Failed to load comments.