2021-06-04 16:38:49 +02:00
|
|
|
package metrics
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
2021-08-24 12:13:58 +02:00
|
|
|
"github.com/prometheus/client_golang/prometheus/collectors"
|
2021-06-04 16:38:49 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-03-22 16:24:34 +02:00
|
|
|
MetricsNamespace = "focalboard"
|
|
|
|
MetricsSubsystemBlocks = "blocks"
|
2022-09-13 22:37:05 +02:00
|
|
|
MetricsSubsystemBoards = "boards"
|
2022-03-22 16:24:34 +02:00
|
|
|
MetricsSubsystemTeams = "teams"
|
|
|
|
MetricsSubsystemSystem = "system"
|
2021-06-04 16:38:49 +02:00
|
|
|
|
|
|
|
MetricsCloudInstallationLabel = "installationId"
|
|
|
|
)
|
|
|
|
|
|
|
|
type InstanceInfo struct {
|
|
|
|
Version string
|
|
|
|
BuildNum string
|
|
|
|
Edition string
|
|
|
|
InstallationID string
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// Metrics used to instrumentate metrics in prometheus.
|
2021-06-04 16:38:49 +02:00
|
|
|
type Metrics struct {
|
|
|
|
registry *prometheus.Registry
|
|
|
|
|
|
|
|
instance *prometheus.GaugeVec
|
|
|
|
startTime prometheus.Gauge
|
|
|
|
|
|
|
|
loginCount prometheus.Counter
|
2021-12-01 11:21:31 +02:00
|
|
|
logoutCount prometheus.Counter
|
2021-06-04 16:38:49 +02:00
|
|
|
loginFailCount prometheus.Counter
|
|
|
|
|
|
|
|
blocksInsertedCount prometheus.Counter
|
2021-08-06 14:10:24 +02:00
|
|
|
blocksPatchedCount prometheus.Counter
|
2021-06-04 16:38:49 +02:00
|
|
|
blocksDeletedCount prometheus.Counter
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
blockCount *prometheus.GaugeVec
|
2022-09-13 22:37:05 +02:00
|
|
|
boardCount prometheus.Gauge
|
2022-03-22 16:24:34 +02:00
|
|
|
teamCount prometheus.Gauge
|
2021-06-04 16:38:49 +02:00
|
|
|
|
|
|
|
blockLastActivity prometheus.Gauge
|
|
|
|
}
|
|
|
|
|
2021-06-21 11:21:42 +02:00
|
|
|
// NewMetrics Factory method to create a new metrics collector.
|
2021-06-04 16:38:49 +02:00
|
|
|
func NewMetrics(info InstanceInfo) *Metrics {
|
|
|
|
m := &Metrics{}
|
|
|
|
|
|
|
|
m.registry = prometheus.NewRegistry()
|
2021-08-24 12:13:58 +02:00
|
|
|
options := collectors.ProcessCollectorOpts{
|
2021-06-04 16:38:49 +02:00
|
|
|
Namespace: MetricsNamespace,
|
|
|
|
}
|
2021-08-24 12:13:58 +02:00
|
|
|
m.registry.MustRegister(collectors.NewProcessCollector(options))
|
|
|
|
m.registry.MustRegister(collectors.NewGoCollector())
|
2021-06-04 16:38:49 +02:00
|
|
|
|
|
|
|
additionalLabels := map[string]string{}
|
|
|
|
if info.InstallationID != "" {
|
|
|
|
additionalLabels[MetricsCloudInstallationLabel] = os.Getenv("MM_CLOUD_INSTALLATION_ID")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.loginCount = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: MetricsNamespace,
|
|
|
|
Subsystem: MetricsSubsystemSystem,
|
|
|
|
Name: "login_total",
|
|
|
|
Help: "Total number of logins.",
|
|
|
|
ConstLabels: additionalLabels,
|
|
|
|
})
|
|
|
|
m.registry.MustRegister(m.loginCount)
|
|
|
|
|
2021-12-01 11:21:31 +02:00
|
|
|
m.logoutCount = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: MetricsNamespace,
|
|
|
|
Subsystem: MetricsSubsystemSystem,
|
|
|
|
Name: "logout_total",
|
|
|
|
Help: "Total number of logouts.",
|
|
|
|
ConstLabels: additionalLabels,
|
|
|
|
})
|
|
|
|
m.registry.MustRegister(m.logoutCount)
|
|
|
|
|
2021-06-04 16:38:49 +02:00
|
|
|
m.loginFailCount = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: MetricsNamespace,
|
|
|
|
Subsystem: MetricsSubsystemSystem,
|
|
|
|
Name: "login_fail_total",
|
|
|
|
Help: "Total number of failed logins.",
|
|
|
|
ConstLabels: additionalLabels,
|
|
|
|
})
|
|
|
|
m.registry.MustRegister(m.loginFailCount)
|
|
|
|
|
|
|
|
m.instance = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
|
Namespace: MetricsNamespace,
|
|
|
|
Subsystem: MetricsSubsystemSystem,
|
|
|
|
Name: "focalboard_instance_info",
|
|
|
|
Help: "Instance information for Focalboard.",
|
|
|
|
ConstLabels: additionalLabels,
|
|
|
|
}, []string{"Version", "BuildNum", "Edition"})
|
|
|
|
m.registry.MustRegister(m.instance)
|
|
|
|
m.instance.WithLabelValues(info.Version, info.BuildNum, info.Edition).Set(1)
|
|
|
|
|
|
|
|
m.startTime = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Namespace: MetricsNamespace,
|
|
|
|
Subsystem: MetricsSubsystemSystem,
|
|
|
|
Name: "server_start_time",
|
|
|
|
Help: "The time the server started.",
|
|
|
|
ConstLabels: additionalLabels,
|
|
|
|
})
|
|
|
|
m.startTime.SetToCurrentTime()
|
|
|
|
m.registry.MustRegister(m.startTime)
|
|
|
|
|
|
|
|
m.blocksInsertedCount = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: MetricsNamespace,
|
|
|
|
Subsystem: MetricsSubsystemBlocks,
|
|
|
|
Name: "blocks_inserted_total",
|
|
|
|
Help: "Total number of blocks inserted.",
|
|
|
|
ConstLabels: additionalLabels,
|
|
|
|
})
|
|
|
|
m.registry.MustRegister(m.blocksInsertedCount)
|
|
|
|
|
2021-08-06 14:10:24 +02:00
|
|
|
m.blocksPatchedCount = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: MetricsNamespace,
|
|
|
|
Subsystem: MetricsSubsystemBlocks,
|
|
|
|
Name: "blocks_patched_total",
|
|
|
|
Help: "Total number of blocks patched.",
|
|
|
|
ConstLabels: additionalLabels,
|
|
|
|
})
|
|
|
|
m.registry.MustRegister(m.blocksPatchedCount)
|
|
|
|
|
2021-06-04 16:38:49 +02:00
|
|
|
m.blocksDeletedCount = prometheus.NewCounter(prometheus.CounterOpts{
|
|
|
|
Namespace: MetricsNamespace,
|
|
|
|
Subsystem: MetricsSubsystemBlocks,
|
|
|
|
Name: "blocks_deleted_total",
|
|
|
|
Help: "Total number of blocks deleted.",
|
|
|
|
ConstLabels: additionalLabels,
|
|
|
|
})
|
|
|
|
m.registry.MustRegister(m.blocksDeletedCount)
|
|
|
|
|
|
|
|
m.blockCount = prometheus.NewGaugeVec(prometheus.GaugeOpts{
|
|
|
|
Namespace: MetricsNamespace,
|
|
|
|
Subsystem: MetricsSubsystemBlocks,
|
|
|
|
Name: "blocks_total",
|
|
|
|
Help: "Total number of blocks.",
|
|
|
|
ConstLabels: additionalLabels,
|
|
|
|
}, []string{"BlockType"})
|
|
|
|
m.registry.MustRegister(m.blockCount)
|
|
|
|
|
2022-09-13 22:37:05 +02:00
|
|
|
m.boardCount = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Namespace: MetricsNamespace,
|
|
|
|
Subsystem: MetricsSubsystemBoards,
|
|
|
|
Name: "boards_total",
|
|
|
|
Help: "Total number of boards.",
|
|
|
|
ConstLabels: additionalLabels,
|
|
|
|
})
|
|
|
|
m.registry.MustRegister(m.boardCount)
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
m.teamCount = prometheus.NewGauge(prometheus.GaugeOpts{
|
2021-06-04 16:38:49 +02:00
|
|
|
Namespace: MetricsNamespace,
|
2022-03-22 16:24:34 +02:00
|
|
|
Subsystem: MetricsSubsystemTeams,
|
|
|
|
Name: "teams_total",
|
|
|
|
Help: "Total number of teams.",
|
2021-06-04 16:38:49 +02:00
|
|
|
ConstLabels: additionalLabels,
|
|
|
|
})
|
2022-03-22 16:24:34 +02:00
|
|
|
m.registry.MustRegister(m.teamCount)
|
2021-06-04 16:38:49 +02:00
|
|
|
|
|
|
|
m.blockLastActivity = prometheus.NewGauge(prometheus.GaugeOpts{
|
|
|
|
Namespace: MetricsNamespace,
|
|
|
|
Subsystem: MetricsSubsystemBlocks,
|
|
|
|
Name: "blocks_last_activity",
|
|
|
|
Help: "Time of last block insert, update, delete.",
|
|
|
|
ConstLabels: additionalLabels,
|
|
|
|
})
|
|
|
|
m.registry.MustRegister(m.blockLastActivity)
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Metrics) IncrementLoginCount(num int) {
|
|
|
|
if m != nil {
|
|
|
|
m.loginCount.Add(float64(num))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-01 11:21:31 +02:00
|
|
|
func (m *Metrics) IncrementLogoutCount(num int) {
|
|
|
|
if m != nil {
|
|
|
|
m.logoutCount.Add(float64(num))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 16:38:49 +02:00
|
|
|
func (m *Metrics) IncrementLoginFailCount(num int) {
|
|
|
|
if m != nil {
|
|
|
|
m.loginFailCount.Add(float64(num))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Metrics) IncrementBlocksInserted(num int) {
|
|
|
|
if m != nil {
|
|
|
|
m.blocksInsertedCount.Add(float64(num))
|
|
|
|
m.blockLastActivity.SetToCurrentTime()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-06 14:10:24 +02:00
|
|
|
func (m *Metrics) IncrementBlocksPatched(num int) {
|
|
|
|
if m != nil {
|
|
|
|
m.blocksPatchedCount.Add(float64(num))
|
|
|
|
m.blockLastActivity.SetToCurrentTime()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-04 16:38:49 +02:00
|
|
|
func (m *Metrics) IncrementBlocksDeleted(num int) {
|
|
|
|
if m != nil {
|
|
|
|
m.blocksDeletedCount.Add(float64(num))
|
|
|
|
m.blockLastActivity.SetToCurrentTime()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Metrics) ObserveBlockCount(blockType string, count int64) {
|
|
|
|
if m != nil {
|
|
|
|
m.blockCount.WithLabelValues(blockType).Set(float64(count))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-13 22:37:05 +02:00
|
|
|
func (m *Metrics) ObserveBoardCount(count int64) {
|
|
|
|
if m != nil {
|
|
|
|
m.boardCount.Set(float64(count))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-22 16:24:34 +02:00
|
|
|
func (m *Metrics) ObserveTeamCount(count int64) {
|
2021-06-04 16:38:49 +02:00
|
|
|
if m != nil {
|
2022-03-22 16:24:34 +02:00
|
|
|
m.teamCount.Set(float64(count))
|
2021-06-04 16:38:49 +02:00
|
|
|
}
|
|
|
|
}
|