-
Notifications
You must be signed in to change notification settings - Fork 321
/
DashboardWebApplication.cs
194 lines (162 loc) · 7.32 KB
/
DashboardWebApplication.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.Net;
using Aspire.Dashboard.Components;
using Aspire.Dashboard.Model;
using Aspire.Dashboard.Otlp.Grpc;
using Aspire.Dashboard.Otlp.Storage;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Server.Kestrel.Core;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.FluentUI.AspNetCore.Components;
namespace Aspire.Dashboard;
public class DashboardWebApplication : IHostedService
{
private const string DashboardOtlpUrlVariableName = "DOTNET_DASHBOARD_OTLP_ENDPOINT_URL";
private const string DashboardOtlpUrlDefaultValue = "http://localhost:18889";
private const string DashboardUrlVariableName = "ASPNETCORE_URLS";
private const string DashboardUrlDefaultValue = "http://localhost:18888";
private readonly bool _isAllHttps;
private readonly WebApplication _app;
private readonly ILogger<DashboardWebApplication> _logger;
public DashboardWebApplication(ILogger<DashboardWebApplication> logger, Action<IServiceCollection> configureServices)
{
_logger = logger;
var builder = WebApplication.CreateBuilder();
builder.Logging.AddFilter("Microsoft.Hosting.Lifetime", LogLevel.None);
builder.Logging.AddFilter("Microsoft.AspNetCore.Server.Kestrel", LogLevel.Error);
var dashboardUris = GetAddressUris(DashboardUrlVariableName, DashboardUrlDefaultValue);
if (dashboardUris.FirstOrDefault() is { } reportedDashboardUri)
{
// dotnet watch needs the trailing slash removed. See https://github.com/dotnet/sdk/issues/36709
_logger.LogInformation("Now listening on: {dashboardUri}", reportedDashboardUri.AbsoluteUri.TrimEnd('/'));
}
var dashboardHttpsPort = dashboardUris.FirstOrDefault(IsHttps)?.Port;
var otlpUris = GetAddressUris(DashboardOtlpUrlVariableName, DashboardOtlpUrlDefaultValue);
if (otlpUris.Length > 1)
{
throw new InvalidOperationException("Only one URL for Aspire dashboard OTLP endpoint is supported.");
}
if (otlpUris.FirstOrDefault() is { } reportedOtlpUri)
{
// dotnet watch needs the trailing slash removed. See https://github.com/dotnet/sdk/issues/36709. Conform to dashboard URL format above
_logger.LogInformation("OTLP server running at: {dashboardUri}", reportedOtlpUri.AbsoluteUri.TrimEnd('/'));
}
_isAllHttps = dashboardHttpsPort is not null && IsHttps(otlpUris[0]);
builder.WebHost.ConfigureKestrel(kestrelOptions =>
{
ConfigureListenAddresses(kestrelOptions, dashboardUris);
ConfigureListenAddresses(kestrelOptions, otlpUris, HttpProtocols.Http2);
});
if (!builder.Environment.IsDevelopment())
{
// This is set up automatically by the DefaultBuilder when IsDevelopment is true
// But since this gets packaged up and used in another app, we need it to look for
// static assets on disk as if it were at development time even when it is not
builder.WebHost.UseStaticWebAssets();
}
if (_isAllHttps)
{
// Explicitly configure the HTTPS redirect port as we're possibly listening on multiple HTTPS addresses
// if the dashboard OTLP URL is configured to use HTTPS too
builder.Services.Configure<HttpsRedirectionOptions>(options => options.HttpsPort = dashboardHttpsPort);
}
// Add services to the container.
builder.Services.AddRazorComponents().AddInteractiveServerComponents();
// OTLP services.
builder.Services.AddGrpc();
builder.Services.AddSingleton<TelemetryRepository>();
builder.Services.AddScoped<IOutgoingPeerResolver, ResourceOutgoingPeerResolver>();
builder.Services.AddTransient<StructuredLogsViewModel>();
builder.Services.AddTransient<TracesViewModel>();
builder.Services.AddFluentUIComponents();
configureServices(builder.Services);
_app = builder.Build();
// Configure the HTTP request pipeline.
if (!_app.Environment.IsDevelopment())
{
_app.UseExceptionHandler("/Error");
}
if (_isAllHttps)
{
_app.UseHttpsRedirection();
}
_app.UseStaticFiles(new StaticFileOptions()
{
OnPrepareResponse = (context) =>
{
// If Cache-Control isn't already set to something, set it to 'no-cache' so that the
// ETag and Last-Modified headers will be respected by the browser.
// This may be able to be removed if https://github.com/dotnet/aspnetcore/issues/44153
// is fixed to make this the default
if (context.Context.Response.Headers.CacheControl.Count == 0)
{
context.Context.Response.Headers.CacheControl = "no-cache";
}
}
});
_app.UseAuthorization();
_app.UseAntiforgery();
_app.MapRazorComponents<App>().AddInteractiveServerRenderMode();
// OTLP gRPC services.
_app.MapGrpcService<OtlpMetricsService>();
_app.MapGrpcService<OtlpTraceService>();
_app.MapGrpcService<OtlpLogsService>();
}
private static Uri[] GetAddressUris(string variableName, string defaultValue)
{
var urls = Environment.GetEnvironmentVariable(variableName) ?? defaultValue;
try
{
return urls.Split(';').Select(url => new Uri(url)).ToArray();
}
catch (Exception ex)
{
throw new InvalidOperationException($"Error parsing URIs from environment variable '{variableName}'.", ex);
}
}
private static void ConfigureListenAddresses(KestrelServerOptions kestrelOptions, Uri[] uris, HttpProtocols? httpProtocols = null)
{
foreach (var uri in uris)
{
if (uri.IsLoopback)
{
kestrelOptions.ListenLocalhost(uri.Port, options =>
{
ConfigureListenOptions(options, uri, httpProtocols);
});
}
else
{
kestrelOptions.Listen(IPAddress.Parse(uri.Host), uri.Port, options =>
{
ConfigureListenOptions(options, uri, httpProtocols);
});
}
}
static void ConfigureListenOptions(ListenOptions options, Uri uri, HttpProtocols? httpProtocols)
{
if (IsHttps(uri))
{
options.UseHttps();
}
if (httpProtocols is not null)
{
options.Protocols = httpProtocols.Value;
}
}
}
private static bool IsHttps(Uri uri) => string.Equals(uri.Scheme, "https", StringComparison.Ordinal);
public async Task StartAsync(CancellationToken cancellationToken)
{
await _app.StartAsync(cancellationToken).ConfigureAwait(false);
}
public async Task StopAsync(CancellationToken cancellationToken)
{
await _app.StopAsync(cancellationToken).ConfigureAwait(false);
}
}