tokio-console 0.1.14

The Tokio console: a debugger for async Rust.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
use crate::intern::{self, InternedStr};
use crate::state::{
    format_location,
    store::{self, Id, SpanId, Store},
    Attribute, Field, Metadata, Visibility,
};
use crate::view;
use console_api as proto;
use ratatui::{style::Color, text::Span};
use std::{
    collections::HashMap,
    convert::{TryFrom, TryInto},
    rc::Rc,
    time::{Duration, SystemTime},
};

#[derive(Default, Debug)]
pub(crate) struct ResourcesState {
    resources: Store<Resource>,
    dropped_events: u64,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub(crate) enum TypeVisibility {
    Public,
    Internal,
}

#[derive(Debug, Copy, Clone)]
#[repr(usize)]
pub(crate) enum SortBy {
    Id = 0,
    ParentId = 1,
    Kind = 2,
    Total = 3,
    Target = 4,
    ConcreteType = 5,
    Visibility = 6,
    Location = 7,
    Attributes = 8,
}

#[derive(Debug)]
pub(crate) struct Resource {
    /// The resource's pretty (console-generated, sequential) ID.
    ///
    /// This is NOT the `tracing::span::Id` for the resource's `tracing` span on the
    /// remote.
    id: Id<Resource>,
    /// The `tracing::span::Id` on the remote process for this resource's span.
    ///
    /// This is used when requesting a resource details stream.
    span_id: SpanId,
    id_str: InternedStr,
    parent: InternedStr,
    parent_id: InternedStr,
    meta_id: u64,
    kind: InternedStr,
    stats: ResourceStats,
    target: InternedStr,
    concrete_type: InternedStr,
    location: String,
    visibility: TypeVisibility,
}

pub(crate) type ResourceRef = store::Ref<Resource>;

#[derive(Debug)]
struct ResourceStats {
    created_at: SystemTime,
    dropped_at: Option<SystemTime>,
    total: Option<Duration>,
    formatted_attributes: Vec<Vec<Span<'static>>>,
}

impl Default for SortBy {
    fn default() -> Self {
        Self::Id
    }
}

impl SortBy {
    pub fn sort(&self, now: SystemTime, resources: &mut [ResourceRef]) {
        match self {
            Self::Id => {
                resources.sort_unstable_by_key(|resource| resource.upgrade().map(|r| r.borrow().id))
            }
            Self::ParentId => resources.sort_unstable_by_key(|resource| {
                resource.upgrade().map(|r| r.borrow().parent_id.clone())
            }),
            Self::Kind => resources.sort_unstable_by_key(|resource| {
                resource.upgrade().map(|r| r.borrow().kind.clone())
            }),
            Self::Total => resources
                .sort_unstable_by_key(|resource| resource.upgrade().map(|r| r.borrow().total(now))),
            Self::Target => resources.sort_unstable_by_key(|resource| {
                resource.upgrade().map(|r| r.borrow().target.clone())
            }),
            Self::ConcreteType => resources.sort_unstable_by_key(|resource| {
                resource.upgrade().map(|r| r.borrow().concrete_type.clone())
            }),
            Self::Visibility => resources
                .sort_unstable_by_key(|resource| resource.upgrade().map(|r| r.borrow().visibility)),
            Self::Location => resources.sort_unstable_by_key(|resource| {
                resource.upgrade().map(|r| r.borrow().location.clone())
            }),
            Self::Attributes => resources.sort_unstable_by_key(|resource| {
                resource.upgrade().and_then(|r| {
                    // FIXME - we are taking only the key of the first attribute as sorting key here.
                    // Instead, attributes should probably be parsed and sorted according to their actual values.
                    //
                    // See https://github.com/tokio-rs/console/issues/496
                    r.borrow()
                        .formatted_attributes()
                        .first()
                        .and_then(|a| a.first())
                        .map(|key| key.content.clone())
                })
            }),
        }
    }
}

impl TryFrom<usize> for SortBy {
    type Error = ();
    fn try_from(idx: usize) -> Result<Self, Self::Error> {
        match idx {
            idx if idx == Self::Id as usize => Ok(Self::Id),
            idx if idx == Self::ParentId as usize => Ok(Self::ParentId),
            idx if idx == Self::Kind as usize => Ok(Self::Kind),
            idx if idx == Self::Total as usize => Ok(Self::Total),
            idx if idx == Self::Target as usize => Ok(Self::Target),
            idx if idx == Self::ConcreteType as usize => Ok(Self::ConcreteType),
            idx if idx == Self::Visibility as usize => Ok(Self::Visibility),
            idx if idx == Self::Location as usize => Ok(Self::Location),
            idx if idx == Self::Attributes as usize => Ok(Self::Attributes),
            _ => Err(()),
        }
    }
}

impl view::SortBy for SortBy {
    fn as_column(&self) -> usize {
        *self as usize
    }
}

impl ResourcesState {
    pub(crate) fn take_new_resources(&mut self) -> impl Iterator<Item = ResourceRef> + '_ {
        self.resources.take_new_items()
    }

