Skip to content

database/sql: collect database metric information using dbstats #39161

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
4orty opened this issue May 20, 2020 · 3 comments
Closed

database/sql: collect database metric information using dbstats #39161

4orty opened this issue May 20, 2020 · 3 comments

Comments

@4orty
Copy link

4orty commented May 20, 2020

What version of Go are you using (go version)?

$ go version 1.14

Does this issue reproduce with the latest release?

yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
windows/amd64

What did you do?

Purpose: I want to get database metric information in go application.
So here is a source code.

db, err := sql.Open("postgres", connectionInfo)

dbstat := db.Stats()
db.SetMaxOpenConns(20)
fmt.Println(dbstat.MaxOpenConnections)

What did you expect to see?

I expected the printed number is 20

What did you see instead?

The result of the code was 0 (default value)

When i took a look at dbstat struct, I realized that all the type of field assigned int or string, not pointer.
Is there any problem that change fields of DBstat struct to pointer value?
If it assigned pointer value, it seems like can collect database metric information in real time.

@seankhliao
Copy link
Member

db.Stats() should be called when you need the data,
this is the same as the other metrics exposed by the stdlib, ex in runtime

having unprotected pointers to mutable data races possible

@cagedmantis
Copy link
Contributor

As mentioned by @seankhliao, db.Stats() should be called at the point when you are ready to collect the data. It should not be able to mutate the state of DB.

I'm going to close this issue since this is working as expected. Please feel free to follow up if this was done in error.

@4orty
Copy link
Author

4orty commented May 21, 2020

@cagedmantis Thanks for reply. If i want to collect DB metrics every second, is it ok to call db.Stats() every second?

@golang golang locked and limited conversation to collaborators May 21, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants