Skip to content

Commit

Permalink
Merge pull request prometheus-community#1170 from breed808/include_ex…
Browse files Browse the repository at this point in the history
…clude

feat!: Deprecate whitelist/blacklist flags
  • Loading branch information
breed808 authored Apr 18, 2023
2 parents e15ff6c + 129b904 commit 1d3847f
Show file tree
Hide file tree
Showing 17 changed files with 465 additions and 161 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ Name | Description
`LISTEN_PORT` | The port to bind to. Defaults to 9182.
`METRICS_PATH` | The path at which to serve metrics. Defaults to `/metrics`
`TEXTFILE_DIR` | As the `--collector.textfile.directory` flag, provide a directory to read text files with metrics from
`REMOTE_ADDR` | Allows setting comma separated remote IP addresses for the Windows Firewall exception (whitelist). Defaults to an empty string (any remote address).
`REMOTE_ADDR` | Allows setting comma separated remote IP addresses for the Windows Firewall exception (allow list). Defaults to an empty string (any remote address).
`EXTRA_FLAGS` | Allows passing full CLI flags. Defaults to an empty string.

Parameters are sent to the installer via `msiexec`. Example invocations:
Expand Down Expand Up @@ -150,7 +150,7 @@ The prometheus metrics will be exposed on [localhost:9182](http://localhost:9182

### Enable only process collector and specify a custom query

.\windows_exporter.exe --collectors.enabled "process" --collector.process.whitelist="firefox.+"
.\windows_exporter.exe --collectors.enabled "process" --collector.process.include="firefox.+"

When there are multiple processes with the same name, WMI represents those after the first instance as `process-name#index`. So to get them all, rather than just the first one, the [regular expression](https://en.wikipedia.org/wiki/Regular_expression) must use `.+`. See [process](docs/collector.process.md) for more information.

Expand Down
137 changes: 109 additions & 28 deletions collector/iis.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package collector

import (
"errors"
"fmt"
"regexp"

Expand All @@ -14,17 +15,32 @@ import (
)

const (
FlagISSSiteBlacklist = "collector.iis.site-blacklist"
FlagISSSiteWhitelist = "collector.iis.site-whitelist"
FlagISSAppBlacklist = "collector.iis.app-blacklist"
FlagISSAppWhitelist = "collector.iis.app-whitelist"
FlagIISSiteOldExclude = "collector.iis.site-blacklist"
FlagIISSiteOldInclude = "collector.iis.site-whitelist"
FlagIISAppOldExclude = "collector.iis.app-blacklist"
FlagIISAppOldInclude = "collector.iis.app-whitelist"

FlagIISSiteExclude = "collector.iis.site-exclude"
FlagIISSiteInclude = "collector.iis.site-include"
FlagIISAppExclude = "collector.iis.app-exclude"
FlagIISAppInclude = "collector.iis.app-include"
)

var (
siteWhitelist *string
siteBlacklist *string
appWhitelist *string
appBlacklist *string
oldSiteInclude *string
oldSiteExclude *string
oldAppInclude *string
oldAppExclude *string

siteInclude *string
siteExclude *string
appInclude *string
appExclude *string

siteIncludeSet bool
siteExcludeSet bool
appIncludeSet bool
appExcludeSet bool
)

type simple_version struct {
Expand Down Expand Up @@ -89,8 +105,8 @@ type IISCollector struct {
TotalNotFoundErrors *prometheus.Desc
TotalRejectedAsyncIORequests *prometheus.Desc

siteWhitelistPattern *regexp.Regexp
siteBlacklistPattern *regexp.Regexp
siteIncludePattern *regexp.Regexp
siteExcludePattern *regexp.Regexp

// APP_POOL_WAS
CurrentApplicationPoolState *prometheus.Desc
Expand Down Expand Up @@ -192,29 +208,94 @@ type IISCollector struct {
ServiceCache_OutputCacheFlushedItemsTotal *prometheus.Desc
ServiceCache_OutputCacheFlushesTotal *prometheus.Desc

appWhitelistPattern *regexp.Regexp
appBlacklistPattern *regexp.Regexp
appIncludePattern *regexp.Regexp
appExcludePattern *regexp.Regexp

iis_version simple_version
}

func newIISCollectorFlags(app *kingpin.Application) {
siteWhitelist = kingpin.Flag(FlagISSSiteWhitelist, "Regexp of sites to whitelist. Site name must both match whitelist and not match blacklist to be included.").Default(".+").String()
siteBlacklist = kingpin.Flag(FlagISSSiteBlacklist, "Regexp of sites to blacklist. Site name must both match whitelist and not match blacklist to be included.").String()
appWhitelist = kingpin.Flag(FlagISSAppWhitelist, "Regexp of apps to whitelist. App name must both match whitelist and not match blacklist to be included.").Default(".+").String()
appBlacklist = kingpin.Flag(FlagISSAppBlacklist, "Regexp of apps to blacklist. App name must both match whitelist and not match blacklist to be included.").String()
oldSiteInclude = app.Flag(FlagIISSiteOldInclude, "DEPRECATED: Use --collector.iis.site-include").Default(".+").Hidden().String()
oldSiteExclude = app.Flag(FlagIISSiteOldExclude, "DEPRECATED: Use --collector.iis.site-exclude").Hidden().String()
oldAppInclude = app.Flag(FlagIISAppOldInclude, "DEPRECATED: Use --collector.iis.app-include").Hidden().String()
oldAppExclude = app.Flag(FlagIISAppOldExclude, "DEPRECATED: Use --collector.iis.app-exclude").Hidden().String()

siteInclude = app.Flag(
FlagIISSiteInclude,
"Regexp of sites to include. Site name must both match include and not match exclude to be included.",
).Default(".+").PreAction(func(c *kingpin.ParseContext) error {
siteIncludeSet = true
return nil
}).String()

siteExclude = app.Flag(
FlagIISSiteExclude,
"Regexp of sites to exclude. Site name must both match include and not match exclude to be included.",
).Default("").PreAction(func(c *kingpin.ParseContext) error {
siteExcludeSet = true
return nil
}).String()

appInclude = app.Flag(
FlagIISAppInclude,
"Regexp of apps to include. App name must both match include and not match exclude to be included.",
).Default(".+").PreAction(func(c *kingpin.ParseContext) error {
appIncludeSet = true
return nil
}).String()

appExclude = app.Flag(
FlagIISAppExclude,
"Regexp of apps to include. App name must both match include and not match exclude to be included.",
).Default("").PreAction(func(c *kingpin.ParseContext) error {
siteExcludeSet = true
return nil
}).String()
}

func newIISCollector() (Collector, error) {
const subsystem = "iis"
if *oldSiteExclude != "" {
if !siteExcludeSet {
log.Warnln("msg", "--collector.iis.site-blacklist is DEPRECATED and will be removed in a future release, use --collector.iis.site-exclude")
*siteExclude = *oldSiteExclude
} else {
return nil, errors.New("--collector.iis.site-blacklist and --collector.iis.site-exclude are mutually exclusive")
}
}
if *oldSiteInclude != "" {
if !siteIncludeSet {
log.Warnln("msg", "--collector.iis.site-whitelist is DEPRECATED and will be removed in a future release, use --collector.iis.site-include")
*siteInclude = *oldSiteInclude
} else {
return nil, errors.New("--collector.iis.site-whitelist and --collector.iis.site-include are mutually exclusive")
}
}

if *oldAppExclude != "" {
if !appExcludeSet {
log.Warnln("msg", "--collector.iis.app-blacklist is DEPRECATED and will be removed in a future release, use --collector.iis.app-exclude")
*appExclude = *oldAppExclude
} else {
return nil, errors.New("--collector.iis.app-blacklist and --collector.iis.app-exclude are mutually exclusive")
}
}
if *oldAppInclude != "" {
if !appIncludeSet {
log.Warnln("msg", "--collector.iis.app-whitelist is DEPRECATED and will be removed in a future release, use --collector.iis.app-include")
*appInclude = *oldAppInclude
} else {
return nil, errors.New("--collector.iis.app-whitelist and --collector.iis.app-include are mutually exclusive")
}
}

const subsystem = "iis"
return &IISCollector{
iis_version: getIISVersion(),

siteWhitelistPattern: regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *siteWhitelist)),
siteBlacklistPattern: regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *siteBlacklist)),
appWhitelistPattern: regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *appWhitelist)),
appBlacklistPattern: regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *appBlacklist)),
siteIncludePattern: regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *siteInclude)),
siteExcludePattern: regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *siteExclude)),
appIncludePattern: regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *appInclude)),
appExcludePattern: regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *appExclude)),

