-
Notifications
You must be signed in to change notification settings - Fork 0
/
status.go
75 lines (70 loc) · 2.17 KB
/
status.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
package router
import (
"github.com/TheThingsNetwork/api"
pb "github.com/TheThingsNetwork/api/router"
"github.com/TheThingsNetwork/ttn/api/stats"
"github.com/rcrowley/go-metrics"
)
type status struct {
uplink metrics.Meter
downlink metrics.Meter
activations metrics.Meter
gatewayStatus metrics.Meter
connectedGateways metrics.Gauge
connectedBrokers metrics.Gauge
}
func (r *router) InitStatus() {
r.status = &status{
uplink: metrics.NewMeter(),
downlink: metrics.NewMeter(),
activations: metrics.NewMeter(),
gatewayStatus: metrics.NewMeter(),
connectedGateways: metrics.NewFunctionalGauge(func() int64 {
r.gatewaysLock.RLock()
defer r.gatewaysLock.RUnlock()
return int64(len(r.gateways))
}),
connectedBrokers: metrics.NewFunctionalGauge(func() int64 {
r.brokersLock.RLock()
defer r.brokersLock.RUnlock()
return int64(len(r.brokers))
}),
}
}
func (r *router) GetStatus() *pb.Status {
status := new(pb.Status)
if r.status == nil {
return status
}
status.System = stats.GetSystem()
status.Component = stats.GetComponent()
uplink := r.status.uplink.Snapshot()
status.Uplink = &api.Rates{
Rate1: float32(uplink.Rate1()),
Rate5: float32(uplink.Rate5()),
Rate15: float32(uplink.Rate15()),
}
downlink := r.status.downlink.Snapshot()
status.Downlink = &api.Rates{
Rate1: float32(downlink.Rate1()),
Rate5: float32(downlink.Rate5()),
Rate15: float32(downlink.Rate15()),
}
activations := r.status.activations.Snapshot()
status.Activations = &api.Rates{
Rate1: float32(activations.Rate1()),
Rate5: float32(activations.Rate5()),
Rate15: float32(activations.Rate15()),
}
gatewayStatus := r.status.gatewayStatus.Snapshot()
status.GatewayStatus = &api.Rates{
Rate1: float32(gatewayStatus.Rate1()),
Rate5: float32(gatewayStatus.Rate5()),
Rate15: float32(gatewayStatus.Rate15()),
}
status.ConnectedGateways = uint32(r.status.connectedGateways.Snapshot().Value())
status.ConnectedBrokers = uint32(r.status.connectedBrokers.Snapshot().Value())
return status
}