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

55 lines
1.2 KiB
Go

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