Deprecate old "stats" and add a new one based on prometheus

This commit is contained in:
Hamcha 2019-05-27 14:39:14 +02:00
parent 025a5b96fd
commit 34171a69d5
Signed by: hamcha
GPG key ID: A40413D21021EAEE
14 changed files with 207 additions and 35 deletions

9
Dockerfile.broker Normal file
View file

@ -0,0 +1,9 @@
FROM golang:alpine
RUN apk add git
RUN go get -u git.fromouter.space/hamcha/tg/cmd/tg-broker
ENTRYPOINT [ "tg-broker" ]
EXPOSE 7313
EXPOSE 7314

6
Dockerfile.mods Normal file
View file

@ -0,0 +1,6 @@
FROM golang:alpine
RUN apk add git
RUN go get -u git.fromouter.space/crunchy-rocks/clessy/mods
ENTRYPOINT [ "mods" ]

6
Dockerfile.stats Normal file
View file

@ -0,0 +1,6 @@
FROM golang:alpine
RUN apk add git
RUN go get -u git.fromouter.space/crunchy-rocks/clessy/stats
ENTRYPOINT [ "stats" ]

46
stats-old/mod/main.go Normal file
View file

@ -0,0 +1,46 @@
package main
import (
"flag"
"github.com/boltdb/bolt"
"github.com/hamcha/tg"
)
func assert(err error) {
if err != nil {
panic(err)
}
}
var db *bolt.DB
var chatID *int64
func process(broker *tg.Broker, update tg.APIMessage) {
// Process messages from marked chat only
if update.Chat.ChatID != *chatID {
return
}
getNick(update.User)
updateStats(update)
}
func main() {
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port")
webBind := flag.String("webserver", "localhost:7315", "Address to bind webserver to")
boltdbFile := flag.String("boltdb", "stats.db", "BoltDB database file")
chatID = flag.Int64("chatid", -14625256, "Telegram Chat ID to count stats for")
flag.Parse()
var err error
db, err = bolt.Open(*boltdbFile, 0600, nil)
assert(err)
defer db.Close()
loadUsers()
loadStats()
go startWebServer(*webBind)
assert(tg.CreateBrokerClient(*brokerAddr, process))
}

View file

6
stats/go.mod Normal file
View file

@ -0,0 +1,6 @@
module git.fromouter.space/crunchy-rocks/clessy/stats
require (
git.fromouter.space/hamcha/tg v0.0.0-20181213132350-d7698a3c50ea
github.com/prometheus/client_golang v0.9.3
)

55
stats/go.sum Normal file
View file

@ -0,0 +1,55 @@
git.fromouter.space/hamcha/tg v0.0.0-20181213132350-d7698a3c50ea h1:2VrIQZEHaUGjf9Rqsli+S+/ZReLpodPWTklVUqQPCX0=
git.fromouter.space/hamcha/tg v0.0.0-20181213132350-d7698a3c50ea/go.mod h1:j0xF3DL3Y+HWAMzaFQlJxl9bLOKmFNEZNemg19vC+Cc=
github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAElWljhcU=
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0 h1:HWo1m869IqiPhD389kmkxeTalrjNbbJTC8LXupb+sl0=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/cespare/xxhash v1.1.0/go.mod h1:XrSqR1VqqWfGrhpAt58auRo0WTKS1nRRg3ghfAqPWnc=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw=
github.com/prometheus/client_golang v0.9.3 h1:9iH4JKXLzFbOAdtqv/a+j8aewx2Y8lAjAydhbaScPF8=
github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso=
github.com/prometheus/client_model v0.0.0-20180712105110-5c3871d89910/go.mod h1:MbSGuTsp3dbXC40dX6PRTWyKYBIrTGTE9sqQNg2J8bo=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90 h1:S/YWwWx/RA8rT8tKFRuGUZhuA90OyIBpPCXkcbwU8DE=
github.com/prometheus/client_model v0.0.0-20190129233127-fd36f4220a90/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA=
github.com/prometheus/common v0.0.0-20181113130724-41aa239b4cce/go.mod h1:daVV7qP5qjZbuso7PdcryaAu0sAZbrN9i7WWcTMWvro=
github.com/prometheus/common v0.4.0 h1:7etb9YClo3a6HjLzfl6rIQaU+FDfi0VSX39io3aQ+DM=
github.com/prometheus/common v0.4.0/go.mod h1:TNfzLD0ON7rHzMJeJkieUDPYmFC7Snx/y86RQel1bk4=
github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R/oaqEKCNdg8wHV1ftS6bRYblBhIjjI8uT2IGk=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084 h1:sofwID9zm4tzrgykg80hfFph1mryUeLRsUfoocVVmRY=
github.com/prometheus/procfs v0.0.0-20190507164030-5867b95ac084/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/tsdb v0.7.1/go.mod h1:qhTCs0VvXwvX/y3TZrWD7rabWM+ijKTux40TwIPHuXU=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/spaolacci/murmur3 v0.0.0-20180118202830-f09979ecbc72/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/net v0.0.0-20181114220301-adae6a3d119a/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181107165924-66b7b1311ac8/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20181116152217-5ac8a444bdc5/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
gopkg.in/alecthomas/kingpin.v2 v2.2.6/go.mod h1:FMv+mEhP44yOT+4EoQTLFTRgOQ1FBLkstjWtayDeSgw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

View file

@ -1,46 +1,57 @@
package main package main // import "git.fromouter.space/crunchy-rocks/clessy/stats"
import ( import (
"flag" "flag"
"net/http"
"strconv"
"strings"
"github.com/boltdb/bolt" "git.fromouter.space/hamcha/tg"
"github.com/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()
registerMetrics()
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) { func assert(err error) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
} }
var db *bolt.DB
var chatID *int64
func process(broker *tg.Broker, update tg.APIMessage) {
// Process messages from marked chat only
if update.Chat.ChatID != *chatID {
return
}
getNick(update.User)
updateStats(update)
}
func main() {
brokerAddr := flag.String("broker", "localhost:7314", "Broker address:port")
webBind := flag.String("webserver", "localhost:7315", "Address to bind webserver to")
boltdbFile := flag.String("boltdb", "stats.db", "BoltDB database file")
chatID = flag.Int64("chatid", -14625256, "Telegram Chat ID to count stats for")
flag.Parse()
var err error
db, err = bolt.Open(*boltdbFile, 0600, nil)
assert(err)
defer db.Close()
loadUsers()
loadStats()
go startWebServer(*webBind)
assert(tg.CreateBrokerClient(*brokerAddr, process))
}

33
stats/metrics.go Normal file
View file

@ -0,0 +1,33 @@
package main
import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var (
opTotalMsg = promauto.NewCounter(prometheus.CounterOpts{
Name: "broker_received_total",
Help: "The total number of received messages",
})
opMsgPerChat = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "broker_received_chat",
Help: "The number of received messages per chat",
}, []string{
"chatid",
"user",
"messagetype",
})
opCommand = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "broker_received_commands",
Help: "The number of commands written in chats (or privately)",
}, []string{
"command",
})
)
func registerMetrics() {
prometheus.MustRegister(opTotalMsg)
prometheus.MustRegister(opMsgPerChat)
prometheus.MustRegister(opCommand)
}