Skip to content

Commit

Permalink
feat: segregate traces by flowid (#760) - WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
fujiapple852 committed Nov 6, 2023
1 parent 841286f commit 2f2ee3a
Show file tree
Hide file tree
Showing 12 changed files with 509 additions and 350 deletions.
369 changes: 252 additions & 117 deletions src/backend.rs

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ pub enum GeoIpMode {
Location,
}

/// TODO How to segregate trace date.
#[derive(Debug, Copy, Clone, ValueEnum, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum SegregationMode {
/// Do not segregate tracing data.
None,
/// Segregate trace data by the TCP/UDP quad (src/dest address and port).
ByFlowId,
/// Segregate trace data by the unique signature of a tracing round.
BySignature,
}

/// How DNS queries will be resolved.
#[derive(Debug, Copy, Clone, ValueEnum, Deserialize)]
#[serde(rename_all = "kebab-case")]
Expand Down
6 changes: 5 additions & 1 deletion src/config/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::config::binding::TuiCommandItem;
use crate::config::theme::TuiThemeItem;
use crate::config::{
AddressMode, AsMode, DnsResolveMethodConfig, GeoIpMode, LogFormat, LogSpanEvents, Mode,
MultipathStrategyConfig, Protocol, TuiColor, TuiKeyBinding,
MultipathStrategyConfig, Protocol, SegregationMode, TuiColor, TuiKeyBinding,
};
use anyhow::anyhow;
use clap::builder::Styles;
Expand Down Expand Up @@ -114,6 +114,10 @@ pub struct Args {
#[arg(long)]
pub read_timeout: Option<String>,

/// TODO How to segregate trace data.
#[arg(long)]
pub segregation_mode: Option<SegregationMode>,

/// How to perform DNS queries [default: system]
#[arg(value_enum, short = 'r', long)]
pub dns_resolve_method: Option<DnsResolveMethodConfig>,
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/render/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use ratatui::Frame;
pub fn render(f: &mut Frame<'_>, rec: Rect, app: &mut TuiApp) {
if let Some(err) = app.selected_tracer_data.error() {
bsod::render(f, rec, err);
} else if app.tracer_data().hops().is_empty() {
} else if app.tracer_data().hop_count() == 0 {
splash::render(f, app, rec);
} else if app.show_chart {
chart::render(f, app, rec);
Expand Down
37 changes: 17 additions & 20 deletions src/frontend/render/chart.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,16 @@ use ratatui::Frame;

/// Render the ping history for all hops as a chart.
pub fn render(f: &mut Frame<'_>, app: &TuiApp, rect: Rect) {
let selected_hop = app.table_state.selected().map_or_else(
|| app.tracer_data().target_hop(),
|s| &app.tracer_data().hops()[s],
);
let samples = app.tui_config.max_samples / app.zoom_factor;
let series_data = app
.selected_tracer_data
.hops()
.iter()
.map(|hop| {
hop.samples()
let selected_index = app.selected_or_target_index();
let sample_count = app.tui_config.max_samples / app.zoom_factor;
let hop_range = app.tracer_data().hop_range();
let series_data = hop_range
.map(|hindex| {
let samples = app.tracer_data().samples(hindex);
samples
.iter()
.enumerate()
.take(samples)
.take(sample_count)
.map(|(i, s)| (i as f64, (s.as_secs_f64() * 1000_f64)))
.collect::<Vec<_>>()
})
Expand All @@ -43,9 +39,7 @@ pub fn render(f: &mut Frame<'_>, app: &TuiApp, rect: Rect) {
.marker(Marker::Braille)
.style(Style::default().fg({
match i {
i if i + 1 == selected_hop.ttl() as usize => {
app.tui_config.theme.hops_chart_selected_color
}
i if i == selected_index => app.tui_config.theme.hops_chart_selected_color,
_ => app.tui_config.theme.hops_chart_unselected_color,
}
}))
Expand All @@ -56,13 +50,16 @@ pub fn render(f: &mut Frame<'_>, app: &TuiApp, rect: Rect) {
.x_axis(
Axis::default()
.title("Samples")
.bounds([0_f64, samples as f64])
.bounds([0_f64, sample_count as f64])
.labels_alignment(Alignment::Right)
.labels(
["0".to_string(), format!("{samples} ({}x)", app.zoom_factor)]
.into_iter()
.map(Span::from)
.collect(),
[
"0".to_string(),
format!("{sample_count} ({}x)", app.zoom_factor),
]
.into_iter()
.map(Span::from)
.collect(),
)
.style(Style::default().fg(app.tui_config.theme.hops_chart_axis_color)),
)
Expand Down
2 changes: 1 addition & 1 deletion src/frontend/render/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ pub fn render(f: &mut Frame<'_>, app: &TuiApp, rect: Rect) {
Span::raw(render_status(app)),
Span::raw(format!(
", discovered {} hops",
app.tracer_data().hops().len()
app.tracer_data().hop_count()
)),
]),
];
Expand Down
11 changes: 5 additions & 6 deletions src/frontend/render/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ use std::time::Duration;

/// Render a histogram of ping frequencies.
pub fn render(f: &mut Frame<'_>, app: &TuiApp, rect: Rect) {
let target_hop = app.table_state.selected().map_or_else(
|| app.tracer_data().target_hop(),
|s| &app.tracer_data().hops()[s],
);
let freq_data = sample_frequency(target_hop.samples());
let hindex = app.selected_or_target_index();
let ttl = app.tracer_data().ttl(hindex);
let samples = app.tracer_data().samples(hindex);
let freq_data = sample_frequency(samples);
let freq_data_ref: Vec<_> = freq_data.iter().map(|(b, c)| (b.as_str(), *c)).collect();
let barchart = BarChart::default()
.block(
Block::default()
.title(format!("Frequency #{}", target_hop.ttl()))
.title(format!("Frequency #{ttl}"))
.style(
Style::default()
.bg(app.tui_config.theme.bg_color)
Expand Down
9 changes: 5 additions & 4 deletions src/frontend/render/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,18 @@ use ratatui::Frame;

/// Render the ping history for the final hop which is typically the target.
pub fn render(f: &mut Frame<'_>, app: &TuiApp, rect: Rect) {
let selected_hop = app.selected_hop_or_target();
let data = selected_hop
.samples()
let hindex = app.selected_or_target_index();
let samples = app.tracer_data().samples(hindex);
let ttl = app.tracer_data().ttl(hindex);
let data = samples
.iter()
.take(rect.width as usize)
.map(|s| (s.as_secs_f64() * 1000_f64) as u64)
.collect::<Vec<_>>();
let history = Sparkline::default()
.block(
Block::default()
.title(format!("Samples #{}", selected_hop.ttl()))
.title(format!("Samples #{ttl}"))
.style(
Style::default()
.bg(app.tui_config.theme.bg_color)
Expand Down
Loading

0 comments on commit 2f2ee3a

Please sign in to comment.