160 lines
5.0 KiB
Go
160 lines
5.0 KiB
Go
package monitoring
|
|
|
|
import (
|
|
"time"
|
|
|
|
"github.com/prometheus/client_golang/prometheus"
|
|
)
|
|
|
|
// MetricsCollector 提供更简洁的指标收集API
|
|
type MetricsCollector struct {
|
|
writeTotal prometheus.Counter
|
|
queryTotal 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
|
|
}
|
|
|
|
// NewMetricsCollector 创建一个新的指标收集器
|
|
func NewMetricsCollector() *MetricsCollector {
|
|
return &MetricsCollector{
|
|
writeTotal: prometheus.NewCounter(prometheus.CounterOpts{
|
|
Name: "gotidb_write_total",
|
|
Help: "Total number of write operations",
|
|
}),
|
|
queryTotal: 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",
|
|
}),
|
|
}
|
|
}
|
|
|
|
// RecordWrite 记录写入操作及其延迟
|
|
func (c *MetricsCollector) RecordWrite(duration time.Duration) {
|
|
c.writeTotal.Inc()
|
|
c.writeLatency.Observe(duration.Seconds())
|
|
}
|
|
|
|
// RecordQuery 记录查询操作及其延迟
|
|
func (c *MetricsCollector) RecordQuery(duration time.Duration) {
|
|
c.queryTotal.Inc()
|
|
c.queryLatency.Observe(duration.Seconds())
|
|
}
|
|
|
|
// IncActiveConnections 增加活跃连接数
|
|
func (c *MetricsCollector) IncActiveConnections() {
|
|
c.activeConnections.Inc()
|
|
}
|
|
|
|
// DecActiveConnections 减少活跃连接数
|
|
func (c *MetricsCollector) DecActiveConnections() {
|
|
c.activeConnections.Dec()
|
|
}
|
|
|
|
// SetDataPointsCount 设置数据点数量
|
|
func (c *MetricsCollector) SetDataPointsCount(count float64) {
|
|
c.dataPointsCount.Set(count)
|
|
}
|
|
|
|
// RecordPersistence 记录持久化操作及其延迟
|
|
func (c *MetricsCollector) RecordPersistence(duration time.Duration, err error) {
|
|
c.persistenceLatency.Observe(duration.Seconds())
|
|
if err != nil {
|
|
c.persistenceErrors.Inc()
|
|
}
|
|
}
|
|
|
|
// RecordMessaging 记录消息操作及其延迟
|
|
func (c *MetricsCollector) RecordMessaging(duration time.Duration, err error) {
|
|
c.messagingLatency.Observe(duration.Seconds())
|
|
if err != nil {
|
|
c.messagingErrors.Inc()
|
|
}
|
|
}
|
|
|
|
// IncWebSocketConnections 增加WebSocket连接数
|
|
func (c *MetricsCollector) IncWebSocketConnections() {
|
|
c.websocketConnections.Inc()
|
|
}
|
|
|
|
// DecWebSocketConnections 减少WebSocket连接数
|
|
func (c *MetricsCollector) DecWebSocketConnections() {
|
|
c.websocketConnections.Dec()
|
|
}
|
|
|
|
// Describe 实现prometheus.Collector接口
|
|
func (c *MetricsCollector) Describe(ch chan<- *prometheus.Desc) {
|
|
c.writeTotal.Describe(ch)
|
|
c.queryTotal.Describe(ch)
|
|
c.writeLatency.Describe(ch)
|
|
c.queryLatency.Describe(ch)
|
|
c.activeConnections.Describe(ch)
|
|
c.dataPointsCount.Describe(ch)
|
|
c.persistenceLatency.Describe(ch)
|
|
c.persistenceErrors.Describe(ch)
|
|
c.messagingLatency.Describe(ch)
|
|
c.messagingErrors.Describe(ch)
|
|
c.websocketConnections.Describe(ch)
|
|
}
|
|
|
|
// Collect 实现prometheus.Collector接口
|
|
func (c *MetricsCollector) Collect(ch chan<- prometheus.Metric) {
|
|
c.writeTotal.Collect(ch)
|
|
c.queryTotal.Collect(ch)
|
|
c.writeLatency.Collect(ch)
|
|
c.queryLatency.Collect(ch)
|
|
c.activeConnections.Collect(ch)
|
|
c.dataPointsCount.Collect(ch)
|
|
c.persistenceLatency.Collect(ch)
|
|
c.persistenceErrors.Collect(ch)
|
|
c.messagingLatency.Collect(ch)
|
|
c.messagingErrors.Collect(ch)
|
|
c.websocketConnections.Collect(ch)
|
|
}
|