-
Notifications
You must be signed in to change notification settings - Fork 18
/
metrics.go
94 lines (82 loc) · 2.08 KB
/
metrics.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
87
88
89
90
91
92
93
94
package mongoex
import (
"context"
"sync"
"go.mongodb.org/mongo-driver/event"
)
type poolMetrics struct {
name string
mu sync.RWMutex
connClosed int64
poolCreated int64
connCreated int64
getFailed int64
getSucceeded int64
connReturned int64
poolCleared int64
poolClosed int64
maxPoolSize uint64
minPoolSize uint64
waitQueueTimeoutMS uint64
}
func newPoolMetrics(name string) *poolMetrics {
return &poolMetrics{
name: name,
}
}
func (c *poolMetrics) MetricName() string {
return c.name
}
func (c *poolMetrics) Gauges(_ context.Context) map[string]float64 {
c.mu.RLock()
defer c.mu.RUnlock()
return map[string]float64{
"connection_closed": float64(c.connClosed),
"pool_created": float64(c.poolCreated),
"get_failed": float64(c.getFailed),
"get_succeeded": float64(c.getSucceeded),
"connection_returned": float64(c.connReturned),
"pool_cleared": float64(c.poolCleared),
"pool_closed": float64(c.poolClosed),
"max_pool_size": float64(c.maxPoolSize),
"min_pool_size": float64(c.minPoolSize),
"wait_queue_timeout_ms": float64(c.waitQueueTimeoutMS),
}
}
func (c *poolMetrics) PoolMonitor(parent *event.PoolMonitor) *event.PoolMonitor {
return &event.PoolMonitor{
Event: func(e *event.PoolEvent) {
if parent != nil {
parent.Event(e)
}
c.updateStats(e)
},
}
}
func (c *poolMetrics) updateStats(e *event.PoolEvent) {
c.mu.Lock()
defer c.mu.Unlock()
switch e.Type {
case event.ConnectionClosed:
c.connClosed++
case event.PoolCreated:
c.poolCreated++
case event.ConnectionCreated:
c.connCreated++
case event.GetFailed:
c.getFailed++
case event.GetSucceeded:
c.getSucceeded++
case event.ConnectionReturned:
c.connReturned++
case event.PoolCleared:
c.poolCleared++
case event.PoolClosedEvent:
c.poolClosed++
}
if e.PoolOptions != nil {
c.maxPoolSize = e.PoolOptions.MaxPoolSize
c.minPoolSize = e.PoolOptions.MinPoolSize
c.waitQueueTimeoutMS = e.PoolOptions.WaitQueueTimeoutMS
}
}