Skip to content

Commit

Permalink
chore: Remove registry based perfdata collector (#1742)
Browse files Browse the repository at this point in the history
Signed-off-by: Jan-Otto Kröpke <[email protected]>
  • Loading branch information
jkroepke authored Nov 17, 2024
1 parent 6206b69 commit e6a15d4
Show file tree
Hide file tree
Showing 213 changed files with 8,063 additions and 12,389 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ output/
*.syso
installer/*.msi
installer/*.wixpdb
local/
local/
!.idea/inspectionProfiles/Project_Default.xml
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ linters-settings:

gci:
sections:
- prefix(github.com/prometheus-community/windows_exporter/internal/initiate)
- prefix(github.com/prometheus-community/windows_exporter/internal/windowsservice)
- standard # Standard section: captures all standard packages.
- default # Default section: contains all imports that could not be matched to another section type.
custom-order: true
Expand Down
72 changes: 19 additions & 53 deletions exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ package main
//goland:noinspection GoUnsortedImport
//nolint:gofumpt
import (
"github.com/prometheus-community/windows_exporter/internal/initiate"
// Its important that we do these first so that we can register with the Windows service control ASAP to avoid timeouts.
"github.com/prometheus-community/windows_exporter/internal/windowsservice"

"context"
"errors"
Expand All @@ -19,7 +20,7 @@ import (
"os/signal"
"os/user"
"runtime"
"sort"
"slices"
"strings"
"time"

Expand All @@ -28,9 +29,6 @@ import (
"github.com/prometheus-community/windows_exporter/internal/httphandler"
"github.com/prometheus-community/windows_exporter/internal/log"
"github.com/prometheus-community/windows_exporter/internal/log/flag"
"github.com/prometheus-community/windows_exporter/internal/toggle"
"github.com/prometheus-community/windows_exporter/internal/types"
"github.com/prometheus-community/windows_exporter/internal/utils"
"github.com/prometheus-community/windows_exporter/pkg/collector"
"github.com/prometheus/common/version"
"github.com/prometheus/exporter-toolkit/web"
Expand All @@ -42,14 +40,14 @@ func main() {
exitCode := run()

// If we are running as a service, we need to signal the service control manager that we are done.
if !initiate.IsService {
if !windowsservice.IsService {
os.Exit(exitCode)
}

initiate.ExitCodeCh <- exitCode
windowsservice.ExitCodeCh <- exitCode

// Wait for the service control manager to signal that we are done.
<-initiate.StopCh
<-windowsservice.StopCh
}

func run() int {
Expand Down Expand Up @@ -80,11 +78,7 @@ func run() int {
enabledCollectors = app.Flag(
"collectors.enabled",
"Comma-separated list of collectors to use. Use '[defaults]' as a placeholder for all the collectors enabled by default.").
Default(types.DefaultCollectors).String()
printCollectors = app.Flag(
"collectors.print",
"If true, print available collectors and exit.",
).Bool()
Default(collector.DefaultCollectors).String()
timeoutMargin = app.Flag(
"scrape.timeout-margin",
"Seconds to subtract from the timeout allowed by the client. Tune to allow for overhead or high loads.",
Expand All @@ -97,11 +91,6 @@ func run() int {
"process.priority",
"Priority of the exporter process. Higher priorities may improve exporter responsiveness during periods of system load. Can be one of [\"realtime\", \"high\", \"abovenormal\", \"normal\", \"belownormal\", \"low\"]",
).Default("normal").String()

togglePDH = app.Flag(
"perfcounter.engine",
"EXPERIMENTAL: Performance counter engine to use. Can be one of \"pdh\", \"registry\". PDH is in experimental state. This flag will be removed in 0.31.",
).Default("registry").String()
)

logConfig := &log.Config{}
Expand Down Expand Up @@ -179,18 +168,6 @@ func run() int {

logger.Debug("Logging has Started")

if v, ok := os.LookupEnv("WINDOWS_EXPORTER_PERF_COUNTERS_ENGINE"); ok && v == "pdh" || *togglePDH == "pdh" {
logger.Info("Using performance data helper from PHD.dll for performance counter collection. This is in experimental state.")

toggle.PHDEnabled = true
}

if *printCollectors {
printCollectorsToStdout()

return 0
}

if err = setPriorityWindows(logger, os.Getpid(), *processPriority); err != nil {
logger.Error("failed to set process priority",
slog.Any("err", err),
Expand All @@ -199,9 +176,11 @@ func run() int {
return 1
}

enabledCollectorList := utils.ExpandEnabledCollectors(*enabledCollectors)
enabledCollectorList := expandEnabledCollectors(*enabledCollectors)
if err := collectors.Enable(enabledCollectorList); err != nil {
logger.Error(err.Error())
logger.Error("Couldn't enable collectors",
slog.Any("err", err),
)

return 1
}
Expand All @@ -215,14 +194,6 @@ func run() int {
return 1
}

if err = collectors.SetPerfCounterQuery(logger); err != nil {
logger.Error("Couldn't set performance counter query",
slog.Any("err", err),
)

return 1
}

logCurrentUser(logger)

logger.Info("Enabled collectors: " + strings.Join(enabledCollectorList, ", "))
Expand Down Expand Up @@ -268,7 +239,7 @@ func run() int {
errCh <- err
}

errCh <- nil
close(errCh)
}()

ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill)
Expand All @@ -277,7 +248,7 @@ func run() int {
select {
case <-ctx.Done():
logger.Info("Shutting down windows_exporter via kill signal")
case <-initiate.StopCh:
case <-windowsservice.StopCh:
logger.Info("Shutting down windows_exporter via service control")
case err := <-errCh:
if err != nil {
Expand All @@ -299,17 +270,6 @@ func run() int {
return 0
}

func printCollectorsToStdout() {
collectorNames := collector.Available()
sort.Strings(collectorNames)

fmt.Println("Available collectors:") //nolint:forbidigo

for _, n := range collectorNames {
fmt.Printf(" - %s\n", n) //nolint:forbidigo
}
}

func logCurrentUser(logger *slog.Logger) {
u, err := user.Current()
if err != nil {
Expand Down Expand Up @@ -367,3 +327,9 @@ func setPriorityWindows(logger *slog.Logger, pid int, priority string) error {

return nil
}

func expandEnabledCollectors(enabled string) []string {
expanded := strings.ReplaceAll(enabled, "[defaults]", collector.DefaultCollectors)

return slices.Compact(strings.Split(expanded, ","))
}
22 changes: 5 additions & 17 deletions internal/collector/ad/ad.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/alecthomas/kingpin/v2"
"github.com/prometheus-community/windows_exporter/internal/mi"
"github.com/prometheus-community/windows_exporter/internal/perfdata"
"github.com/prometheus-community/windows_exporter/internal/toggle"
"github.com/prometheus-community/windows_exporter/internal/types"
"github.com/prometheus/client_golang/prometheus"
)
Expand All @@ -21,11 +20,10 @@ type Config struct{}

var ConfigDefaults = Config{}

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

perfDataCollector perfdata.Collector
perfDataCollector *perfdata.Collector

addressBookClientSessions *prometheus.Desc
addressBookOperationsTotal *prometheus.Desc
Expand Down Expand Up @@ -111,14 +109,8 @@ func (c *Collector) GetName() string {
return Name
}

func (c *Collector) GetPerfCounter(_ *slog.Logger) ([]string, error) {
return []string{}, nil
}

func (c *Collector) Close(_ *slog.Logger) error {
if toggle.IsPDHEnabled() {
c.perfDataCollector.Close()
}
func (c *Collector) Close() error {
c.perfDataCollector.Close()

return nil
}
Expand Down Expand Up @@ -273,7 +265,7 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {

var err error

c.perfDataCollector, err = perfdata.NewCollector(perfdata.V2, "DirectoryServices", perfdata.AllInstances, counters)
c.perfDataCollector, err = perfdata.NewCollector("DirectoryServices", perfdata.InstanceAll, counters)
if err != nil {
return fmt.Errorf("failed to create DirectoryServices collector: %w", err)
}
Expand Down Expand Up @@ -657,11 +649,7 @@ func (c *Collector) Build(_ *slog.Logger, _ *mi.Session) error {

// Collect sends the metric values for each metric
// to the provided prometheus Metric channel.
func (c *Collector) Collect(_ *types.ScrapeContext, _ *slog.Logger, ch chan<- prometheus.Metric) error {
return c.collect(ch)
}

func (c *Collector) collect(ch chan<- prometheus.Metric) error {
func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
perfData, err := c.perfDataCollector.Collect()
if err != nil {
return fmt.Errorf("failed to collect DirectoryServices (AD) metrics: %w", err)
Expand Down
4 changes: 3 additions & 1 deletion internal/collector/ad/ad_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
//go:build windows

package ad_test

import (
"testing"

"github.com/prometheus-community/windows_exporter/internal/collector/ad"
"github.com/prometheus-community/windows_exporter/internal/testutils"
"github.com/prometheus-community/windows_exporter/internal/utils/testutils"
)

func BenchmarkCollector(b *testing.B) {
Expand Down
Loading

0 comments on commit e6a15d4

Please sign in to comment.