1use crate::{
2 metadata::StreamMetadata, payload::parse_block, properties::property_set::PropertySet,
3 scope::BorrowedScopeDesc,
4};
5use anyhow::{Context, Result};
6use micromegas_telemetry::block_wire_format::BlockPayload;
7use micromegas_tracing::prelude::*;
8use micromegas_transit::value::{Object, Value};
9
10fn on_async_event<'a, F>(obj: &Object<'a>, mut fun: F) -> Result<bool>
12where
13 F: FnMut(&'a Object<'a>, u64, u64, u32, i64) -> Result<bool>,
14{
15 let span_id = obj.get::<u64>("span_id")?;
16 let parent_span_id = obj.get::<u64>("parent_span_id")?;
17 let depth = obj.get::<u32>("depth")?;
18 let time = obj.get::<i64>("time")?;
19 let span_desc = obj.get::<&Object>("span_desc")?;
20 fun(span_desc, span_id, parent_span_id, depth, time)
21}
22
23fn on_async_named_event<'a, F>(obj: &Object<'a>, mut fun: F) -> Result<bool>
25where
26 F: FnMut(&'a Object<'a>, &'a str, u64, u64, u32, i64) -> Result<bool>,
27{
28 let span_id = obj.get::<u64>("span_id")?;
29 let parent_span_id = obj.get::<u64>("parent_span_id")?;
30 let depth = obj.get::<u32>("depth")?;
31 let time = obj.get::<i64>("time")?;
32 let span_location = obj.get::<&Object>("span_location")?;
33 let name = obj.get::<&str>("name")?;
34 fun(span_location, name, span_id, parent_span_id, depth, time)
35}
36
37fn end_properties<'a>(obj: &'a Object<'a>) -> Result<PropertySet<'a>> {
39 if obj.type_name.starts_with("TaggedEnd") {
40 Ok(obj.get::<&Object>("properties")?.into())
41 } else {
42 Ok(PropertySet::empty())
43 }
44}
45
46pub trait AsyncBlockProcessor {
48 fn on_begin_async_scope(
49 &mut self,
50 block_id: &str,
51 scope: BorrowedScopeDesc<'_>,
52 ts: i64,
53 span_id: i64,
54 parent_span_id: i64,
55 depth: u32,
56 ) -> Result<bool>;
57 #[allow(clippy::too_many_arguments)]
59 fn on_end_async_scope(
60 &mut self,
61 block_id: &str,
62 scope: BorrowedScopeDesc<'_>,
63 ts: i64,
64 span_id: i64,
65 parent_span_id: i64,
66 depth: u32,
67 properties: PropertySet<'_>,
68 ) -> Result<bool>;
69}
70
71#[span_fn]
73pub fn parse_async_block_payload<Proc: AsyncBlockProcessor>(
74 block_id: &str,
75 _object_offset: i64,
76 payload: &BlockPayload,
77 stream: &StreamMetadata,
78 processor: &mut Proc,
79) -> Result<bool> {
80 parse_block(stream, payload, |val| {
81 if let Value::Object(obj) = val {
82 match obj.type_name {
83 "BeginAsyncSpanEvent" => {
84 on_async_event(obj, |span_desc, span_id, parent_span_id, depth, ts| {
85 let name = span_desc.get::<&str>("name")?;
86 let filename = span_desc.get::<&str>("file")?;
87 let target = span_desc.get::<&str>("target")?;
88 let line = span_desc.get::<u32>("line")?;
89 let scope_desc = BorrowedScopeDesc::new(name, filename, target, line);
90 processor.on_begin_async_scope(
91 block_id,
92 scope_desc,
93 ts,
94 span_id as i64,
95 parent_span_id as i64,
96 depth,
97 )
98 })
99 .with_context(|| "reading BeginAsyncSpanEvent")
100 }
101 "EndAsyncSpanEvent" | "TaggedEndAsyncSpanEvent" => {
102 on_async_event(obj, |span_desc, span_id, parent_span_id, depth, ts| {
103 let name = span_desc.get::<&str>("name")?;
104 let filename = span_desc.get::<&str>("file")?;
105 let target = span_desc.get::<&str>("target")?;
106 let line = span_desc.get::<u32>("line")?;
107 let scope_desc = BorrowedScopeDesc::new(name, filename, target, line);
108 processor.on_end_async_scope(
109 block_id,
110 scope_desc,
111 ts,
112 span_id as i64,
113 parent_span_id as i64,
114 depth,
115 end_properties(obj)?,
116 )
117 })
118 .with_context(|| "reading EndAsyncSpanEvent")
119 }
120 "BeginAsyncNamedSpanEvent" | "BeginAsyncDynNamedSpanEvent" => on_async_named_event(
121 obj,
122 |span_location, name, span_id, parent_span_id, depth, ts| {
123 let filename = span_location.get::<&str>("file")?;
124 let target = span_location.get::<&str>("target")?;
125 let line = span_location.get::<u32>("line")?;
126 let scope_desc = BorrowedScopeDesc::new(name, filename, target, line);
127 processor.on_begin_async_scope(
128 block_id,
129 scope_desc,
130 ts,
131 span_id as i64,
132 parent_span_id as i64,
133 depth,
134 )
135 },
136 )
137 .with_context(|| "reading BeginAsyncNamedSpanEvent"),
138 "EndAsyncNamedSpanEvent"
139 | "EndAsyncDynNamedSpanEvent"
140 | "TaggedEndAsyncNamedSpanEvent" => on_async_named_event(
141 obj,
142 |span_location, name, span_id, parent_span_id, depth, ts| {
143 let filename = span_location.get::<&str>("file")?;
144 let target = span_location.get::<&str>("target")?;
145 let line = span_location.get::<u32>("line")?;
146 let scope_desc = BorrowedScopeDesc::new(name, filename, target, line);
147 processor.on_end_async_scope(
148 block_id,
149 scope_desc,
150 ts,
151 span_id as i64,
152 parent_span_id as i64,
153 depth,
154 end_properties(obj)?,
155 )
156 },
157 )
158 .with_context(|| "reading EndAsyncNamedSpanEvent"),
159 _ => Ok(true),
160 }
161 } else {
162 Ok(true)
163 }
164 })
165}