    pub(crate) fn ids_mut(&mut self) -> &mut store::Ids<Resource> {
        self.resources.ids_mut()
    }

    pub(crate) fn update_resources(
        &mut self,
        styles: &view::Styles,
        strings: &mut intern::Strings,
        metas: &HashMap<u64, Metadata>,
        update: proto::resources::ResourceUpdate,
        visibility: Visibility,
    ) {
        let parents: HashMap<Id<Resource>, ResourceRef> = update
            .new_resources
            .iter()
            .filter_map(|resource| {
                let parent_id = resource.parent_resource_id?.id;
                let parent = self.resources.get_by_span(parent_id)?;
                Some((parent.borrow().id, Rc::downgrade(parent)))
            })
            .collect();

        let mut stats_update = update.stats_update;
        self.resources
            .insert_with(visibility, update.new_resources, |ids, resource| {
                let span_id = match resource.id.as_ref() {
                    Some(id) => id.id,
                    None => {
                        tracing::warn!(?resource, "skipping resource with no id");
                        return None;
                    }
                };

                let meta_id = match resource.metadata.as_ref() {
                    Some(id) => id.id,
                    None => {
                        tracing::warn!(?resource, "resource has no metadata id skipping");
                        return None;
                    }
                };
                let meta = match metas.get(&meta_id) {
                    Some(meta) => meta,
                    None => {
                        tracing::warn!(?resource, meta_id, "no metadata for resource, skipping");
                        return None;
                    }
                };
                let kind = match kind_from_proto(resource.kind?, strings) {
                    Ok(kind) => kind,
                    Err(err) => {
                        tracing::warn!(%err, "resource kind cannot be parsed");
                        return None;
                    }
                };

                let stats = ResourceStats::from_proto(
                    stats_update.remove(&span_id)?,
                    meta,
                    styles,
                    strings,
                );

                let id = ids.id_for(span_id);
                let parent_id = resource.parent_resource_id.map(|id| ids.id_for(id.id));

                let parent = strings.string(match parent_id {
                    Some(id) => parents
                        .get(&id)
                        .and_then(|r| r.upgrade())
                        .map(|r| {
                            let r = r.borrow();
                            format!("{} ({}::{})", r.id(), r.target(), r.concrete_type())
                        })
                        .unwrap_or_else(|| id.to_string()),
                    None => "n/a".to_string(),
                });

                let parent_id = strings.string(
                    parent_id
                        .as_ref()
                        .map(Id::<Resource>::to_string)
                        .unwrap_or_else(|| "n/a".to_string()),
                );

                let location = format_location(resource.location);
                let visibility = if resource.is_internal {
                    TypeVisibility::Internal
                } else {
                    TypeVisibility::Public
                };

                let resource = Resource {
                    id,
                    span_id,
                    id_str: strings.string(id.to_string()),
                    parent,
                    parent_id,
                    kind,
                    stats,
                    target: meta.target.clone(),
                    concrete_type: strings.string(resource.concrete_type),
                    meta_id,
                    location,
                    visibility,
                };
                Some((id, resource))
            });

        self.dropped_events += update.dropped_events;

        for (stats, mut resource) in self.resources.updated(stats_update) {
            if let Some(meta) = metas.get(&resource.meta_id) {
                tracing::trace!(?resource, ?stats, "processing stats update for");
                resource.stats = ResourceStats::from_proto(stats, meta, styles, strings);
            }
        }
    }

