Skip to content

[receiver/sqlquery] Multiple connections despite max_open_conn = 1 #39270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
cbandy opened this issue Apr 9, 2025 · 4 comments · Fixed by #39312
Closed

[receiver/sqlquery] Multiple connections despite max_open_conn = 1 #39270

cbandy opened this issue Apr 9, 2025 · 4 comments · Fixed by #39312
Assignees
Labels
bug Something isn't working receiver/sqlquery SQL query receiver

Comments

@cbandy
Copy link
Contributor

cbandy commented Apr 9, 2025

Component(s)

receiver/sqlquery

What happened?

Description

#36752 is happening despite #37748. The sqlquery receiver opens one connection for every query, regardless of the max_open_conn setting.

Steps to Reproduce

Use the configuration below with a recent collector and Postgres:

$ docker run --rm -it --net host --env POSTGRES_HOST_AUTH_METHOD=trust docker.io/library/postgres

$ docker run --rm -it --net host -v "$(pwd)/config.yaml:/etc/otelcol-contrib/config.yaml:ro" \
ghcr.io/open-telemetry/opentelemetry-collector-releases/opentelemetry-collector-contrib:0.123.1

Use psql to see the open connections at Postgres:

$ psql -h localhost -U postgres -c 'SELECT pid, backend_type, datname, usename, query, state FROM pg_stat_activity'
┌─────┬──────────────────────────────┬──────────┬──────────┬────────────────────────────────────────────────────────────────────────────────┬────────┐
│ pid │         backend_type         │ datname  │ usename  │                                     query                                      │ state  │
├─────┼──────────────────────────────┼──────────┼──────────┼────────────────────────────────────────────────────────────────────────────────┼────────┤
│ 102 │ client backend               │ postgres │ postgres │ SELECT 1 AS number                                                             │ idle   │
│ 103 │ client backend               │ postgres │ postgres │ SELECT 2 AS number                                                             │ idle   │
│ 104 │ client backend               │ postgres │ postgres │ SELECT 3 AS number                                                             │ idle   │
│ 110 │ client backend               │ postgres │ postgres │ SELECT pid, backend_type, datname, usename, query, state FROM pg_stat_activity │ active │
│  65 │ autovacuum launcher          │ ␀        │ ␀        │                                                                                │ ␀      │
│  66 │ logical replication launcher │ ␀        │ postgres │                                                                                │ ␀      │
│  61 │ checkpointer                 │ ␀        │ ␀        │                                                                                │ ␀      │
│  62 │ background writer            │ ␀        │ ␀        │                                                                                │ ␀      │
│  64 │ walwriter                    │ ␀        │ ␀        │                                                                                │ ␀      │
└─────┴──────────────────────────────┴──────────┴──────────┴────────────────────────────────────────────────────────────────────────────────┴────────┘
(9 rows)

Expected Result

I expected one "client backend" connection because max_open_conn: 1 in the receiver config.

Actual Result

Three "client backend" connections, one for each query.

Collector version

v0.123.1

Environment information

Environment

OS: Debian 12 (bookworm)

OpenTelemetry Collector configuration

---
receivers:
  sqlquery:
    driver: postgres
    datasource: host=localhost sslmode=disable user=postgres
    max_open_conn: 1
    collection_interval: 10s
    queries:
      - sql: 'SELECT 1 AS number'
        metrics:
          - metric_name: num_1
            value_column: number
      - sql: 'SELECT 2 AS number'
        metrics:
          - metric_name: num_2
            value_column: number
      - sql: 'SELECT 3 AS number'
        metrics:
          - metric_name: num_3
            value_column: number
exporters:
  debug: {}
service:
  pipelines:
    metrics:
      receivers: [sqlquery]
      processors: []
      exporters: [debug]

Log output

