Browse Source

StatsD integration

tags/v0.3.0 v0.3.0
Stephen 6 months ago
parent
commit
93ed457af2
6 changed files with 104 additions and 25 deletions
  1. +70
    -7
      Cargo.lock
  2. +2
    -1
      Cargo.toml
  3. +2
    -1
      config.example
  4. +2
    -1
      debian/config.json
  5. +2
    -1
      src/config.rs
  6. +26
    -14
      src/main.rs

+ 70
- 7
Cargo.lock View File

@ -124,7 +124,7 @@ dependencies = [
[[package]]
name = "cat_disruptor_7000"
version = "0.2.0"
version = "0.3.0"
dependencies = [
"async-trait",
"matrix-sdk",
@ -135,6 +135,7 @@ dependencies = [
"ruma-identifiers 0.16.2",
"serde",
"serde_json",
"statsd",
"tokio",
"tracing",
"tracing-subscriber",
@ -289,6 +290,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00b0228411908ca8685dba7fc2cdd70ec9990a6e753e89b6ac91a84c40fbaf4b"
[[package]]
name = "fuchsia-cprng"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a06f77d526c1a601b7c4cdd98f54b5eaabffc14d5f2f0296febdc7f357c6d3ba"
[[package]]
name = "fuchsia-zircon"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
@ -1072,6 +1079,29 @@ dependencies = [
[[package]]
name = "rand"
version = "0.3.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64ac302d8f83c0c1974bf758f6b041c6c8ada916fbb44a609158ca8b064cc76c"
dependencies = [
"libc",
"rand 0.4.6",
]
[[package]]
name = "rand"
version = "0.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "552840b97013b1a26992c11eac34bdd778e464601a4c2054b5f0bff7c6761293"
dependencies = [
"fuchsia-cprng",
"libc",
"rand_core 0.3.1",
"rdrand",
"winapi 0.3.9",
]
[[package]]
name = "rand"
version = "0.7.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03"
@ -1079,7 +1109,7 @@ dependencies = [
"getrandom",
"libc",
"rand_chacha",
"rand_core",
"rand_core 0.5.1",
"rand_hc",
]
@ -1090,11 +1120,26 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402"
dependencies = [
"ppv-lite86",
"rand_core",
"rand_core 0.5.1",
]
[[package]]
name = "rand_core"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7a6fdeb83b075e8266dcc8762c22776f6877a63111121f5f8c7411e5be7eed4b"
dependencies = [
"rand_core 0.4.2",
]
[[package]]
name = "rand_core"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c33a3c44ca05fa6f1807d8e6743f3824e8509beca625669633be0acbdf509dc"
[[package]]
name = "rand_core"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19"
@ -1108,7 +1153,16 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ca3129af7b92a17112d59ad498c6f81eaf463253766b90396d39ea7a39d6613c"
dependencies = [
"rand_core",
"rand_core 0.5.1",
]
[[package]]
name = "rdrand"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "678054eb77286b51581ba43620cc911abf02758c91f93f479767aed0f90458b2"
dependencies = [
"rand_core 0.3.1",
]
[[package]]
@ -1302,7 +1356,7 @@ version = "0.14.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07e442c700a3b33fc4dd4a1c4b463ebdd252d2c2db31b83da6bb3009307039b9"
dependencies = [
"rand",
"rand 0.7.3",
"serde",
"url",
]
@ -1557,6 +1611,15 @@ dependencies = [
]
[[package]]
name = "statsd"
version = "0.13.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd6fa1b2cc4135c60cd91027e5d28624fb4b405fd64ae32ac4227f6d4317d0b4"
dependencies = [
"rand 0.3.23",
]
[[package]]
name = "strum"
version = "0.18.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
@ -1608,7 +1671,7 @@ checksum = "7a6e24d9338a0a5be79593e2fa15a648add6138caa803e2d5bc782c371732ca9"
dependencies = [
"cfg-if",
"libc",
"rand",
"rand 0.7.3",
"redox_syscall",
"remove_dir_all",
"winapi 0.3.9",
@ -1868,7 +1931,7 @@ version = "0.8.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11"
dependencies = [
"rand",
"rand 0.7.3",
]
[[package]]


+ 2
- 1
Cargo.toml View File

@ -1,6 +1,6 @@
[package]
name = "cat_disruptor_7000"
version = "0.2.0"
version = "0.3.0"
authors = ["Stephen D <@stephen:m.scd31.com>"]
edition = "2018"
@ -19,6 +19,7 @@ serde_json = "1.0"
tracing = "0.1"
tracing-subscriber = "0.2"
uuid = { version = "0.8.1", features = ["v4"] }
statsd = "^0.13.1"
[package.metadata.deb]
maintainer = "Stephen D <@stephen:m.scd31.com>"


+ 2
- 1
config.example View File

@ -2,5 +2,6 @@
"homeserver_url": "changeme",
"username": "changeme",
"password": "changeme",
"store_path": "crypto"
"store_path": "crypto",
"statsd_server": "127.0.0.1:8125"
}

+ 2
- 1
debian/config.json View File

@ -2,5 +2,6 @@
"homeserver_url": "changeme",
"username": "changeme",
"password": "changeme",
"store_path": "/etc/cat_disruptor_7000/store"
"store_path": "/etc/cat_disruptor_7000/store",
"statsd_server": "127.0.0.1:8125"
}

+ 2
- 1
src/config.rs View File

@ -7,7 +7,8 @@ pub struct Config {
pub homeserver_url: String,
pub username: String,
pub password: String,
pub store_path: String
pub store_path: String,
pub statsd_server: String
}
pub fn get_config() -> Config {


+ 26
- 14
src/main.rs View File

@ -30,12 +30,13 @@ mod config;
struct EventCallback {
client: Client,
user_id: UserId
user_id: UserId,
statsd_client: statsd::Client
}
impl EventCallback {
pub fn new(client: Client, user_id: UserId) -> Self {
Self { client, user_id }
pub fn new(client: Client, user_id: UserId, statsd_client: statsd::Client) -> Self {
Self { client, user_id, statsd_client }
}
}
@ -69,8 +70,11 @@ impl EventEmitter for EventCallback {
};
match self.client.send(request).await {
Ok(_) => {},
Err(x) => println!("Error while reacting: {}", x)
Ok(_) => self.statsd_client.incr("reacted.cats"),
Err(x) => {
println!("Error while reacting: {}", x);
self.statsd_client.incr("reacted.error");
}
}
}
}
@ -88,8 +92,14 @@ impl EventEmitter for EventCallback {
let room = x.read().await;
println!("Invitation received from {}", room.display_name());
match self.client.join_room_by_id(&room.room_id).await {
Ok(_) => println!("Joined {}", room.display_name()),
Err(_) => println!("There was an error while joining {}", room.display_name())
Ok(_) => {
println!("Joined {}", room.display_name());
self.statsd_client.incr("joined.success");
},
Err(_) => {
println!("There was an error while joining {}", room.display_name());
self.statsd_client.incr("joined.error");
}
};
},
_ => {}
@ -102,7 +112,8 @@ async fn login(
homeserver_url: String,
username: String,
password: String,
config: ClientConfig
config: ClientConfig,
statsd_client: statsd::Client
) -> Result<(), matrix_sdk::Error> {
let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
let mut client = Client::new_with_config(homeserver_url, config).unwrap();
@ -110,9 +121,8 @@ async fn login(
let login_response = client.login(username, password, None, Some("Cat Disruptor".to_string())).await?;
println!("Logged in as {}", login_response.user_id);
let event_callback = EventCallback::new(client.clone(), login_response.user_id);
let event_callback = EventCallback::new(client.clone(), login_response.user_id, statsd_client);
client.add_event_emitter(Box::new(event_callback)).await;
client.sync_forever(SyncSettings::new(), |_| async {}).await;
Ok(())
@ -125,12 +135,14 @@ async fn main() -> Result<(), matrix_sdk::Error> {
if config.homeserver_url == "changeme" &&
config.username == "changeme" &&
config.password == "changeme" {
panic!("Please update the values in the config file!");
config.password == "changeme"
{
panic!("Please update the values in the config file!");
}
let store_path = fs::canonicalize(Path::new(&config.store_path)).unwrap();
let statsd_client = statsd::Client::new(config.statsd_server, "cat-disruptor").unwrap();
let store_path = fs::canonicalize(Path::new(&config.store_path)).unwrap();
if !store_path.exists() {
fs::create_dir(config.store_path.clone()).unwrap();
}
@ -138,5 +150,5 @@ async fn main() -> Result<(), matrix_sdk::Error> {
let matrix_config = ClientConfig::new()
.store_path(store_path);
login(config.homeserver_url, config.username, config.password, matrix_config).await
login(config.homeserver_url, config.username, config.password, matrix_config, statsd_client).await
}

Loading…
Cancel
Save