Skip to content

Commit da63825

Browse files
akstronatoulme
andauthored
Allow User to Specify Max Connections to DB (#37748)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue. Ex. Adding a feature - Explain what this achieves.--> #### Description Added additional field `MaxOpenConn` which allow users to specify the maximum number of connections to DB server. <!-- Issue number (e.g. #1234) or full URL to issue, if applicable. --> #### Link to tracking issue Fixes: #36752 <!--Describe what testing was performed and which tests were added.--> #### Testing `TestMysqlIntegrationMetrics` --------- Signed-off-by: Alok Kumar Singh <[email protected]> Co-authored-by: Antoine Toulme <[email protected]>
1 parent 2337fc5 commit da63825

File tree

5 files changed

+40
-1
lines changed

5 files changed

+40
-1
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: 'enhancement'
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
7+
component: 'sqlqueryreceiver'
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: "Allow users to specify the maximum number of concurrent open connections to DB server using `max_open_conn` config parameter"
11+
12+
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
13+
issues: [36752]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# If your change doesn't affect end users or the exported elements of any package,
21+
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.
22+
# Optional: The change log or logs in which this entry should be included.
23+
# e.g. '[user]' or '[user, api]'
24+
# Include 'user' if the change is relevant to end users.
25+
# Include 'api' if there is a change to a library API.
26+
# Default: '[user]'
27+
change_logs: [user]

receiver/sqlqueryreceiver/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ The configuration supports the following top-level fields:
4040
- `telemetry` (optional) Defines settings for the component's own telemetry - logs, metrics or traces.
4141
- `telemetry.logs` (optional) Defines settings for the component's own logs.
4242
- `telemetry.logs.query` (optional, default `false`) If set to `true`, every time a SQL query is run, the text of the query and the values of its parameters will be logged together with the debug log `"Running query"`.
43+
- `max_open_conn` (optional, default `0`): The maximumn number of open connections to the sql server. <= 0 means unlimited
4344

4445
[storage_extension]: https://github.com/open-telemetry/opentelemetry-collector-contrib/tree/main/extension/storage/filestorage
4546

receiver/sqlqueryreceiver/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import (
1414

1515
type Config struct {
1616
sqlquery.Config `mapstructure:",squash"`
17+
// The maximumn number of open connections to the sql server. <= 0 means unlimited
18+
MaxOpenConn int `mapstructure:"max_open_conn"`
1719
}
1820

1921
func createDefaultConfig() component.Config {

receiver/sqlqueryreceiver/integration_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,7 @@ func TestMysqlIntegrationMetrics(t *testing.T) {
713713
rCfg := cfg.(*Config)
714714
rCfg.Driver = MySQL.Driver
715715
rCfg.DataSource = MySQL.ConnectionString(ci.Host(t), nat.Port(ci.MappedPort(t, MySQL.Port)))
716+
rCfg.MaxOpenConn = 5
716717
rCfg.Queries = []sqlquery.Query{
717718
{
718719
SQL: "select genre, count(*), avg(imdb_rating) from movie group by genre order by genre desc",

receiver/sqlqueryreceiver/receiver.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,15 @@ func createMetricsReceiverFunc(sqlOpenerFunc sqlquery.SQLOpenerFunc, clientProvi
4545
}
4646
id := component.MustNewIDWithName("sqlqueryreceiver", fmt.Sprintf("query-%d: %s", i, query.SQL))
4747
dbProviderFunc := func() (*sql.DB, error) {
48-
return sqlOpenerFunc(sqlCfg.Driver, sqlCfg.DataSource)
48+
dbPool, err := sqlOpenerFunc(sqlCfg.Driver, sqlCfg.DataSource)
49+
if err != nil {
50+
return nil, err
51+
}
52+
53+
if dbPool != nil {
54+
dbPool.SetMaxOpenConns(sqlCfg.MaxOpenConn)
55+
}
56+
return dbPool, nil
4957
}
5058
scope := pcommon.NewInstrumentationScope()
5159
scope.SetName(metadata.ScopeName)

0 commit comments

Comments
 (0)