Add Prometheus for some Owncast metrics (#1744)
* Add Prometheus for some Owncast metrics. Closes #1303 * Wrap prometheus metrics endpoint in admin middleware
This commit is contained in:
@@ -5,12 +5,19 @@ import (
|
||||
"net/http"
|
||||
"sort"
|
||||
|
||||
"github.com/owncast/owncast/config"
|
||||
"github.com/owncast/owncast/core/chat/events"
|
||||
"github.com/owncast/owncast/core/data"
|
||||
"github.com/owncast/owncast/models"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
"github.com/prometheus/client_golang/prometheus/promauto"
|
||||
log "github.com/sirupsen/logrus"
|
||||
)
|
||||
|
||||
var getStatus func() models.Status
|
||||
var (
|
||||
getStatus func() models.Status
|
||||
chatMessagesSentCounter prometheus.Gauge
|
||||
)
|
||||
|
||||
// Start begins the chat server.
|
||||
func Start(getStatusFunc func() models.Status) error {
|
||||
@@ -23,6 +30,15 @@ func Start(getStatusFunc func() models.Status) error {
|
||||
|
||||
log.Traceln("Chat server started with max connection count of", _server.maxSocketConnectionLimit)
|
||||
|
||||
chatMessagesSentCounter = promauto.NewGauge(prometheus.GaugeOpts{
|
||||
Name: "total_chat_message_count",
|
||||
Help: "The number of chat messages incremented over time.",
|
||||
ConstLabels: map[string]string{
|
||||
"version": config.VersionNumber,
|
||||
"host": data.GetServerURL(),
|
||||
},
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -51,6 +67,10 @@ func FindClientByID(clientID uint) (*Client, bool) {
|
||||
func GetClients() []*Client {
|
||||
clients := []*Client{}
|
||||
|
||||
if _server == nil {
|
||||
return clients
|
||||
}
|
||||
|
||||
// Convert the keyed map to a slice.
|
||||
for _, client := range _server.clients {
|
||||
clients = append(clients, client)
|
||||
|
||||
@@ -108,9 +108,9 @@ func (s *Server) userMessageSent(eventData chatClientEvent) {
|
||||
|
||||
// Send chat message sent webhook
|
||||
webhooks.SendChatEvent(&event)
|
||||
chatMessagesSentCounter.Inc()
|
||||
|
||||
SaveUserMessage(event)
|
||||
|
||||
eventData.client.MessageCount++
|
||||
_lastSeenCache[event.User.ID] = time.Now()
|
||||
}
|
||||
|
||||
@@ -35,3 +35,20 @@ func CreateMessagesTable(db *sql.DB) {
|
||||
log.Fatal("error creating chat messages table", err)
|
||||
}
|
||||
}
|
||||
|
||||
// GetMessagesCount will return the number of messages in the database.
|
||||
func GetMessagesCount() int64 {
|
||||
query := `SELECT COUNT(*) FROM messages`
|
||||
rows, err := _db.Query(query)
|
||||
if err != nil || rows.Err() != nil {
|
||||
return 0
|
||||
}
|
||||
defer rows.Close()
|
||||
var count int64
|
||||
for rows.Next() {
|
||||
if err := rows.Scan(&count); err != nil {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
@@ -38,3 +38,20 @@ func createUsersTable(db *sql.DB) {
|
||||
log.Warnln(err)
|
||||
}
|
||||
}
|
||||
|
||||
// GetUsersCount will return the number of users in the database.
|
||||
func GetUsersCount() int64 {
|
||||
query := `SELECT COUNT(*) FROM users`
|
||||
rows, err := _db.Query(query)
|
||||
if err != nil || rows.Err() != nil {
|
||||
return 0
|
||||
}
|
||||
defer rows.Close()
|
||||
var count int64
|
||||
for rows.Next() {
|
||||
if err := rows.Scan(&count); err != nil {
|
||||
return 0
|
||||
}
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user