feat: add basic api/gateway metrics

This commit is contained in:
alyssa 2024-11-14 13:39:47 +09:00
parent 8831e8fabe
commit 93f8786da1
8 changed files with 141 additions and 53 deletions

View file

@ -1,4 +1,5 @@
use libpk::_config::ClusterSettings;
use metrics::counter;
use std::sync::{mpsc::Sender, Arc};
use tracing::{info, warn};
use twilight_gateway::{
@ -85,6 +86,12 @@ pub async fn runner(
while let Some(item) = shard.next_event(EventTypeFlags::all()).await {
match item {
Ok(event) => {
counter!(
"pluralkit_gateway_events",
"shard_id" => shard.id().number().to_string(),
"event_type" => serde_variant::to_variant_name(&event.kind()).unwrap(),
)
.increment(1);
if let Err(error) = shard_state
.handle_event(shard.id().number(), event.clone())
.await

View file

@ -1,5 +1,6 @@
use bytes::Bytes;
use fred::{clients::RedisPool, interfaces::HashesInterface};
use metrics::{counter, gauge};
use prost::Message;
use tracing::info;
use twilight_gateway::Event;
@ -59,6 +60,13 @@ impl ShardStateManager {
shard_id,
if resumed { "resumed" } else { "ready" }
);
counter!(
"pluralkit_gateway_shard_reconnect",
"shard_id" => shard_id.to_string(),
"resumed" => resumed.to_string(),
)
.increment(1);
gauge!("pluralkit_gateway_shard_up").increment(1);
let mut info = self.get_shard(shard_id).await?;
info.last_connection = chrono::offset::Utc::now().timestamp() as i32;
info.up = true;
@ -68,6 +76,7 @@ impl ShardStateManager {
async fn socket_closed(&self, shard_id: u32) -> anyhow::Result<()> {
info!("shard {} closed", shard_id);
gauge!("pluralkit_gateway_shard_up").decrement(1);
let mut info = self.get_shard(shard_id).await?;
info.up = false;
info.disconnection_count += 1;