Skip to content

Commit 7a7908e

Browse files
committed
Add basic metrics to sys-lend
1 parent b0a530c commit 7a7908e

File tree

3 files changed

+57
-23
lines changed

3 files changed

+57
-23
lines changed

src/bin/sys-lend.rs

+40-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use {
1919
},
2020
std::collections::{HashMap, HashSet},
2121
sys::{
22-
app_version,
22+
app_version, metrics,
2323
notifier::*,
2424
priority_fee::{apply_priority_fee, PriorityFee},
2525
send_transaction_until_expired,
@@ -43,6 +43,32 @@ enum Operation {
4343
Withdraw,
4444
}
4545

46+
mod dp {
47+
use super::*;
48+
49+
pub fn supply_balance(
50+
pool: &str,
51+
address: &Pubkey,
52+
maybe_token: MaybeToken,
53+
ui_amount: f64,
54+
) -> metrics::Point {
55+
metrics::Point::new("sys_lend::supply_balance")
56+
.tag("pool", pool)
57+
.tag("address", metrics::dp::pubkey_to_value(address))
58+
.tag("token", maybe_token.name())
59+
.field("amount", ui_amount)
60+
.timestamp(metrics::dp::now())
61+
}
62+
63+
pub fn supply_apy(pool: &str, maybe_token: MaybeToken, apy_bps: u64) -> metrics::Point {
64+
metrics::Point::new("sys_lend::supply_apy")
65+
.tag("pool", pool)
66+
.tag("token", maybe_token.name())
67+
.field("apy_bps", apy_bps as f64)
68+
.timestamp(metrics::dp::now())
69+
}
70+
}
71+
4672
#[tokio::main]
4773
async fn main() -> Result<(), Box<dyn std::error::Error>> {
4874
solana_logger::setup_with_default("solana=info");
@@ -292,9 +318,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
292318
let bps = matches.is_present("bps");
293319

294320
let apy = apr_to_apy(pool_supply_apr(&rpc_client, &pool, token)?) * 100.;
321+
let apy_as_bps = (apy * 100.) as u64;
295322

296323
let value = if bps {
297-
format!("{:.0}", apy * 100.)
324+
format!("{}", apy_as_bps)
298325
} else {
299326
format!("{:.2}", apy)
300327
};
@@ -307,23 +334,31 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
307334
if !raw {
308335
notifier.send(&msg).await;
309336
}
337+
metrics::push(dp::supply_apy(&pool, token, apy_as_bps)).await;
310338
println!("{msg}");
311339
}
312340
("supply-balance", Some(matches)) => {
313341
let pool = value_t_or_exit!(matches, "pool", String);
314342
let address = pubkey_of(matches, "address").unwrap();
315343
let token = MaybeToken::from(value_t!(matches, "token", Token).ok());
316344

317-
let supply_balance = pool_supply_balance(&rpc_client, &pool, token, address)?;
345+
let amount = pool_supply_balance(&rpc_client, &pool, token, address)?;
318346
let apr = pool_supply_apr(&rpc_client, &pool, token)?;
319347

320348
let msg = format!(
321349
"{}: {} supplied at {:.2}%",
322350
pool,
323-
token.format_amount(supply_balance),
351+
token.format_amount(amount),
324352
apr_to_apy(apr) * 100.
325353
);
326354
notifier.send(&msg).await;
355+
metrics::push(dp::supply_balance(
356+
&pool,
357+
&address,
358+
token,
359+
token.ui_amount(amount),
360+
))
361+
.await;
327362
println!("{msg}");
328363
}
329364
("deposit" | "withdraw", Some(matches)) => {
@@ -504,6 +539,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
504539
_ => unreachable!(),
505540
}
506541

542+
metrics::send(metrics::env_config()).await;
507543
Ok(())
508544
}
509545

src/main.rs

-10
Original file line numberDiff line numberDiff line change
@@ -4342,13 +4342,6 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
43424342
.required(true)
43434343
.help("Access Token"),
43444344
)
4345-
.arg(
4346-
Arg::with_name("org_id")
4347-
.value_name("ORGANIZATION ID")
4348-
.takes_value(true)
4349-
.required(true)
4350-
.help("Organization Id"),
4351-
)
43524345
.arg(
43534346
Arg::with_name("bucket")
43544347
.value_name("BUCKET")
@@ -5940,20 +5933,17 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
59405933
Some(MetricsConfig {
59415934
url,
59425935
token: _,
5943-
org_id,
59445936
bucket,
59455937
}) => {
59465938
println!("Url: {url}");
59475939
println!("Token: ********");
5948-
println!("Organization Id: {org_id}");
59495940
println!("Bucket: {bucket}");
59505941
}
59515942
},
59525943
("set", Some(arg_matches)) => {
59535944
db.set_metrics_config(MetricsConfig {
59545945
url: value_t_or_exit!(arg_matches, "url", String),
59555946
token: value_t_or_exit!(arg_matches, "token", String),
5956-
org_id: value_t_or_exit!(arg_matches, "org_id", String),
59575947
bucket: value_t_or_exit!(arg_matches, "bucket", String),
59585948
})?;
59595949
println!("InfluxDb configuration set");

src/metrics.rs

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
pub use influxdb_client::Client;
1+
pub use influxdb_client::{Client, Point};
22
use {
3-
influxdb_client::{Point, Precision},
3+
influxdb_client::Precision,
44
serde::{Deserialize, Serialize},
5-
std::sync::Arc,
5+
std::{env, sync::Arc},
66
tokio::sync::RwLock,
77
};
88

@@ -14,25 +14,33 @@ lazy_static::lazy_static! {
1414
pub struct MetricsConfig {
1515
pub url: String,
1616
pub token: String,
17-
pub org_id: String,
1817
pub bucket: String,
1918
}
2019

20+
pub fn env_config() -> Option<MetricsConfig> {
21+
Some(MetricsConfig {
22+
url: env::var("INFLUX_URL").ok()?,
23+
token: env::var("INFLUX_API_TOKEN").ok()?,
24+
bucket: env::var("INFLUX_BUCKET")
25+
.ok()
26+
.unwrap_or_else(|| "sys".into()),
27+
})
28+
}
29+
2130
pub async fn push(point: Point) {
2231
POINTS.write().await.push(point);
2332
}
2433

2534
pub async fn send(config: Option<MetricsConfig>) {
2635
if let Some(config) = config {
2736
let client = Client::new(config.url, config.token)
28-
.with_org_id(config.org_id)
2937
.with_bucket(config.bucket)
3038
.with_precision(Precision::MS);
31-
// let client = client.insert_to_stdout();
39+
//let client = client.insert_to_stdout();
3240
client
3341
.insert_points(
3442
&*POINTS.write().await,
35-
influxdb_client::TimestampOptions::FromPoint,
43+
influxdb_client::TimestampOptions::None,
3644
)
3745
.await
3846
.unwrap_or_else(|err| eprintln!("Failed to send metrics: {err:?}"));
@@ -50,11 +58,11 @@ pub mod dp {
5058
solana_sdk::pubkey::Pubkey,
5159
};
5260

53-
fn pubkey_to_value(p: &Pubkey) -> Value {
61+
pub fn pubkey_to_value(p: &Pubkey) -> Value {
5462
Value::Str(p.to_string())
5563
}
5664

57-
fn now() -> Timestamp {
65+
pub fn now() -> Timestamp {
5866
Timestamp::Int(Utc::now().timestamp_millis())
5967
}
6068

0 commit comments

Comments
 (0)