diff --git a/documentation/project-docs/telemetry.md b/documentation/project-docs/telemetry.md index e44a855ce010..0ab7216fa8bc 100644 --- a/documentation/project-docs/telemetry.md +++ b/documentation/project-docs/telemetry.md @@ -90,8 +90,10 @@ and delivery. [TelemetryClient](../../src/Cli/dotnet/Telemetry/TelemetryClient.c use a separate 300-ms trace allowance in each runtime; there is no cross-runtime deadline coordination. Normal exports before shutdown remain network-first; this is not synchronous persistence of every ended span, and abrupt termination can lose queued spans. An upload already in flight - can still delay shutdown. Successful local shutdown waits for the batch processor to finish rather than - applying the old 10-ms timeout to persistence. Failed-command persistence is subject to the 300-ms timeout. + can still delay shutdown. Successful local shutdown gives the trace batch processor 100 ms to finalize + persistence; the zero drain budget prevents waiting for the subsequent upload. This is long enough for a cold + exporter to complete its atomic file write without reintroducing an unbounded exit delay. Failed-command + persistence is subject to the 300-ms timeout. 2. **Subsequent invocations.** Azure schedules an eager background drain after a 50-ms startup delay; actual start time also depends on thread scheduling and storage work. The two-minute periodic retry timer remains. Accepted payloads are deleted; retryable failures remain for later attempts. Storage partitioning, retention, @@ -113,8 +115,8 @@ and delivery. [TelemetryClient](../../src/Cli/dotnet/Telemetry/TelemetryClient.c be persisted, and a timeout can leave telemetry undelivered. 4. **Persistent MSBuild hosts.** A logger finishes a build request with nonterminal `ForceFlush`, allowing the same provider to handle later builds. Locally the SDK enables - `Azure.Monitor.OpenTelemetry.Exporter.PersistOnForceFlush`; process exit uses `Shutdown`. CI uses a bounded - network-first flush. A force-flush result is not an ingestion acknowledgment. + `Azure.Monitor.OpenTelemetry.Exporter.PersistOnForceFlush` and gives trace persistence 100 ms; process exit uses + `Shutdown`. CI uses a bounded network-first flush. A force-flush result is not an ingestion acknowledgment. These Azure settings are process-wide. OTLP-enabled invocations retain bounded provider shutdown, and the trace and metric providers share the configured wait budget. This bounds provider waits, not all process-exit work. Azure's diff --git a/src/Cli/dotnet/Telemetry/TelemetryClient.cs b/src/Cli/dotnet/Telemetry/TelemetryClient.cs index 3faa17d8f0a4..32f80710a622 100644 --- a/src/Cli/dotnet/Telemetry/TelemetryClient.cs +++ b/src/Cli/dotnet/Telemetry/TelemetryClient.cs @@ -19,6 +19,8 @@ namespace Microsoft.DotNet.Cli.Telemetry; public class TelemetryClient : ITelemetryClient { + private const int LocalTracePersistenceTimeoutMilliseconds = 100; + private static FrozenDictionary s_commonProperties = []; private Task? _trackEventTask; @@ -280,17 +282,21 @@ internal static void FlushProviders(int exitCode) } else { - s_tracerProvider?.Shutdown(); + // This timeout includes batch serialization and the atomic storage write. Ten milliseconds can + // interrupt the write before its temporary file is finalized, leaving nothing a later run can drain. + s_tracerProvider?.Shutdown(LocalTracePersistenceTimeoutMilliseconds); s_metricsProvider?.Shutdown(timeoutMilliseconds: 10); } } internal static void ForceFlushProviders() { - int timeout = s_isCIEnvironment || s_enableOtlpExporter ? s_shutdownTimeoutMs : Timeout.Infinite; + int timeout = s_isCIEnvironment || s_enableOtlpExporter + ? s_shutdownTimeoutMs + : LocalTracePersistenceTimeoutMilliseconds; long started = Stopwatch.GetTimestamp(); s_tracerProvider?.ForceFlush(timeout); - int remaining = timeout == Timeout.Infinite ? 10 : Math.Max(0, timeout - (int)Stopwatch.GetElapsedTime(started).TotalMilliseconds); + int remaining = Math.Max(0, timeout - (int)Stopwatch.GetElapsedTime(started).TotalMilliseconds); s_metricsProvider?.ForceFlush(remaining); } diff --git a/test/Microsoft.DotNet.Cli.Telemetry.Tests/AzureExporterLifecycleTests.cs b/test/Microsoft.DotNet.Cli.Telemetry.Tests/AzureExporterLifecycleTests.cs index e2cdd9c7e554..4ac4a4b7f2c9 100644 --- a/test/Microsoft.DotNet.Cli.Telemetry.Tests/AzureExporterLifecycleTests.cs +++ b/test/Microsoft.DotNet.Cli.Telemetry.Tests/AzureExporterLifecycleTests.cs @@ -41,6 +41,38 @@ public void LocalShutdownPersistsQueuedActivitiesBeforeReturning(int activityCou .Should().BeEquivalentTo(Enumerable.Range(0, activityCount).Select(index => $"session-{index}")); } + [TestMethod] + public void LocalShutdownPersistsQueuedActivitiesWithinCliBudget() + { + using var settings = new ExporterSettingsScope(ci: false); + using var scope = new AzureExporterTestScope(); + using var handler = new RecordingHandler(); + using var provider = scope.CreateProvider(handler); + using var activity = scope.Emit("cli-budget"); + + provider.Shutdown(100).Should().BeTrue(); + + handler.Payloads.Should().BeEmpty(); + scope.StoredFiles().Select(File.ReadAllText) + .Should().Contain(payload => payload.Contains("cli-budget", StringComparison.Ordinal)); + } + + [TestMethod] + public void LocalForceFlushPersistsQueuedActivitiesWithinCliBudget() + { + using var settings = new ExporterSettingsScope(ci: false); + using var scope = new AzureExporterTestScope(); + using var handler = new RecordingHandler(); + using var provider = scope.CreateProvider(handler); + using var activity = scope.Emit("cli-force-flush-budget"); + + provider.ForceFlush(100).Should().BeTrue(); + + handler.Payloads.Should().BeEmpty(); + scope.StoredFiles().Select(File.ReadAllText) + .Should().Contain(payload => payload.Contains("cli-force-flush-budget", StringComparison.Ordinal)); + } + [TestMethod] public async Task LocalShutdownDoesNotWaitForItsBackgroundUpload() { diff --git a/test/dotnet.Tests/TelemetryTests/TelemetryClientTests.cs b/test/dotnet.Tests/TelemetryTests/TelemetryClientTests.cs index fe4ec0be9db5..cdaa9885a725 100644 --- a/test/dotnet.Tests/TelemetryTests/TelemetryClientTests.cs +++ b/test/dotnet.Tests/TelemetryTests/TelemetryClientTests.cs @@ -46,7 +46,7 @@ public void ShutdownUsesExitCodeAndRestoresDrainSetting(int exitCode, bool throw AppContext.SetData(drainBudgetName, 17); Action shutdown = () => TelemetryClient.FlushProviders(exitCode); shutdown.Should().NotThrow(); - processor.Timeout.Should().Be(boundedExport ? boundedTimeout : exitCode == 0 ? Timeout.Infinite : 300); + processor.Timeout.Should().Be(boundedExport ? boundedTimeout : exitCode == 0 ? 100 : 300); processor.DrainBudget.Should().Be(boundedExport || exitCode == 0 ? 17 : 300); AppContext.GetData(drainBudgetName).Should().Be(17); }