Skip to content

Commit

Permalink
[Nullable Context] Enable on Microsoft.Diagnostics.Monitoring.WebApi (
Browse files Browse the repository at this point in the history
  • Loading branch information
schmittjoseph authored Jul 1, 2024
1 parent eed24c7 commit c82a161
Show file tree
Hide file tree
Showing 57 changed files with 245 additions and 217 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public sealed class ExecutionResult<T>
{
private static readonly ExecutionResult<T> _empty = new ExecutionResult<T>();

public Exception Exception { get; private set; }
public T Result { get; private set; }
public ProblemDetails ProblemDetails { get; private set; }
public Exception? Exception { get; private set; }
public T? Result { get; private set; }
public ProblemDetails? ProblemDetails { get; private set; }

private ExecutionResult() { }

Expand Down Expand Up @@ -95,7 +95,7 @@ internal static class ActionContextExtensions
{
public static Task ProblemAsync(this ActionContext context, BadRequestObjectResult result)
{
if (context.HttpContext.Features.Get<IHttpResponseFeature>().HasStarted)
if (context.HttpContext.Features.Get<IHttpResponseFeature>()?.HasStarted == true)
{
// If already started writing response, do not rewrite
// as this will throw an InvalidOperationException.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Microsoft.Diagnostics.Monitoring.WebApi
{
internal static class AssemblyExtensions
{
public static string GetInformationalVersionString(this Assembly assembly)
public static string? GetInformationalVersionString(this Assembly assembly)
{
if (assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
is AssemblyInformationalVersionAttribute assemblyVersionAttribute)
Expand All @@ -16,7 +16,7 @@ public static string GetInformationalVersionString(this Assembly assembly)
}
else
{
return assembly.GetName().Version.ToString();
return assembly.GetName().Version?.ToString();
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public static ActionResult InvokeService(this ControllerBase controller, Func<Ac
{
//We can convert ActionResult to ActionResult<T>
//and then safely convert back.
return controller.InvokeService<object>(() => serviceCall(), logger).Result;
return controller.InvokeService<object>(() => serviceCall(), logger).Result!;
}

public static ActionResult<T> InvokeService<T>(this ControllerBase controller, Func<ActionResult<T>> serviceCall, ILogger logger)
Expand All @@ -50,7 +50,7 @@ public static async Task<ActionResult> InvokeService(this ControllerBase control
//Task<ActionResult> -> Task<ActionResult<T>>
//Then unwrap the result back to ActionResult
ActionResult<object> result = await controller.InvokeService<object>(async () => await serviceCall(), logger);
return result.Result;
return result.Result!;
}

public static async Task<ActionResult<T>> InvokeService<T>(this ControllerBase controller, Func<Task<ActionResult<T>>> serviceCall, ILogger logger)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ public Task<ActionResult> CaptureMetrics(
[FromQuery]
Guid? uid = null,
[FromQuery]
string name = null,
string? name = null,
[FromQuery][Range(-1, int.MaxValue)]
int durationSeconds = 30,
[FromQuery]
string egressProvider = null,
string? egressProvider = null,
[FromQuery]
string tags = null)
string? tags = null)
{
ProcessKey? processKey = Utilities.GetProcessKey(pid, uid, name);

Expand Down Expand Up @@ -83,13 +83,13 @@ public Task<ActionResult> CaptureMetricsCustom(
[FromQuery]
Guid? uid = null,
[FromQuery]
string name = null,
string? name = null,
[FromQuery][Range(-1, int.MaxValue)]
int durationSeconds = 30,
[FromQuery]
string egressProvider = null,
string? egressProvider = null,
[FromQuery]
string tags = null)
string? tags = null)
{
ProcessKey? processKey = Utilities.GetProcessKey(pid, uid, name);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ public partial class DiagController : DiagnosticsControllerBase
{
private const TraceProfile DefaultTraceProfiles = TraceProfile.Cpu | TraceProfile.Http | TraceProfile.Metrics | TraceProfile.GcCollect;

#nullable disable
private readonly IOptions<DiagnosticPortOptions> _diagnosticPortOptions;
#nullable restore
private readonly IOptions<CallStacksOptions> _callStacksOptions;
private readonly IOptions<ParameterCapturingOptions> _parameterCapturingOptions;
private readonly IOptionsMonitor<GlobalCounterOptions> _counterOptions;
Expand Down Expand Up @@ -77,7 +79,7 @@ public Task<ActionResult<IEnumerable<ProcessIdentifier>>> GetProcesses()
{
return this.InvokeService(async () =>
{
IProcessInfo defaultProcessInfo = null;
IProcessInfo? defaultProcessInfo = null;
try
{
defaultProcessInfo = await DiagnosticServices.GetProcessAsync(null, HttpContext.RequestAborted);
Expand Down Expand Up @@ -127,7 +129,7 @@ public Task<ActionResult<IEnumerable<ProcessIdentifier>>> GetProcesses()
[FromQuery]
Guid? uid = null,
[FromQuery]
string name = null)
string? name = null)
{
ProcessKey? processKey = Utilities.GetProcessKey(pid, uid, name);

Expand Down Expand Up @@ -165,7 +167,7 @@ public Task<ActionResult<Dictionary<string, string>>> GetProcessEnvironment(
[FromQuery]
Guid? uid = null,
[FromQuery]
string name = null)
string? name = null)
{
ProcessKey? processKey = Utilities.GetProcessKey(pid, uid, name);

Expand Down Expand Up @@ -213,13 +215,13 @@ public Task<ActionResult> CaptureDump(
[FromQuery]
Guid? uid = null,
[FromQuery]
string name = null,
string? name = null,
[FromQuery]
Models.DumpType type = Models.DumpType.WithHeap,
[FromQuery]
string egressProvider = null,
string? egressProvider = null,
[FromQuery]
string tags = null)
string? tags = null)
{
ProcessKey? processKey = Utilities.GetProcessKey(pid, uid, name);

Expand Down Expand Up @@ -257,11 +259,11 @@ public Task<ActionResult> CaptureGcDump(
[FromQuery]
Guid? uid = null,
[FromQuery]
string name = null,
string? name = null,
[FromQuery]
string egressProvider = null,
string? egressProvider = null,
[FromQuery]
string tags = null)
string? tags = null)
{
ProcessKey? processKey = Utilities.GetProcessKey(pid, uid, name);

Expand Down Expand Up @@ -300,15 +302,15 @@ public Task<ActionResult> CaptureTrace(
[FromQuery]
Guid? uid = null,
[FromQuery]
string name = null,
string? name = null,
[FromQuery]
TraceProfile profile = DefaultTraceProfiles,
[FromQuery][Range(-1, int.MaxValue)]
int durationSeconds = 30,
[FromQuery]
string egressProvider = null,
string? egressProvider = null,
[FromQuery]
string tags = null)
string? tags = null)
{
ProcessKey? processKey = Utilities.GetProcessKey(pid, uid, name);

Expand Down Expand Up @@ -348,13 +350,13 @@ public Task<ActionResult> CaptureTraceCustom(
[FromQuery]
Guid? uid = null,
[FromQuery]
string name = null,
string? name = null,
[FromQuery][Range(-1, int.MaxValue)]
int durationSeconds = 30,
[FromQuery]
string egressProvider = null,
string? egressProvider = null,
[FromQuery]
string tags = null)
string? tags = null)
{
ProcessKey? processKey = Utilities.GetProcessKey(pid, uid, name);

Expand All @@ -363,7 +365,7 @@ public Task<ActionResult> CaptureTraceCustom(
foreach (Models.EventPipeProvider provider in configuration.Providers)
{
if (!CounterValidator.ValidateProvider(_counterOptions.CurrentValue,
provider, out string errorMessage))
provider, out string? errorMessage))
{
throw new ValidationException(errorMessage);
}
Expand Down Expand Up @@ -399,15 +401,15 @@ public Task<ActionResult> CaptureLogs(
[FromQuery]
Guid? uid = null,
[FromQuery]
string name = null,
string? name = null,
[FromQuery][Range(-1, int.MaxValue)]
int durationSeconds = 30,
[FromQuery]
LogLevel? level = null,
[FromQuery]
string egressProvider = null,
string? egressProvider = null,
[FromQuery]
string tags = null)
string? tags = null)
{
ProcessKey? processKey = Utilities.GetProcessKey(pid, uid, name);

Expand Down Expand Up @@ -459,13 +461,13 @@ public Task<ActionResult> CaptureLogsCustom(
[FromQuery]
Guid? uid = null,
[FromQuery]
string name = null,
string? name = null,
[FromQuery][Range(-1, int.MaxValue)]
int durationSeconds = 30,
[FromQuery]
string egressProvider = null,
string? egressProvider = null,
[FromQuery]
string tags = null)
string? tags = null)
{
ProcessKey? processKey = Utilities.GetProcessKey(pid, uid, name);

Expand Down Expand Up @@ -495,7 +497,7 @@ public ActionResult<DotnetMonitorInfo> GetInfo()
{
return this.InvokeService(() =>
{
string version = Assembly.GetExecutingAssembly().GetInformationalVersionString();
string? version = Assembly.GetExecutingAssembly().GetInformationalVersionString();
string runtimeVersion = Environment.Version.ToString();
DiagnosticPortConnectionMode diagnosticPortMode = _diagnosticPortOptions.Value.GetConnectionMode();
string diagnosticPortName = GetDiagnosticPortName();
Expand Down Expand Up @@ -528,7 +530,7 @@ public Task<ActionResult<Dictionary<string, CollectionRuleDescription>>> GetColl
[FromQuery]
Guid? uid = null,
[FromQuery]
string name = null)
string? name = null)
{
return InvokeForProcess<Dictionary<string, CollectionRuleDescription>>(processInfo =>
{
Expand All @@ -554,7 +556,7 @@ public Task<ActionResult<CollectionRuleDetailedDescription>> GetCollectionRuleDe
[FromQuery]
Guid? uid = null,
[FromQuery]
string name = null)
string? name = null)
{
return InvokeForProcess<CollectionRuleDetailedDescription>(processInfo =>
{
Expand All @@ -579,11 +581,11 @@ public async Task<ActionResult> CaptureParameters(
[FromQuery]
Guid? uid = null,
[FromQuery]
string name = null,
string? name = null,
[FromQuery]
string egressProvider = null,
string? egressProvider = null,
[FromQuery]
string tags = null)
string? tags = null)
{
if (!_parameterCapturingOptions.Value.GetEnabled())
{
Expand Down Expand Up @@ -621,11 +623,11 @@ public Task<ActionResult> CaptureStacks(
[FromQuery]
Guid? uid = null,
[FromQuery]
string name = null,
string? name = null,
[FromQuery]
string egressProvider = null,
string? egressProvider = null,
[FromQuery]
string tags = null)
string? tags = null)
{
if (!_callStacksOptions.Value.GetEnabled())
{
Expand Down Expand Up @@ -660,8 +662,8 @@ private Task<ActionResult> StartTrace(
IProcessInfo processInfo,
MonitoringSourceConfiguration configuration,
TimeSpan duration,
string egressProvider,
string tags)
string? egressProvider,
string? tags)
{
IArtifactOperation traceOperation = _traceOperationFactory.Create(
processInfo.EndpointInfo,
Expand All @@ -679,8 +681,8 @@ private Task<ActionResult> StartTrace(
private Task<ActionResult> StartLogs(
IProcessInfo processInfo,
EventLogsPipelineSettings settings,
string egressProvider,
string tags)
string? egressProvider,
string? tags)
{
LogFormat? format = ComputeLogFormat(Request.GetTypedHeaders().Accept);
if (null == format)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ private protected DiagnosticsControllerBase(IDiagnosticServices diagnosticServic
Logger = logger ?? throw new ArgumentNullException(nameof(logger));
}

protected Task<ActionResult> InvokeForProcess(Func<IProcessInfo, ActionResult> func, ProcessKey? processKey, string artifactType = null)
protected Task<ActionResult> InvokeForProcess(Func<IProcessInfo, ActionResult> func, ProcessKey? processKey, string? artifactType = null)
{
Func<IProcessInfo, Task<ActionResult>> asyncFunc =
processInfo => Task.FromResult(func(processInfo));

return InvokeForProcess(asyncFunc, processKey, artifactType);
}

protected async Task<ActionResult> InvokeForProcess(Func<IProcessInfo, Task<ActionResult>> func, ProcessKey? processKey, string artifactType)
protected async Task<ActionResult> InvokeForProcess(Func<IProcessInfo, Task<ActionResult>> func, ProcessKey? processKey, string? artifactType)
{
ActionResult<object> result = await InvokeForProcess<object>(async processInfo => await func(processInfo), processKey, artifactType);

return result.Result;
return result.Result!;
}

protected Task<ActionResult<T>> InvokeForProcess<T>(Func<IProcessInfo, ActionResult<T>> func, ProcessKey? processKey, string artifactType = null)
protected Task<ActionResult<T>> InvokeForProcess<T>(Func<IProcessInfo, ActionResult<T>> func, ProcessKey? processKey, string? artifactType = null)
{
return InvokeForProcess(processInfo => Task.FromResult(func(processInfo)), processKey, artifactType);
}

protected async Task<ActionResult<T>> InvokeForProcess<T>(Func<IProcessInfo, Task<ActionResult<T>>> func, ProcessKey? processKey, string artifactType = null)
protected async Task<ActionResult<T>> InvokeForProcess<T>(Func<IProcessInfo, Task<ActionResult<T>>> func, ProcessKey? processKey, string? artifactType = null)
{
IDisposable artifactTypeRegistration = null;
IDisposable? artifactTypeRegistration = null;
if (!string.IsNullOrEmpty(artifactType))
{
KeyValueLogScope artifactTypeScope = new KeyValueLogScope();
Expand Down Expand Up @@ -75,10 +75,10 @@ protected async Task<ActionResult<T>> InvokeForProcess<T>(Func<IProcessInfo, Tas

protected async Task<ActionResult> Result(
string artifactType,
string providerName,
string? providerName,
IArtifactOperation operation,
IProcessInfo processInfo,
string tags,
string? tags,
bool asAttachment = true)
{
KeyValueLogScope scope = Utilities.CreateArtifactScope(artifactType, processInfo.EndpointInfo);
Expand All @@ -103,15 +103,15 @@ protected async Task<ActionResult> Result(
}
}

private async Task RegisterCurrentHttpResponseAsOperation(IProcessInfo processInfo, string artifactType, string tags, IArtifactOperation operation)
private async Task RegisterCurrentHttpResponseAsOperation(IProcessInfo processInfo, string artifactType, string? tags, IArtifactOperation operation)
{
// While not strictly a Location redirect, use the same header as externally egressed operations for consistency.
HttpContext.Response.Headers["Location"] = await RegisterOperation(
new HttpResponseEgressOperation(HttpContext, processInfo, tags, operation),
limitKey: artifactType);
}

private async Task<string> RegisterOperation(IEgressOperation egressOperation, string limitKey)
private async Task<string?> RegisterOperation(IEgressOperation egressOperation, string limitKey)
{
// Will throw TooManyRequestsException if there are too many concurrent operations.
Guid operationId = await OperationStore.AddOperation(egressOperation, limitKey);
Expand All @@ -123,7 +123,7 @@ private async Task<string> RegisterOperation(IEgressOperation egressOperation, s

private async Task<ActionResult> SendToEgress(IEgressOperation egressOperation, string limitKey)
{
string operationUrl = await RegisterOperation(egressOperation, limitKey);
string? operationUrl = await RegisterOperation(egressOperation, limitKey);
return Accepted(operationUrl);
}

Expand Down
Loading

0 comments on commit c82a161

Please sign in to comment.