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

Refactor registration to artifact operation #3960

Merged
merged 1 commit into from
Mar 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -642,12 +642,6 @@ private Task<ActionResult> StartTrace(
configuration,
duration);

if (_diagnosticPortOptions.Value.ConnectionMode == DiagnosticPortConnectionMode.Listen)
{
IDisposable operationRegistration = _operationTrackerService.Register(processInfo.EndpointInfo);
HttpContext.Response.RegisterForDispose(operationRegistration);
}

return Result(
Utilities.ArtifactType_Trace,
egressProvider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,12 @@ private sealed class CollectTraceAction :
{
private readonly IServiceProvider _serviceProvider;
private readonly IOptionsMonitor<GlobalCounterOptions> _counterOptions;
private readonly OperationTrackerService _operationTrackerService;

public CollectTraceAction(IServiceProvider serviceProvider, IEndpointInfo endpointInfo, CollectTraceOptions options)
: base(endpointInfo, options)
{
_serviceProvider = serviceProvider ?? throw new ArgumentNullException(nameof(serviceProvider));
_counterOptions = _serviceProvider.GetRequiredService<IOptionsMonitor<GlobalCounterOptions>>();
_operationTrackerService = _serviceProvider.GetRequiredService<OperationTrackerService>();
}

protected override async Task<CollectionRuleActionResult> ExecuteCoreAsync(
Expand Down Expand Up @@ -109,7 +107,6 @@ protected override async Task<CollectionRuleActionResult> ExecuteCoreAsync(
EgressOperation egressOperation = new EgressOperation(
async (outputStream, token) =>
{
using IDisposable operationRegistration = _operationTrackerService.Register(EndpointInfo);
await operation.ExecuteAsync(outputStream, startCompletionSource, token);
},
egressProvider,
Expand Down
4 changes: 2 additions & 2 deletions src/Tools/dotnet-monitor/Logs/LogsOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ internal sealed class LogsOperation : PipelineArtifactOperation<EventLogsPipelin
private readonly LogFormat _format;
private readonly EventLogsPipelineSettings _settings;

public LogsOperation(IEndpointInfo endpointInfo, EventLogsPipelineSettings settings, LogFormat format, ILogger logger)
: base(logger, Utils.ArtifactType_Logs, endpointInfo)
public LogsOperation(IEndpointInfo endpointInfo, EventLogsPipelineSettings settings, LogFormat format, OperationTrackerService trackerService, ILogger logger)
: base(trackerService, logger, Utils.ArtifactType_Logs, endpointInfo)
{
_format = format;
_settings = settings;
Expand Down
6 changes: 4 additions & 2 deletions src/Tools/dotnet-monitor/Logs/LogsOperationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,18 @@ namespace Microsoft.Diagnostics.Tools.Monitor
{
internal sealed class LogsOperationFactory : ILogsOperationFactory
{
private readonly OperationTrackerService _operationTrackerService;
private readonly ILogger<LogsOperation> _logger;

public LogsOperationFactory(ILogger<LogsOperation> logger)
public LogsOperationFactory(OperationTrackerService operationTrackerService, ILogger<LogsOperation> logger)
{
_operationTrackerService = operationTrackerService;
_logger = logger;
}

public IArtifactOperation Create(IEndpointInfo endpointInfo, EventLogsPipelineSettings settings, LogFormat format)
{
return new LogsOperation(endpointInfo, settings, format, _logger);
return new LogsOperation(endpointInfo, settings, format, _operationTrackerService, _logger);
}
}
}
4 changes: 2 additions & 2 deletions src/Tools/dotnet-monitor/Metrics/MetricsOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ internal sealed class MetricsOperation : PipelineArtifactOperation<MetricsPipeli
{
private readonly MetricsPipelineSettings _settings;

public MetricsOperation(IEndpointInfo endpointInfo, MetricsPipelineSettings settings, ILogger logger)
: base(logger, Utils.ArtifactType_Metrics, endpointInfo)
public MetricsOperation(IEndpointInfo endpointInfo, MetricsPipelineSettings settings, OperationTrackerService trackerService, ILogger logger)
: base(trackerService, logger, Utils.ArtifactType_Metrics, endpointInfo)
{
_settings = settings;
}
Expand Down
6 changes: 4 additions & 2 deletions src/Tools/dotnet-monitor/Metrics/MetricsOperationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ namespace Microsoft.Diagnostics.Tools.Monitor
{
internal sealed class MetricsOperationFactory : IMetricsOperationFactory
{
private readonly OperationTrackerService _operationTrackerService;
private readonly ILogger<MetricsOperation> _logger;

public MetricsOperationFactory(ILogger<MetricsOperation> logger)
public MetricsOperationFactory(OperationTrackerService operationTrackerService, ILogger<MetricsOperation> logger)
{
_operationTrackerService = operationTrackerService;
_logger = logger;
}

public IArtifactOperation Create(IEndpointInfo endpointInfo, MetricsPipelineSettings settings)
{
return new MetricsOperation(endpointInfo, settings, _logger);
return new MetricsOperation(endpointInfo, settings, _operationTrackerService, _logger);
}
}
}
10 changes: 9 additions & 1 deletion src/Tools/dotnet-monitor/PipelineArtifactOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ internal abstract class PipelineArtifactOperation<T> :

private Func<CancellationToken, Task> _stopFunc;

protected PipelineArtifactOperation(ILogger logger, string artifactType, IEndpointInfo endpointInfo, bool isStoppable = true)
protected OperationTrackerService OperationTrackerService { get; }

protected PipelineArtifactOperation(OperationTrackerService trackerService, ILogger logger, string artifactType, IEndpointInfo endpointInfo, bool isStoppable = true, bool register = false)
{
_artifactType = artifactType;
OperationTrackerService = trackerService;

Logger = logger;
EndpointInfo = endpointInfo;
IsStoppable = isStoppable;
Register = register;
}

public async Task ExecuteAsync(Stream outputStream, TaskCompletionSource<object> startCompletionSource, CancellationToken token)
Expand All @@ -34,6 +38,8 @@ public async Task ExecuteAsync(Stream outputStream, TaskCompletionSource<object>

_stopFunc = pipeline.StopAsync;

using IDisposable trackerRegistration = Register ? OperationTrackerService.Register(EndpointInfo) : null;

Task runTask = await StartPipelineAsync(pipeline, token);

Logger.StartCollectArtifact(_artifactType);
Expand Down Expand Up @@ -65,6 +71,8 @@ public async Task StopAsync(CancellationToken token)

public bool IsStoppable { get; }

public bool Register { get; }

protected abstract T CreatePipeline(Stream outputStream);

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions src/Tools/dotnet-monitor/Trace/AbstractTraceOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ internal abstract class AbstractTraceOperation : PipelineArtifactOperation<Event

protected readonly EventTracePipelineSettings _settings;

public AbstractTraceOperation(IEndpointInfo endpointInfo, EventTracePipelineSettings settings, ILogger logger)
: base(logger, Utils.ArtifactType_Trace, endpointInfo)
public AbstractTraceOperation(IEndpointInfo endpointInfo, EventTracePipelineSettings settings, OperationTrackerService trackerService, ILogger logger)
: base(trackerService, logger, Utils.ArtifactType_Trace, endpointInfo, register: true)
{
_settings = settings;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Tools/dotnet-monitor/Trace/TraceOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ internal sealed class TraceOperation : AbstractTraceOperation
{
private readonly TaskCompletionSource<object> _eventStreamAvailableCompletionSource = new(TaskCreationOptions.RunContinuationsAsynchronously);

public TraceOperation(IEndpointInfo endpointInfo, EventTracePipelineSettings settings, ILogger logger)
: base(endpointInfo, settings, logger) { }
public TraceOperation(IEndpointInfo endpointInfo, EventTracePipelineSettings settings, OperationTrackerService trackerService, ILogger logger)
: base(endpointInfo, settings, trackerService, logger) { }

protected override EventTracePipeline CreatePipeline(Stream outputStream)
{
Expand Down
8 changes: 5 additions & 3 deletions src/Tools/dotnet-monitor/Trace/TraceOperationFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ namespace Microsoft.Diagnostics.Tools.Monitor
{
internal sealed class TraceOperationFactory : ITraceOperationFactory
{
private readonly OperationTrackerService _operationTrackerService;
private readonly ILogger<TraceOperation> _logger;

public TraceOperationFactory(ILogger<TraceOperation> logger)
public TraceOperationFactory(OperationTrackerService operationTrackerService, ILogger<TraceOperation> logger)
{
_operationTrackerService = operationTrackerService;
_logger = logger;
}

Expand All @@ -26,7 +28,7 @@ public IArtifactOperation Create(IEndpointInfo endpointInfo, MonitoringSourceCon
Duration = duration
};

return new TraceOperation(endpointInfo, settings, _logger);
return new TraceOperation(endpointInfo, settings, _operationTrackerService, _logger);
}

public IArtifactOperation Create(IEndpointInfo endpointInfo, MonitoringSourceConfiguration configuration, TimeSpan duration, string providerName, string eventName, IDictionary<string, string> payloadFilter)
Expand All @@ -37,7 +39,7 @@ public IArtifactOperation Create(IEndpointInfo endpointInfo, MonitoringSourceCon
Duration = duration
};

return new TraceUntilEventOperation(endpointInfo, settings, providerName, eventName, payloadFilter, _logger);
return new TraceUntilEventOperation(endpointInfo, settings, providerName, eventName, payloadFilter, _operationTrackerService, _logger);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ public TraceUntilEventOperation(
string providerName,
string eventName,
IDictionary<string, string> payloadFilter,
OperationTrackerService trackerService,
ILogger logger)
: base(endpointInfo, settings, logger)
: base(endpointInfo, settings, trackerService, logger)
{
_providerName = providerName;
_eventName = eventName;
Expand Down