Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question: ActivitySource behavior with multiple subscribed ActivityListeners #41306

Closed
alanwest opened this issue Aug 25, 2020 · 4 comments
Closed
Labels
area-System.Diagnostics.Tracing question Answer questions and provide assistance, not an issue with source code or documentation.

Comments

@alanwest
Copy link
Contributor

alanwest commented Aug 25, 2020

Description

When an Activity is started with the new ActivitySource API if any subscribed ActivityListener responds with anything higher than ActivityDataRequest.None then all subscribed ActivityListener.ActivityStarted/Stopped events are fired.

For example:

public static void Main(string[] args)
        {
            var listenerA = new ActivityListener()
            {
                ActivityStarted = (a) => System.Console.WriteLine("listenerA.ActivityStarted"),
                ActivityStopped = (a) => System.Console.WriteLine("listenerA.ActivityStopped"),
                ShouldListenTo = _ => true,
                GetRequestedDataUsingParentId = (ref ActivityCreationOptions<string> options) => ActivityDataRequest.None,
                GetRequestedDataUsingContext = (ref ActivityCreationOptions<ActivityContext> options) => ActivityDataRequest.None,
            };

            var listenerB = new ActivityListener()
            {
                ActivityStarted = (a) => System.Console.WriteLine("listenerB.ActivityStarted"),
                ActivityStopped = (a) => System.Console.WriteLine("listenerB.ActivityStopped"),
                ShouldListenTo = _ => true,
                GetRequestedDataUsingParentId = (ref ActivityCreationOptions<string> options) => ActivityDataRequest.AllData,
                GetRequestedDataUsingContext = (ref ActivityCreationOptions<ActivityContext> options) => ActivityDataRequest.AllData,
            };

            ActivitySource.AddActivityListener(listenerA);
            ActivitySource.AddActivityListener(listenerB);

            var activitySource = new ActivitySource("MyActivitySource");

            var activity = activitySource.StartActivity("MyActivity");
            activity?.Stop();
        }

This code produces this output:

listenerA.ActivityStarted
listenerB.ActivityStarted
listenerA.ActivityStopped
listenerB.ActivityStopped

Even though listenerA responds ActivityDataRequest.None it's ActivityStarted/Stopped events are fired. Is this the correct behavior? It does not seem intuitive.

@cijothomas

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Diagnostics.Tracing untriaged New issue has not been triaged by the area owner labels Aug 25, 2020
@ghost
Copy link

ghost commented Aug 25, 2020

Tagging subscribers to this area: @tarekgh, @tommcdon, @pjanotti
See info in area-owners.md if you want to be subscribed.

@tarekgh
Copy link
Member

tarekgh commented Aug 25, 2020

This is by design, the responding value of ActivityDataRequest is not intended for getting the notification or not. it is intended to know if need to create the Activity object or not. If any listener responded with any value different than ActivityDataRequest.None then we'll create the Activity object and all listeners will get the notifications. The listener is free to set ActivityStarted/ActivityStopped to null if needed to stop getting any notifications.

@tarekgh tarekgh closed this as completed Aug 25, 2020
@tarekgh
Copy link
Member

tarekgh commented Aug 25, 2020

CC @noahfalk

@tarekgh tarekgh added question Answer questions and provide assistance, not an issue with source code or documentation. and removed untriaged New issue has not been triaged by the area owner labels Aug 25, 2020
@cijothomas
Copy link
Contributor

related: #31862 (comment)

@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Diagnostics.Tracing question Answer questions and provide assistance, not an issue with source code or documentation.
Projects
None yet
Development

No branches or pull requests

4 participants