2019-05-27 12:39:14 +00:00
|
|
|
package main // import "git.fromouter.space/crunchy-rocks/clessy/stats"
|
2016-02-09 15:00:50 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2019-05-27 12:39:14 +00:00
|
|
|
"net/http"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2016-02-09 15:00:50 +00:00
|
|
|
|
2019-05-27 12:39:14 +00:00
|
|
|
"git.fromouter.space/hamcha/tg"
|
2016-02-10 14:31:53 +00:00
|
|
|
|
2019-05-27 12:39:14 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
|
|
)
|
2016-02-10 14:31:53 +00:00
|
|
|
|
2019-05-27 12:39:14 +00:00
|
|
|
func process(broker *tg.Broker, update tg.APIUpdate) {
|
|
|
|
if update.Message != nil {
|
|
|
|
opTotalMsg.Inc()
|
|
|
|
// Check if it's a chat
|
|
|
|
if update.Message.Chat != nil {
|
|
|
|
opMsgPerChat.With(prometheus.Labels{
|
|
|
|
"chatid": strconv.FormatInt(update.Message.Chat.ChatID, 10),
|
|
|
|
"user": update.Message.User.Username,
|
|
|
|
}).Inc()
|
|
|
|
}
|
|
|
|
if update.Message.Text != nil {
|
|
|
|
if strings.HasPrefix(*update.Message.Text, "/") {
|
|
|
|
opCommand.With(prometheus.Labels{
|
|
|
|
"command": strings.SplitN(*update.Message.Text, " ", 2)[0],
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2016-02-10 16:27:15 +00:00
|
|
|
}
|
2016-02-09 15:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port")
|
|
|
|
flag.Parse()
|
|
|
|
|
2019-05-27 12:39:14 +00:00
|
|
|
registerMetrics()
|
|
|
|
|
|
|
|
broker, err := tg.ConnectToBroker(*brokerAddr)
|
2016-02-10 14:31:53 +00:00
|
|
|
assert(err)
|
2019-05-27 12:39:14 +00:00
|
|
|
defer broker.Close()
|
2016-02-10 14:31:53 +00:00
|
|
|
|
2019-05-27 12:39:14 +00:00
|
|
|
go func() {
|
|
|
|
http.Handle("/metrics", promhttp.Handler())
|
|
|
|
http.ListenAndServe(":2112", nil)
|
|
|
|
}()
|
2016-02-10 14:31:53 +00:00
|
|
|
|
2019-05-27 12:39:14 +00:00
|
|
|
assert(tg.RunBrokerClient(broker, process))
|
|
|
|
}
|
2016-02-11 10:00:35 +00:00
|
|
|
|
2019-05-27 12:39:14 +00:00
|
|
|
func assert(err error) {
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2016-02-09 15:00:50 +00:00
|
|
|
}
|