Use server time to fix missing non-realtime samples in vsphere (#4791)

This commit is contained in:
Pontus Rydin 2018-10-03 15:02:06 -04:00 committed by Daniel Nelson
parent b9c64df5fc
commit 21b488a3d3
2 changed files with 23 additions and 6 deletions

View File

@ -6,6 +6,7 @@ import (
"log" "log"
"net/url" "net/url"
"sync" "sync"
"time"
"github.com/vmware/govmomi" "github.com/vmware/govmomi"
"github.com/vmware/govmomi/performance" "github.com/vmware/govmomi/performance"
@ -171,3 +172,12 @@ func (c *Client) close() {
} }
}) })
} }
// GetServerTime returns the time at the vCenter server
func (c *Client) GetServerTime(ctx context.Context) (time.Time, error) {
t, err := methods.GetCurrentTime(ctx, c.Client)
if err != nil {
return time.Time{}, err
}
return *t, nil
}

View File

@ -613,10 +613,17 @@ func (e *Endpoint) collectResource(ctx context.Context, resourceType string, acc
// Do we have new data yet? // Do we have new data yet?
res := e.resourceKinds[resourceType] res := e.resourceKinds[resourceType]
now := time.Now() client, err := e.clientFactory.GetClient(ctx)
if err != nil {
return err
}
now, err := client.GetServerTime(ctx)
if err != nil {
return err
}
latest, hasLatest := e.lastColls[resourceType] latest, hasLatest := e.lastColls[resourceType]
if hasLatest { if hasLatest {
elapsed := time.Now().Sub(latest).Seconds() + 5.0 // Allow 5 second jitter. elapsed := now.Sub(latest).Seconds() + 5.0 // Allow 5 second jitter.
log.Printf("D! [input.vsphere]: Latest: %s, elapsed: %f, resource: %s", latest, elapsed, resourceType) log.Printf("D! [input.vsphere]: Latest: %s, elapsed: %f, resource: %s", latest, elapsed, resourceType)
if !res.realTime && elapsed < float64(res.sampling) { if !res.realTime && elapsed < float64(res.sampling) {
// No new data would be available. We're outta herE! [input.vsphere]: // No new data would be available. We're outta herE! [input.vsphere]:
@ -625,7 +632,7 @@ func (e *Endpoint) collectResource(ctx context.Context, resourceType string, acc
return nil return nil
} }
} else { } else {
latest = time.Now().Add(time.Duration(-res.sampling) * time.Second) latest = now.Add(time.Duration(-res.sampling) * time.Second)
} }
internalTags := map[string]string{"resourcetype": resourceType} internalTags := map[string]string{"resourcetype": resourceType}
@ -659,12 +666,12 @@ func (e *Endpoint) collectResource(ctx context.Context, resourceType string, acc
// Drain the pool. We're getting errors back. They should all be nil // Drain the pool. We're getting errors back. They should all be nil
var mux sync.Mutex var mux sync.Mutex
err := make(multiError, 0) merr := make(multiError, 0)
wp.Drain(ctx, func(ctx context.Context, in interface{}) bool { wp.Drain(ctx, func(ctx context.Context, in interface{}) bool {
if in != nil { if in != nil {
mux.Lock() mux.Lock()
defer mux.Unlock() defer mux.Unlock()
err = append(err, in.(error)) merr = append(merr, in.(error))
return false return false
} }
return true return true
@ -673,7 +680,7 @@ func (e *Endpoint) collectResource(ctx context.Context, resourceType string, acc
sw.Stop() sw.Stop()
SendInternalCounterWithTags("gather_count", e.URL.Host, internalTags, count) SendInternalCounterWithTags("gather_count", e.URL.Host, internalTags, count)
if len(err) > 0 { if len(merr) > 0 {
return err return err
} }
return nil return nil