mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-03-04 01:26:37 +00:00
chore: Remove registry based perfdata collector (#1742)
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
//go:build windows
|
||||
|
||||
// Copyright 2015 The Prometheus Authors
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
@@ -11,8 +13,6 @@
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
//go:build !notextfile
|
||||
|
||||
package textfile
|
||||
|
||||
import (
|
||||
@@ -48,6 +48,7 @@ var ConfigDefaults = Config{
|
||||
|
||||
type Collector struct {
|
||||
config Config
|
||||
logger *slog.Logger
|
||||
|
||||
// Only set for testing to get predictable output.
|
||||
mTime *float64
|
||||
@@ -96,18 +97,14 @@ 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 {
|
||||
func (c *Collector) Close() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Collector) Build(logger *slog.Logger, _ *mi.Session) error {
|
||||
logger.Info("textfile Collector directories: "+strings.Join(c.config.TextFileDirectories, ","),
|
||||
slog.String("collector", Name),
|
||||
)
|
||||
c.logger = logger.With(slog.String("collector", Name))
|
||||
|
||||
c.logger.Info("textfile Collector directories: " + strings.Join(c.config.TextFileDirectories, ","))
|
||||
|
||||
c.mTimeDesc = prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, "textfile", "mtime_seconds"),
|
||||
@@ -308,9 +305,7 @@ func (cr carriageReturnFilteringReader) Read(p []byte) (int, error) {
|
||||
}
|
||||
|
||||
// Collect implements the Collector interface.
|
||||
func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan<- prometheus.Metric) error {
|
||||
logger = logger.With(slog.String("collector", Name))
|
||||
errorMetric := 0.0
|
||||
func (c *Collector) Collect(ch chan<- prometheus.Metric) error {
|
||||
mTimes := map[string]time.Time{}
|
||||
|
||||
// Create empty metricFamily slice here and append parsedFamilies to it inside the loop.
|
||||
@@ -322,44 +317,32 @@ func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan
|
||||
for _, directory := range c.config.TextFileDirectories {
|
||||
err := filepath.WalkDir(directory, func(path string, dirEntry os.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
logger.Error("Error reading directory: "+path,
|
||||
slog.Any("err", err),
|
||||
)
|
||||
|
||||
errorMetric = 1.0
|
||||
|
||||
return nil
|
||||
return fmt.Errorf("error reading directory: %w", err)
|
||||
}
|
||||
|
||||
if !dirEntry.IsDir() && strings.HasSuffix(dirEntry.Name(), ".prom") {
|
||||
logger.Debug("Processing file: " + path)
|
||||
c.logger.Debug("Processing file: " + path)
|
||||
|
||||
families_array, err := scrapeFile(path, logger)
|
||||
families_array, err := scrapeFile(path, c.logger)
|
||||
if err != nil {
|
||||
logger.Error(fmt.Sprintf("Error scraping file: %q. Skip File.", path),
|
||||
c.logger.Error(fmt.Sprintf("Error scraping file: %q. Skip File.", path),
|
||||
slog.Any("err", err),
|
||||
)
|
||||
|
||||
errorMetric = 1.0
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
fileInfo, err := os.Stat(path)
|
||||
if err != nil {
|
||||
logger.Error(fmt.Sprintf("Error reading file info: %q. Skip File.", path),
|
||||
c.logger.Error(fmt.Sprintf("Error reading file info: %q. Skip File.", path),
|
||||
slog.Any("err", err),
|
||||
)
|
||||
|
||||
errorMetric = 1.0
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
if _, hasName := mTimes[fileInfo.Name()]; hasName {
|
||||
logger.Error(fmt.Sprintf("Duplicate filename detected: %q. Skip File.", path))
|
||||
|
||||
errorMetric = 1.0
|
||||
c.logger.Error(fmt.Sprintf("Duplicate filename detected: %q. Skip File.", path))
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -372,35 +355,22 @@ func (c *Collector) Collect(_ *types.ScrapeContext, logger *slog.Logger, ch chan
|
||||
return nil
|
||||
})
|
||||
if err != nil && directory != "" {
|
||||
logger.Error("Error reading textfile Collector directory: "+directory,
|
||||
c.logger.Error("Error reading textfile Collector directory: "+directory,
|
||||
slog.Any("err", err),
|
||||
)
|
||||
|
||||
errorMetric = 1.0
|
||||
}
|
||||
}
|
||||
|
||||
// If duplicates are detected across *multiple* files, return error.
|
||||
if duplicateMetricEntry(metricFamilies) {
|
||||
logger.Error("Duplicate metrics detected across multiple files")
|
||||
|
||||
errorMetric = 1.0
|
||||
c.logger.Error("Duplicate metrics detected across multiple files")
|
||||
} else {
|
||||
for _, mf := range metricFamilies {
|
||||
c.convertMetricFamily(logger, mf, ch)
|
||||
c.convertMetricFamily(c.logger, mf, ch)
|
||||
}
|
||||
}
|
||||
|
||||
c.exportMTimes(mTimes, ch)
|
||||
// Export if there were errors.
|
||||
ch <- prometheus.MustNewConstMetric(
|
||||
prometheus.NewDesc(
|
||||
prometheus.BuildFQName(types.Namespace, "textfile", "scrape_error"),
|
||||
"1 if there was an error opening or reading a file, 0 otherwise",
|
||||
nil, nil,
|
||||
),
|
||||
prometheus.GaugeValue, errorMetric,
|
||||
)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build windows
|
||||
|
||||
package textfile
|
||||
|
||||
import (
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
//go:build windows
|
||||
|
||||
package textfile_test
|
||||
|
||||
import (
|
||||
@@ -29,11 +31,6 @@ func TestMultipleDirectories(t *testing.T) {
|
||||
collectors := collector.New(map[string]collector.Collector{textfile.Name: textFileCollector})
|
||||
require.NoError(t, collectors.Build(logger))
|
||||
|
||||
scrapeContext, err := collectors.PrepareScrapeContext()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %s", err)
|
||||
}
|
||||
|
||||
metrics := make(chan prometheus.Metric)
|
||||
got := ""
|
||||
|
||||
@@ -52,7 +49,7 @@ func TestMultipleDirectories(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
|
||||
err = textFileCollector.Collect(scrapeContext, logger, metrics)
|
||||
err := textFileCollector.Collect(metrics)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %s", err)
|
||||
}
|
||||
@@ -75,11 +72,6 @@ func TestDuplicateFileName(t *testing.T) {
|
||||
collectors := collector.New(map[string]collector.Collector{textfile.Name: textFileCollector})
|
||||
require.NoError(t, collectors.Build(logger))
|
||||
|
||||
scrapeContext, err := collectors.PrepareScrapeContext()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %s", err)
|
||||
}
|
||||
|
||||
metrics := make(chan prometheus.Metric)
|
||||
got := ""
|
||||
|
||||
@@ -98,7 +90,7 @@ func TestDuplicateFileName(t *testing.T) {
|
||||
}
|
||||
}()
|
||||
|
||||
err = textFileCollector.Collect(scrapeContext, logger, metrics)
|
||||
err := textFileCollector.Collect(metrics)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error %s", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user