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

Switch pipeline trace to DiagnosticSource #1169

Merged
merged 47 commits into from Jul 21, 2020
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
28a8abd
Removed obsolete diagnostic interface IContainerAwareComponent.
tillig Jul 6, 2020
aac41f3
Adapted .editorconfig rules from corefx to help with naming suggestio…
tillig Jul 6, 2020
c09f4ce
Add reference to DiagnosticSource package.
tillig Jul 6, 2020
87f2148
Extensions for writing the diagnostic messages.
tillig Jul 6, 2020
8f01e36
DiagosticSource is not CLSCompliant.
tillig Jul 7, 2020
98dbdb0
Remove IResolvePipelineTracer to start tracking down places to insert…
tillig Jul 7, 2020
8ea2d94
TODO cleanup so task-level TODO items are easier to track.
tillig Jul 7, 2020
aa6764f
Replaced pipeline tracer with DiagnosticListener and subscriptions.
tillig Jul 7, 2020
aaf370c
Existing tests pass with the new trace setup.
tillig Jul 7, 2020
40efaec
this. settings like corefx. I got overruled!
tillig Jul 7, 2020
7010c9b
Reference updates to match conventions.
tillig Jul 7, 2020
b09abc6
Docs and minor refactoring.
tillig Jul 8, 2020
ac8a48d
Tests for DiagnosticSourceExtensions.
tillig Jul 9, 2020
1b3a2d9
Tests for DiagnosticTracerBase.
tillig Jul 9, 2020
6875d12
PSScriptAnalyzer null check fix.
tillig Jul 9, 2020
e0c320c
"Full operation" tracing base class, common to default and DOT genera…
tillig Jul 10, 2020
c316344
Remove the concept of IsTopLevelOperation; it is no longer required.
alistairjevans Jul 11, 2020
842b86a
Merge pull request #1166 from alistairjevans/remove-toplevel-operation
tillig Jul 12, 2020
9ad6a0d
Enable TracerMessages generation based on https://github.com/microsof…
tillig Jul 13, 2020
c650a51
Figure out resx generation later. CS2002 occurs with full info specif…
tillig Jul 13, 2020
8d73ee6
Additional labels for DOT graph display.
tillig Jul 13, 2020
2d0fcae
Naming updates to reflect no more nested ops.
tillig Jul 13, 2020
eee56ad
Graphs work!
tillig Jul 13, 2020
ca4815b
Clean up string manipulation code.
tillig Jul 13, 2020
b7fd58e
Easier testing setup - activator may be null in a mock.
tillig Jul 13, 2020
b4fcb5b
Rounded out DOT test suite.
tillig Jul 13, 2020
ad1b052
Removed middleware operations from the DOT tracer.
tillig Jul 14, 2020
c48b53d
Graph overhaul and file header update.
tillig Jul 15, 2020
f1f9115
Merge branch 'v6' into issue-788
tillig Jul 16, 2020
5dbebea
Graph now normalized based on dependency chain instances.
tillig Jul 16, 2020
bfef17a
Testing may not always be able to mock right; fix code to be more tes…
tillig Jul 16, 2020
ad0b26f
netstandard2.0 doesn't have KeyedCollection.TryGetValue.
tillig Jul 16, 2020
3dfc836
Test cleanup.
tillig Jul 16, 2020
54550a7
Reduce redundant info in the graph.
tillig Jul 16, 2020
c5efb26
Removed DOT tracer in prep for new package.
tillig Jul 17, 2020
75128b9
Community files moved to .github repo.
tillig Jul 17, 2020
2770326
Moved Diagnostics to Autofac.Diagnostics namespace.
tillig Jul 17, 2020
9777ad6
Namespace move, forgot the resources location.
tillig Jul 17, 2020
9bad3eb
Removed all this qualifiers from product source.
tillig Jul 20, 2020
09e0802
Disabled subscriptions on default diagnostic tracer to ensure traces …
tillig Jul 20, 2020
879d496
Trace event handling methods now protected instead of public.
tillig Jul 20, 2020
01cfe4e
Only IContainer has a public DiagnosticSource to avoid incorrect subs…
tillig Jul 20, 2020
10d0306
Docs about custom event handling.
tillig Jul 20, 2020
aaeb9a0
Diagnostic operation complete event args generic to handle different …
tillig Jul 20, 2020
876f157
Removed redundant checks for diagnostics being enabled.
tillig Jul 20, 2020
6795a91
Checking basic IsEnabled() on DiagnosticSource before checking on the…
tillig Jul 20, 2020
5a6b977
Optimizing hot path checks for diagnostics being enabled.
tillig Jul 21, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/Autofac/Builder/DeferredCallback.cs
Expand Up @@ -28,8 +28,8 @@ public DeferredCallback(Action<IComponentRegistryBuilder> callback)
throw new ArgumentNullException(nameof(callback));
}

