-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix potential missing datastore metrics in vSphere plugin (#4968)
- Loading branch information
1 parent
0e07bbb
commit 2d782fb
Showing
6 changed files
with
228 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package vsphere | ||
|
||
import ( | ||
"log" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// TSCache is a cache of timestamps used to determine the validity of datapoints | ||
type TSCache struct { | ||
ttl time.Duration | ||
table map[string]time.Time | ||
done chan struct{} | ||
mux sync.RWMutex | ||
} | ||
|
||
// NewTSCache creates a new TSCache with a specified time-to-live after which timestamps are discarded. | ||
func NewTSCache(ttl time.Duration) *TSCache { | ||
return &TSCache{ | ||
ttl: ttl, | ||
table: make(map[string]time.Time), | ||
done: make(chan struct{}), | ||
} | ||
} | ||
|
||
// Purge removes timestamps that are older than the time-to-live | ||
func (t *TSCache) Purge() { | ||
t.mux.Lock() | ||
defer t.mux.Unlock() | ||
n := 0 | ||
for k, v := range t.table { | ||
if time.Now().Sub(v) > t.ttl { | ||
delete(t.table, k) | ||
n++ | ||
} | ||
} | ||
log.Printf("D! [input.vsphere] Purged timestamp cache. %d deleted with %d remaining", n, len(t.table)) | ||
} | ||
|
||
// IsNew returns true if the supplied timestamp for the supplied key is more recent than the | ||
// timestamp we have on record. | ||
func (t *TSCache) IsNew(key string, tm time.Time) bool { | ||
t.mux.RLock() | ||
defer t.mux.RUnlock() | ||
v, ok := t.table[key] | ||
if !ok { | ||
return true // We've never seen this before, so consider everything a new sample | ||
} | ||
return !tm.Before(v) | ||
} | ||
|
||
// Put updates the latest timestamp for the supplied key. | ||
func (t *TSCache) Put(key string, time time.Time) { | ||
t.mux.Lock() | ||
defer t.mux.Unlock() | ||
t.table[key] = time | ||
} |
Oops, something went wrong.