Skip to content

Create an activity more often than before #9361

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

Merged
merged 6 commits into from
Apr 18, 2019
Merged

Conversation

davidfowl
Copy link
Member

@davidfowl davidfowl commented Apr 14, 2019

  • Create an Activity if there's a diagnostic listener attached with the activity name or if logging is enabled.
  • Add the activity id to the logging scope (and call the field ActivityId)

cc @lmolkova @SergeyKanzhelev

#5926
#5918

@@ -51,13 +51,13 @@ public void BeginRequest(HttpContext httpContext, ref HostingApplication.Context
var diagnosticListenerEnabled = _diagnosticListener.IsEnabled();
var loggingEnabled = _logger.IsEnabled(LogLevel.Critical);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is _logger.IsEnabled(LogLevel.Critical) the right level to create an activity? (As generally it will always be on at critical or above)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In theory, there should always be an activity (I need to run some perf tests to see how this affects the default scenarios). This basically replaces the current request id. The current logic assumes if you're going to log a a scope, then the activity should be created.

We added this but there's kinda a chicken and egg problem as we need to make the activity to be a part of the scope before knowing if the scope if there.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There may also need to be another explicit way to turn it on or off outside logging but that's sorta why this is a draft.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resetable Activity on the connection using something like: #6895

Currently problematic as Activity doesn't support being cleared.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The server would need to own activity creation in that case. I'm not sure we'll come up with anything reasonable in the 3.0 time frame for #6895. I think coupling it to logging is the best bet mostly because the data is for the logging system and that scenario has a bunch of allocations already (the scope, the messages being logged etc).

Maybe the log level needs to be adjusted to something like information but the category is this internal category (which might be a bit weird).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dotnet/extensions#1422

Then something like?

var scope = logger.BeginScope(
    (state) => new HostingLogScope(state.httpContext, state.activityId), 
    (httpContext, activityId));

if (!scope.IsNullScope() || (diagnosticListenerEnabled && _diagnosticListener.IsEnabled(ActivityName, httpContext)))
{
    context.Activity = StartActivity(httpContext);
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only helps when scopes are off. We could just add a DisableActivityCreation to the WebHostOptions if we wanted to remove the allocation in benchmarks.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anyway to do an inversion? Say for example AppInisights is set to sample 5% of activities rather than 100% is there a way to ask it "will you consume this activity" and create based on that; rather than create it 100% of the time and throw it away unobserved 95% of the time?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes there's a plan to support sampling but this feature needs to work with logging when app insights isn't on so that it's possible to correlate logs without too much setup.

See #9340 for the sampling PR. The activity still needs to be created though (so there's a chicken and egg problem)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benaadams we will need an Activity not only to track something, but also to propagate the decision that we decided NOT to track something. So head-based sampling would work across the components.

- Create an Activity if there's a diagnostic listener attached with the activity name or if logging is enabled.
- Add the activity id to the logging scope (and call the field ActivityId)
@davidfowl davidfowl force-pushed the davidfowl/activities branch from fc46424 to bb1968a Compare April 17, 2019 04:58
@davidfowl davidfowl requested a review from pranavkm April 17, 2019 06:16
@davidfowl davidfowl marked this pull request as ready for review April 17, 2019 08:51
@@ -222,20 +223,18 @@ public async Task ProblemDetails_IsSerialized()
// Arrange
using (new ActivityReplacer())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could get rid of this type

@analogrelay analogrelay added this to the 3.0.0-preview5 milestone Apr 17, 2019
@Tratcher Tratcher removed their request for review April 17, 2019 15:07
// Scope may be relevant for a different level of logging, so we always create it
// see: https://github.com/aspnet/Hosting/pull/944
// Scope can be null if logging is not on.
context.Scope = _logger.RequestScope(httpContext, correlationId);
context.Scope = _logger.RequestScope(httpContext, context.Activity.Id);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SergeyKanzhelev @lmolkova should we split this out into span id and trace id in the logs?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidfowl davidfowl merged commit 32c46c7 into master Apr 18, 2019
@davidfowl davidfowl deleted the davidfowl/activities branch April 19, 2019 00:46
@amcasey amcasey added the area-hosting Includes Hosting label Jun 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-hosting Includes Hosting
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants