NewDBStatsCollector not declared by package prometheus

27 views
Skip to first unread message

afridi

unread,
Aug 18, 2021, 7:38:52 AM8/18/21
to Prometheus Users

Hey everyone,

Need some help regarding the usage for the  collector for database/sql(DBStats). I am instrumenting my go code and want to get metrics for connection pool.

I was planning to use this awesome nice library by dlmiddlecote dbstates. I created a demo and it works fine.

//sample code for above library
collector := sqlstats.NewStatsCollector(conf.DB.Type, db.GetConnection())// Register it with Prometheus
// prometheus.MustRegister(collector)

However, recently I have notice that client_go(already using this) have this new same feature in latest release version 1.11.0 and it was merged in this PR.

//sample code for client_go
reg := prometheus.NewRegistry()
reg.MustRegister(prometheus.NewDBStatsCollector(db.GetConnection(), "db_A"))

Now my question, can someone provide me POC for this feature like how to instrument my code. I tried but due lake of experience I am failing, you can see the error in the subject of this post that I am getting. Any help would be appreciated.

Regards,
Afr
Message has been deleted

Brian Candler

unread,
Aug 18, 2021, 1:37:39 PM8/18/21
to Prometheus Users
You need to show more of your code, but it looks like you're not importing this library properly.

The code appears in a package called "collectors": see https://github.com/prometheus/client_golang/pull/862/files
so unless you import it under a different name, that's what you'll have to use.  And you'll need to use the right import path.

afridi

unread,
Aug 18, 2021, 4:11:17 PM8/18/21
to Prometheus Users
Hey Brian,
Thank you for the feedback. Indeed it was an import issue. I was able to solved it. It was so nice of you for providing the playground example. :) Cheers buddy.

Regards,
Afr

Bjoern Rabenstein

unread,
Aug 20, 2021, 8:11:51 AM8/20/21
to afridi, Prometheus Users
On 18.08.21 06:23, afridi wrote:
>
> *// below code somewhere in the main func*
> *prometheus.NewRegistry().MustRegister(collectors.NewDBStatsCollector(db.GetConnection(),
> "postgres"))*

You must also expose the metrics registered with your new registry. If
you do it like above, you don't have a handle to the registry and
cannot expose it.

You should register the DBStatsCollector with the same registry you
use for all your other metrics. If that's the default registry, just
write

prometheus.MustRegister(collectors.NewDBStatsCollector(db.GetConnection(), "postgres"))

--
Björn Rabenstein
[PGP-ID] 0x851C3DA17D748D03
[email] bjo...@rabenste.in
Reply all
Reply to author
Forward
0 new messages