From ddfc43d4f78c8744416132ec0b9a66111d791370 Mon Sep 17 00:00:00 2001 From: Anders Swanson Date: Fri, 19 Sep 2025 08:35:21 -0700 Subject: [PATCH] Empty databases array disables metric Signed-off-by: Anders Swanson --- collector/default_metrics.toml | 2 +- collector/metrics.go | 19 +++++++++++++---- default-metrics.toml | 2 +- site/docs/configuration/custom-metrics.md | 26 +++++++++++------------ 4 files changed, 30 insertions(+), 19 deletions(-) diff --git a/collector/default_metrics.toml b/collector/default_metrics.toml index bdcfe64e..6cb043e1 100644 --- a/collector/default_metrics.toml +++ b/collector/default_metrics.toml @@ -114,7 +114,7 @@ context = "db_platform" labels = [ "inst_id", "platform_name" ] metricsdesc = { value = "Database platform" } request = ''' -SELECT platform_name, 1 as value +SELECT inst_id, platform_name, 1 as value FROM gv$database GROUP BY inst_id, platform_name ''' diff --git a/collector/metrics.go b/collector/metrics.go index 3d7a2409..6d5ef676 100644 --- a/collector/metrics.go +++ b/collector/metrics.go @@ -14,11 +14,11 @@ import ( // 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, d *Database) bool { - if len(metric.Databases) > 0 { - if !slices.Contains(metric.Databases, d.Name) { - return false - } + // If the metric isn't enabled for the database, don't scrape it. + if !metric.IsEnabledForDatabase(d) { + return false } + // Always scrape the metric if we don't have a current tick. if tick == nil { return true @@ -92,3 +92,14 @@ func (m *Metric) GetLabels() []string { } return labels } + +// IsEnabledForDatabase checks if a metric is enabled for a database. +// If the m.Databases slice is nil, the metric is enabled for all databases. +// If the m.Databases slice contains the database name, the metric is enabled for that database. +// Otherwise, the metric is disabled for all databases (non-nil, empty m.Databases slice) +func (m *Metric) IsEnabledForDatabase(d *Database) bool { + if m.Databases == nil || slices.Contains(m.Databases, d.Name) { + return true + } + return false +} diff --git a/default-metrics.toml b/default-metrics.toml index bdcfe64e..6cb043e1 100644 --- a/default-metrics.toml +++ b/default-metrics.toml @@ -114,7 +114,7 @@ context = "db_platform" labels = [ "inst_id", "platform_name" ] metricsdesc = { value = "Database platform" } request = ''' -SELECT platform_name, 1 as value +SELECT inst_id, platform_name, 1 as value FROM gv$database GROUP BY inst_id, platform_name ''' diff --git a/site/docs/configuration/custom-metrics.md b/site/docs/configuration/custom-metrics.md index 2b4a92cf..83cb1e6d 100644 --- a/site/docs/configuration/custom-metrics.md +++ b/site/docs/configuration/custom-metrics.md @@ -27,19 +27,19 @@ You may also use `--custom.metrics` flag followed by a comma separated list of T 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: -| Field Name | Description | Type | Required | Default | -|------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------|----------|-----------------------------------| -| context | Metric context, used to build metric FQN | String | Yes | | -| 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 | | -| metricsdesc | Mapping between field(s) in the request and comment(s) | Dictionary of Strings | Yes | | -| metricstype | Mapping between field(s) in the request and [Prometheus metric types](https://prometheus.io/docs/concepts/metric_types/) | Dictionary of Strings | No | | -| 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 | | -| fieldtoappend | Field from the request to append to the metric FQN. This field will **not** be included in the metric labels. | String | No | | -| request | Oracle database query to run for metrics scraping | String | Yes | | -| ignorezeroresult | Whether or not an error will be printed if the request does not return any results | Boolean | No | false | -| querytimeout | Oracle Database query timeout duration, e.g., 300ms, 0.5h | String duration | No | Value of query.timeout in seconds | -| scrapeinterval | Custom metric scrape interval, used if scrape.interval is provided, otherwise metrics are always scraped on request. | String duration | No | | -| databases | Optional array of databases to scrape from. If not specified, the metric is scraped from all databases. | Array of Strings | No | | +| Field Name | Description | Type | Required | Default | +|------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------|----------|-----------------------------------| +| context | Metric context, used to build metric FQN | String | Yes | | +| 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 | | +| metricsdesc | Mapping between field(s) in the request and comment(s) | Dictionary of Strings | Yes | | +| metricstype | Mapping between field(s) in the request and [Prometheus metric types](https://prometheus.io/docs/concepts/metric_types/) | Dictionary of Strings | No | | +| 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 | | +| fieldtoappend | Field from the request to append to the metric FQN. This field will **not** be included in the metric labels. | String | No | | +| request | Oracle database query to run for metrics scraping | String | Yes | | +| ignorezeroresult | Whether or not an error will be printed if the request does not return any results | Boolean | No | false | +| querytimeout | Oracle Database query timeout duration, e.g., 300ms, 0.5h | String duration | No | Value of query.timeout in seconds | +| scrapeinterval | Custom metric scrape interval, used if scrape.interval is provided, otherwise metrics are always scraped on request. | String duration | No | | +| 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 | | ### Example Metric Definition