Skip to content

Commit

Permalink
Add timeout to WMI query for disk metrics, which sometimes hangs
Browse files Browse the repository at this point in the history
  • Loading branch information
astj committed Jul 25, 2018
1 parent 4961820 commit 5cd1fbd
Showing 1 changed file with 27 additions and 2 deletions.
29 changes: 27 additions & 2 deletions metrics/windows/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
package windows

import (
"context"
"errors"
"fmt"
"time"

Expand Down Expand Up @@ -33,8 +35,7 @@ type win32PerfFormattedDataPerfDiskPhysicalDisk struct {
func (g *DiskGenerator) Generate() (metrics.Values, error) {
time.Sleep(g.Interval)

var records []win32PerfFormattedDataPerfDiskPhysicalDisk
err := wmi.Query("SELECT * FROM Win32_PerfFormattedData_PerfDisk_LogicalDisk ", &records)
records, err := g.queryWmiWithTimeout()
if err != nil {
return nil, err
}
Expand All @@ -53,3 +54,27 @@ func (g *DiskGenerator) Generate() (metrics.Values, error) {
diskLogger.Debugf("%q", results)
return results, nil
}

const queryWmiTimeout = 30 * time.Second

func (g *DiskGenerator) queryWmiWithTimeout() ([]win32PerfFormattedDataPerfDiskPhysicalDisk, error) {
ctx, cancel := context.WithTimeout(context.TODO(), queryWmiTimeout)
defer cancel()
errCh := make(chan error)
recordsCh := make(chan []win32PerfFormattedDataPerfDiskPhysicalDisk)
go func() {
var records []win32PerfFormattedDataPerfDiskPhysicalDisk
if err := wmi.Query("SELECT * FROM Win32_PerfFormattedData_PerfDisk_LogicalDisk ", &records); err != nil {
errCh <- err
}
recordsCh <- records
}()
select {
case <-ctx.Done():
return nil, errors.New("Timeouted while retrieving disk metrics")
case err := <-errCh:
return nil, err
case records := <-recordsCh:
return records, nil
}
}

0 comments on commit 5cd1fbd

Please sign in to comment.