Skip to content

Conversation

@eerhardt
Copy link
Member

@eerhardt eerhardt commented Aug 3, 2021

Log events when a ServiceProvider is created:

  • How many singleton, scoped, transient services?
  • Log the list of registrations

Fix #56313

Of the list in #56313, I left out How many container instances are in the processes? because this can be determined from the number of ServiceProviderBuilt events that have occurred in the process.

Log events when a ServiceProvider is created:

* How many singleton, scoped, transient services?
* Log the list of registrations

Fix dotnet#56313
@ghost
Copy link

ghost commented Aug 3, 2021

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

Issue Details

Log events when a ServiceProvider is created:

  • How many singleton, scoped, transient services?
  • Log the list of registrations

Fix #56313

Of the list in #56313, I left out How many container instances are in the processes? because this can be determined from the number of ServiceProviderBuilt events that have occurred in the process.

Author: eerhardt
Assignees: -
Labels:

area-Extensions-DependencyInjection

Milestone: -

… becomes enabled.

This allows for listeners to attach to a process after it is running, and get the DI information.
@eerhardt
Copy link
Member Author

eerhardt commented Aug 5, 2021

@brianrob @noahfalk @josalem - can you give this a quick once over to ensure I'm using EventSource correctly?

Copy link
Contributor

@josalem josalem left a comment

Choose a reason for hiding this comment

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

A couple comments, but the EventSource end of things looks okay to me after a quick once-over.

int scopedServices = 0;
int transientServices = 0;

StringBuilder descriptorBuilder = new StringBuilder("{ \"descriptors\":[ ");
Copy link
Member

Choose a reason for hiding this comment

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

Generally speaking, the pattern is to do less string formatting and to do more writing of events with payloads that can then be processed out-of-proc. This looks like it might be a place where building up the state in-proc is preferred, or that you have to string-ify things anyway, but just wanted to call that out. Definitely good that this is under its own keyword.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, here we want to dump a bunch of data - the list of registered DI services. We are doing something similar above already:

public void CallSiteBuilt(Type serviceType, ServiceCallSite callSite)
{
if (IsEnabled(EventLevel.Verbose, EventKeywords.All))
{
string format = CallSiteJsonFormatter.Instance.Format(callSite);
int chunkCount = format.Length / MaxChunkSize + (format.Length % MaxChunkSize > 0 ? 1 : 0);
for (int i = 0; i < chunkCount; i++)
{
CallSiteBuilt(
serviceType.ToString(),
format.Substring(i * MaxChunkSize, Math.Min(MaxChunkSize, format.Length - i * MaxChunkSize)), i, chunkCount);
}
}
}

// Event source doesn't support large payloads so we chunk formatted call site tree
public static class Keywords
{
public const EventKeywords ServiceProviderInitialized = (EventKeywords)0x1;
Copy link
Member

Choose a reason for hiding this comment

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

I think you might need to specify a keyword for the other events in this provider now. If you turn on keyword 0x1, you'll just get the ServiceProviderInitialized events. If you specify keyword 0, I'm not sure you'll get the others - worth checking via ETW.

Copy link
Member Author

Choose a reason for hiding this comment

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

If you turn on keyword 0x1, you'll just get the ServiceProviderInitialized events.

That was my intention. The existing events all check for EventKeywords.All:

public void CallSiteBuilt(Type serviceType, ServiceCallSite callSite)
{
if (IsEnabled(EventLevel.Verbose, EventKeywords.All))

So if you don't specify All, you won't get those events.

If you specify keyword 0, I'm not sure you'll get the others - worth checking via ETW.

When I do

dotnet trace collect --providers Microsoft-Extensions-DependencyInjection:0:Verbose --name OrchardCoreTest

I don't get any events logged.

When I do

 dotnet trace collect --providers Microsoft-Extensions-DependencyInjection::Verbose --name OrchardCoreTest

I get them all.

And when I do

dotnet trace collect --providers Microsoft-Extensions-DependencyInjection:1:Verbose --name OrchardCoreTest

I just get the 2 new ones.

@eerhardt
Copy link
Member Author

eerhardt commented Aug 7, 2021

@davidfowl @maryamariyan - any other feedback here? I think this is ready to merge.

@eerhardt
Copy link
Member Author

eerhardt commented Aug 7, 2021

runtime-staging failures are unrelated. Merging

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Additional Diagnostics in Dependency Injection

4 participants