From f668a4ac0c7ef324febe0c041ad030b904776bb6 Mon Sep 17 00:00:00 2001 From: Alex Orlenko Date: Thu, 26 Sep 2024 13:26:04 +0100 Subject: [PATCH] Add `current_span:update_name` method to Lua --- casper-server/src/lua/trace.rs | 35 ++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/casper-server/src/lua/trace.rs b/casper-server/src/lua/trace.rs index bf7a634..d324f65 100644 --- a/casper-server/src/lua/trace.rs +++ b/casper-server/src/lua/trace.rs @@ -46,6 +46,14 @@ impl UserData for LuaCurrentSpan { Ok(()) }) }); + + // Update the span name. + methods.add_method_mut("update_name", |_, _, name: String| { + get_active_span(|span| { + span.update_name(name); + Ok(()) + }) + }); } } @@ -208,6 +216,33 @@ mod tests { Ok(()) } + #[test] + #[serial] + fn test_current_span_update_name() -> Result<()> { + let lua = Lua::new(); + let trace = super::create_module(&lua)?; + + let (tracer, provider, exporter) = build_test_tracer(); + global::set_tracer_provider(provider); + + tracer.in_span("root", |_cx| { + lua.load(chunk! { + local span = $trace.current_span + span:update_name("new_root") + }) + .exec() + .unwrap(); + }); + + global::shutdown_tracer_provider(); // flush all spans + let spans = exporter.0.lock().unwrap(); + assert_eq!(spans.len(), 1); + let span = &spans[0]; + assert_eq!(span.name, "new_root"); + + Ok(()) + } + #[test] #[serial] fn test_new_span() -> Result<()> {