1 Star 0 Fork 2

何吕 / volantmq

forked from JUMEI_ARCH / volantmq 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
sessions.go 4.06 KB
一键复制 编辑 原始数据 按行查看 历史
hawklin 提交于 2018-05-17 16:31 . support prometheus
package systree
import (
"encoding/json"
"strconv"
"sync/atomic"
"github.com/VolantMQ/volantmq/packet"
"github.com/VolantMQ/volantmq/types"
"github.com/prometheus/client_golang/prometheus"
)
// SessionCreatedStatus report when session status once created
type SessionCreatedStatus struct {
ExpiryInterval string `json:"expiryInterval,omitempty"`
WillDelay string `json:"willDelay,omitempty"`
Timestamp string `json:"timestamp"`
Clean bool `json:"clean"`
}
// SessionDeletedStatus report when session status once deleted
type SessionDeletedStatus struct {
Timestamp string `json:"timestamp"`
Reason string `json:"reason"`
}
type sessions struct {
stat
topicsManager types.TopicMessenger
topic string
prom prometheus.Collector
}
type promSessions struct {
cur *promMetricEntry
max *promMetricEntry
}
func (p *promSessions) Describe(ch chan<- *prometheus.Desc) {
ch <- p.cur.desc
ch <- p.max.desc
}
func (p *promSessions) Collect(ch chan<- prometheus.Metric) {
val := p.cur.valFunc()
v, err := strconv.ParseFloat(string(val), 64)
if err != nil {
ch <- prometheus.MustNewConstMetric(p.cur.desc, p.cur.valType, 0)
} else {
ch <- prometheus.MustNewConstMetric(p.cur.desc, p.cur.valType, v)
}
val = p.max.valFunc()
v, err = strconv.ParseFloat(string(val), 64)
if err != nil {
ch <- prometheus.MustNewConstMetric(p.max.desc, p.max.valType, 0)
} else {
ch <- prometheus.MustNewConstMetric(p.max.desc, p.max.valType, v)
}
}
func newSessionsCollector(t stat) prometheus.Collector {
p := &promSessions{
cur: &promMetricEntry{
valFunc: t.curr.getValue,
desc: prometheus.NewDesc("jmmqtt_cur_sessions",
"Number of current sessions",
nil, nil,
),
valType: prometheus.CounterValue,
},
max: &promMetricEntry{
valFunc: t.max.getValue,
desc: prometheus.NewDesc("jmmqtt_max_sessions",
"Number of max sessions",
nil, nil,
),
valType: prometheus.CounterValue,
},
}
return p
}
func newSessions(topicPrefix string, retained *[]types.RetainObject) sessions {
c := sessions{
stat: newStat(topicPrefix+"/stats/sessions", retained),
topic: topicPrefix + "/sessions/",
}
c.prom = newSessionsCollector(c.stat)
prometheus.MustRegister(c.prom)
return c
}
// Connected add to statistic new client
func (t *sessions) Created(id string, status *SessionCreatedStatus) {
newVal := atomic.AddUint64(&t.curr.val, 1)
if atomic.LoadUint64(&t.max.val) < newVal {
atomic.StoreUint64(&t.max.val, newVal)
}
if t.topicsManager != nil {
// notify client connected
nm, _ := packet.New(packet.ProtocolV311, packet.PUBLISH)
notifyMsg, _ := nm.(*packet.Publish)
notifyMsg.SetRetain(false)
notifyMsg.SetQoS(packet.QoS0) // nolint: errcheck
notifyMsg.SetTopic(t.topic + id + "/created") // nolint: errcheck
if out, err := json.Marshal(&status); err != nil {
// todo: put reliable message
notifyMsg.SetPayload([]byte("data error"))
} else {
notifyMsg.SetPayload(out)
}
t.topicsManager.Publish(notifyMsg) // nolint: errcheck
t.topicsManager.Retain(notifyMsg) // nolint: errcheck
}
}
// Disconnected remove client from statistic
func (t *sessions) Removed(id string, status *SessionDeletedStatus) {
atomic.AddUint64(&t.curr.val, ^uint64(0))
if t.topicsManager != nil {
nm, _ := packet.New(packet.ProtocolV311, packet.PUBLISH)
notifyMsg, _ := nm.(*packet.Publish)
notifyMsg.SetRetain(false)
notifyMsg.SetQoS(packet.QoS0) // nolint: errcheck
notifyMsg.SetTopic(t.topic + id) // nolint: errcheck
t.topicsManager.Retain(notifyMsg) // nolint: errcheck
nm, _ = packet.New(packet.ProtocolV311, packet.PUBLISH)
notifyMsg, _ = nm.(*packet.Publish)
notifyMsg.SetRetain(false)
notifyMsg.SetQoS(packet.QoS0) // nolint: errcheck
notifyMsg.SetTopic(t.topic + id) // nolint: errcheck
notifyMsg.SetTopic(t.topic + id + "/removed") // nolint: errcheck
if out, err := json.Marshal(&status); err != nil {
notifyMsg.SetPayload([]byte("data error"))
} else {
notifyMsg.SetPayload(out)
}
t.topicsManager.Publish(notifyMsg) // nolint: errcheck
}
}
Go
1
https://gitee.com/kaifazhe/volantmq.git
git@gitee.com:kaifazhe/volantmq.git
kaifazhe
volantmq
volantmq
v0.0.4

搜索帮助

53164aa7 5694891 3bd8fe86 5694891