Skip to content

Commit

Permalink
fix(core): Correctly handle and report errors listing pools
Browse files Browse the repository at this point in the history
Fixes #18
  • Loading branch information
pdf committed Jan 30, 2022
1 parent 4d74fb1 commit efbcceb
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
19 changes: 13 additions & 6 deletions collector/zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,18 +121,20 @@ func (c *ZFS) Collect(ch chan<- prometheus.Metric) {
c.ready <- struct{}{}
}()

pools, err := c.getPools(c.Pools)
if err != nil {
_ = level.Error(c.logger).Log("msg", "Error finding pools", "err", err)
return
}
pools, poolErr := c.getPools(c.Pools)

for name, state := range c.Collectors {
if !*state.Enabled {
wg.Done()
continue
}

if poolErr != nil {
c.publishCollectorMetrics(ctx, name, poolErr, 0, proxy)
wg.Done()
continue
}

collector, err := state.factory(c.logger, c.client, strings.Split(*state.Properties, `,`))
if err != nil {
_ = level.Error(c.logger).Log("Error instantiating collector", "collector", name, "err", err)
Expand All @@ -147,7 +149,7 @@ func (c *ZFS) Collect(ch chan<- prometheus.Metric) {

// Wait for completion or timeout
<-ctx.Done()
err = ctx.Err()
err := ctx.Err()
if err == context.Canceled {
finalize()
} else if err != nil {
Expand Down Expand Up @@ -206,6 +208,11 @@ func (c *ZFS) execute(ctx context.Context, name string, collector Collector, ch
begin := time.Now()
err := collector.update(ch, pools, c.excludes)
duration := time.Since(begin)

c.publishCollectorMetrics(ctx, name, err, duration, ch)
}

func (c *ZFS) publishCollectorMetrics(ctx context.Context, name string, err error, duration time.Duration, ch chan<- metric) {
var success float64

if err != nil {
Expand Down
43 changes: 43 additions & 0 deletions collector/zfs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package collector

import (
"context"
"fmt"
"testing"

"github.com/golang/mock/gomock"
"github.com/pdf/zfs_exporter/v2/zfs/mock_zfs"
)

func TestZFSCollectInvalidPools(t *testing.T) {
const result = `# HELP zfs_scrape_collector_duration_seconds zfs_exporter: Duration of a collector scrape.
# TYPE zfs_scrape_collector_duration_seconds gauge
zfs_scrape_collector_duration_seconds{collector="pool"} 0
# HELP zfs_scrape_collector_success zfs_exporter: Whether a collector succeeded.
# TYPE zfs_scrape_collector_success gauge
zfs_scrape_collector_success{collector="pool"} 0
`

ctrl, ctx := gomock.WithContext(context.Background(), t)
zfsClient := mock_zfs.NewMockClient(ctrl)
zfsClient.EXPECT().PoolNames().Return(nil, fmt.Errorf(`Error returned from PoolNames()`)).Times(1)

config := defaultConfig(zfsClient)
config.DisableMetrics = false
collector, err := NewZFS(config)
collector.Collectors = map[string]State{
`pool`: {
Name: "pool",
Enabled: boolPointer(true),
Properties: stringPointer(``),
factory: newPoolCollector,
},
}
if err != nil {
t.Fatal(err)
}

if err = callCollector(ctx, collector, []byte(result), []string{`zfs_scrape_collector_duration_seconds`, `zfs_scrape_collector_success`}); err != nil {
t.Fatal(err)
}
}

0 comments on commit efbcceb

Please sign in to comment.