this.Id = Guid.NewGuid();
this.Callback = callback;
Id = Guid.NewGuid();
Callback = callback;
}

/// <summary>
Expand All @@ -46,7 +46,7 @@ public Action<IComponentRegistryBuilder> Callback
{
get
{
return this._callback;
return _callback;
}

set
Expand All @@ -56,7 +56,7 @@ public Action<IComponentRegistryBuilder> Callback
throw new ArgumentNullException(nameof(value));
}

this._callback = value;
_callback = value;
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/Autofac/Builder/SimpleActivatorData.cs
Expand Up @@ -39,9 +39,7 @@ public class SimpleActivatorData : IConcreteActivatorData
/// <param name="activator">The activator to return.</param>
public SimpleActivatorData(IInstanceActivator activator)
{
if (activator == null) throw new ArgumentNullException(nameof(activator));

Activator = activator;
Activator = activator ?? throw new ArgumentNullException(nameof(activator));
}

/// <summary>
Expand Down
Expand Up @@ -67,7 +67,7 @@ private object GetInstance()
CheckNotDisposed();

if (_activated)
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, ProvidedInstanceActivatorResources.InstanceAlreadyActivated, this._instance.GetType()));
throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, ProvidedInstanceActivatorResources.InstanceAlreadyActivated, _instance.GetType()));

_activated = true;

Expand Down
2 changes: 1 addition & 1 deletion src/Autofac/Core/Activators/Reflection/BoundConstructor.cs
Expand Up @@ -119,7 +119,7 @@ public object Instantiate()
{
if (!CanInstantiate)
{
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, BoundConstructorResources.CannotInstantitate, this.Description));
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, BoundConstructorResources.CannotInstantitate, Description));
}

var values = new object?[_valueRetrievers!.Length];
Expand Down
Expand Up @@ -51,7 +51,7 @@ public NoConstructorsFoundException(Type offendingType, string message)
: base(message)
{
if (offendingType == null) throw new ArgumentNullException(nameof(offendingType));
this.OffendingType = offendingType;
OffendingType = offendingType;
}

