Skip to content

Commit dfb9ca5

Browse files
Connection Pooling Parameters (#185)
* Connection Pooling Parameters Signed-off-by: Anders Swanson <anders.swanson@oracle.com>
1 parent 1a23830 commit dfb9ca5

File tree

4 files changed

+108
-80
lines changed

4 files changed

+108
-80
lines changed

‎README.md

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -710,27 +710,32 @@ The following command line arguments (flags) can be passed to the exporter (the
710710

711711
```bash
712712
Usage of oracledb_exporter:
713-
--web.telemetry-path="/metrics"
713+
--web.telemetry-path="/metrics"
714714
Path under which to expose metrics. (env: TELEMETRY_PATH)
715-
--default.metrics="default-metrics.toml"
715+
--default.metrics="default-metrics.toml"
716716
File with default metrics in a TOML file. (env: DEFAULT_METRICS)
717717
--custom.metrics="" Comma separated list of file(s) that contain various custom metrics in a TOML format. (env: CUSTOM_METRICS)
718718
--query.timeout=5 Query timeout (in seconds). (env: QUERY_TIMEOUT)
719719
--database.maxIdleConns=0 Number of maximum idle connections in the connection pool. (env: DATABASE_MAXIDLECONNS)
720-
--database.maxOpenConns=10
720+
--database.maxOpenConns=10
721721
Number of maximum open connections in the connection pool. (env: DATABASE_MAXOPENCONNS)
722+
--database.poolIncrement=-1
723+
Connection increment when the connection pool reaches max capacity. (env: DATABASE_POOLINCREMENT)
724+
--database.poolMaxConnections=-1
725+
Maximum number of connections in the connection pool. (env: DATABASE_POOLMAXCONNECTIONS)
726+
--database.poolMinConnections=-1
727+
Minimum number of connections in the connection pool. (env: DATABASE_POOLMINCONNECTIONS)
722728
--scrape.interval=0s Interval between each scrape. Default is to scrape on collect requests.
723729
--log.disable=0 Set to 1 to disable alert logs
724730
--log.interval=15s Interval between log updates (e.g. 5s).
725-
--log.destination="/log/alert.log"
731+
--log.destination="/log/alert.log"
726732
File to output the alert log to. (env: LOG_DESTINATION)
727-
--web.listen-address=:9161 ...
728-
Addresses on which to expose metrics and web interface. Repeatable for multiple addresses.
733+
--web.listen-address=:9161 ...
734+
Addresses on which to expose metrics and web interface. Repeatable for multiple addresses. Examples: `:9100` or `[::1]:9100` for http, `vsock://:9100` for vsock
729735
--web.config.file="" Path to configuration file that can enable TLS or authentication. See: https://github.com/prometheus/exporter-toolkit/blob/master/docs/web-configuration.md
730736
--log.level=info Only log messages with the given severity or above. One of: [debug, info, warn, error]
731737
--log.format=logfmt Output format of log messages. One of: [logfmt, json]
732738
--[no-]version Show application version.
733-
734739
```
735740

736741
You may provide the connection details using these variables:

‎collector/collector.go

Lines changed: 18 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021, 2024, Oracle and/or its affiliates.
1+
// Copyright (c) 2021, 2025, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33
// Portions Copyright (c) 2016 Seth Miller <seth@sethmiller.me>
44

@@ -27,75 +27,6 @@ import (
2727
"github.com/prometheus/client_golang/prometheus"
2828
)
2929

30-
// Exporter collects Oracle DB metrics. It implements prometheus.Collector.
31-
type Exporter struct {
32-
config *Config
33-
mu *sync.Mutex
34-
metricsToScrape Metrics
35-
scrapeInterval *time.Duration
36-
user string
37-
password string
38-
connectString string
39-
configDir string
40-
externalAuth bool
41-
duration, error prometheus.Gauge
42-
totalScrapes prometheus.Counter
43-
scrapeErrors *prometheus.CounterVec
44-
scrapeResults []prometheus.Metric
45-
up prometheus.Gauge
46-
dbtype int
47-
dbtypeGauge prometheus.Gauge
48-
db *sql.DB
49-
logger log.Logger
50-
lastTick *time.Time
51-
}
52-
53-
// Config is the configuration of the exporter
54-
type Config struct {
55-
User string
56-
Password string
57-
ConnectString string
58-
DbRole dsn.AdminRole
59-
ConfigDir string
60-
ExternalAuth bool
61-
MaxIdleConns int
62-
MaxOpenConns int
63-
CustomMetrics string
64-
QueryTimeout int
65-
DefaultMetricsFile string
66-
}
67-
68-
// CreateDefaultConfig returns the default configuration of the Exporter
69-
// it is to be of note that the DNS will be empty when
70-
func CreateDefaultConfig() *Config {
71-
return &Config{
72-
MaxIdleConns: 0,
73-
MaxOpenConns: 10,
74-
CustomMetrics: "",
75-
QueryTimeout: 5,
76-
DefaultMetricsFile: "",
77-
}
78-
}
79-
80-
// Metric is an object description
81-
type Metric struct {
82-
Context string
83-
Labels []string
84-
MetricsDesc map[string]string
85-
MetricsType map[string]string
86-
MetricsBuckets map[string]map[string]string
87-
FieldToAppend string
88-
Request string
89-
IgnoreZeroResult bool
90-
QueryTimeout string
91-
ScrapeInterval string
92-
}
93-
94-
// Metrics is a container structure for prometheus metrics
95-
type Metrics struct {
96-
Metric []Metric
97-
}
98-
9930
var (
10031
additionalMetrics Metrics
10132
hashMap = make(map[int][]byte)
@@ -416,6 +347,21 @@ func (e *Exporter) connect() error {
416347
}
417348
P.Username, P.Password, P.ConnectString, P.ExternalAuth = e.user, godror.NewPassword(e.password), e.connectString, externalAuth
418349

350+
if e.config.PoolIncrement > 0 {
351+
level.Debug(e.logger).Log("msg", "set pool increment to ", e.config.PoolIncrement)
352+
P.PoolParams.SessionIncrement = e.config.PoolIncrement
353+
}
354+
if e.config.PoolMaxConnections > 0 {
355+
level.Debug(e.logger).Log("msg", "set pool max connections to ", e.config.PoolMaxConnections)
356+
P.PoolParams.MaxSessions = e.config.PoolMaxConnections
357+
}
358+
if e.config.PoolMinConnections > 0 {
359+
level.Debug(e.logger).Log("msg", "set pool min connections to ", e.config.PoolMinConnections)
360+
P.PoolParams.MinSessions = e.config.PoolMinConnections
361+
}
362+
363+
P.PoolParams.WaitTimeout = time.Second * 5
364+
419365
// if TNS_ADMIN env var is set, set ConfigDir to that location
420366
P.ConfigDir = e.configDir
421367

@@ -443,9 +389,9 @@ func (e *Exporter) connect() error {
443389
// note that this just configures the connection, it does not actually connect until later
444390
// when we call db.Ping()
445391
db := sql.OpenDB(godror.NewConnector(P))
446-
level.Debug(e.logger).Log("set max idle connections to ", e.config.MaxIdleConns)
392+
level.Debug(e.logger).Log("msg", "set max idle connections to ", e.config.MaxIdleConns)
447393
db.SetMaxIdleConns(e.config.MaxIdleConns)
448-
level.Debug(e.logger).Log("set max open connections to ", e.config.MaxOpenConns)
394+
level.Debug(e.logger).Log("msg", "set max open connections to ", e.config.MaxOpenConns)
449395
db.SetMaxOpenConns(e.config.MaxOpenConns)
450396
db.SetConnMaxLifetime(0)
451397
level.Debug(e.logger).Log("msg", "Successfully configured connection to "+maskDsn(e.connectString))

‎collector/types.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright (c) 2025, Oracle and/or its affiliates.
2+
3+
package collector
4+
5+
import (
6+
"database/sql"
7+
"github.com/go-kit/log"
8+
"github.com/godror/godror/dsn"
9+
"github.com/prometheus/client_golang/prometheus"
10+
"sync"
11+
"time"
12+
)
13+
14+
// Exporter collects Oracle DB metrics. It implements prometheus.Collector.
15+
type Exporter struct {
16+
config *Config
17+
mu *sync.Mutex
18+
metricsToScrape Metrics
19+
scrapeInterval *time.Duration
20+
user string
21+
password string
22+
connectString string
23+
configDir string
24+
externalAuth bool
25+
duration, error prometheus.Gauge
26+
totalScrapes prometheus.Counter
27+
scrapeErrors *prometheus.CounterVec
28+
scrapeResults []prometheus.Metric
29+
up prometheus.Gauge
30+
dbtype int
31+
dbtypeGauge prometheus.Gauge
32+
db *sql.DB
33+
logger log.Logger
34+
lastTick *time.Time
35+
}
36+
37+
type Config struct {
38+
User string
39+
Password string
40+
ConnectString string
41+
DbRole dsn.AdminRole
42+
ConfigDir string
43+
ExternalAuth bool
44+
MaxIdleConns int
45+
MaxOpenConns int
46+
PoolIncrement int
47+
PoolMaxConnections int
48+
PoolMinConnections int
49+
CustomMetrics string
50+
QueryTimeout int
51+
DefaultMetricsFile string
52+
}
53+
54+
// Metric is an object description
55+
type Metric struct {
56+
Context string
57+
Labels []string
58+
MetricsDesc map[string]string
59+
MetricsType map[string]string
60+
MetricsBuckets map[string]map[string]string
61+
FieldToAppend string
62+
Request string
63+
IgnoreZeroResult bool
64+
QueryTimeout string
65+
ScrapeInterval string
66+
}
67+
68+
// Metrics is a container structure for prometheus metrics
69+
type Metrics struct {
70+
Metric []Metric
71+
}

‎main.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2021, 2024, Oracle and/or its affiliates.
1+
// Copyright (c) 2021, 2025, Oracle and/or its affiliates.
22
// Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
33
// Portions Copyright (c) 2016 Seth Miller <seth@sethmiller.me>
44

@@ -42,6 +42,9 @@ var (
4242
queryTimeout = kingpin.Flag("query.timeout", "Query timeout (in seconds). (env: QUERY_TIMEOUT)").Default(getEnv("QUERY_TIMEOUT", "5")).Int()
4343
maxIdleConns = kingpin.Flag("database.maxIdleConns", "Number of maximum idle connections in the connection pool. (env: DATABASE_MAXIDLECONNS)").Default(getEnv("DATABASE_MAXIDLECONNS", "0")).Int()
4444
maxOpenConns = kingpin.Flag("database.maxOpenConns", "Number of maximum open connections in the connection pool. (env: DATABASE_MAXOPENCONNS)").Default(getEnv("DATABASE_MAXOPENCONNS", "10")).Int()
45+
poolIncrement = kingpin.Flag("database.poolIncrement", "Connection increment when the connection pool reaches max capacity. (env: DATABASE_POOLINCREMENT)").Default(getEnv("DATABASE_POOLINCREMENT", "-1")).Int()
46+
poolMaxConnections = kingpin.Flag("database.poolMaxConnections", "Maximum number of connections in the connection pool. (env: DATABASE_POOLMAXCONNECTIONS)").Default(getEnv("DATABASE_POOLMAXCONNECTIONS", "-1")).Int()
47+
poolMinConnections = kingpin.Flag("database.poolMinConnections", "Minimum number of connections in the connection pool. (env: DATABASE_POOLMINCONNECTIONS)").Default(getEnv("DATABASE_POOLMINCONNECTIONS", "-1")).Int()
4548
scrapeInterval = kingpin.Flag("scrape.interval", "Interval between each scrape. Default is to scrape on collect requests.").Default("0s").Duration()
4649
logDisable = kingpin.Flag("log.disable", "Set to 1 to disable alert logs").Default("0").Int()
4750
logInterval = kingpin.Flag("log.interval", "Interval between log updates (e.g. 5s).").Default("15s").Duration()
@@ -93,6 +96,9 @@ func main() {
9396
ExternalAuth: externalAuth,
9497
MaxOpenConns: *maxOpenConns,
9598
MaxIdleConns: *maxIdleConns,
99+
PoolIncrement: *poolIncrement,
100+
PoolMaxConnections: *poolMaxConnections,
101+
PoolMinConnections: *poolMinConnections,
96102
CustomMetrics: *customMetrics,
97103
QueryTimeout: *queryTimeout,
98104
DefaultMetricsFile: *defaultFileMetrics,

0 commit comments

Comments
 (0)