Create span as a child of a span Asp.net apm agent

Hi,

I have created spans in my ASP.net application using CaptureSpan(), but this creates spans as a child of a transaction . I was wondering, is there a way I could create spans as child of a span?

Thanks

Hi @Jyothi_Singh,

yes, you can do this.

Once you have an ISpan instance you can call the CaptureSpan() method on the ISpan instance - it's the same as on ITransaction, except it'll be a child span of the given span.

Like this:

transaction.CaptureSpan("span1", "sampleSpan", (firstSpan) =>
{
	firstSpan.CaptureSpan("span2", "sample", (childSpan) =>
	{
		// childSpan is the child span of the 1. span. 
	});
		 
});

You can also use the StartSpan method like this: (this is also the same as StartSpan on a transaction, except this time it'll be a child span of the given span):

transaction.CaptureSpan("span1", "sampleSpan", (firstSpan) =>
{
	var childSpan = firstSpan.StartSpan("span2", "sample");
	try
	{
		// Code that you capture as span
	}
	catch (Exception e)
	{
		childSpan.CaptureException(e);
		throw;
	}
	finally
	{
		childSpan.End();
	}
});

You can also use the Agent.Tracer.CurrentSpan; to get the current span at any point in the code - this returns null if there is no currently active span.

Awesome, thank you so much for a quick reply. I am going to try this out!

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.