forked from ussyalfaks/Grainlify-Stellar-Contracts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmonitoring.rs
More file actions
276 lines (234 loc) · 9.26 KB
/
Copy pathmonitoring.rs
File metadata and controls
276 lines (234 loc) · 9.26 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
//! Monitoring and Analytics Module
//!
//! Note on Health Check Semantics:
//! The `health_check` function evaluates health using a rolling time window.
//! An alert state triggered by a high error rate will clear (`is_healthy` flips to `true`)
//! purely from the passage of time once the `WINDOW_DURATION` has elapsed, even if there
//! are no new successful operations. This is a time-based clear, not a recovery-based clear.
//! Off-chain monitoring relying on `health_check` should be aware that a healthy status
//! might simply mean the error window decayed, and not necessarily that the underlying
//! issue was fixed.
use soroban_sdk::{contracttype, symbol_short, Address, Env, String, Symbol};
// Storage keys
const OPERATION_COUNT: &str = "op_count"; // Lifetime metric (never decays)
const USER_COUNT: &str = "usr_count"; // Lifetime metric (never decays)
const ERROR_COUNT: &str = "err_count"; // Lifetime metric (never decays)
// Window keys for metric decay
const WINDOW_START: &str = "win_start";
const WINDOW_OPS: &str = "win_ops";
const WINDOW_ERRS: &str = "win_errs";
const WINDOW_DURATION: u64 = 3600; // 1 hour rolling window
const ERROR_RATE_THRESHOLD_BPS: u32 = 5000; // 50% error rate trips alert
// Event: Operation metric
#[contracttype]
#[derive(Clone, Debug)]
pub struct OperationMetric {
pub operation: Symbol,
pub caller: Address,
pub timestamp: u64,
pub success: bool,
}
// Event: Performance metric
#[contracttype]
#[derive(Clone, Debug)]
pub struct PerformanceMetric {
pub function: Symbol,
pub duration: u64,
pub timestamp: u64,
}
// Data: Health status
#[contracttype]
#[derive(Clone, Debug)]
pub struct HealthStatus {
pub is_healthy: bool,
pub last_operation: u64,
pub total_operations: u64,
pub contract_version: String,
}
// Data: Analytics
#[contracttype]
#[derive(Clone, Debug)]
pub struct Analytics {
pub operation_count: u64,
pub unique_users: u64,
pub error_count: u64,
pub error_rate: u32,
}
// Data: State snapshot
#[contracttype]
#[derive(Clone, Debug)]
pub struct StateSnapshot {
pub timestamp: u64,
pub total_operations: u64,
pub total_users: u64,
pub total_errors: u64,
}
// Data: Performance stats
#[contracttype]
#[derive(Clone, Debug)]
pub struct PerformanceStats {
pub function_name: Symbol,
pub call_count: u64,
pub total_time: u64,
pub avg_time: u64,
pub last_called: u64,
}
// Track operation
pub fn track_operation(env: &Env, operation: Symbol, caller: Address, success: bool) {
let key = Symbol::new(env, OPERATION_COUNT);
let count: u64 = env.storage().persistent().get(&key).unwrap_or(0);
env.storage().persistent().set(&key, &(count + 1));
if !success {
let err_key = Symbol::new(env, ERROR_COUNT);
let err_count: u64 = env.storage().persistent().get(&err_key).unwrap_or(0);
env.storage().persistent().set(&err_key, &(err_count + 1));
}
// Window logic for metric decay/reset
let start_key = Symbol::new(env, WINDOW_START);
let win_ops_key = Symbol::new(env, WINDOW_OPS);
let win_errs_key = Symbol::new(env, WINDOW_ERRS);
let now = env.ledger().timestamp();
let win_start_opt: Option<u64> = env.storage().persistent().get(&start_key);
let win_start_val = win_start_opt.unwrap_or(now);
// Explicit reset after WINDOW_DURATION or on first operation
if win_start_opt.is_none() || now.saturating_sub(win_start_val) >= WINDOW_DURATION {
env.storage().persistent().set(&start_key, &now);
env.storage().persistent().set(&win_ops_key, &1u64);
env.storage().persistent().set(&win_errs_key, &(if success { 0u64 } else { 1u64 }));
} else {
let w_ops: u64 = env.storage().persistent().get(&win_ops_key).unwrap_or(0);
env.storage().persistent().set(&win_ops_key, &(w_ops + 1));
if !success {
let w_errs: u64 = env.storage().persistent().get(&win_errs_key).unwrap_or(0);
env.storage().persistent().set(&win_errs_key, &(w_errs + 1));
}
}
env.events().publish(
(symbol_short!("metric"), symbol_short!("op")),
OperationMetric {
operation,
caller,
timestamp: now,
success,
},
);
}
// Track performance
pub fn emit_performance(env: &Env, function: Symbol, duration: u64) {
let count_key = (Symbol::new(env, "perf_cnt"), function.clone());
let time_key = (Symbol::new(env, "perf_time"), function.clone());
let count: u64 = env.storage().persistent().get(&count_key).unwrap_or(0);
let total: u64 = env.storage().persistent().get(&time_key).unwrap_or(0);
env.storage().persistent().set(&count_key, &(count + 1));
env.storage()
.persistent()
.set(&time_key, &(total + duration));
env.events().publish(
(symbol_short!("metric"), symbol_short!("perf")),
PerformanceMetric {
function,
duration,
timestamp: env.ledger().timestamp(),
},
);
}
// Health check
pub fn health_check(env: &Env) -> HealthStatus {
let key = Symbol::new(env, OPERATION_COUNT);
let ops: u64 = env.storage().persistent().get(&key).unwrap_or(0);
let start_key = Symbol::new(env, WINDOW_START);
let win_start_opt: Option<u64> = env.storage().persistent().get(&start_key);
let win_start_val = win_start_opt.unwrap_or(0); // If none, then error rate check doesn't matter much, but we handle it
let now = env.ledger().timestamp();
// An alert triggered by a stale metric clears once the window decays
let is_healthy = if win_start_opt.is_none() || now.saturating_sub(win_start_val) >= WINDOW_DURATION {
true
} else {
let win_ops_key = Symbol::new(env, WINDOW_OPS);
let win_errs_key = Symbol::new(env, WINDOW_ERRS);
let w_ops: u64 = env.storage().persistent().get(&win_ops_key).unwrap_or(0);
let w_errs: u64 = env.storage().persistent().get(&win_errs_key).unwrap_or(0);
if w_ops > 0 {
let error_rate = (w_errs as u128 * 10_000) / (w_ops as u128);
error_rate < ERROR_RATE_THRESHOLD_BPS as u128
} else {
true
}
};
HealthStatus {
is_healthy,
last_operation: env.ledger().timestamp(),
total_operations: ops,
contract_version: String::from_str(env, "1.0.0"),
}
}
// Get analytics
pub fn get_analytics(env: &Env) -> Analytics {
let op_key = Symbol::new(env, OPERATION_COUNT);
let usr_key = Symbol::new(env, USER_COUNT);
let err_key = Symbol::new(env, ERROR_COUNT);
let ops: u64 = env.storage().persistent().get(&op_key).unwrap_or(0);
let users: u64 = env.storage().persistent().get(&usr_key).unwrap_or(0);
let errors: u64 = env.storage().persistent().get(&err_key).unwrap_or(0);
// Basis points via truncating integer division (floor toward zero).
// Off-chain alert thresholds should account for this slight under-report
// versus the true floating-point rate (e.g. 1/3 => 3333 bps, not 3334).
let error_rate = if ops > 0 {
((errors as u128 * 10000) / ops as u128) as u32
} else {
0
};
Analytics {
operation_count: ops,
unique_users: users,
error_count: errors,
error_rate,
}
}
// Get state snapshot
pub fn get_state_snapshot(env: &Env) -> StateSnapshot {
let op_key = Symbol::new(env, OPERATION_COUNT);
let usr_key = Symbol::new(env, USER_COUNT);
let err_key = Symbol::new(env, ERROR_COUNT);
StateSnapshot {
timestamp: env.ledger().timestamp(),
total_operations: env.storage().persistent().get(&op_key).unwrap_or(0),
total_users: env.storage().persistent().get(&usr_key).unwrap_or(0),
total_errors: env.storage().persistent().get(&err_key).unwrap_or(0),
}
}
// Get performance stats
pub fn get_performance_stats(env: &Env, function_name: Symbol) -> PerformanceStats {
let count_key = (Symbol::new(env, "perf_cnt"), function_name.clone());
let time_key = (Symbol::new(env, "perf_time"), function_name.clone());
let last_key = (Symbol::new(env, "perf_last"), function_name.clone());
let count: u64 = env.storage().persistent().get(&count_key).unwrap_or(0);
let total: u64 = env.storage().persistent().get(&time_key).unwrap_or(0);
let last: u64 = env.storage().persistent().get(&last_key).unwrap_or(0);
let avg = if count > 0 { total / count } else { 0 };
PerformanceStats {
function_name,
call_count: count,
total_time: total,
avg_time: avg,
last_called: last,
}
}
const LARGE_PAYOUT_THRESHOLD: &str = "large_payout_threshold";
const DEFAULT_LARGE_PAYOUT_THRESHOLD_BPS: u32 = 1000;
const LARGE_PAYOUT_THRESHOLD_DENOMINATOR: i128 = 10_000;
pub fn set_large_payout_threshold_bps(env: &Env, threshold_bps: u32) {
env.storage()
.instance()
.set(&Symbol::new(env, LARGE_PAYOUT_THRESHOLD), &threshold_bps);
}
pub fn get_large_payout_threshold_bps(env: &Env) -> u32 {
env.storage()
.instance()
.get(&Symbol::new(env, LARGE_PAYOUT_THRESHOLD))
.unwrap_or(DEFAULT_LARGE_PAYOUT_THRESHOLD_BPS)
}
pub fn get_large_payout_threshold_amount(env: &Env, total_funds: i128) -> i128 {
let threshold_bps = get_large_payout_threshold_bps(env) as i128;
total_funds * threshold_bps / LARGE_PAYOUT_THRESHOLD_DENOMINATOR
}