Skip to content

Commit

Permalink
Add threads view (#5)
Browse files Browse the repository at this point in the history
Add the `tui`'s Threads view

This new view relies on the Logstash's hot-threads API to display the busiest threads and their traces. 
In addition to that, it also added the `User-Agent` header to HTTP requests, and fixed minor UI issues.
  • Loading branch information
edmocosta authored Jun 27, 2024
1 parent 66407a3 commit 00526d6
Show file tree
Hide file tree
Showing 19 changed files with 807 additions and 40 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.4.0
- Introduced a new view `Threads`, which relies on the Logstash's hot-threads API to display the busiest threads and their traces.
- Added the `User-Agent` header to requests so the source can be identified.
- Minor UI fixes.

## 0.3.0
- Bumped a few dependencies.
- Added a command option (`diagnostic-path`) to poll the data from a Logstash diagnostic path.
Expand Down
47 changes: 46 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ documentation = "https://github.com/edmocosta/tuistash"
keywords = ["logstash", "tui", "cli", "terminal"]
categories = ["command-line-utilities"]
authors = ["Edmo Vamerlatti Costa <[email protected]>"]
version = "0.3.0"
version = "0.4.0"
edition = "2021"

[dependencies]
Expand All @@ -29,7 +29,8 @@ crossterm = { version = "0.27.0", default-features = false, features = ["event-s
num-format = { version = "0.4", default-features = false, features = ["with-num-bigint"] }
human_format = { version = "1.1" }
uuid = { version = "1.4", features = ["v4"] }
time = { version = "0.3", features = ["default", "formatting", "local-offset"] }
time = { version = "0.3", features = ["default", "formatting", "local-offset", "parsing"] }
regex = { version = "1.10.5", features = [] }

[[bin]]
name = "tuistash"
Expand Down
Binary file modified docs/img/demo.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions src/cli/api/hot_threads.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use std::collections::HashMap;

use serde::{Deserialize, Serialize};

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct NodeHotThreads {
pub hot_threads: HotThreads,
}

#[derive(Default, Debug, Clone, Serialize, Deserialize)]
#[serde(default)]
pub struct HotThreads {
pub time: String,
pub busiest_threads: u64,
#[serde(with = "threads")]
pub threads: HashMap<i64, Thread>,
}

mod threads {
use std::collections::HashMap;

use serde::de::{Deserialize, Deserializer};
use serde::ser::Serializer;

use super::Thread;

pub fn serialize<S>(map: &HashMap<i64, Thread>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_seq(map.values())
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<HashMap<i64, Thread>, D::Error>
where
D: Deserializer<'de>,
{
let vertices = Vec::<Thread>::deserialize(deserializer)?;
let mut map = HashMap::with_capacity(vertices.len());

for item in vertices {
map.insert(item.thread_id, item);
}

Ok(map)
}
}

#[derive(Default, Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(default)]
pub struct Thread {
pub name: String,
pub thread_id: i64,
pub percent_of_cpu_time: f64,
pub state: String,
pub traces: Vec<String>,
}
19 changes: 11 additions & 8 deletions src/cli/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ use std::sync::Arc;

use base64::prelude::BASE64_STANDARD;
use base64::write::EncoderWriter;
use ureq::{Agent, Response};
use ureq::{Agent, AgentBuilder, Response};

use crate::api::tls::SkipServerVerification;
use crate::errors::AnyError;

pub mod hot_threads;
pub mod node;
pub mod node_api;
pub mod stats;
Expand Down Expand Up @@ -46,21 +47,23 @@ impl<'a> Client<'a> {
password: Option<&'a str>,
skip_tls_verification: bool,
) -> Result<Self, AnyError> {
let agent: Agent = if skip_tls_verification {
let user_agent = "tuistash";

let agent_builder: AgentBuilder = if skip_tls_verification {
let tls_config = rustls::ClientConfig::builder()
.dangerous()
.with_custom_certificate_verifier(SkipServerVerification::new())
.with_no_client_auth();

ureq::AgentBuilder::new()
AgentBuilder::new()
.user_agent(user_agent)
.tls_config(Arc::new(tls_config))
.build()
} else {
ureq::AgentBuilder::new().build()
};
AgentBuilder::new().user_agent(user_agent)
}
.user_agent(format!("tuistash/{}", env!("CARGO_PKG_VERSION")).as_str());

Ok(Self {
client: agent,
client: agent_builder.build(),
config: ClientConfig {
base_url: host.to_string(),
username,
Expand Down
10 changes: 10 additions & 0 deletions src/cli/api/node_api.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::api::hot_threads::NodeHotThreads;
use crate::api::node::{NodeInfo, NodeInfoType};
use crate::api::stats::NodeStats;
use crate::api::Client;
Expand Down Expand Up @@ -44,6 +45,15 @@ impl Client<'_> {
Ok(node_stats)
}

pub fn get_hot_threads(
&self,
query: Option<&[(&str, &str)]>,
) -> Result<NodeHotThreads, AnyError> {
let response = self.request("GET", &self.node_request_path("hot_threads"), query)?;
let hot_threads: NodeHotThreads = response.into_json()?;
Ok(hot_threads)
}

fn node_info_request_path(&self, types: &[NodeInfoType]) -> String {
let filterable_types = types
.iter()
Expand Down
28 changes: 27 additions & 1 deletion src/cli/commands/tui/app.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::time::Duration;

use crate::api::hot_threads::NodeHotThreads;
use crossterm::event::{KeyCode, KeyEvent};

use crate::api::node::NodeInfo;
Expand All @@ -11,6 +12,7 @@ use crate::commands::tui::flows::state::FlowsState;
use crate::commands::tui::node::state::NodeState;
use crate::commands::tui::pipelines::state::PipelinesState;
use crate::commands::tui::shared_state::SharedState;
use crate::commands::tui::threads::state::ThreadsState;
use crate::commands::tui::widgets::TabsState;
use crate::errors::AnyError;

Expand All @@ -20,6 +22,7 @@ pub(crate) struct AppData<'a> {
data_fetcher: &'a dyn DataFetcher<'a>,
node_info: Option<NodeInfo>,
node_stats: Option<NodeStats>,
hot_threads: Option<NodeHotThreads>,
}

impl<'a> AppData<'a> {
Expand All @@ -30,6 +33,7 @@ impl<'a> AppData<'a> {
data_fetcher,
node_stats: None,
node_info: None,
hot_threads: None,
}
}

Expand Down Expand Up @@ -65,6 +69,16 @@ impl<'a> AppData<'a> {
}
}

match self.data_fetcher.fetch_hot_threads() {
Ok(hot_threads) => {
self.hot_threads = Some(hot_threads);
}
Err(e) => {
self.handle_error(&e);
return Err(e);
}
}

data_decorator::decorate(
self.node_info.as_mut().unwrap(),
self.node_stats.as_mut().unwrap(),
Expand All @@ -84,6 +98,10 @@ impl<'a> AppData<'a> {
return self.node_stats.as_ref();
}

pub(crate) fn hot_threads(&self) -> Option<&NodeHotThreads> {
return self.hot_threads.as_ref();
}

pub(crate) fn errored(&self) -> bool {
self.errored
}
Expand All @@ -102,6 +120,7 @@ pub(crate) struct App<'a> {
pub node_state: NodeState,
pub pipelines_state: PipelinesState<'a>,
pub flows_state: FlowsState,
pub threads_state: ThreadsState,
pub data: AppData<'a>,
pub host: &'a str,
pub sampling_interval: Option<Duration>,
Expand All @@ -110,7 +129,8 @@ pub(crate) struct App<'a> {
impl<'a> App<'a> {
pub const TAB_PIPELINES: usize = 0;
pub const TAB_FLOWS: usize = 1;
pub const TAB_NODE: usize = 2;
pub const TAB_THREADS: usize = 2;
pub const TAB_NODE: usize = 3;

pub fn new(
title: &'a str,
Expand All @@ -130,6 +150,7 @@ impl<'a> App<'a> {
host,
shared_state: SharedState::new(),
flows_state: FlowsState::new(),
threads_state: ThreadsState::new(),
}
}

Expand Down Expand Up @@ -200,6 +221,9 @@ impl<'a> App<'a> {
"n" => {
self.select_tab(Self::TAB_NODE);
}
"t" => {
self.select_tab(Self::TAB_THREADS);
}
_ => {}
}
}
Expand All @@ -225,6 +249,7 @@ impl<'a> App<'a> {
&mut self.pipelines_state,
&mut self.flows_state,
&mut self.node_state,
&mut self.threads_state,
];

for listener in listeners {
Expand All @@ -249,6 +274,7 @@ impl<'a> App<'a> {
Self::TAB_PIPELINES => Some(&mut self.pipelines_state),
Self::TAB_NODE => Some(&mut self.node_state),
Self::TAB_FLOWS => Some(&mut self.flows_state),
Self::TAB_THREADS => Some(&mut self.threads_state),
_ => None,
};

Expand Down
Loading

0 comments on commit 00526d6

Please sign in to comment.