mirror of
https://github.com/prometheus-community/windows_exporter.git
synced 2026-02-10 15:06:36 +00:00
feat: Support OpenMetrics (#1772)
Signed-off-by: Jan-Otto Kröpke <mail@jkroepke.de>
This commit is contained in:
@@ -49,7 +49,6 @@ type MetricsHTTPHandler struct {
|
||||
type Options struct {
|
||||
DisableExporterMetrics bool
|
||||
TimeoutMargin float64
|
||||
MaxRequests int
|
||||
}
|
||||
|
||||
func New(logger *slog.Logger, metricCollectors *collector.MetricCollectors, options *Options) *MetricsHTTPHandler {
|
||||
@@ -57,7 +56,6 @@ func New(logger *slog.Logger, metricCollectors *collector.MetricCollectors, opti
|
||||
options = &Options{
|
||||
DisableExporterMetrics: false,
|
||||
TimeoutMargin: 0.5,
|
||||
MaxRequests: 5,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -65,7 +63,9 @@ func New(logger *slog.Logger, metricCollectors *collector.MetricCollectors, opti
|
||||
metricCollectors: metricCollectors,
|
||||
logger: logger,
|
||||
options: *options,
|
||||
concurrencyCh: make(chan struct{}, options.MaxRequests),
|
||||
|
||||
// We are expose metrics directly from the memory region of the Win32 API. We should not allow more than one request at a time.
|
||||
concurrencyCh: make(chan struct{}, 1),
|
||||
}
|
||||
|
||||
if !options.DisableExporterMetrics {
|
||||
@@ -131,21 +131,11 @@ func (c *MetricsHTTPHandler) handlerFactory(logger *slog.Logger, scrapeTimeout t
|
||||
if len(requestedCollectors) == 0 {
|
||||
metricCollectors = c.metricCollectors
|
||||
} else {
|
||||
filteredCollectors := make(collector.Map)
|
||||
var err error
|
||||
|
||||
for _, name := range requestedCollectors {
|
||||
metricCollector, ok := c.metricCollectors.Collectors[name]
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("couldn't find collector %s", name)
|
||||
}
|
||||
|
||||
filteredCollectors[name] = metricCollector
|
||||
}
|
||||
|
||||
metricCollectors = &collector.MetricCollectors{
|
||||
Collectors: filteredCollectors,
|
||||
MISession: c.metricCollectors.MISession,
|
||||
PerfCounterQuery: c.metricCollectors.PerfCounterQuery,
|
||||
metricCollectors, err = c.metricCollectors.CloneWithCollectors(requestedCollectors)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't clone metric collectors: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -162,8 +152,10 @@ func (c *MetricsHTTPHandler) handlerFactory(logger *slog.Logger, scrapeTimeout t
|
||||
promhttp.HandlerOpts{
|
||||
ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError),
|
||||
ErrorHandling: promhttp.ContinueOnError,
|
||||
MaxRequestsInFlight: c.options.MaxRequests,
|
||||
MaxRequestsInFlight: 1,
|
||||
Registry: c.exporterMetricsRegistry,
|
||||
EnableOpenMetrics: true,
|
||||
ProcessStartTime: c.metricCollectors.GetStartTime(),
|
||||
},
|
||||
)
|
||||
|
||||
@@ -178,7 +170,9 @@ func (c *MetricsHTTPHandler) handlerFactory(logger *slog.Logger, scrapeTimeout t
|
||||
promhttp.HandlerOpts{
|
||||
ErrorLog: slog.NewLogLogger(logger.Handler(), slog.LevelError),
|
||||
ErrorHandling: promhttp.ContinueOnError,
|
||||
MaxRequestsInFlight: c.options.MaxRequests,
|
||||
MaxRequestsInFlight: 1,
|
||||
EnableOpenMetrics: true,
|
||||
ProcessStartTime: c.metricCollectors.GetStartTime(),
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -187,10 +181,6 @@ func (c *MetricsHTTPHandler) handlerFactory(logger *slog.Logger, scrapeTimeout t
|
||||
}
|
||||
|
||||
func (c *MetricsHTTPHandler) withConcurrencyLimit(next http.HandlerFunc) http.HandlerFunc {
|
||||
if c.options.MaxRequests <= 0 {
|
||||
return next
|
||||
}
|
||||
|
||||
return func(w http.ResponseWriter, r *http.Request) {
|
||||
select {
|
||||
case c.concurrencyCh <- struct{}{}:
|
||||
|
||||
@@ -193,6 +193,13 @@ func (c *Collector) Collect() (CounterValues, error) {
|
||||
}
|
||||
|
||||
func (c *Collector) collectRoutine() {
|
||||
var (
|
||||
itemCount uint32
|
||||
bytesNeeded uint32
|
||||
)
|
||||
|
||||
buf := make([]byte, 1)
|
||||
|
||||
for range c.collectCh {
|
||||
if ret := PdhCollectQueryData(c.handle); ret != ErrorSuccess {
|
||||
c.counterValuesCh <- nil
|
||||
@@ -207,25 +214,24 @@ func (c *Collector) collectRoutine() {
|
||||
for _, counter := range c.counters {
|
||||
for _, instance := range counter.Instances {
|
||||
// Get the info with the current buffer size
|
||||
var itemCount uint32
|
||||
bytesNeeded = uint32(cap(buf))
|
||||
|
||||
// Get the info with the current buffer size
|
||||
bufLen := uint32(0)
|
||||
for {
|
||||
ret := PdhGetRawCounterArray(instance, &bytesNeeded, &itemCount, &buf[0])
|
||||
|
||||
ret := PdhGetRawCounterArray(instance, &bufLen, &itemCount, nil)
|
||||
if ret != PdhMoreData {
|
||||
return nil, fmt.Errorf("PdhGetRawCounterArray: %w", NewPdhError(ret))
|
||||
}
|
||||
if ret == ErrorSuccess {
|
||||
break
|
||||
}
|
||||
|
||||
buf := make([]byte, bufLen)
|
||||
|
||||
ret = PdhGetRawCounterArray(instance, &bufLen, &itemCount, &buf[0])
|
||||
if ret != ErrorSuccess {
|
||||
if err := NewPdhError(ret); !isKnownCounterDataError(err) {
|
||||
if err := NewPdhError(ret); ret != PdhMoreData && !isKnownCounterDataError(err) {
|
||||
return nil, fmt.Errorf("PdhGetRawCounterArray: %w", err)
|
||||
}
|
||||
|
||||
continue
|
||||
if bytesNeeded <= uint32(cap(buf)) {
|
||||
return nil, fmt.Errorf("PdhGetRawCounterArray reports buffer too small (%d), but buffer is large enough (%d): %w", uint32(cap(buf)), bytesNeeded, NewPdhError(ret))
|
||||
}
|
||||
|
||||
buf = make([]byte, bytesNeeded)
|
||||
}
|
||||
|
||||
items := unsafe.Slice((*PdhRawCounterItem)(unsafe.Pointer(&buf[0])), itemCount)
|
||||
|
||||
@@ -61,4 +61,6 @@ func BenchmarkTestCollector(b *testing.B) {
|
||||
}
|
||||
|
||||
performanceData.Close()
|
||||
|
||||
b.ReportAllocs()
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user