Repository navigation
[dashboard-oop] Allow dashboard to run without resource service - #1795
Drew Noakes (drewnoakes) wants to merge 7 commits into
Conversation
And tidy up some code that parsed `DOTNET_DASHBOARD_GRPC_ENDPOINT_URL`. Change that code to only support a single URI for that purpose, as it only makes one connection.
When the `DOTNET_DASHBOARD_GRPC_ENDPOINT_URL` variable is not in the dashboard's environment, the `DashboardClient` disables itself. In this state, it will throw when invoked, though you may query the `IsEnabled` property safely before making other calls.
Makes it possible to unit test environment variables.
Hides the "Resources" and "Console logs" navigation menu items whenever the `DashboardClient` is not enabled (due to `DOTNET_DASHBOARD_GRPC_ENDPOINT_URL` not being available).
|
Test failure in |
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
|
Is the intention to merge this in before going to main, or will you retarget to main once this merges in? |
|
I'm ok either way. I'm not sure whether we need this for p3 or not. I assumed that with the dashboard moving OOP, people will start trying to use it in different ways, and this supports that. David Fowler (@davidfowl) Damian Edwards (@DamianEdwards) for input on scheduling this. |
|
Given we don't currently think we'll have the container published in preview 3 and that's the way we'll mostly facilitate the dashboard to run outside of the AppHost scenario, I don't think it's important we get this in preview 3, but definitely for preview 4. |
|
I'm fine with this if we can get the changes in tomorrow so we can stablize the rest of the week. This is great! |
| _channel = null!; | ||
| _client = null!; |
There was a problem hiding this comment.
I'm not a fan of this. But making it clean would involve another nested type.
There was a problem hiding this comment.
Won't the dashboard client throw an error when disposed because the channel is null?
There was a problem hiding this comment.
Maybe they should be nullable and then there is AssertNotDisabled() in methods that use these types. It would be annotated so that the compiled knows they're not null if it doesn't throw.
There was a problem hiding this comment.
I experimented with MemberNotNull but didn't like how it looked. For example:
@@ -126,8 +125,28 @@ GrpcChannel CreateChannel()
public bool IsEnabled => _state is not StateDisabled;
+ [MemberNotNull(nameof(_channel))]
+ [MemberNotNull(nameof(_client))]
+ [MemberNotNull(nameof(_connection))]
+ private void AssertNotDisabled()
+#pragma warning disable CS8774 // Member must have a non-null value when exiting.
+ {
+ // If we're in disabled state, we're not changing out of it.
+ // We don't need to worry about concurrency for this read.
+ if (_state is StateDisabled)
+ {
+ throw new InvalidOperationException($"{nameof(DashboardClient)} is disabled. Check the {nameof(IsEnabled)} property before calling this.");
+ }
+ }
+#pragma warning restore CS8774 // Member must have a non-null value when exiting.
+
+ [MemberNotNull(nameof(_channel))]
+ [MemberNotNull(nameof(_client))]
+ [MemberNotNull(nameof(_connection))]
private void EnsureInitialized()
{
+ AssertNotDisabled();
+
var priorState = Interlocked.CompareExchange(ref _state, value: StateInitialized, comparand: StateNone);
if (priorState is StateDisabled)Or, annotating only EnsureInitialized:
@@ -126,6 +125,9 @@ GrpcChannel CreateChannel()
public bool IsEnabled => _state is not StateDisabled;
+ [MemberNotNull(nameof(_channel))]
+ [MemberNotNull(nameof(_client))]
+ [MemberNotNull(nameof(_connection))]
private void EnsureInitialized()
{
var priorState = Interlocked.CompareExchange(ref _state, value: StateInitialized, comparand: StateNone);
@@ -138,12 +140,16 @@ private void EnsureInitialized()
if (priorState is not StateNone)
{
ObjectDisposedException.ThrowIf(priorState is StateDisposed, this);
+#pragma warning disable CS8774 // Member must have a non-null value when exiting.
return;
+#pragma warning restore CS8774 // Member must have a non-null value when exiting.
}
_connection = Task.Run(() => ConnectAndWatchResourcesAsync(_cts.Token), _cts.Token);
+#pragma warning disable CS8774 // Member must have a non-null value when exiting.
return;
+#pragma warning restore CS8774 // Member must have a non-null value when exiting.
async Task ConnectAndWatchResourcesAsync(CancellationToken cancellationToken)
{
@@ -155,7 +161,7 @@ async Task ConnectAsync()
{
try
{
- var response = await _client.GetApplicationInformationAsync(new(), cancellationToken: cancellationToken);
+ var response = await _client!.GetApplicationInformationAsync(new(), cancellationToken: cancellationToken);
_applicationName = response.ApplicationName;
@@ -208,7 +214,7 @@ static TimeSpan ExponentialBackOff(int errorCount, double maxSeconds)
async Task WatchResourcesAsync()
{
- var call = _client.WatchResources(new WatchResourcesRequest { IsReconnect = errorCount != 0 }, cancellationToken: cancellationToken);
+ var call = _client!.WatchResources(new WatchResourcesRequest { IsReconnect = errorCount != 0 }, cancellationToken: cancellationToken);
await foreach (var response in call.ResponseStream.ReadAllAsync(cancellationToken: cancellationToken))
{I don't like either of those more than what we have now.
Won't the dashboard client throw an error when disposed because the channel is null?
Yes, thanks I'll fix that. This is what nullability is supposed to catch. I'm going to make the fields nullable, remove two suppressions and add three more, rather than either of the above. If you know a cleaner way, I'd be interested to hear it.
| [return: NotNullIfNotNull(nameof(defaultValue))] | ||
| public string? GetString(string variableName, string? defaultValue = null) | ||
| { | ||
| var value = ImmutableInterlocked.GetOrAdd(ref _valueByName, key: variableName, valueFactory: Environment.GetEnvironmentVariable); |
There was a problem hiding this comment.
Why do we need any caching at all? Why not just pass through to Environment.GetEnvironmentVariable?
There was a problem hiding this comment.
Looks like it allocates and P/Invokes each time:
I don't know if we care, but we do check env vars a fair bit around the place, and caching takes two lines of code.
|
I will also update the dashboard's README, which currently states there is a default value (no longer true with this PR). |
|
GitHub closed this when I merged the feature branch, and I can't see a way to retarget it. I think we'll need a new PR. Apologies to reviewers. I'll apply feedback to the new PR before creating it. There are some merge conflicts to fix too. |
|
David Fowler (@davidfowl) James Newton-King (@JamesNK) this was closed when the feature branch merged. I've discussed your feedback here and applied changes to the new PR #1815. |
Relates to #1556
With the dashboard running OOP, it is configured with the resource server endpoint via the
DOTNET_DASHBOARD_GRPC_ENDPOINT_URLenvironment variable.The dashboard can actually perform useful work even without this connection, however it requires some code changes to support this.
In this PR, if the environment variable is not defined, then
IDashboardClient.IsEnabledwill be false, and some code paths become inactive based off the flag.To get tests passing, we introduce an abstraction for environment variables so we can inject the values we need.
The navigation tree will not display the "Resources" or "Console logs" items when no resource service endpoint is available:
Microsoft Reviewers: Open in CodeFlow