You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the AspNetCore instrumentation and the OpenTracing shim results in multiple traces created per HTTP request when using IScope instantiated through the ITracer.BuildSpan("Span name").StartActive() method with using blocks or statements since the trace context is not properly preserved.
What is the expected behavior?
When making a HTTP request a single trace must be created and spans must follow a hierarchy matching the created IScopes.
What is the actual behavior?
The initial scope on the HTTP request matches to the HttpRequestIn span and the current activity
Calling ITracer.BuildSpan("Span name").StartActive() from that initial scope does not create a new span an any tags created get attached to the HttpRequestIn span and data is overridden
When the scope of the using finishes, the current activity is set to null and a further call to ITracer.BuildSpan("Span name").StartActive() creates a new trace
Nested spans created using ITracer.BuildSpan("Span name").StartActive() do get correctly attached unless the parent is the HttpRequestIn
Reproduce
Create a new dotnet project using the command dotnet new webapi and install the above mentioned dependencies copying this code block to the *.csproj file created:
Now go to the WeatherForecastController.cs file in the Controllers folder and replace the class declaration for the following code:
[ApiController][Route("[controller]")]publicclassWeatherForecastController:ControllerBase{privatestaticreadonlystring[]Summaries=new[]{"Freezing","Bracing","Chilly","Cool","Mild","Warm","Balmy","Hot","Sweltering","Scorching"};privatereadonlyILogger<WeatherForecastController>_logger;privatereadonlyITracer_tracer;publicWeatherForecastController(ILogger<WeatherForecastController>logger,ITracertracer){_logger=logger;_tracer=tracer;}[HttpGet]publicIEnumerable<WeatherForecast>Get(){varrng=newRandom();returnEnumerable.Range(1,5).Select(index =>GetRandomForecast(rng,index)).ToArray();}privateWeatherForecastGetRandomForecast(Randomrng,intindex){usingvarmethodScope=_tracer.BuildSpan($"Random forecast for index={index}").StartActive();methodScope.Span.SetTag("Started",true);using(vardatabaseScope=_tracer.BuildSpan($"Calling some external resource").StartActive()){databaseScope.Span.SetTag("Database","Demo");}WeatherForecastresult;using(varcomputationScope=_tracer.BuildSpan($"Computing forecast for index={index}").StartActive()){vardate=DateTime.Now.AddDays(index);vartemperatureC=rng.Next(-20,55);varsummary=Summaries[rng.Next(Summaries.Length)];computationScope.Span.SetTag("Date",date.ToShortDateString());computationScope.Span.SetTag("Temperature",temperatureC);computationScope.Span.SetTag("Summary",summary);result=newWeatherForecast{Date=date,TemperatureC=temperatureC,Summary=summary};}methodScope.Span.SetTag("Completed",true);returnresult;}}
Finally go to your appsettings.json file and set the values for the Endpoint and Headers key with your preferred trace visualization tool. I use Honeycomb's free tier for simplicity with these values:
Run the application, open a browser and call the Get method in the WeatherForecastController class by following the URL https://localhost:5001/WeatherForecast. Observe the traces created in your Honeycomb dataset (or any other trace visualization tool).
Additional Context
While debugging I took some notes:
the fact that the first call to the ITracer.BuildSpan("Span name").StartActive() gets attached to the current activity is linked to this code here
using ITracer.BuildSpan("Span name").AddReference(References.ChildOf, _tracer.ActiveSpan.Context).StartActive() the first time does create a child span of the HttpRequestIn span, but if the using scope finishes the activity is set to null too and the context does not propagate to "sibling using blocks"
the AspNetCore creates a sibling activity based on this, can this be the reason the correct context is not restored?
The text was updated successfully, but these errors were encountered:
This issue was marked stale due to lack of activity and will be closed in 7 days. Commenting will instruct the bot to automatically remove the label. This bot runs once per day.
Bug Report
List of NuGet packages and
version that you are using:
Runtime version:
netcoreapp3.1
.Symptom
Using the AspNetCore instrumentation and the OpenTracing shim results in multiple traces created per HTTP request when using
IScope
instantiated through theITracer.BuildSpan("Span name").StartActive()
method withusing
blocks or statements since the trace context is not properly preserved.What is the expected behavior?
When making a HTTP request a single trace must be created and spans must follow a hierarchy matching the created
IScopes
.What is the actual behavior?
ITracer.BuildSpan("Span name").StartActive()
from that initial scope does not create a new span an any tags created get attached to the HttpRequestIn span and data is overriddenusing
finishes, the current activity is set to null and a further call toITracer.BuildSpan("Span name").StartActive()
creates a new traceITracer.BuildSpan("Span name").StartActive()
do get correctly attached unless the parent is the HttpRequestInReproduce
Create a new dotnet project using the command
dotnet new webapi
and install the above mentioned dependencies copying this code block to the*.csproj
file created:Then go to the
Startup.cs
file and add the following code to theConfigureServices
method:Now go to the
WeatherForecastController.cs
file in theControllers
folder and replace the class declaration for the following code:Finally go to your
appsettings.json
file and set the values for theEndpoint
andHeaders
key with your preferred trace visualization tool. I use Honeycomb's free tier for simplicity with these values:Run the application, open a browser and call the
Get
method in theWeatherForecastController
class by following the URL https://localhost:5001/WeatherForecast. Observe the traces created in your Honeycomb dataset (or any other trace visualization tool).Additional Context
While debugging I took some notes:
ITracer.BuildSpan("Span name").StartActive()
gets attached to the current activity is linked to this code hereITracer.BuildSpan("Span name").AddReference(References.ChildOf, _tracer.ActiveSpan.Context).StartActive()
the first time does create a child span of the HttpRequestIn span, but if theusing
scope finishes the activity is set to null too and the context does not propagate to "siblingusing
blocks"The text was updated successfully, but these errors were encountered: