|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using System.Diagnostics.CodeAnalysis; |
| 6 | +using System.Security.Cryptography.X509Certificates; |
| 7 | + |
| 8 | +namespace Aspire.Dashboard.Configuration; |
| 9 | + |
| 10 | +public sealed class DashboardOptions |
| 11 | +{ |
| 12 | + public string? ApplicationName { get; set; } |
| 13 | + public OtlpOptions Otlp { get; set; } = new OtlpOptions(); |
| 14 | + public FrontendOptions Frontend { get; set; } = new FrontendOptions(); |
| 15 | + public ResourceServiceClientOptions ResourceServiceClient { get; set; } = new ResourceServiceClientOptions(); |
| 16 | + public TelemetryLimitOptions TelemetryLimits { get; set; } = new TelemetryLimitOptions(); |
| 17 | +} |
| 18 | + |
| 19 | +public sealed class ResourceServiceClientOptions |
| 20 | +{ |
| 21 | + private Uri? _parsedUrl; |
| 22 | + |
| 23 | + public string? Url { get; set; } |
| 24 | + public ResourceClientAuthMode? AuthMode { get; set; } |
| 25 | + public ResourceServiceClientCertificateOptions ClientCertificates { get; set; } = new ResourceServiceClientCertificateOptions(); |
| 26 | + |
| 27 | + public Uri? GetUri() => _parsedUrl; |
| 28 | + |
| 29 | + internal bool TryParseOptions([NotNullWhen(false)] out string? errorMessage) |
| 30 | + { |
| 31 | + if (!string.IsNullOrEmpty(Url)) |
| 32 | + { |
| 33 | + if (!Uri.TryCreate(Url, UriKind.Absolute, out _parsedUrl)) |
| 34 | + { |
| 35 | + errorMessage = $"Failed to parse resource service client endpoint URL '{Url}'."; |
| 36 | + return false; |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + errorMessage = null; |
| 41 | + return true; |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +public sealed class ResourceServiceClientCertificateOptions |
| 46 | +{ |
| 47 | + public DashboardClientCertificateSource? Source { get; set; } |
| 48 | + public string? FilePath { get; set; } |
| 49 | + public string? Password { get; set; } |
| 50 | + public string? Subject { get; set; } |
| 51 | + public string? Store { get; set; } |
| 52 | + public StoreLocation? Location { get; set; } |
| 53 | +} |
| 54 | + |
| 55 | +public sealed class OtlpOptions |
| 56 | +{ |
| 57 | + private Uri? _parsedEndpointUrl; |
| 58 | + |
| 59 | + public string? PrimaryApiKey { get; set; } |
| 60 | + public string? SecondaryApiKey { get; set; } |
| 61 | + public OtlpAuthMode? AuthMode { get; set; } |
| 62 | + public string? EndpointUrl { get; set; } |
| 63 | + |
| 64 | + public Uri GetEndpointUri() |
| 65 | + { |
| 66 | + Debug.Assert(_parsedEndpointUrl is not null, "Should have been parsed during validation."); |
| 67 | + return _parsedEndpointUrl; |
| 68 | + } |
| 69 | + |
| 70 | + internal bool TryParseOptions([NotNullWhen(false)] out string? errorMessage) |
| 71 | + { |
| 72 | + if (string.IsNullOrEmpty(EndpointUrl)) |
| 73 | + { |
| 74 | + errorMessage = "OTLP endpoint URL is not configured. Specify a Dashboard:Otlp:EndpointUrl value."; |
| 75 | + return false; |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + if (!Uri.TryCreate(EndpointUrl, UriKind.Absolute, out _parsedEndpointUrl)) |
| 80 | + { |
| 81 | + errorMessage = $"Failed to parse OTLP endpoint URL '{EndpointUrl}'."; |
| 82 | + return false; |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + errorMessage = null; |
| 87 | + return true; |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +public sealed class FrontendOptions |
| 92 | +{ |
| 93 | + private List<Uri>? _parsedEndpointUrls; |
| 94 | + |
| 95 | + public string? EndpointUrls { get; set; } |
| 96 | + public FrontendAuthMode? AuthMode { get; set; } |
| 97 | + |
| 98 | + public IReadOnlyList<Uri> GetEndpointUris() |
| 99 | + { |
| 100 | + Debug.Assert(_parsedEndpointUrls is not null, "Should have been parsed during validation."); |
| 101 | + return _parsedEndpointUrls; |
| 102 | + } |
| 103 | + |
| 104 | + internal bool TryParseOptions([NotNullWhen(false)] out string? errorMessage) |
| 105 | + { |
| 106 | + if (string.IsNullOrEmpty(EndpointUrls)) |
| 107 | + { |
| 108 | + errorMessage = "One or more frontend endpoint URLs are not configured. Specify a Dashboard:Frontend:EndpointUrls value."; |
| 109 | + return false; |
| 110 | + } |
| 111 | + else |
| 112 | + { |
| 113 | + var parts = EndpointUrls.Split(';', StringSplitOptions.RemoveEmptyEntries | StringSplitOptions.TrimEntries); |
| 114 | + var uris = new List<Uri>(parts.Length); |
| 115 | + foreach (var part in parts) |
| 116 | + { |
| 117 | + if (!Uri.TryCreate(part, UriKind.Absolute, out var uri)) |
| 118 | + { |
| 119 | + errorMessage = $"Failed to parse frontend endpoint URLs '{EndpointUrls}'."; |
| 120 | + return false; |
| 121 | + } |
| 122 | + |
| 123 | + uris.Add(uri); |
| 124 | + } |
| 125 | + _parsedEndpointUrls = uris; |
| 126 | + } |
| 127 | + |
| 128 | + errorMessage = null; |
| 129 | + return true; |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +public sealed class TelemetryLimitOptions |
| 134 | +{ |
| 135 | + public int MaxLogCount { get; set; } = 10_000; |
| 136 | + public int MaxTraceCount { get; set; } = 10_000; |
| 137 | + public int MaxMetricsCount { get; set; } = 50_000; // Allows for 1 metric point per second for over 12 hours. |
| 138 | + public int MaxAttributeCount { get; set; } = 128; |
| 139 | + public int MaxAttributeLength { get; set; } = int.MaxValue; |
| 140 | + public int MaxSpanEventCount { get; set; } = int.MaxValue; |
| 141 | +} |
0 commit comments