Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions documentation/project-docs/telemetry.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
12 changes: 9 additions & 3 deletions src/Cli/dotnet/Telemetry/TelemetryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ namespace Microsoft.DotNet.Cli.Telemetry;

public class TelemetryClient : ITelemetryClient
{
private const int LocalTracePersistenceTimeoutMilliseconds = 100;

private static FrozenDictionary<string, string?> s_commonProperties = [];
private Task? _trackEventTask;

Expand Down Expand Up @@ -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;
Comment thread
nagilson marked this conversation as resolved.
Resolved
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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand Down
2 changes: 1 addition & 1 deletion test/dotnet.Tests/TelemetryTests/TelemetryClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
Loading