This repository was archived by the owner on May 26, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics.go
More file actions
174 lines (153 loc) · 5.27 KB
/
metrics.go
File metadata and controls
174 lines (153 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"context"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"gorm.io/gorm"
)
// Metrics contains all Prometheus metrics for the application
type Metrics struct {
// WebSocket connection metrics
ConnectedClients prometheus.Gauge
ConnectionsTotal prometheus.Counter
MessageReceived prometheus.Counter
MessageSent prometheus.Counter
// Authentication metrics
AuthRequests prometheus.Counter
AuthSuccess prometheus.Counter
AuthFailure prometheus.Counter
// Channel metrics
ChannelsTotal prometheus.Gauge
ChannelsOpen prometheus.Gauge
ChannelsClosed prometheus.Gauge
// RPC method metrics
RPCRequests *prometheus.CounterVec
// Application metrics
AppSessionsTotal prometheus.Gauge
// Smart contract metrics
BrokerBalanceAvailable *prometheus.GaugeVec
BrokerChannelCount *prometheus.GaugeVec
}
// NewMetrics initializes and registers Prometheus metrics
func NewMetrics() *Metrics {
metrics := &Metrics{
ConnectedClients: promauto.NewGauge(prometheus.GaugeOpts{
Name: "clearnet_connected_clients",
Help: "The current number of connected clients",
}),
ConnectionsTotal: promauto.NewCounter(prometheus.CounterOpts{
Name: "clearnet_connections_total",
Help: "The total number of WebSocket connections made since server start",
}),
MessageReceived: promauto.NewCounter(prometheus.CounterOpts{
Name: "clearnet_ws_messages_received_total",
Help: "The total number of WebSocket messages received",
}),
MessageSent: promauto.NewCounter(prometheus.CounterOpts{
Name: "clearnet_ws_messages_sent_total",
Help: "The total number of WebSocket messages sent",
}),
AuthRequests: promauto.NewCounter(prometheus.CounterOpts{
Name: "clearnet_auth_requests_total",
Help: "The total number of authentication requests",
}),
AuthSuccess: promauto.NewCounter(prometheus.CounterOpts{
Name: "clearnet_auth_success_total",
Help: "The total number of successful authentications",
}),
AuthFailure: promauto.NewCounter(prometheus.CounterOpts{
Name: "clearnet_auth_failure_total",
Help: "The total number of failed authentications",
}),
ChannelsTotal: promauto.NewGauge(prometheus.GaugeOpts{
Name: "clearnet_channels_total",
Help: "The total number of channels",
}),
ChannelsOpen: promauto.NewGauge(prometheus.GaugeOpts{
Name: "clearnet_channels_open",
Help: "The number of open channels",
}),
ChannelsClosed: promauto.NewGauge(prometheus.GaugeOpts{
Name: "clearnet_channels_closed",
Help: "The number of closed channels",
}),
RPCRequests: promauto.NewCounterVec(
prometheus.CounterOpts{
Name: "clearnet_rpc_requests_total",
Help: "The total number of RPC requests by method",
},
[]string{"method"},
),
AppSessionsTotal: promauto.NewGauge(prometheus.GaugeOpts{
Name: "clearnet_app_sessions_total",
Help: "The total number of application sessions",
}),
BrokerBalanceAvailable: promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "clearnet_broker_balance_available",
Help: "Available balance of the broker on the custody contract",
},
[]string{"network", "token"},
),
BrokerChannelCount: promauto.NewGaugeVec(
prometheus.GaugeOpts{
Name: "clearnet_broker_channel_count",
Help: "Number of channels for the broker on the custody contract",
},
[]string{"network", "token"},
),
}
return metrics
}
func (m *Metrics) RecordMetricsPeriodically(db *gorm.DB, custodyClients map[string]*Custody) {
dbTicker := time.NewTicker(15 * time.Second)
defer dbTicker.Stop()
balanceTicker := time.NewTicker(30 * time.Second)
defer balanceTicker.Stop()
for {
select {
case <-dbTicker.C:
m.UpdateChannelMetrics(db)
m.UpdateAppSessionMetrics(db)
case <-balanceTicker.C:
// Refresh the list of tokens to monitor
monitoredTokens := GetUniqueTokenAddresses(db)
// Update metrics for each custody client
for _, client := range custodyClients {
client.UpdateBalanceMetrics(context.Background(), monitoredTokens, m)
}
}
}
}
// UpdateChannelMetrics updates the channel metrics from the database
func (m *Metrics) UpdateChannelMetrics(db *gorm.DB) {
var total, open, closed int64
db.Model(&Channel{}).Count(&total)
db.Model(&Channel{}).Where("status = ?", ChannelStatusOpen).Count(&open)
db.Model(&Channel{}).Where("status = ?", ChannelStatusClosed).Count(&closed)
m.ChannelsTotal.Set(float64(total))
m.ChannelsOpen.Set(float64(open))
m.ChannelsClosed.Set(float64(closed))
}
// UpdateAppSessionMetrics updates the application session metrics from the database
func (m *Metrics) UpdateAppSessionMetrics(db *gorm.DB) {
var count int64
db.Model(&AppSession{}).Count(&count)
m.AppSessionsTotal.Set(float64(count))
}
// GetUniqueTokenAddresses returns a list of unique token addresses from the database
func GetUniqueTokenAddresses(db *gorm.DB) []common.Address {
var tokens []string
// Query unique token addresses from the channels table
db.Model(&Channel{}).Distinct().Pluck("token", &tokens)
// Convert to common.Address and remove empty strings
addresses := make([]common.Address, 0, len(tokens))
for _, tokenStr := range tokens {
if tokenStr != "" {
addresses = append(addresses, common.HexToAddress(tokenStr))
}
}
return addresses
}