This repository has been archived on 2023-07-05. You can view files and clone it, but cannot push or open issues or pull requests.
clessy/stats/main.go

58 lines
1.2 KiB
Go
Raw Normal View History

package main // import "git.fromouter.space/crunchy-rocks/clessy/stats"
2016-02-09 15:00:50 +00:00
import (
"flag"
"net/http"
"strconv"
"strings"
2016-02-09 15:00:50 +00:00
"git.fromouter.space/hamcha/tg"
2016-02-10 14:31:53 +00:00
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
2016-02-10 14:31:53 +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-09 15:00:50 +00:00
}
func main() {
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port")
flag.Parse()
registerMetrics()
broker, err := tg.ConnectToBroker(*brokerAddr)
2016-02-10 14:31:53 +00:00
assert(err)
defer broker.Close()
2016-02-10 14:31:53 +00:00
go func() {
http.Handle("/metrics", promhttp.Handler())
http.ListenAndServe(":2112", nil)
}()
2016-02-10 14:31:53 +00:00
assert(tg.RunBrokerClient(broker, process))
}
2016-02-11 10:00:35 +00:00
func assert(err error) {
if err != nil {
panic(err)
}
2016-02-09 15:00:50 +00:00
}