| File: Telemetry\DashboardStartupTelemetry.cs | Web Access |
| Project: src\src\Aspire.Dashboard\Aspire.Dashboard.csproj (Aspire.Dashboard) |
// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.Diagnostics; using Aspire.Dashboard.Configuration; using Aspire.Shared; namespace Aspire.Dashboard.Telemetry; internal sealed class DashboardStartupTelemetry { private readonly DashboardTelemetryService _telemetry; private readonly string _launchContext; private readonly string _persistenceMode; private readonly long _startTimestamp; private int _recorded; public DashboardStartupTelemetry(DashboardTelemetryService telemetry, DashboardOptions options, long startTimestamp) { _telemetry = telemetry; _startTimestamp = startTimestamp; // Failed startup can supply unvalidated options. Parse errors leave the enum at its // previous value, so check the error before mapping the enum to a bounded telemetry value. _persistenceMode = options.Data switch { { PersistenceModeParseError: not null } => "unknown", { PersistenceMode: DashboardPersistenceMode.None } => "none", { PersistenceMode: DashboardPersistenceMode.Run } => "run", { PersistenceMode: DashboardPersistenceMode.Resume } => "resume", _ => "unknown" }; _launchContext = options.LaunchContext?.Trim().ToLowerInvariant() switch { KnownDashboardLaunchContexts.AppHost => KnownDashboardLaunchContexts.AppHost, KnownDashboardLaunchContexts.Cli => KnownDashboardLaunchContexts.Cli, KnownDashboardLaunchContexts.Container => KnownDashboardLaunchContexts.Container, _ => KnownDashboardLaunchContexts.Unknown }; } public void RecordSuccess() => Record(success: true, errorType: null); public void RecordFailure(string errorType) => Record(success: false, errorType); private void Record(bool success, string? errorType) { if (Interlocked.Exchange(ref _recorded, 1) != 0) { return; } Dictionary<string, AspireTelemetryProperty> properties = new() { [TelemetryPropertyKeys.StartupSuccess] = new(success), [TelemetryPropertyKeys.StartupLaunchContext] = new(_launchContext), [TelemetryPropertyKeys.StartupPersistenceMode] = new(_persistenceMode), [TelemetryPropertyKeys.StartupDurationMilliseconds] = new( Stopwatch.GetElapsedTime(_startTimestamp).TotalMilliseconds, AspireTelemetryPropertyType.Metric) }; if (errorType is not null) { properties[TelemetryPropertyKeys.ErrorType] = new(errorType); } _telemetry.RecordEvent(TelemetryEventKeys.Startup, properties); } }