2025-04-09T04:03:08.577Z        info    [email protected]/service.go:197 Setting up own telemetry...
2025-04-09T04:03:08.578Z        info    builders/builders.go:26 Development component. May change in the future.
2025-04-09T04:03:08.579Z        info    [email protected]/service.go:264 Starting otelcol-contrib...     {"Version": "0.123.1", "NumCPU": 12}
2025-04-09T04:03:08.579Z        info    extensions/extensions.go:41     Starting extensions...
2025-04-09T04:03:08.579Z        info    [email protected]/service.go:287 Everything is ready. Begin running and processing data.
2025-04-09T04:03:09.600Z        info    Metrics {"resource metrics": 3, "metrics": 3, "data points": 3}
2025-04-09T04:03:19.583Z        info    Metrics {"resource metrics": 3, "metrics": 3, "data points": 3}
2025-04-09T04:03:29.583Z        info    Metrics {"resource metrics": 3, "metrics": 3, "data points": 3}
2025-04-09T04:03:39.583Z        info    Metrics {"resource metrics": 3, "metrics": 3, "data points": 3}

Additional context

This function is called during every Scraper's startup, creating multiple pools:

dbProviderFunc := func() (*sql.DB, error) {
dbPool, err := sqlOpenerFunc(sqlCfg.Driver, sqlCfg.DataSource)

@cbandy cbandy added bug Something isn't working needs triage New item requiring triage labels Apr 9, 2025
@github-actions github-actions bot added the receiver/sqlquery SQL query receiver label Apr 9, 2025
Copy link
Contributor

github-actions bot commented Apr 9, 2025

Pinging code owners:

See Adding Labels via Comments if you do not have permissions to add labels yourself.

@crobert-1
Copy link
Member

Thanks for filing this issue and including so much relevant information, @cbandy, agreed this is a bug that needs resolved.

@crobert-1 crobert-1 removed the needs triage New item requiring triage label Apr 9, 2025
@cbandy
Copy link
Contributor Author

cbandy commented Apr 9, 2025

I can submit a fix in the next day or two.

@cbandy
Copy link
Contributor Author

cbandy commented Apr 11, 2025

I've opened #39312 to address this.

cbandy added a commit to cbandy/opentelemetry-collector-contrib that referenced this issue Apr 11, 2025
atoulme pushed a commit that referenced this issue Apr 17, 2025
)

#### Description
Rather than creating a separate `*sql.DB` provider function for each
query, this creates one provider for the receiver and passes a method
value to each query. The method uses a mutex to ensure exactly one
`*sql.DB` is created when it is called concurrently.

The `max_open_conn` config is defined in `sqlqueryreceiver`, so I put
the implementation in its `internal` package.

#### Link to tracking issue
Fixes #39270

#### Testing
Using the configuration and steps in #39270, I see only one connection
to Postgres:

```console
$ psql -h localhost -U postgres -c 'SELECT pid, backend_type, datname, usename, query, state FROM pg_stat_activity'
┌─────┬──────────────────────────────┬──────────┬──────────┬────────────────────────────────────────────────────────────────────────────────┬────────┐
│ pid │         backend_type         │ datname  │ usename  │                                     query                                      │ state  │
├─────┼──────────────────────────────┼──────────┼──────────┼────────────────────────────────────────────────────────────────────────────────┼────────┤
│  68 │ client backend               │ postgres │ postgres │ SELECT 3 AS number                                                             │ idle   │
│  70 │ client backend               │ postgres │ postgres │ SELECT pid, backend_type, datname, usename, query, state FROM pg_stat_activity │ active │
│  65 │ autovacuum launcher          │ ␀        │ ␀        │                                                                                │ ␀      │
│  66 │ logical replication launcher │ ␀        │ postgres │                                                                                │ ␀      │
│  61 │ checkpointer                 │ ␀        │ ␀        │                                                                                │ ␀      │
│  62 │ background writer            │ ␀        │ ␀        │                                                                                │ ␀      │
│  64 │ walwriter                    │ ␀        │ ␀        │                                                                                │ ␀      │
└─────┴──────────────────────────────┴──────────┴──────────┴────────────────────────────────────────────────────────────────────────────────┴────────┘
(7 rows)
```

Signed-off-by: Chris Bandy <[email protected]>
akshays-19 pushed a commit to akshays-19/opentelemetry-collector-contrib that referenced this issue Apr 23, 2025
…n-telemetry#39312)

