Skip to content

Commit

Permalink
feat(metrics): Allow disabling exporter metrics
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
pdf committed Sep 4, 2021
1 parent 066c7d2 commit 1ca8717
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 27 deletions.
46 changes: 28 additions & 18 deletions collector/zfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,25 +28,30 @@ func (c regexpCollection) MatchString(input string) bool {

// ZFSConfig configures a ZFS collector
type ZFSConfig struct {
Deadline time.Duration
Pools []string
Excludes []string
Logger log.Logger
DisableMetrics bool
Deadline time.Duration
Pools []string
Excludes []string
Logger log.Logger
}

// ZFS collector
type ZFS struct {
Pools []string
Collectors map[string]State
deadline time.Duration
cache *metricCache
ready chan struct{}
logger log.Logger
excludes regexpCollection
Pools []string
Collectors map[string]State
disableMetrics bool
deadline time.Duration
cache *metricCache
ready chan struct{}
logger log.Logger
excludes regexpCollection
}

// Describe implements the prometheus.Collector interface.
func (c *ZFS) Describe(ch chan<- *prometheus.Desc) {
if c.disableMetrics {
return
}
ch <- scrapeDurationDesc
ch <- scrapeSuccessDesc
}
Expand Down Expand Up @@ -207,6 +212,10 @@ func (c *ZFS) execute(ctx context.Context, name string, collector Collector, ch
success = 1
}
}

if c.disableMetrics {
return
}
ch <- metric{
name: scrapeDurationDescName,
prometheus: prometheus.MustNewConstMetric(scrapeDurationDesc, prometheus.GaugeValue, duration.Seconds(), name),
Expand All @@ -228,12 +237,13 @@ func NewZFS(config ZFSConfig) (*ZFS, error) {
ready := make(chan struct{}, 1)
ready <- struct{}{}
return &ZFS{
deadline: config.Deadline,
Pools: config.Pools,
Collectors: collectorStates,
excludes: excludes,
cache: newMetricCache(),
ready: ready,
logger: config.Logger,
disableMetrics: config.DisableMetrics,
deadline: config.Deadline,
Pools: config.Pools,
Collectors: collectorStates,
excludes: excludes,
cache: newMetricCache(),
ready: ready,
logger: config.Logger,
}, nil
}
25 changes: 16 additions & 9 deletions zfs_exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ import (

func main() {
var (
listenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface.").Default(":9134").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
deadline = kingpin.Flag("deadline", "Maximum duration that a collection should run before returning cached data. Should be set to a value shorter than your scrape timeout duration. The current collection run will continue and update the cache when complete (default: 8s)").Default("8s").Duration()
pools = kingpin.Flag("pool", "Name of the pool(s) to collect, repeat for multiple pools (default: all pools).").Strings()
excludes = kingpin.Flag("exclude", "Exclude datasets/snapshots/volumes that match the provided regex (e.g. '^rpool/docker/'), may be specified multiple times.").Strings()
listenAddress = kingpin.Flag("web.listen-address", "Address on which to expose metrics and web interface.").Default(":9134").String()
metricsPath = kingpin.Flag("web.telemetry-path", "Path under which to expose metrics.").Default("/metrics").String()
metricsExporterDisabled = kingpin.Flag(`web.disable-exporter-metrics`, `Exclude metrics about the exporter itself (promhttp_*, process_*, go_*).`).Default(`false`).Bool()
deadline = kingpin.Flag("deadline", "Maximum duration that a collection should run before returning cached data. Should be set to a value shorter than your scrape timeout duration. The current collection run will continue and update the cache when complete (default: 8s)").Default("8s").Duration()
pools = kingpin.Flag("pool", "Name of the pool(s) to collect, repeat for multiple pools (default: all pools).").Strings()
excludes = kingpin.Flag("exclude", "Exclude datasets/snapshots/volumes that match the provided regex (e.g. '^rpool/docker/'), may be specified multiple times.").Strings()
)

promlogConfig := &promlog.Config{}
Expand All @@ -36,16 +37,22 @@ func main() {
_ = level.Info(logger).Log("msg", "Build context", "context", version.BuildContext())

c, err := collector.NewZFS(collector.ZFSConfig{
Deadline: *deadline,
Pools: *pools,
Excludes: *excludes,
Logger: logger,
DisableMetrics: *metricsExporterDisabled,
Deadline: *deadline,
Pools: *pools,
Excludes: *excludes,
Logger: logger,
})
if err != nil {
_ = level.Error(logger).Log("msg", "Error creating an exporter", "err", err)
os.Exit(1)
}

if *metricsExporterDisabled {
r := prometheus.NewRegistry()
prometheus.DefaultRegisterer = r
prometheus.DefaultGatherer = r
}
prometheus.MustRegister(c)
prometheus.MustRegister(version.NewCollector("zfs_exporter"))

Expand Down

0 comments on commit 1ca8717

Please sign in to comment.