183 lines
5.4 KiB
Go
183 lines
5.4 KiB
Go
package monitoring
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
|
)
|
|
|
|
// Metrics 指标收集器
|
|
type Metrics struct {
|
|
registry *prometheus.Registry
|
|
writeCounter prometheus.Counter
|
|
queryCounter prometheus.Counter
|
|
writeLatency prometheus.Histogram
|
|
queryLatency prometheus.Histogram
|
|
activeConnections prometheus.Gauge
|
|
dataPointsCount prometheus.Gauge
|
|
persistenceLatency prometheus.Histogram
|
|
persistenceErrors prometheus.Counter
|
|
messagingLatency prometheus.Histogram
|
|
messagingErrors prometheus.Counter
|
|
websocketConnections prometheus.Gauge
|
|
}
|
|
|
|
// NewMetrics 创建一个新的指标收集器
|
|
func NewMetrics() *Metrics {
|
|
registry := prometheus.NewRegistry()
|
|
|
|
writeCounter := prometheus.NewCounter(prometheus.CounterOpts{
|
|
Name: "gotidb_write_total",
|
|
Help: "Total number of write operations",
|
|
})
|
|
|
|
queryCounter := prometheus.NewCounter(prometheus.CounterOpts{
|
|
Name: "gotidb_query_total",
|
|
Help: "Total number of query operations",
|
|
})
|
|
|
|
writeLatency := prometheus.NewHistogram(prometheus.HistogramOpts{
|
|
Name: "gotidb_write_latency_seconds",
|
|
Help: "Write operation latency in seconds",
|
|
Buckets: prometheus.DefBuckets,
|
|
})
|
|
|
|
queryLatency := prometheus.NewHistogram(prometheus.HistogramOpts{
|
|
Name: "gotidb_query_latency_seconds",
|
|
Help: "Query operation latency in seconds",
|
|
Buckets: prometheus.DefBuckets,
|
|
})
|
|
|
|
activeConnections := prometheus.NewGauge(prometheus.GaugeOpts{
|
|
Name: "gotidb_active_connections",
|
|
Help: "Number of active connections",
|
|
})
|
|
|
|
dataPointsCount := prometheus.NewGauge(prometheus.GaugeOpts{
|
|
Name: "gotidb_data_points_count",
|
|
Help: "Number of data points in the database",
|
|
})
|
|
|
|
persistenceLatency := prometheus.NewHistogram(prometheus.HistogramOpts{
|
|
Name: "gotidb_persistence_latency_seconds",
|
|
Help: "Persistence operation latency in seconds",
|
|
Buckets: prometheus.DefBuckets,
|
|
})
|
|
|
|
persistenceErrors := prometheus.NewCounter(prometheus.CounterOpts{
|
|
Name: "gotidb_persistence_errors_total",
|
|
Help: "Total number of persistence errors",
|
|
})
|
|
|
|
messagingLatency := prometheus.NewHistogram(prometheus.HistogramOpts{
|
|
Name: "gotidb_messaging_latency_seconds",
|
|
Help: "Messaging operation latency in seconds",
|
|
Buckets: prometheus.DefBuckets,
|
|
})
|
|
|
|
messagingErrors := prometheus.NewCounter(prometheus.CounterOpts{
|
|
Name: "gotidb_messaging_errors_total",
|
|
Help: "Total number of messaging errors",
|
|
})
|
|
|
|
websocketConnections := prometheus.NewGauge(prometheus.GaugeOpts{
|
|
Name: "gotidb_websocket_connections",
|
|
Help: "Number of active WebSocket connections",
|
|
})
|
|
|
|
// 注册指标
|
|
registry.MustRegister(writeCounter)
|
|
registry.MustRegister(queryCounter)
|
|
registry.MustRegister(writeLatency)
|
|
registry.MustRegister(queryLatency)
|
|
registry.MustRegister(activeConnections)
|
|
registry.MustRegister(dataPointsCount)
|
|
registry.MustRegister(persistenceLatency)
|
|
registry.MustRegister(persistenceErrors)
|
|
registry.MustRegister(messagingLatency)
|
|
registry.MustRegister(messagingErrors)
|
|
registry.MustRegister(websocketConnections)
|
|
|
|
return &Metrics{
|
|
registry: registry,
|
|
writeCounter: writeCounter,
|
|
queryCounter: queryCounter,
|
|
writeLatency: writeLatency,
|
|
queryLatency: queryLatency,
|
|
activeConnections: activeConnections,
|
|
dataPointsCount: dataPointsCount,
|
|
persistenceLatency: persistenceLatency,
|
|
persistenceErrors: persistenceErrors,
|
|
messagingLatency: messagingLatency,
|
|
messagingErrors: messagingErrors,
|
|
websocketConnections: websocketConnections,
|
|
}
|
|
}
|
|
|
|
// IncrementWriteCounter 增加写入计数器
|
|
func (m *Metrics) IncrementWriteCounter() {
|
|
m.writeCounter.Inc()
|
|
}
|
|
|
|
// IncrementQueryCounter 增加查询计数器
|
|
func (m *Metrics) IncrementQueryCounter() {
|
|
m.queryCounter.Inc()
|
|
}
|
|
|
|
// ObserveWriteLatency 观察写入延迟
|
|
func (m *Metrics) ObserveWriteLatency(seconds float64) {
|
|
m.writeLatency.Observe(seconds)
|
|
}
|
|
|
|
// ObserveQueryLatency 观察查询延迟
|
|
func (m *Metrics) ObserveQueryLatency(seconds float64) {
|
|
m.queryLatency.Observe(seconds)
|
|
}
|
|
|
|
// SetActiveConnections 设置活跃连接数
|
|
func (m *Metrics) SetActiveConnections(count float64) {
|
|
m.activeConnections.Set(count)
|
|
}
|
|
|
|
// SetDataPointsCount 设置数据点数量
|
|
func (m *Metrics) SetDataPointsCount(count float64) {
|
|
m.dataPointsCount.Set(count)
|
|
}
|
|
|
|
// ObservePersistenceLatency 观察持久化延迟
|
|
func (m *Metrics) ObservePersistenceLatency(seconds float64) {
|
|
m.persistenceLatency.Observe(seconds)
|
|
}
|
|
|
|
// IncrementPersistenceErrors 增加持久化错误计数器
|
|
func (m *Metrics) IncrementPersistenceErrors() {
|
|
m.persistenceErrors.Inc()
|
|
}
|
|
|
|
// ObserveMessagingLatency 观察消息延迟
|
|
func (m *Metrics) ObserveMessagingLatency(seconds float64) {
|
|
m.messagingLatency.Observe(seconds)
|
|
}
|
|
|
|
// IncrementMessagingErrors 增加消息错误计数器
|
|
func (m *Metrics) IncrementMessagingErrors() {
|
|
m.messagingErrors.Inc()
|
|
}
|
|
|
|
// SetWebsocketConnections 设置WebSocket连接数
|
|
func (m *Metrics) SetWebsocketConnections(count float64) {
|
|
m.websocketConnections.Set(count)
|
|
}
|
|
|
|
// Handler 返回Prometheus HTTP处理器
|
|
func (m *Metrics) Handler() http.Handler {
|
|
return promhttp.HandlerFor(m.registry, promhttp.HandlerOpts{})
|
|
}
|
|
|
|
// StartServer 启动指标服务器
|
|
func (m *Metrics) StartServer(addr string) error {
|
|
http.Handle("/metrics", m.Handler())
|
|
return http.ListenAndServe(addr, nil)
|
|
}
|