Skip to content

Commit

Permalink
chore: Refactor Config Collector API (#1558)
Browse files Browse the repository at this point in the history
  • Loading branch information
jkroepke authored Aug 11, 2024
1 parent 7bb16d2 commit dd956c9
Show file tree
Hide file tree
Showing 62 changed files with 1,174 additions and 779 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
run: |
$ErrorActionPreference = "Stop"
$Version = git describe --tag
$Version = git describe --tags --always
$Version = $Version -replace 'v', ''
# '+' symbols are invalid characters in image tags
$Version = $Version -replace '\+', '_'
Expand Down
18 changes: 10 additions & 8 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ import (
"golang.org/x/sys/windows"
)

// https://learn.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights
const PROCESS_ALL_ACCESS = windows.STANDARD_RIGHTS_REQUIRED | windows.SYNCHRONIZE | windows.SPECIFIC_RIGHTS_ALL

// Same struct prometheus uses for their /version endpoint.
// Separate copy to avoid pulling all of prometheus as a dependency.
type prometheusVersion struct {
Expand All @@ -63,18 +60,23 @@ var priorityStringToInt = map[string]uint32{
}

func setPriorityWindows(pid int, priority uint32) error {
handle, err := windows.OpenProcess(PROCESS_ALL_ACCESS, false, uint32(pid))
// https://learn.microsoft.com/en-us/windows/win32/procthread/process-security-and-access-rights
handle, err := windows.OpenProcess(
windows.STANDARD_RIGHTS_REQUIRED|windows.SYNCHRONIZE|windows.SPECIFIC_RIGHTS_ALL,
false, uint32(pid),
)
if err != nil {
return err
}
//nolint:errcheck
defer windows.CloseHandle(handle) // Technically this can fail, but we ignore it

err = windows.SetPriorityClass(handle, priority)
if err != nil {
if err = windows.SetPriorityClass(handle, priority); err != nil {
return err
}

if err = windows.CloseHandle(handle); err != nil {
return fmt.Errorf("failed to close handle: %w", err)
}

return nil
}

Expand Down
12 changes: 10 additions & 2 deletions pkg/collector/ad/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var ConfigDefaults = Config{}

// A Collector is a Prometheus Collector for WMI Win32_PerfRawData_DirectoryServices_DirectoryServices metrics.
type Collector struct {
config Config
logger log.Logger

addressBookClientSessions *prometheus.Desc
Expand Down Expand Up @@ -87,8 +88,15 @@ type Collector struct {
tombstonesObjectsVisitedTotal *prometheus.Desc
}

func New(logger log.Logger, _ *Config) *Collector {
c := &Collector{}
func New(logger log.Logger, config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}

c := &Collector{
config: *config,
}

c.SetLogger(logger)

return c
Expand Down
12 changes: 10 additions & 2 deletions pkg/collector/adcs/adcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Config struct{}
var ConfigDefaults = Config{}

type Collector struct {
config Config
logger log.Logger

challengeResponseProcessingTime *prometheus.Desc
Expand All @@ -39,8 +40,15 @@ type Collector struct {
signedCertificateTimestampListsPerSecond *prometheus.Desc
}

func New(logger log.Logger, _ *Config) *Collector {
c := &Collector{}
func New(logger log.Logger, config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}

c := &Collector{
config: *config,
}

c.SetLogger(logger)

return c
Expand Down
12 changes: 10 additions & 2 deletions pkg/collector/adfs/adfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Config struct{}
var ConfigDefaults = Config{}

type Collector struct {
config Config
logger log.Logger

adLoginConnectionFailures *prometheus.Desc
Expand Down Expand Up @@ -66,8 +67,15 @@ type Collector struct {
wstrustTokenRequests *prometheus.Desc
}

func New(logger log.Logger, _ *Config) *Collector {
c := &Collector{}
func New(logger log.Logger, config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}

c := &Collector{
config: *config,
}

c.SetLogger(logger)

return c
Expand Down
12 changes: 10 additions & 2 deletions pkg/collector/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ var ConfigDefaults = Config{}

// A Collector is a Prometheus Collector for Perflib Cache metrics.
type Collector struct {
config Config
logger log.Logger

asyncCopyReadsTotal *prometheus.Desc
Expand Down Expand Up @@ -53,8 +54,15 @@ type Collector struct {
syncPinReadsTotal *prometheus.Desc
}

func New(logger log.Logger, _ *Config) *Collector {
c := &Collector{}
func New(logger log.Logger, config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}

c := &Collector{
config: *config,
}

c.SetLogger(logger)

return c
Expand Down
12 changes: 10 additions & 2 deletions pkg/collector/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var ConfigDefaults = Config{}

// A Collector is a Prometheus Collector for containers metrics.
type Collector struct {
config Config
logger log.Logger

// Presence
Expand Down Expand Up @@ -56,8 +57,15 @@ type Collector struct {
}

// New constructs a new Collector.
func New(logger log.Logger, _ *Config) *Collector {
c := &Collector{}
func New(logger log.Logger, config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}

c := &Collector{
config: *config,
}

c.SetLogger(logger)

return c
Expand Down
12 changes: 10 additions & 2 deletions pkg/collector/cpu/cpu.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type Config struct{}
var ConfigDefaults = Config{}

type Collector struct {
config Config
logger log.Logger

cStateSecondsTotal *prometheus.Desc
Expand All @@ -37,8 +38,15 @@ type Collector struct {
processorPrivilegedUtility *prometheus.Desc
}

func New(logger log.Logger, _ *Config) *Collector {
c := &Collector{}
func New(logger log.Logger, config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}

c := &Collector{
config: *config,
}

c.SetLogger(logger)

return c
Expand Down
12 changes: 10 additions & 2 deletions pkg/collector/cpu_info/cpu_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,21 @@ var ConfigDefaults = Config{}

// A Collector is a Prometheus Collector for a few WMI metrics in Win32_Processor.
type Collector struct {
config Config
logger log.Logger

cpuInfo *prometheus.Desc
}

func New(logger log.Logger, _ *Config) *Collector {
c := &Collector{}
func New(logger log.Logger, config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}

c := &Collector{
config: *config,
}

c.SetLogger(logger)

return c
Expand Down
12 changes: 10 additions & 2 deletions pkg/collector/cs/cs.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,23 @@ var ConfigDefaults = Config{}

// A Collector is a Prometheus Collector for WMI metrics.
type Collector struct {
config Config
logger log.Logger

physicalMemoryBytes *prometheus.Desc
logicalProcessors *prometheus.Desc
hostname *prometheus.Desc
}

func New(logger log.Logger, _ *Config) *Collector {
c := &Collector{}
func New(logger log.Logger, config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}

c := &Collector{
config: *config,
}

c.SetLogger(logger)

return c
Expand Down
42 changes: 27 additions & 15 deletions pkg/collector/dfsr/dfsr.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,32 @@
package dfsr

import (
"slices"
"strings"

"github.com/alecthomas/kingpin/v2"
"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/prometheus-community/windows_exporter/pkg/perflib"
"github.com/prometheus-community/windows_exporter/pkg/types"
"github.com/prometheus-community/windows_exporter/pkg/utils"
"github.com/prometheus/client_golang/prometheus"
)

const Name = "dfsr"

type Config struct {
EnabledCollectors string `yaml:"enabled_collectors"`
CollectorsEnabled []string `yaml:"collectors_enabled"`
}

var ConfigDefaults = Config{
EnabledCollectors: "connection,folder,volume",
CollectorsEnabled: []string{"connection", "folder", "volume"},
}

// Collector contains the metric and state data of the DFSR collectors.
type Collector struct {
config Config
logger log.Logger

dfsrEnabledCollectors *string

// connection source
connectionBandwidthSavingsUsingDFSReplicationTotal *prometheus.Desc
connectionBytesReceivedTotal *prometheus.Desc
Expand Down Expand Up @@ -102,21 +103,29 @@ func New(logger log.Logger, config *Config) *Collector {
config = &ConfigDefaults
}

if config.CollectorsEnabled == nil {
config.CollectorsEnabled = ConfigDefaults.CollectorsEnabled
}

c := &Collector{
dfsrEnabledCollectors: &config.EnabledCollectors,
config: *config,
}

c.SetLogger(logger)

return c
}

func NewWithFlags(app *kingpin.Application) *Collector {
return &Collector{
dfsrEnabledCollectors: app.
Flag("collectors.dfsr.sources-enabled", "Comma-separated list of DFSR Perflib sources to use.").
Default(ConfigDefaults.EnabledCollectors).
String(),
c := &Collector{
config: ConfigDefaults,
}
c.config.CollectorsEnabled = make([]string, 0)

app.Flag("collectors.dfsr.sources-enabled", "Comma-separated list of DFSR Perflib sources to use.").
Default(strings.Join(ConfigDefaults.CollectorsEnabled, ",")).StringsVar(&c.config.CollectorsEnabled)

return c
}

func (c *Collector) GetName() string {
Expand All @@ -129,8 +138,9 @@ func (c *Collector) SetLogger(logger log.Logger) {

func (c *Collector) GetPerfCounter() ([]string, error) {
// Perflib sources are dynamic, depending on the enabled child collectors
expandedChildCollectors := utils.ExpandEnabledChildCollectors(*c.dfsrEnabledCollectors)
expandedChildCollectors := slices.Compact(c.config.CollectorsEnabled)
perflibDependencies := make([]string, 0, len(expandedChildCollectors))

for _, source := range expandedChildCollectors {
perflibDependencies = append(perflibDependencies, dfsrGetPerfObjectName(source))
}
Expand Down Expand Up @@ -209,8 +219,8 @@ func (c *Collector) Build() error {
nil,
)

c. // folder
folderBandwidthSavingsUsingDFSReplicationTotal = prometheus.NewDesc(
// folder
c.folderBandwidthSavingsUsingDFSReplicationTotal = prometheus.NewDesc(
prometheus.BuildFQName(types.Namespace, Name, "folder_bandwidth_savings_using_dfs_replication_bytes_total"),
"Total bytes of bandwidth saved using DFS Replication for this folder",
[]string{"name"},
Expand Down Expand Up @@ -435,7 +445,9 @@ func (c *Collector) Build() error {
nil,
)

c.dfsrChildCollectors = c.getDFSRChildCollectors(utils.ExpandEnabledChildCollectors(*c.dfsrEnabledCollectors))
// Perflib sources are dynamic, depending on the enabled child collectors
expandedChildCollectors := slices.Compact(c.config.CollectorsEnabled)
c.dfsrChildCollectors = c.getDFSRChildCollectors(expandedChildCollectors)

return nil
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/collector/dhcp/dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var ConfigDefaults = Config{}

// A Collector is a Prometheus Collector perflib DHCP metrics.
type Collector struct {
config Config
logger log.Logger

acksTotal *prometheus.Desc
Expand Down Expand Up @@ -47,8 +48,15 @@ type Collector struct {
requestsTotal *prometheus.Desc
}

func New(logger log.Logger, _ *Config) *Collector {
c := &Collector{}
func New(logger log.Logger, config *Config) *Collector {
if config == nil {
config = &ConfigDefaults
}

c := &Collector{
config: *config,
}

c.SetLogger(logger)

return c
Expand Down
Loading

0 comments on commit dd956c9

Please sign in to comment.