    pub(crate) fn retain_active(&mut self, now: SystemTime, retain_for: Duration) {
        self.resources.retain(|_, resource| {
            let resource = resource.borrow();

            resource
                .stats
                .dropped_at
                .map(|d| {
                    let dropped_for = now.duration_since(d).unwrap_or_default();
                    retain_for > dropped_for
                })
                .unwrap_or(true)
        })
    }

    pub(crate) fn dropped_events(&self) -> u64 {
        self.dropped_events
    }
}

impl Resource {
    pub(crate) fn id(&self) -> Id<Resource> {
        self.id
    }

    pub(crate) fn span_id(&self) -> u64 {
        self.span_id
    }

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

    pub(crate) fn parent(&self) -> &str {
        &self.parent
    }

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

    pub(crate) fn type_visibility(&self) -> TypeVisibility {
        self.visibility
    }

    pub(crate) fn target(&self) -> &str {
        &self.target
    }

    pub(crate) fn concrete_type(&self) -> &str {
        &self.concrete_type
    }

    pub(crate) fn kind(&self) -> &str {
        &self.kind
    }

    pub(crate) fn formatted_attributes(&self) -> &[Vec<Span<'static>>] {
        &self.stats.formatted_attributes
    }

    pub(crate) fn total(&self, since: SystemTime) -> Duration {
        self.stats.total.unwrap_or_else(|| {
            since
                .duration_since(self.stats.created_at)
                .unwrap_or_default()
        })
    }

    pub(crate) fn dropped(&self) -> bool {
        self.stats.total.is_some()
    }

    pub(crate) fn location(&self) -> &str {
        &self.location
    }
}

impl ResourceStats {
    fn from_proto(
        pb: proto::resources::Stats,
        meta: &Metadata,
        styles: &view::Styles,
        strings: &mut intern::Strings,
    ) -> Self {
        let mut pb = pb;
        let mut attributes = pb
            .attributes
            .drain(..)
            .filter_map(|pb| {
                let field = pb.field?;
                let field = Field::from_proto(field, meta, strings)?;
                Some(Attribute {
                    field,
                    unit: pb.unit,
                })
            })
            .collect::<Vec<_>>();

        let formatted_attributes = Attribute::make_formatted(styles, &mut attributes);
        let created_at = pb
            .created_at
            .expect("resource span was never created")
            .try_into()
            .unwrap();
        let dropped_at: Option<SystemTime> = pb.dropped_at.map(|v| v.try_into().unwrap());
        let total = dropped_at.map(|d| d.duration_since(created_at).unwrap_or_default());

        Self {
            created_at,
            dropped_at,
            total,
            formatted_attributes,
        }
    }
}

fn kind_from_proto(
    pb: proto::resources::resource::Kind,
    strings: &mut intern::Strings,
) -> Result<InternedStr, String> {
    use proto::resources::resource::kind::Kind::Known as PbKnown;
    use proto::resources::resource::kind::Kind::Other as PBOther;
    use proto::resources::resource::kind::Known::Timer as PbTimer;

    match pb.kind.expect("a resource should have a kind field") {
        PbKnown(known) if known == (PbTimer as i32) => Ok(strings.string("Timer".to_string())),
        PbKnown(known) => Err(format!("failed to parse known kind from {}", known)),
        PBOther(other) => Ok(strings.string(other)),
    }
}

impl TypeVisibility {
    pub(crate) fn render(self, styles: &crate::view::Styles) -> Span<'static> {
        const INT_UTF8: &str = "\u{1F512}";
        const PUB_UTF8: &str = "\u{2705}";
        match self {
            Self::Internal => Span::styled(styles.if_utf8(INT_UTF8, "INT"), styles.fg(Color::Red)),
            Self::Public => Span::styled(styles.if_utf8(PUB_UTF8, "PUB"), styles.fg(Color::Green)),
        }
    }
}