-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathmetrics.go
67 lines (61 loc) · 2.23 KB
/
metrics.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright (c) 2024, 2025, Oracle and/or its affiliates.
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl
package collector
import (
"strconv"
"strings"
"time"
)
// isScrapeMetric returns true if a metric should be scraped. Metrics may not be scraped if they have a custom scrape interval,
// and the time since the last scrape is less than the custom scrape interval.
// If there is no tick time or last known tick, the metric is always scraped.
func (e *Exporter) isScrapeMetric(tick *time.Time, metric Metric) bool {
// Always scrape the metric if we don't have a current or last known tick.
if tick == nil || e.lastTick == nil {
return true
}
// If the metric doesn't have a custom scrape interval, scrape it.
interval, ok := e.getScrapeInterval(metric.Context, metric.ScrapeInterval)
if !ok {
return true
}
// If the metric's scrape interval is less than the time elapsed since the last scrape,
// we should scrape the metric.
return interval < tick.Sub(*e.lastTick)
}
func (e *Exporter) getScrapeInterval(context, scrapeInterval string) (time.Duration, bool) {
if len(scrapeInterval) > 0 {
si, err := time.ParseDuration(scrapeInterval)
if err != nil {
e.logger.Error("Unable to convert scrapeinterval to duration (metric=" + context + ")")
return 0, false
}
return si, true
}
return 0, false
}
func (e *Exporter) getQueryTimeout(metric Metric) time.Duration {
if len(metric.QueryTimeout) > 0 {
qt, err := time.ParseDuration(metric.QueryTimeout)
if err != nil {
e.logger.Error("Unable to convert querytimeout to duration (metric=" + metric.Context + ")")
return time.Duration(e.config.QueryTimeout) * time.Second
}
return qt
}
return time.Duration(e.config.QueryTimeout) * time.Second
}
func (e *Exporter) parseFloat(metric, metricHelp string, row map[string]string) (float64, bool) {
value, ok := row[metric]
if !ok || value == "<nil>" {
// treat nil value as 0
return 0.0, ok
}
valueFloat, err := strconv.ParseFloat(strings.TrimSpace(value), 64)
if err != nil {
e.logger.Error("Unable to convert current value to float (metric=" + metric +
",metricHelp=" + metricHelp + ",value=<" + row[metric] + ">)")
return -1, false
}
return valueFloat, true
}