pubchats: add shh_unique_authors_total metric (#4)

This commit is contained in:
Adam Babik 2018-10-10 15:52:41 +02:00 committed by GitHub
parent dee846cafc
commit 61857aa544
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 0 deletions

View File

@ -94,6 +94,9 @@ func main() {
log.Println("waiting for messages...")
// mapping chat => participants
chatParticipants := make(map[string]map[string]struct{})
for {
select {
case msg := <-messages:
@ -101,6 +104,21 @@ func main() {
source := hex.EncodeToString(msg.Sig)
log.Printf("received a message: topic=%v (%s) data=%s author=%s", msg.Topic, chatName, msg.Payload, source)
messagesCounter.WithLabelValues(chatName).Inc()
// detect unique participants per chat
var (
ok bool
participans map[string]struct{}
)
participans, ok = chatParticipants[chatName]
if !ok {
participans = make(map[string]struct{})
chatParticipants[chatName] = participans
}
if _, ok := participans[source]; !ok {
uniqueCounter.WithLabelValues(chatName).Inc()
participans[source] = struct{}{}
}
case err := <-subErr:
log.Fatalf("subscription error: %v", err)
case <-signals:

View File

@ -14,10 +14,16 @@ var (
Name: "messages_total",
Help: "Received messages counter.",
}, []string{"chat"})
uniqueCounter = prometheus.NewCounterVec(prometheus.CounterOpts{
Namespace: "shh",
Name: "unique_authors_total",
Help: "Unique authoers of the messages.",
}, []string{"chat"})
)
func init() {
prometheus.MustRegister(messagesCounter)
prometheus.MustRegister(uniqueCounter)
}
func startMetricsServer(addr string) {