Skip to content

Commit 99d1373

Browse files
Empty databases array disables metric (#362)
Signed-off-by: Anders Swanson <[email protected]>
1 parent 4e3bf65 commit 99d1373

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

collector/default_metrics.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ context = "db_platform"
114114
labels = [ "inst_id", "platform_name" ]
115115
metricsdesc = { value = "Database platform" }
116116
request = '''
117-
SELECT platform_name, 1 as value
117+
SELECT inst_id, platform_name, 1 as value
118118
FROM gv$database
119119
GROUP BY inst_id, platform_name
120120
'''

collector/metrics.go

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414
// and the time since the last scrape is less than the custom scrape interval.
1515
// If there is no tick time or last known tick, the metric is always scraped.
1616
func (e *Exporter) isScrapeMetric(tick *time.Time, metric *Metric, d *Database) bool {
17-
if len(metric.Databases) > 0 {
18-
if !slices.Contains(metric.Databases, d.Name) {
19-
return false
20-
}
17+
// If the metric isn't enabled for the database, don't scrape it.
18+
if !metric.IsEnabledForDatabase(d) {
19+
return false
2120
}
21+
2222
// Always scrape the metric if we don't have a current tick.
2323
if tick == nil {
2424
return true
@@ -92,3 +92,14 @@ func (m *Metric) GetLabels() []string {
9292
}
9393
return labels
9494
}
95+
96+
// IsEnabledForDatabase checks if a metric is enabled for a database.
97+
// If the m.Databases slice is nil, the metric is enabled for all databases.
98+
// If the m.Databases slice contains the database name, the metric is enabled for that database.
99+
// Otherwise, the metric is disabled for all databases (non-nil, empty m.Databases slice)
100+
func (m *Metric) IsEnabledForDatabase(d *Database) bool {
101+
if m.Databases == nil || slices.Contains(m.Databases, d.Name) {
102+
return true
103+
}
104+
return false
105+
}

default-metrics.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ context = "db_platform"
114114
labels = [ "inst_id", "platform_name" ]
115115
metricsdesc = { value = "Database platform" }
116116
request = '''
117-
SELECT platform_name, 1 as value
117+
SELECT inst_id, platform_name, 1 as value
118118
FROM gv$database
119119
GROUP BY inst_id, platform_name
120120
'''

site/docs/configuration/custom-metrics.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,19 @@ You may also use `--custom.metrics` flag followed by a comma separated list of T
2727

2828
Metrics files must contain a series of `[[metric]]` definitions, in TOML, or the equivalent definition in a YAML file. Each metric definition must follow the exporter's metric schema:
2929

30-
| Field Name | Description | Type | Required | Default |
31-
|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------|----------|-----------------------------------|
32-
| context | Metric context, used to build metric FQN | String | Yes | |
33-
| labels | Metric labels, which must match column names in the query. Any column that is not a label will be parsed as a metric | Array of Strings | No | |
34-
| metricsdesc | Mapping between field(s) in the request and comment(s) | Dictionary of Strings | Yes | |
35-
| metricstype | Mapping between field(s) in the request and [Prometheus metric types](https://prometheus.io/docs/concepts/metric_types/) | Dictionary of Strings | No | |
36-
| metricsbuckets | Split [histogram](https://prometheus.io/docs/concepts/metric_types/#histogram) metric types into buckets based on value ([example](https://github.com/oracle/oracle-db-appdev-monitoring/blob/main/custom-metrics-example/metric-histogram-example.toml)) | Dictionary of String dictionaries | No | |
37-
| fieldtoappend | Field from the request to append to the metric FQN. This field will **not** be included in the metric labels. | String | No | |
38-
| request | Oracle database query to run for metrics scraping | String | Yes | |
39-
| ignorezeroresult | Whether or not an error will be printed if the request does not return any results | Boolean | No | false |
40-
| querytimeout | Oracle Database query timeout duration, e.g., 300ms, 0.5h | String duration | No | Value of query.timeout in seconds |
41-
| scrapeinterval | Custom metric scrape interval, used if scrape.interval is provided, otherwise metrics are always scraped on request. | String duration | No | |
42-
| databases | Optional array of databases to scrape from. If not specified, the metric is scraped from all databases. | Array of Strings | No | |
30+
| Field Name | Description | Type | Required | Default |
31+
|------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------|----------|-----------------------------------|
32+
| context | Metric context, used to build metric FQN | String | Yes | |
33+
| labels | Metric labels, which must match column names in the query. Any column that is not a label will be parsed as a metric | Array of Strings | No | |
34+
| metricsdesc | Mapping between field(s) in the request and comment(s) | Dictionary of Strings | Yes | |
35+
| metricstype | Mapping between field(s) in the request and [Prometheus metric types](https://prometheus.io/docs/concepts/metric_types/) | Dictionary of Strings | No | |
36+
| metricsbuckets | Split [histogram](https://prometheus.io/docs/concepts/metric_types/#histogram) metric types into buckets based on value ([example](https://github.com/oracle/oracle-db-appdev-monitoring/blob/main/custom-metrics-example/metric-histogram-example.toml)) | Dictionary of String dictionaries | No | |
37+
| fieldtoappend | Field from the request to append to the metric FQN. This field will **not** be included in the metric labels. | String | No | |
38+
| request | Oracle database query to run for metrics scraping | String | Yes | |
39+
| ignorezeroresult | Whether or not an error will be printed if the request does not return any results | Boolean | No | false |
40+
| querytimeout | Oracle Database query timeout duration, e.g., 300ms, 0.5h | String duration | No | Value of query.timeout in seconds |
41+
| scrapeinterval | Custom metric scrape interval, used if scrape.interval is provided, otherwise metrics are always scraped on request. | String duration | No | |
42+
| databases | Array of databases the metric will be scraped from, using the database name from the exporter config file. If not present, the metric is scraped from all databases. If the databases array is empty (`databases = []`) the metric will not be scraped, effectively being disabled. | Array of Strings | No | |
4343

4444
### Example Metric Definition
4545

0 commit comments

Comments
 (0)