Skip to content

Commit

Permalink
chore
Browse files Browse the repository at this point in the history
  • Loading branch information
gesellix committed Oct 21, 2023
1 parent 32dc206 commit 57fb93e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 30 deletions.
1 change: 1 addition & 0 deletions couchdb-exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ func main() {
Username: exporterConfig.couchdbUsername,
Password: exporterConfig.couchdbPassword},
lib.CollectorConfig{
ScrapeOnCollect: true,
Databases: databases,
CollectViews: exporterConfig.databaseViews,
CollectSchedulerJobs: exporterConfig.schedulerJobs,
Expand Down
25 changes: 16 additions & 9 deletions couchdb-exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"encoding/base64"
"errors"
"fmt"
"log"
"net"
Expand Down Expand Up @@ -126,6 +127,7 @@ func performCouchdbStatsTest(t *testing.T, couchdbVersion string, expectedMetric
server := httptest.NewServer(handler)

e := lib.NewExporter(server.URL, basicAuth, lib.CollectorConfig{
ScrapeOnCollect: true,
Databases: []string{"example", "another-example"},
CollectViews: true,
CollectSchedulerJobs: true,
Expand Down Expand Up @@ -208,8 +210,9 @@ func TestCouchdbStatsV1Integration(t *testing.T) {

t.Run("node_up", func(t *testing.T) {
e := lib.NewExporter(dbUrl, basicAuth, lib.CollectorConfig{
Databases: []string{},
CollectViews: true,
ScrapeOnCollect: true,
Databases: []string{},
CollectViews: true,
}, true)

ch := make(chan prometheus.Metric)
Expand All @@ -232,8 +235,9 @@ func TestCouchdbStatsV1Integration(t *testing.T) {

t.Run("_all_dbs", func(t *testing.T) {
e := lib.NewExporter(dbUrl, basicAuth, lib.CollectorConfig{
Databases: []string{"_all_dbs"},
CollectViews: true,
ScrapeOnCollect: true,
Databases: []string{"_all_dbs"},
CollectViews: true,
}, true)

ch := make(chan prometheus.Metric)
Expand Down Expand Up @@ -272,7 +276,8 @@ func awaitMembership(t *testing.T, basicAuth lib.BasicAuth) func(address string)
c := lib.NewCouchdbClient(dbUrl, basicAuth, true)
nodeNames, err := c.GetNodeNames()
if err != nil {
if err, ok := err.(net.Error); ok && (err.Timeout() || err.Temporary()) {
var err net.Error
if errors.As(err, &err) && (err.Timeout() || err.Temporary()) {
return false, nil
}
return false, nil
Expand Down Expand Up @@ -318,8 +323,9 @@ func TestCouchdbStatsV2Integration(t *testing.T) {

t.Run("node_up", func(t *testing.T) {
e := lib.NewExporter(dbUrl, basicAuth, lib.CollectorConfig{
Databases: []string{},
CollectViews: true,
ScrapeOnCollect: true,
Databases: []string{},
CollectViews: true,
}, true)

ch := make(chan prometheus.Metric)
Expand All @@ -342,8 +348,9 @@ func TestCouchdbStatsV2Integration(t *testing.T) {

t.Run("_all_dbs", func(t *testing.T) {
e := lib.NewExporter(dbUrl, basicAuth, lib.CollectorConfig{
Databases: []string{"_all_dbs"},
CollectViews: true,
ScrapeOnCollect: true,
Databases: []string{"_all_dbs"},
CollectViews: true,
}, true)

ch := make(chan prometheus.Metric)
Expand Down
14 changes: 0 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
cloud.google.com/go/compute v1.20.1/go.mod h1:4tCnrn48xsqlwSAiLf1HXMQk8CONslYbdiEZc9FEIbM=
cloud.google.com/go/compute/metadata v0.2.3/go.mod h1:VAV5nSsACxMJvgaAuX6Pk2AawlZn8kiOGuCv6gTkwuA=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/alecthomas/kingpin/v2 v2.3.2/go.mod h1:0gyi0zQnjuFk8xrkNKamJoyUo382HRL7ATRpFZCw6tE=
github.com/alecthomas/units v0.0.0-20211218093645-b94a6e3cc137/go.mod h1:OMCwj8VM1Kc9e19TLln2VL61YJF0x1XFtfdL4JdbSyE=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
Expand Down Expand Up @@ -34,17 +30,12 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0 h1:jWpvCLoY8Z/e3VKvlsiIGKtc+UG6U5vzxaoagmhXfyg=
github.com/matttproud/golang_protobuf_extensions/v2 v2.0.0/go.mod h1:QUyp042oQthUoa9bqDv0ER0wrtXnBruoNd7aNjkbP+k=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/okeuday/erlang_go/v2 v2.0.6 h1:Hdiz8Fx8br4kRV/yItaqBLbfy8H7Xal1hkG+D4RwMOQ=
Expand All @@ -65,12 +56,10 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/xhit/go-str2duration/v2 v2.1.0/go.mod h1:ohY8p+0f07DiV6Em5LKB0s2YpLtXVyJfNt1+BlmyAsU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
Expand All @@ -79,7 +68,6 @@ golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.14.0 h1:wBqGXzWJW6m1XrIKlAH0Hs1JJ7+9KBwnIO8v66Q9cHc=
golang.org/x/crypto v0.14.0/go.mod h1:MVFd36DqK4CsrnJYDkBA3VC4m2GkXAM0PvzMCn4JQf4=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
Expand All @@ -100,7 +88,6 @@ golang.org/x/sys v0.13.0 h1:Af8nKPmuFypiUBjVoU9V20FiaFXOcuZI21p0ycVYYGE=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.13.0/go.mod h1:LTmsnFJwVN6bCy1rVCoS+qHT1HhALEFxKncY3WNNh4U=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
Expand All @@ -110,7 +97,6 @@ golang.org/x/text v0.13.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.6.8 h1:IhEN5q69dyKagZPYMSdIjS2HqprW324FRQZJcGqPAsM=
Expand Down
37 changes: 30 additions & 7 deletions lib/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var (
)

type CollectorConfig struct {
ScrapeOnCollect bool
Databases []string
ObservedDatabases []string
CollectViews bool
Expand Down Expand Up @@ -291,15 +292,11 @@ func (e *Exporter) getObservedDatabaseNames(candidates []string) ([]string, erro
return candidates, nil
}

func (e *Exporter) collect(ch chan<- prometheus.Metric) error {
e.up.Set(0)
sendStatus := func() {
ch <- e.up
}
defer sendStatus()

func (e *Exporter) scrape() error {
e.resetAllMetrics()

e.up.Set(0)

e.requestCount.Set(-1)
e.client.ResetRequestCount()

Expand Down Expand Up @@ -327,6 +324,26 @@ func (e *Exporter) collect(ch chan<- prometheus.Metric) error {
return err
}
}
return nil
}

func (e *Exporter) collect(ch chan<- prometheus.Metric) error {
sendStatus := func() {
ch <- e.up
}
defer sendStatus()

if e.collectorConfig.ScrapeOnCollect {
// sync
// old behaviour: scrape the CouchDB when the exporter is being scraped by Prometheus
err := e.scrape()
if err != nil {
return err
}
} else {
// async
// new behaviour: scrape the CouchDB at an interval, deliver metrics when the exporter is being scraped by Prometheus
}

e.databasesTotal.Collect(ch)
e.nodeUp.Collect(ch)
Expand Down Expand Up @@ -421,6 +438,12 @@ func (e *Exporter) collect(ch chan<- prometheus.Metric) error {
func (e *Exporter) Collect(ch chan<- prometheus.Metric) {
e.mutex.Lock() // To protect metrics from concurrent collects.
defer e.mutex.Unlock()
//registry := prometheus.NewRegistry()
//registry.Register(e)
//gather, err := registry.Gather()
//for _, m := range gather{
// registry.
//}.
if err := e.collect(ch); err != nil {
klog.Error(fmt.Sprintf("Error collecting stats: %s", err))
}
Expand Down

0 comments on commit 57fb93e

Please sign in to comment.