Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
ActivitySource issue

Background and Motivation

I cross-posted some of my motivation for this proposal here in the OpenTelemetry repository:

Motiviation: not all application context can be propagated or consumed the same way. In the world of capturing this through OpenTracing we had the ability to create custom IScopeManager and IScope objects - this allowed applications like mine (actor-based) to manage scope using actor-static variables, rather than AsyncLocal which wouldn't work in our case.

Proposed API

I'd suggest adding something akin to an IScopeManager from OpenTracing to help resolve this - call it the IActivityScope and IActivityScopeManager. I'd pair the IScopeManager with a specific ActivitySource so it doesn't change the scope of all created Activity objects globally.

// Can restore previous context according to the strategy used by the IActivityScopeManager
public interface IActivityScope : IDisposable{
   public Activity Activity { get; }

public interface IActivityScopeManager{
   public IActivityScope  Current { get; }

Problem is that each ActivitySource would need its own Activity.Current for this to work - in other words, there wouldn't necessarily be a "single source of truth" for Activity under this model. Multiple contexts running concurrently will have their own contexts. This is how the ITracer model in OpenTracing worked, fundamentally - allow multiple contexts to live in parallel but allow for them to be bridged through a custom IScopeManager when the two touch.

And this would allow someone who can't work with the default AsyncLocal context accordingly, by creating a new ActivitySource with its own IActivityScopeManager:

// only affects this ActivitySource
var actorSource = new ActivitySource("", "v1", new ActorScopeManager());
var activity = activitySource.Start("foo");

// should point to activity created earlier
var current = activitySource.Current; 

Usage Examples

Alternative Designs


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.