Skip to content

Commit

Permalink
add request_count metric to observe the exporter's impact for each sc…
Browse files Browse the repository at this point in the history
…rape
  • Loading branch information
gesellix committed Mar 11, 2019
1 parent 7415c5c commit 84c340f
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
14 changes: 11 additions & 3 deletions couchdb-exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func couchdbResponse(t *testing.T, versionSuffix string) Handler {
}
}

func performCouchdbStatsTest(t *testing.T, couchdbVersion string, expectedMetricsCount int, expectedGetRequestCount float64, expectedDiskSize float64) {
func performCouchdbStatsTest(t *testing.T, couchdbVersion string, expectedMetricsCount int, expectedGetRequestCount float64, expectedDiskSize float64, expectedRequestCount float64) {
basicAuth := lib.BasicAuth{Username: "username", Password: "password"}
handler := http.HandlerFunc(BasicAuthHandler(basicAuth, couchdbResponse(t, couchdbVersion)))
server := httptest.NewServer(handler)
Expand Down Expand Up @@ -148,14 +148,22 @@ func performCouchdbStatsTest(t *testing.T, couchdbVersion string, expectedMetric
if expectedDiskSize != actualDiskSize {
t.Errorf("expected %f disk size, but got %f instead", expectedDiskSize, actualDiskSize)
}

actualRequestCount, err := testutil.GetGaugeValue(metricFamilies, "couchdb_exporter_request_count", "", "")
if err != nil {
t.Error(err)
}
if expectedRequestCount != actualRequestCount {
t.Errorf("expected %f request count, but got %f instead", expectedRequestCount, actualRequestCount)
}
}

func TestCouchdbStatsV1(t *testing.T) {
performCouchdbStatsTest(t, "v1", 55, 4711, 12396)
performCouchdbStatsTest(t, "v1", 56, 4711, 12396, 11)
}

func TestCouchdbStatsV2(t *testing.T) {
performCouchdbStatsTest(t, "v2", 102, 4712, 58570)
performCouchdbStatsTest(t, "v2", 103, 4712, 58570, 14)
}

func TestCouchdbStatsV1Integration(t *testing.T) {
Expand Down
8 changes: 8 additions & 0 deletions lib/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func (e *Exporter) Describe(ch chan<- *prometheus.Desc) {
e.activeTasksReplicationLastUpdate.Describe(ch)

e.viewStaleness.Describe(ch)

e.requestCount.Describe(ch)
}

func (e *Exporter) resetAllMetrics() {
Expand Down Expand Up @@ -138,6 +140,9 @@ func (e *Exporter) collect(ch chan<- prometheus.Metric) error {

e.resetAllMetrics()

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

var databases, err = e.getObservedDatabaseNames(e.collectorConfig.Databases)
if err != nil {
return err
Expand All @@ -149,6 +154,7 @@ func (e *Exporter) collect(ch chan<- prometheus.Metric) error {
return fmt.Errorf("error collecting couchdb stats: %v", err)
}
e.up.Set(1)
e.requestCount.Set(float64(e.client.GetRequestCount()))

if stats.ApiVersion == "2" {
err = e.collectV2(stats, exposedHttpStatusCodes, e.collectorConfig)
Expand Down Expand Up @@ -199,6 +205,8 @@ func (e *Exporter) collect(ch chan<- prometheus.Metric) error {

e.viewStaleness.Collect(ch)

e.requestCount.Collect(ch)

return nil
}

Expand Down
34 changes: 29 additions & 5 deletions lib/couchdb-client.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ type BasicAuth struct {
}

type CouchdbClient struct {
BaseUri string
basicAuth BasicAuth
client *http.Client
BaseUri string
basicAuth BasicAuth
client *http.Client
ResetRequestCount func()
GetRequestCount func() int
}

type MembershipResponse struct {
Expand Down Expand Up @@ -342,16 +344,38 @@ func (c *CouchdbClient) Request(method string, uri string, body io.Reader) (resp
return respData, nil
}

type requestCountingRoundTripper struct {
RequestCount int
rt http.RoundTripper
}

func (rt *requestCountingRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
rt.RequestCount = rt.RequestCount + 1
//glog.Infof("req[%d] %s", rt.RequestCount, req.URL.String())
return rt.rt.RoundTrip(req)
}

func NewCouchdbClient(uri string, basicAuth BasicAuth, insecure bool) *CouchdbClient {
httpClient := &http.Client{
Transport: &http.Transport{
countingRoundTripper := &requestCountingRoundTripper{
0,
&http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
},
}

httpClient := &http.Client{
Transport: countingRoundTripper,
}

return &CouchdbClient{
BaseUri: uri,
basicAuth: basicAuth,
client: httpClient,
ResetRequestCount: func() {
countingRoundTripper.RequestCount = 0
},
GetRequestCount: func() int {
return countingRoundTripper.RequestCount
},
}
}
11 changes: 11 additions & 0 deletions lib/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ type Exporter struct {
collectorConfig CollectorConfig
mutex sync.RWMutex

requestCount prometheus.Gauge

up prometheus.Gauge
databasesTotal prometheus.Gauge
nodeUp *prometheus.GaugeVec
Expand Down Expand Up @@ -59,13 +61,22 @@ func NewExporter(uri string, basicAuth BasicAuth, collectorConfig CollectorConfi
client: NewCouchdbClient(uri, basicAuth, insecure),
collectorConfig: collectorConfig,

requestCount: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "exporter",
Name: "request_count",
Help: "Number of CouchDB requests for this scrape.",
}),

up: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Subsystem: "httpd",
Name: "up",
Help: "Was the last query of CouchDB stats successful.",
}),

databasesTotal: prometheus.NewGauge(
prometheus.GaugeOpts{
Namespace: namespace,
Expand Down
3 changes: 3 additions & 0 deletions testutil/metric-reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ func GetGaugeValue(metricFamilies map[string]*dto.MetricFamily, metricDesc strin
for desc, metrics := range metricFamilies {
if metricDesc == "" || desc == metricDesc {
for _, metric := range metrics.Metric {
if len(metric.Label) == 0 && labelName == "" && labelValue == "" {
return *metric.Gauge.Value, nil
}
for _, label := range metric.Label {
if *label.Name == labelName && *label.Value == labelValue {
return *metric.Gauge.Value, nil
Expand Down

0 comments on commit 84c340f

Please sign in to comment.