/// <summary>
Expand All @@ -74,7 +74,7 @@ public NoConstructorsFoundException(Type offendingType, string message, Exceptio
: base(message, innerException)
{
if (offendingType == null) throw new ArgumentNullException(nameof(offendingType));
this.OffendingType = offendingType;
OffendingType = offendingType;
}

/// <summary>
Expand Down
Expand Up @@ -109,7 +109,7 @@ public void ConfigurePipeline(IComponentRegistryServices componentRegistryServic

_constructorBinders = binders;

pipelineBuilder.Use(this.ToString(), PipelinePhase.Activation, MiddlewareInsertionMode.EndOfPhase, (ctxt, next) =>
pipelineBuilder.Use(ToString(), PipelinePhase.Activation, MiddlewareInsertionMode.EndOfPhase, (ctxt, next) =>
{
ctxt.Instance = ActivateInstance(ctxt, ctxt.Parameters);

Expand Down
4 changes: 2 additions & 2 deletions src/Autofac/Core/Container.cs
Expand Up @@ -39,7 +39,7 @@ namespace Autofac.Core
[DebuggerDisplay("Tag = {Tag}, IsDisposed = {IsDisposed}")]
public class Container : Disposable, IContainer, IServiceProvider
{
private readonly ILifetimeScope _rootLifetimeScope;
private readonly LifetimeScope _rootLifetimeScope;

/// <summary>
/// Initializes a new instance of the <see cref="Container"/> class.
Expand Down Expand Up @@ -100,7 +100,7 @@ public ILifetimeScope BeginLifetimeScope(object tag, Action<ContainerBuilder> co
}

/// <inheritdoc/>
public DiagnosticListener DiagnosticSource => this._rootLifetimeScope.DiagnosticSource;
public DiagnosticListener DiagnosticSource => _rootLifetimeScope.DiagnosticSource;

/// <summary>
/// Gets the disposer associated with this container. Instances can be associated
Expand Down
14 changes: 9 additions & 5 deletions src/Autofac/Core/Lifetime/LifetimeScope.cs
Expand Up @@ -81,7 +81,7 @@ protected LifetimeScope(IComponentRegistry componentRegistry, LifetimeScope pare

_sharedInstances[SelfRegistrationId] = this;
RootLifetimeScope = _parentScope.RootLifetimeScope;
this.DiagnosticSource = _parentScope.DiagnosticSource;
DiagnosticSource = _parentScope.DiagnosticSource;
}

/// <summary>
Expand All @@ -96,7 +96,8 @@ public LifetimeScope(IComponentRegistry componentRegistry, object tag)

_sharedInstances[SelfRegistrationId] = this;
RootLifetimeScope = this;
this.DiagnosticSource = new DiagnosticListener("Autofac");
DiagnosticSource = new DiagnosticListener("Autofac");
Disposer.AddInstanceForDisposal(DiagnosticSource);
}

/// <summary>
Expand Down Expand Up @@ -160,8 +161,11 @@ private void RaiseBeginning(ILifetimeScope scope)
handler?.Invoke(this, new LifetimeScopeBeginningEventArgs(scope));
}

/// <inheritdoc/>
public DiagnosticListener DiagnosticSource { get; }
/// <summary>
/// Gets the <see cref="System.Diagnostics.DiagnosticListener"/> to which
/// trace events should be written.
/// </summary>
internal DiagnosticListener DiagnosticSource { get; }

/// <summary>
/// Begin a new anonymous sub-scope, with additional components available to it.
Expand Down Expand Up @@ -292,7 +296,7 @@ public object ResolveComponent(ResolveRequest request)

CheckNotDisposed();

var operation = new ResolveOperation(this);
var operation = new ResolveOperation(this, DiagnosticSource);
var handler = ResolveOperationBeginning;
handler?.Invoke(this, new ResolveOperationBeginningEventArgs(operation));
return operation.Execute(request);
Expand Down
13 changes: 4 additions & 9 deletions src/Autofac/Core/Lifetime/MatchingScopeLifetime.cs
Expand Up @@ -43,12 +43,7 @@ public class MatchingScopeLifetime : IComponentLifetime
/// <param name="lifetimeScopeTagsToMatch">The tags applied to matching scopes.</param>
public MatchingScopeLifetime(params object[] lifetimeScopeTagsToMatch)
{
if (lifetimeScopeTagsToMatch == null)
{
throw new ArgumentNullException(nameof(lifetimeScopeTagsToMatch));
}

this._tagsToMatch = lifetimeScopeTagsToMatch;
_tagsToMatch = lifetimeScopeTagsToMatch ?? throw new ArgumentNullException(nameof(lifetimeScopeTagsToMatch));
}

/// <summary>
Expand All @@ -62,7 +57,7 @@ public IEnumerable<object> TagsToMatch
{
get
{
return this._tagsToMatch;
return _tagsToMatch;
}
}

Expand All @@ -82,7 +77,7 @@ public ISharingLifetimeScope FindScope(ISharingLifetimeScope mostNestedVisibleSc
ISharingLifetimeScope? next = mostNestedVisibleScope;
while (next != null)
{
if (this._tagsToMatch.Contains(next.Tag))
if (_tagsToMatch.Contains(next.Tag))
{
return next;
}
Expand All @@ -91,7 +86,7 @@ public ISharingLifetimeScope FindScope(ISharingLifetimeScope mostNestedVisibleSc
}

throw new DependencyResolutionException(string.Format(
CultureInfo.CurrentCulture, MatchingScopeLifetimeResources.MatchingScopeNotFound, string.Join(", ", this._tagsToMatch)));
CultureInfo.CurrentCulture, MatchingScopeLifetimeResources.MatchingScopeNotFound, string.Join(", ", _tagsToMatch)));
}
}
}
35 changes: 13 additions & 22 deletions src/Autofac/Core/Resolving/Pipeline/ResolvePipelineBuilder.cs
Expand Up @@ -270,34 +270,25 @@ Action<ResolveRequestContextBase> Chain(Action<ResolveRequestContextBase> next,

return (ctxt) =>
{
// Optimise the path depending on whether diagnostics are enabled.
if (ctxt.DiagnosticSource.MiddlewareDiagnosticsEnabled())
ctxt.DiagnosticSource.MiddlewareStart(ctxt, stage);
tillig marked this conversation as resolved.
Show resolved Hide resolved
var succeeded = false;
try
{
ctxt.DiagnosticSource.MiddlewareStart(ctxt, stage);
var succeeded = false;
try
ctxt.PhaseReached = stagePhase;
stage.Execute(ctxt, next);
succeeded = true;
}
finally
{
if (succeeded)
{
ctxt.PhaseReached = stagePhase;
stage.Execute(ctxt, next);
succeeded = true;
ctxt.DiagnosticSource.MiddlewareSuccess(ctxt, stage);
}
finally
else
{
if (succeeded)
{
ctxt.DiagnosticSource.MiddlewareSuccess(ctxt, stage);
}
else
{
ctxt.DiagnosticSource.MiddlewareFailure(ctxt, stage);
}
ctxt.DiagnosticSource.MiddlewareFailure(ctxt, stage);
}
}
else
{
ctxt.PhaseReached = stagePhase;
stage.Execute(ctxt, next);
}
};
}

Expand Down
10 changes: 8 additions & 2 deletions src/Autofac/Core/Resolving/Pipeline/ResolveRequestContext.cs
Expand Up @@ -23,6 +23,8 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using System.Diagnostics;

namespace Autofac.Core.Resolving.Pipeline
{
/// <summary>
Expand All @@ -36,11 +38,15 @@ internal sealed class ResolveRequestContext : ResolveRequestContextBase
/// <param name="owningOperation">The owning resolve operation.</param>
/// <param name="request">The initiating resolve request.</param>
/// <param name="scope">The lifetime scope.</param>
/// <param name="diagnosticSource">
/// The <see cref="System.Diagnostics.DiagnosticListener"/> to which trace events should be written.
/// </param>
internal ResolveRequestContext(
ResolveOperationBase owningOperation,
ResolveRequest request,
ISharingLifetimeScope scope)
: base(owningOperation, request, scope)
ISharingLifetimeScope scope,
DiagnosticListener diagnosticSource)
: base(owningOperation, request, scope, diagnosticSource)
{
}

Expand Down
12 changes: 8 additions & 4 deletions src/Autofac/Core/Resolving/Pipeline/ResolveRequestContextBase.cs
Expand Up @@ -48,16 +48,20 @@ public abstract class ResolveRequestContextBase : IComponentContext
/// <param name="owningOperation">The owning resolve operation.</param>
/// <param name="request">The initiating resolve request.</param>
/// <param name="scope">The lifetime scope.</param>
/// <param name="diagnosticSource">
/// The <see cref="System.Diagnostics.DiagnosticListener"/> to which trace events should be written.
/// </param>
internal ResolveRequestContextBase(
ResolveOperationBase owningOperation,
ResolveRequest request,
ISharingLifetimeScope scope)
ISharingLifetimeScope scope,
DiagnosticListener diagnosticSource)
{
Operation = owningOperation;
ActivationScope = scope;
Parameters = request.Parameters;
PhaseReached = PipelinePhase.ResolveRequestStart;
this.DiagnosticSource = scope.DiagnosticSource;
DiagnosticSource = diagnosticSource;
_resolveRequest = request;
}

