-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix telemetry allocation regression: per-engine collector ownership #13516
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JanProvaznik
merged 4 commits into
dotnet:main
from
JanProvaznik:fix/telemetry-allocation-regression
Apr 13, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
825626b
Move telemetry data ownership to per-engine ITelemetryForwarder
JanProvaznik b134b5c
Rename TelemetryForwarder to TelemetryCollector
JanProvaznik 584611d
Update doc comments to reference BuildRequestEngine explicitly
JanProvaznik 1fe0392
Fix naming issues: PascalCase for class/property, restore BOMs
JanProvaznik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using Microsoft.Build.BackEnd.Logging; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Framework.Telemetry; | ||
|
|
||
| namespace Microsoft.Build.TelemetryInfra; | ||
|
|
||
| /// <summary> | ||
| /// Collects task and target telemetry for a single BuildRequestEngine's lifetime. | ||
| /// Not thread-safe: only one RequestBuilder is active at a time per | ||
| /// BuildRequestEngine, so <see cref="AddTarget"/> and <see cref="AddTask"/> | ||
| /// are always called from a single thread. | ||
| /// Created per BuildRequestEngine by <see cref="TelemetryCollectorProvider.CreateCollector"/>. | ||
| /// </summary> | ||
| internal interface ITelemetryCollector | ||
| { | ||
| bool IsTelemetryCollected { get; } | ||
|
|
||
| void AddTarget(TaskOrTargetTelemetryKey key, bool wasExecuted, TargetSkipReason skipReason = TargetSkipReason.None); | ||
|
|
||
| void AddTask( | ||
| TaskOrTargetTelemetryKey key, | ||
| TimeSpan cumulativeExecutionTime, | ||
| int executionsCount, | ||
| long totalMemoryConsumed, | ||
| string? taskFactoryName, | ||
| string? taskHostRuntime); | ||
|
|
||
| /// <summary> | ||
| /// Sends accumulated telemetry as a <see cref="WorkerNodeTelemetryEventArgs"/> and resets for the next build. | ||
| /// </summary> | ||
| void FinalizeProcessing(LoggingContext loggingContext); | ||
| } |
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,93 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using System; | ||
| using Microsoft.Build.BackEnd; | ||
| using Microsoft.Build.BackEnd.Logging; | ||
| using Microsoft.Build.Framework; | ||
| using Microsoft.Build.Framework.Telemetry; | ||
| using Microsoft.Build.Shared; | ||
|
|
||
| namespace Microsoft.Build.TelemetryInfra; | ||
|
|
||
| /// <summary> | ||
| /// Build component that creates per-BuildRequestEngine <see cref="ITelemetryCollector"/> instances. | ||
| /// Registered as a singleton, but holds no mutable state — each BuildRequestEngine gets its own | ||
| /// collector via <see cref="CreateCollector"/>. | ||
| /// </summary> | ||
|
JanProvaznik marked this conversation as resolved.
|
||
| internal class TelemetryCollectorProvider : IBuildComponent | ||
| { | ||
| private bool _telemetryEnabled; | ||
|
|
||
| /// <summary> | ||
| /// Creates a new <see cref="ITelemetryCollector"/> scoped to one <see cref="BuildRequestEngine"/>'s build lifetime. | ||
| /// Returns a no-op collector when telemetry is disabled. | ||
| /// </summary> | ||
| internal ITelemetryCollector CreateCollector() | ||
| => _telemetryEnabled ? new TelemetryCollector() : NullTelemetryCollector.Instance; | ||
|
|
||
|
JanProvaznik marked this conversation as resolved.
|
||
| internal static IBuildComponent CreateComponent(BuildComponentType type) | ||
| { | ||
| ErrorUtilities.VerifyThrow(type == BuildComponentType.TelemetryCollector, "Cannot create components of type {0}", type); | ||
| return new TelemetryCollectorProvider(); | ||
| } | ||
|
|
||
| public void InitializeComponent(IBuildComponentHost host) | ||
| { | ||
| ErrorUtilities.VerifyThrow(host != null, "BuildComponentHost was null"); | ||
| _telemetryEnabled = host!.BuildParameters.IsTelemetryEnabled; | ||
| } | ||
|
|
||
| public void ShutdownComponent() | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Collects task/target telemetry for one BuildRequestEngine. Not thread-safe — | ||
| /// only one RequestBuilder is active at a time per BuildRequestEngine. | ||
| /// </summary> | ||
| internal class TelemetryCollector : ITelemetryCollector | ||
| { | ||
| private WorkerNodeTelemetryData _data = new(); | ||
|
|
||
| public bool IsTelemetryCollected => true; | ||
|
|
||
| public void AddTarget(TaskOrTargetTelemetryKey key, bool wasExecuted, TargetSkipReason skipReason = TargetSkipReason.None) | ||
| { | ||
| _data.AddTarget(key, wasExecuted, skipReason); | ||
| } | ||
|
|
||
| public void AddTask(TaskOrTargetTelemetryKey key, TimeSpan cumulativeExecutionTime, int executionsCount, long totalMemoryConsumed, string? taskFactoryName, string? taskHostRuntime) | ||
| { | ||
| _data.AddTask(key, cumulativeExecutionTime, executionsCount, totalMemoryConsumed, taskFactoryName, taskHostRuntime); | ||
| } | ||
|
|
||
| public void FinalizeProcessing(LoggingContext loggingContext) | ||
| { | ||
| if (_data.IsEmpty) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| WorkerNodeTelemetryData snapshot = _data; | ||
| _data = new(); | ||
|
|
||
| WorkerNodeTelemetryEventArgs telemetryArgs = new(snapshot) | ||
| { BuildEventContext = loggingContext.BuildEventContext }; | ||
| loggingContext.LogBuildEvent(telemetryArgs); | ||
| } | ||
| } | ||
|
|
||
| internal class NullTelemetryCollector : ITelemetryCollector | ||
| { | ||
| internal static readonly NullTelemetryCollector Instance = new(); | ||
|
|
||
| public bool IsTelemetryCollected => false; | ||
|
|
||
| public void AddTarget(TaskOrTargetTelemetryKey key, bool wasExecuted, TargetSkipReason skipReason = TargetSkipReason.None) { } | ||
|
|
||
| public void AddTask(TaskOrTargetTelemetryKey key, TimeSpan cumulativeExecutionTime, int executionsCount, long totalMemoryConsumed, string? taskFactoryName, string? taskHostRuntime) { } | ||
|
|
||
| public void FinalizeProcessing(LoggingContext loggingContext) { } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.