Skip to content

Commit 4ec26a8

Browse files
authored
fix(console): don't make details requests with rewritten IDs (#251)
## Motivation PR #244 moved the rewriting of `tracing` span IDs to sequential low-number IDs from the `console-subscriber` crate to the console CLI. However, this introduced a bug with task details, because that PR didn't change the part of the console that makes `watch_details` RPCs to use the `tracing` span ID recieved from the remote --- it continued to use the `Task` and `Resource` structs' `id` fields. These are now different from the `tracing` ID recieved from the remote, because they're rewritten in the console CLI. This means that `watch_details` RPCs would generally recieve an error, or (in the off-chance that a rewritten ID collides with a low-numbered `tracing` ID) the details for anoter task or resource. ## Solution This branch fixes the bug by changing the `Task` and `Resource` structs to store both the span ID received from the remote _and_ the rewritten pretty ID. This way, when we make `watch_details` RPC calls, we do it with the span ID we received from the remote process. I also changed some naming to make the distinction between rewritten IDs and span IDs clearer.
1 parent a931b7e commit 4ec26a8

File tree

5 files changed

+68
-40
lines changed

5 files changed

+68
-40
lines changed

console/src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ async fn main() -> color_eyre::Result<()> {
8181
let _ = update_tx.send(update_kind);
8282
match update_kind {
8383
UpdateKind::SelectTask(task_id) => {
84+
tracing::info!(task_id, "starting details watch");
8485
match conn.watch_details(task_id).await {
8586
Ok(stream) => {
8687
tokio::spawn(watch_details_stream(task_id, stream, update_rx.clone(), details_tx.clone()));

console/src/state/async_ops.rs

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub(crate) enum SortBy {
3434

3535
#[derive(Debug)]
3636
pub(crate) struct AsyncOp {
37-
id: u64,
37+
num: u64,
3838
parent_id: InternedStr,
3939
resource_id: u64,
4040
meta_id: u64,
@@ -69,7 +69,7 @@ impl Default for SortBy {
6969
impl SortBy {
7070
pub fn sort(&self, now: SystemTime, ops: &mut Vec<Weak<RefCell<AsyncOp>>>) {
7171
match self {
72-
Self::Aid => ops.sort_unstable_by_key(|ao| ao.upgrade().map(|a| a.borrow().id)),
72+
Self::Aid => ops.sort_unstable_by_key(|ao| ao.upgrade().map(|a| a.borrow().num)),
7373
Self::Task => ops.sort_unstable_by_key(|ao| ao.upgrade().map(|a| a.borrow().task_id())),
7474
Self::Source => {
7575
ops.sort_unstable_by_key(|ao| ao.upgrade().map(|a| a.borrow().source.clone()))
@@ -154,10 +154,11 @@ impl AsyncOpsState {
154154
}
155155
};
156156

157-
let id = async_op.id?.id;
158-
let stats = AsyncOpStats::from_proto(stats_update.remove(&id)?, meta, styles, strings);
157+
let span_id = async_op.id?.id;
158+
let stats =
159+
AsyncOpStats::from_proto(stats_update.remove(&span_id)?, meta, styles, strings);
159160

160-
let id = self.ids.id_for(id);
161+
let num = self.ids.id_for(span_id);
161162
let resource_id = resource_ids.id_for(async_op.resource_id?.id);
162163
let parent_id = match async_op.parent_async_op_id {
163164
Some(id) => strings.string(format!("{}", self.ids.id_for(id.id))),
@@ -167,7 +168,7 @@ impl AsyncOpsState {
167168
let source = strings.string(async_op.source);
168169

169170
let async_op = AsyncOp {
170-
id,
171+
num,
171172
parent_id,
172173
resource_id,
173174
meta_id,
@@ -176,14 +177,14 @@ impl AsyncOpsState {
176177
};
177178
let async_op = Rc::new(RefCell::new(async_op));
178179
new_list.push(Rc::downgrade(&async_op));
179-
Some((id, async_op))
180+
Some((num, async_op))
180181
});
181182

182183
self.async_ops.extend(new_async_ops);
183184

184-
for (id, stats) in stats_update {
185-
let id = self.ids.id_for(id);
186-
if let Some(async_op) = self.async_ops.get_mut(&id) {
185+
for (span_id, stats) in stats_update {
186+
let num = self.ids.id_for(span_id);
187+
if let Some(async_op) = self.async_ops.get_mut(&num) {
187188
let mut async_op = async_op.borrow_mut();
188189
if let Some(meta) = metas.get(&async_op.meta_id) {
189190
async_op.stats = AsyncOpStats::from_proto(stats, meta, styles, strings);
@@ -210,7 +211,7 @@ impl AsyncOpsState {
210211

211212
impl AsyncOp {
212213
pub(crate) fn id(&self) -> u64 {
213-
self.id
214+
self.num
214215
}
215216

216217
pub(crate) fn parent_id(&self) -> &str {

console/src/state/resources.rs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,15 @@ pub(crate) enum SortBy {
3636

3737
#[derive(Debug)]
3838
pub(crate) struct Resource {
39-
id: u64,
39+
/// The resource's pretty (console-generated, sequential) ID.
40+
///
41+
/// This is NOT the `tracing::span::Id` for the resource's `tracing` span on the
42+
/// remote.
43+
num: u64,
44+
/// The `tracing::span::Id` on the remote process for this resource's span.
45+
///
46+
/// This is used when requesting a resource details stream.
47+
span_id: u64,
4048
id_str: InternedStr,
4149
parent: InternedStr,
4250
parent_id: InternedStr,
@@ -68,9 +76,8 @@ impl Default for SortBy {
6876
impl SortBy {
6977
pub fn sort(&self, now: SystemTime, resources: &mut Vec<Weak<RefCell<Resource>>>) {
7078
match self {
71-
Self::Rid => {
72-
resources.sort_unstable_by_key(|resource| resource.upgrade().map(|r| r.borrow().id))
73-
}
79+
Self::Rid => resources
80+
.sort_unstable_by_key(|resource| resource.upgrade().map(|r| r.borrow().num)),
7481
Self::Kind => resources.sort_unstable_by_key(|resource| {
7582
resource.upgrade().map(|r| r.borrow().kind.clone())
7683
}),
@@ -166,10 +173,11 @@ impl ResourcesState {
166173
}
167174
};
168175

169-
let id = resource.id?.id;
170-
let stats = ResourceStats::from_proto(stats_update.remove(&id)?, meta, styles, strings);
176+
let span_id = resource.id?.id;
177+
let stats =
178+
ResourceStats::from_proto(stats_update.remove(&span_id)?, meta, styles, strings);
171179

172-
let id = self.ids.id_for(id);
180+
let num = self.ids.id_for(span_id);
173181
let parent_id = resource.parent_resource_id.map(|id| self.ids.id_for(id.id));
174182

175183
let parent = strings.string(match parent_id {
@@ -199,8 +207,9 @@ impl ResourcesState {
199207
};
200208

201209
let resource = Resource {
202-
id,
203-
id_str: strings.string(id.to_string()),
210+
num,
211+
span_id,
212+
id_str: strings.string(num.to_string()),
204213
parent,
205214
parent_id,
206215
kind,
@@ -213,14 +222,14 @@ impl ResourcesState {
213222
};
214223
let resource = Rc::new(RefCell::new(resource));
215224
new_list.push(Rc::downgrade(&resource));
216-
Some((id, resource))
225+
Some((num, resource))
217226
});
218227

219228
self.resources.extend(new_resources);
220229

221-
for (id, stats) in stats_update {
222-
let id = self.ids.id_for(id);
223-
if let Some(resource) = self.resources.get_mut(&id) {
230+
for (span_id, stats) in stats_update {
231+
let num = self.ids.id_for(span_id);
232+
if let Some(resource) = self.resources.get_mut(&num) {
224233
let mut r = resource.borrow_mut();
225234
if let Some(meta) = metas.get(&r.meta_id) {
226235
r.stats = ResourceStats::from_proto(stats, meta, styles, strings);
@@ -247,7 +256,11 @@ impl ResourcesState {
247256

248257
impl Resource {
249258
pub(crate) fn id(&self) -> u64 {
250-
self.id
259+
self.num
260+
}
261+
262+
pub(crate) fn span_id(&self) -> u64 {
263+
self.span_id
251264
}
252265

253266
pub(crate) fn id_str(&self) -> &str {

console/src/state/tasks.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,15 @@ pub(crate) type TaskRef = Weak<RefCell<Task>>;
5656

5757
#[derive(Debug)]
5858
pub(crate) struct Task {
59-
id: u64,
59+
/// The task's pretty (console-generated, sequential) task ID.
60+
///
61+
/// This is NOT the `tracing::span::Id` for the task's tracing span on the
62+
/// remote.
63+
num: u64,
64+
/// The `tracing::span::Id` on the remote process for this task's span.
65+
///
66+
/// This is used when requesting a task details stream.
67+
span_id: u64,
6068
short_desc: InternedStr,
6169
formatted_fields: Vec<Vec<Span<'static>>>,
6270
stats: TaskStats,
@@ -150,22 +158,23 @@ impl TasksState {
150158
.collect::<Vec<_>>();
151159

152160
let formatted_fields = Field::make_formatted(styles, &mut fields);
153-
let id = task.id?;
161+
let span_id = task.id?.id;
154162

155-
let stats = stats_update.remove(&id.id)?.into();
163+
let stats = stats_update.remove(&span_id)?.into();
156164
let location = format_location(task.location);
157165

158166
// remap the server's ID to a pretty, sequential task ID
159-
let id = self.ids.id_for(id.id);
167+
let num = self.ids.id_for(span_id);
160168

161169
let short_desc = strings.string(match name.as_ref() {
162-
Some(name) => format!("{} ({})", id, name),
163-
None => format!("{}", id),
170+
Some(name) => format!("{} ({})", num, name),
171+
None => format!("{}", num),
164172
});
165173

166174
let mut task = Task {
167175
name,
168-
id,
176+
num,
177+
span_id,
169178
short_desc,
170179
formatted_fields,
171180
stats,
@@ -176,12 +185,12 @@ impl TasksState {
176185
task.lint(linters);
177186
let task = Rc::new(RefCell::new(task));
178187
new_list.push(Rc::downgrade(&task));
179-
Some((id, task))
188+
Some((num, task))
180189
});
181190
self.tasks.extend(new_tasks);
182-
for (id, stats) in stats_update {
183-
let id = self.ids.id_for(id);
184-
if let Some(task) = self.tasks.get_mut(&id) {
191+
for (span_id, stats) in stats_update {
192+
let num = self.ids.id_for(span_id);
193+
if let Some(task) = self.tasks.get_mut(&num) {
185194
let mut task = task.borrow_mut();
186195
tracing::trace!(?task, "processing stats update for");
187196
task.stats = stats.into();
@@ -225,7 +234,11 @@ impl Details {
225234

226235
impl Task {
227236
pub(crate) fn id(&self) -> u64 {
228-
self.id
237+
self.num
238+
}
239+
240+
pub(crate) fn span_id(&self) -> u64 {
241+
self.span_id
229242
}
230243

231244
pub(crate) fn target(&self) -> &str {
@@ -407,7 +420,7 @@ impl Default for SortBy {
407420
impl SortBy {
408421
pub fn sort(&self, now: SystemTime, tasks: &mut Vec<Weak<RefCell<Task>>>) {
409422
match self {
410-
Self::Tid => tasks.sort_unstable_by_key(|task| task.upgrade().map(|t| t.borrow().id)),
423+
Self::Tid => tasks.sort_unstable_by_key(|task| task.upgrade().map(|t| t.borrow().num)),
411424
Self::Name => {
412425
tasks.sort_unstable_by_key(|task| task.upgrade().map(|t| t.borrow().name.clone()))
413426
}

console/src/view/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ impl View {
103103
match event {
104104
key!(Enter) => {
105105
if let Some(task) = self.tasks_list.selected_item().upgrade() {
106-
update_kind = UpdateKind::SelectTask(task.borrow().id());
106+
update_kind = UpdateKind::SelectTask(task.borrow().span_id());
107107
self.state = TaskInstance(self::task::TaskView::new(
108108
task,
109109
state.task_details_ref(),
@@ -123,7 +123,7 @@ impl View {
123123
match event {
124124
key!(Enter) => {
125125
if let Some(res) = self.resources_list.selected_item().upgrade() {
126-
update_kind = UpdateKind::SelectResource(res.borrow().id());
126+
update_kind = UpdateKind::SelectResource(res.borrow().span_id());
127127
self.state = ResourceInstance(self::resource::ResourceView::new(res));
128128
}
129129
}

0 commit comments

Comments
 (0)