#### Description
Rather than creating a separate `*sql.DB` provider function for each
query, this creates one provider for the receiver and passes a method
value to each query. The method uses a mutex to ensure exactly one
`*sql.DB` is created when it is called concurrently.

The `max_open_conn` config is defined in `sqlqueryreceiver`, so I put
the implementation in its `internal` package.

#### Link to tracking issue
Fixes open-telemetry#39270

#### Testing
Using the configuration and steps in open-telemetry#39270, I see only one connection
to Postgres:

```console
$ psql -h localhost -U postgres -c 'SELECT pid, backend_type, datname, usename, query, state FROM pg_stat_activity'
┌─────┬──────────────────────────────┬──────────┬──────────┬────────────────────────────────────────────────────────────────────────────────┬────────┐
│ pid │         backend_type         │ datname  │ usename  │                                     query                                      │ state  │
├─────┼──────────────────────────────┼──────────┼──────────┼────────────────────────────────────────────────────────────────────────────────┼────────┤
│  68 │ client backend               │ postgres │ postgres │ SELECT 3 AS number                                                             │ idle   │
│  70 │ client backend               │ postgres │ postgres │ SELECT pid, backend_type, datname, usename, query, state FROM pg_stat_activity │ active │
│  65 │ autovacuum launcher          │ ␀        │ ␀        │                                                                                │ ␀      │
│  66 │ logical replication launcher │ ␀        │ postgres │                                                                                │ ␀      │
│  61 │ checkpointer                 │ ␀        │ ␀        │                                                                                │ ␀      │
│  62 │ background writer            │ ␀        │ ␀        │                                                                                │ ␀      │
│  64 │ walwriter                    │ ␀        │ ␀        │                                                                                │ ␀      │
└─────┴──────────────────────────────┴──────────┴──────────┴────────────────────────────────────────────────────────────────────────────────┴────────┘
(7 rows)
```

Signed-off-by: Chris Bandy <[email protected]>
Fiery-Fenix pushed a commit to Fiery-Fenix/opentelemetry-collector-contrib that referenced this issue Apr 24, 2025
…n-telemetry#39312)

#### Description
Rather than creating a separate `*sql.DB` provider function for each
query, this creates one provider for the receiver and passes a method
value to each query. The method uses a mutex to ensure exactly one
`*sql.DB` is created when it is called concurrently.

The `max_open_conn` config is defined in `sqlqueryreceiver`, so I put
the implementation in its `internal` package.

#### Link to tracking issue
Fixes open-telemetry#39270

#### Testing
Using the configuration and steps in open-telemetry#39270, I see only one connection
to Postgres:

```console
$ psql -h localhost -U postgres -c 'SELECT pid, backend_type, datname, usename, query, state FROM pg_stat_activity'
┌─────┬──────────────────────────────┬──────────┬──────────┬────────────────────────────────────────────────────────────────────────────────┬────────┐
│ pid │         backend_type         │ datname  │ usename  │                                     query                                      │ state  │
├─────┼──────────────────────────────┼──────────┼──────────┼────────────────────────────────────────────────────────────────────────────────┼────────┤
│  68 │ client backend               │ postgres │ postgres │ SELECT 3 AS number                                                             │ idle   │
│  70 │ client backend               │ postgres │ postgres │ SELECT pid, backend_type, datname, usename, query, state FROM pg_stat_activity │ active │
│  65 │ autovacuum launcher          │ ␀        │ ␀        │                                                                                │ ␀      │
│  66 │ logical replication launcher │ ␀        │ postgres │                                                                                │ ␀      │
│  61 │ checkpointer                 │ ␀        │ ␀        │                                                                                │ ␀      │
│  62 │ background writer            │ ␀        │ ␀        │                                                                                │ ␀      │
│  64 │ walwriter                    │ ␀        │ ␀        │                                                                                │ ␀      │
└─────┴──────────────────────────────┴──────────┴──────────┴────────────────────────────────────────────────────────────────────────────────┴────────┘
(7 rows)
```

Signed-off-by: Chris Bandy <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working receiver/sqlquery SQL query receiver
Projects
None yet
2 participants