// Web Service
CurrentAnonymousUsers: prometheus.NewDesc(
Expand Down Expand Up @@ -903,7 +984,7 @@ func (c *IISCollector) collectWebService(ctx *ScrapeContext, ch chan<- prometheu
}

for _, app := range WebService {
if app.Name == "_Total" || c.siteBlacklistPattern.MatchString(app.Name) || !c.siteWhitelistPattern.MatchString(app.Name) {
if app.Name == "_Total" || c.siteExcludePattern.MatchString(app.Name) || !c.siteIncludePattern.MatchString(app.Name) {
continue
}

Expand Down Expand Up @@ -1188,8 +1269,8 @@ func (c *IISCollector) collectAPP_POOL_WAS(ctx *ScrapeContext, ch chan<- prometh

for _, app := range APP_POOL_WAS {
if app.Name == "_Total" ||
c.appBlacklistPattern.MatchString(app.Name) ||
!c.appWhitelistPattern.MatchString(app.Name) {
c.appExcludePattern.MatchString(app.Name) ||
!c.appIncludePattern.MatchString(app.Name) {
continue
}

Expand Down Expand Up @@ -1366,8 +1447,8 @@ func (c *IISCollector) collectW3SVC_W3WP(ctx *ScrapeContext, ch chan<- prometheu
pid := workerProcessNameExtractor.ReplaceAllString(app.Name, "$1")
name := workerProcessNameExtractor.ReplaceAllString(app.Name, "$2")
if name == "" || name == "_Total" ||
c.appBlacklistPattern.MatchString(name) ||
!c.appWhitelistPattern.MatchString(name) {
c.appExcludePattern.MatchString(name) ||
!c.appIncludePattern.MatchString(name) {
continue
}

Expand Down Expand Up @@ -1618,8 +1699,8 @@ func (c *IISCollector) collectW3SVC_W3WP(ctx *ScrapeContext, ch chan<- prometheu
pid := workerProcessNameExtractor.ReplaceAllString(app.Name, "$1")
name := workerProcessNameExtractor.ReplaceAllString(app.Name, "$2")
if name == "" || name == "_Total" ||
c.appBlacklistPattern.MatchString(name) ||
!c.appWhitelistPattern.MatchString(name) {
c.appExcludePattern.MatchString(name) ||
!c.appIncludePattern.MatchString(name) {
continue
}

Expand Down
79 changes: 61 additions & 18 deletions collector/logical_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package collector

import (
"errors"
"fmt"
"regexp"

Expand All @@ -13,13 +14,22 @@ import (
)

const (
FlagLogicalDiskVolumeBlacklist = "collector.logical_disk.volume-blacklist"
FlagLogicalDiskVolumeWhitelist = "collector.logical_disk.volume-whitelist"
FlagLogicalDiskVolumeOldExclude = "collector.logical_disk.volume-blacklist"
FlagLogicalDiskVolumeOldInclude = "collector.logical_disk.volume-whitelist"

FlagLogicalDiskVolumeExclude = "collector.logical_disk.volume-exclude"
FlagLogicalDiskVolumeInclude = "collector.logical_disk.volume-include"
)

var (
volumeWhitelist *string
volumeBlacklist *string
volumeOldInclude *string
volumeOldExclude *string

volumeInclude *string
volumeExclude *string

volumeIncludeSet bool
volumeExcludeSet bool
)

// A LogicalDiskCollector is a Prometheus collector for perflib logicalDisk metrics
Expand All @@ -41,24 +51,57 @@ type LogicalDiskCollector struct {
WriteLatency *prometheus.Desc
ReadWriteLatency *prometheus.Desc

volumeWhitelistPattern *regexp.Regexp
volumeBlacklistPattern *regexp.Regexp
volumeIncludePattern *regexp.Regexp
volumeExcludePattern *regexp.Regexp
}

// newLogicalDiskCollectorFlags ...
func newLogicalDiskCollectorFlags(app *kingpin.Application) {
volumeWhitelist = app.Flag(
FlagLogicalDiskVolumeWhitelist,
"Regexp of volumes to whitelist. Volume name must both match whitelist and not match blacklist to be included.",
).Default(".+").String()
volumeBlacklist = app.Flag(
FlagLogicalDiskVolumeBlacklist,
"Regexp of volumes to blacklist. Volume name must both match whitelist and not match blacklist to be included.",
).Default("").String()
volumeInclude = app.Flag(
FlagLogicalDiskVolumeInclude,
"Regexp of volumes to include. Volume name must both match include and not match exclude to be included.",
).Default(".+").PreAction(func(c *kingpin.ParseContext) error {
volumeIncludeSet = true
return nil
}).String()

volumeExclude = app.Flag(
FlagLogicalDiskVolumeExclude,
"Regexp of volumes to exclude. Volume name must both match include and not match exclude to be included.",
).Default("").PreAction(func(c *kingpin.ParseContext) error {
volumeExcludeSet = true
return nil
}).String()

volumeOldInclude = app.Flag(
FlagLogicalDiskVolumeOldInclude,
"DEPRECATED: Use --collector.logical_disk.volume-include",
).Hidden().String()
volumeOldExclude = app.Flag(
FlagLogicalDiskVolumeOldExclude,
"DEPRECATED: Use --collector.logical_disk.volume-exclude",
).Hidden().String()
}

// newLogicalDiskCollector ...
func newLogicalDiskCollector() (Collector, error) {
if *volumeOldExclude != "" {
if !volumeExcludeSet {
log.Warnln("msg", "--collector.logical_disk.volume-blacklist is DEPRECATED and will be removed in a future release, use --collector.logical_disk.volume-exclude")
*volumeExclude = *volumeOldExclude
} else {
return nil, errors.New("--collector.logical_disk.volume-blacklist and --collector.logical_disk.volume-exclude are mutually exclusive")
}
}
if *volumeOldInclude != "" {
if !volumeIncludeSet {
log.Warnln("msg", "--collector.logical_disk.volume-whitelist is DEPRECATED and will be removed in a future release, use --collector.logical_disk.volume-include")
*volumeInclude = *volumeOldInclude
} else {
return nil, errors.New("--collector.logical_disk.volume-whitelist and --collector.logical_disk.volume-include are mutually exclusive")
}
}

const subsystem = "logical_disk"

return &LogicalDiskCollector{
Expand Down Expand Up @@ -174,8 +217,8 @@ func newLogicalDiskCollector() (Collector, error) {
nil,
),

volumeWhitelistPattern: regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *volumeWhitelist)),
volumeBlacklistPattern: regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *volumeBlacklist)),
volumeIncludePattern: regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *volumeInclude)),
volumeExcludePattern: regexp.MustCompile(fmt.Sprintf("^(?:%s)$", *volumeExclude)),
}, nil
}

Expand Down Expand Up @@ -220,8 +263,8 @@ func (c *LogicalDiskCollector) collect(ctx *ScrapeContext, ch chan<- prometheus.

for _, volume := range dst {
if volume.Name == "_Total" ||
c.volumeBlacklistPattern.MatchString(volume.Name) ||
!c.volumeWhitelistPattern.MatchString(volume.Name) {
c.volumeExcludePattern.MatchString(volume.Name) ||
!c.volumeIncludePattern.MatchString(volume.Name) {
continue
}

Expand Down
4 changes: 2 additions & 2 deletions collector/logical_disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

func BenchmarkLogicalDiskCollector(b *testing.B) {
// Whitelist is not set in testing context (kingpin flags not parsed), causing the collector to skip all disks.
localVolumeWhitelist := ".+"
volumeWhitelist = &localVolumeWhitelist
localVolumeInclude := ".+"
volumeInclude = &localVolumeInclude

benchmarkCollector(b, "logical_disk", newLogicalDiskCollector)
}
Loading

0 comments on commit 1d3847f

Please sign in to comment.