forked from cloudfoundry/gorouter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fake_metric_sender.go
86 lines (67 loc) · 1.89 KB
/
fake_metric_sender.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package fake
import (
"sync"
)
type FakeMetricSender struct {
counters map[string]uint64
values map[string]Metric
containerMetrics map[string]ContainerMetric
sync.RWMutex
}
type Metric struct {
Value float64
Unit string
}
type ContainerMetric struct {
ApplicationId string
InstanceIndex int32
CpuPercentage float64
MemoryBytes uint64
DiskBytes uint64
}
func NewFakeMetricSender() *FakeMetricSender {
return &FakeMetricSender{
counters: make(map[string]uint64),
values: make(map[string]Metric),
containerMetrics: make(map[string]ContainerMetric),
}
}
func (fms *FakeMetricSender) SendValue(name string, value float64, unit string) error {
fms.Lock()
defer fms.Unlock()
fms.values[name] = Metric{Value: value, Unit: unit}
return nil
}
func (fms *FakeMetricSender) IncrementCounter(name string) error {
fms.Lock()
defer fms.Unlock()
fms.counters[name]++
return nil
}
func (fms *FakeMetricSender) AddToCounter(name string, delta uint64) error {
fms.Lock()
defer fms.Unlock()
fms.counters[name] = fms.counters[name] + delta
return nil
}
func (fms *FakeMetricSender) SendContainerMetric(applicationId string, instanceIndex int32, cpuPercentage float64, memoryBytes uint64, diskBytes uint64) error {
fms.Lock()
defer fms.Unlock()
fms.containerMetrics[applicationId] = ContainerMetric{ApplicationId: applicationId, InstanceIndex: instanceIndex, CpuPercentage: cpuPercentage, MemoryBytes: memoryBytes, DiskBytes: diskBytes}
return nil
}
func (fms *FakeMetricSender) GetValue(name string) Metric {
fms.RLock()
defer fms.RUnlock()
return fms.values[name]
}
func (fms *FakeMetricSender) GetCounter(name string) uint64 {
fms.RLock()
defer fms.RUnlock()
return fms.counters[name]
}
func (fms *FakeMetricSender) GetContainerMetric(applicationId string) ContainerMetric {
fms.RLock()
defer fms.RUnlock()
return fms.containerMetrics[applicationId]
}