Expand Down Expand Up @@ -107,9 +111,9 @@ public abstract class ResolveRequestContextBase : IComponentContext
public bool NewInstanceActivated => Instance is object && PhaseReached == PipelinePhase.Activation;

/// <summary>
/// Gets the <see cref="System.Diagnostics.DiagnosticSource"/> for the request.
/// Gets the <see cref="System.Diagnostics.DiagnosticListener"/> to which trace events should be written.
/// </summary>
public DiagnosticSource DiagnosticSource { get; }
public DiagnosticListener DiagnosticSource { get; }

/// <summary>
/// Gets the current resolve parameters. These can be changed using the <see cref="ChangeParameters(IEnumerable{Parameter})"/> method.
Expand Down
12 changes: 6 additions & 6 deletions src/Autofac/Core/Resolving/ResolveOperation.cs
Expand Up @@ -23,9 +23,7 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

using System.Diagnostics.CodeAnalysis;
using Autofac.Core.Resolving.Pipeline;
using Autofac.Diagnostics;
using System.Diagnostics;

namespace Autofac.Core.Resolving
{
Expand All @@ -40,16 +38,18 @@ internal sealed class ResolveOperation : ResolveOperationBase
/// </summary>
/// <param name="mostNestedLifetimeScope">The most nested scope in which to begin the operation. The operation
/// can move upward to less nested scopes as components with wider sharing scopes are activated.</param>
public ResolveOperation(ISharingLifetimeScope mostNestedLifetimeScope)
: base(mostNestedLifetimeScope)
/// <param name="diagnosticSource">
/// The <see cref="System.Diagnostics.DiagnosticListener"/> to which trace events should be written.
/// </param>
public ResolveOperation(ISharingLifetimeScope mostNestedLifetimeScope, DiagnosticListener diagnosticSource)
: base(mostNestedLifetimeScope, diagnosticSource)
{
}

/// <summary>
/// Execute the complete resolve operation.
/// </summary>
/// <param name="request">The resolution context.</param>
[SuppressMessage("CA1031", "CA1031", Justification = "General exception gets rethrown in a DependencyResolutionException.")]
public object Execute(ResolveRequest request)
{
return ExecuteOperation(request);
Expand Down