Skip to content

Commit

Permalink
Add error code to DTB error telemetry
Browse files Browse the repository at this point in the history
  • Loading branch information
drewnoakes committed Dec 10, 2024
1 parent 2790bb5 commit 6354040
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,24 +60,22 @@ private sealed class DesignTimeTelemetryLogger(ITelemetryService telemetryServic
private readonly Dictionary<int, TargetRecord> _targetRecordById = new(capacity: 140);

/// <summary>
/// The names of any targets that reported errors. Names may be hashed.
/// May be empty even when errors exist, as not all errors come from a target.
/// Data about any errors that occurred during the build. Note that design-time build
/// targets are supposed to be authored to respect ContinueOnError, so errors might
/// not fail the build. However it's still useful to know which targets reported errors.
///
/// Similarly, this collection may be empty even when the build fails.
/// </summary>
/// <remarks>
/// Targets can run concurrently, so use of this collection must be protected by a lock.
/// </remarks>
private readonly List<string> _errorTargets = [];
private readonly List<ErrorData> _errors = [];

/// <summary>
/// Whether the build succeeded.
/// </summary>
private bool? _succeeded;

/// <summary>
/// The number of errors reported during the build.
/// </summary>
private int _errorCount;

LoggerVerbosity ILogger.Verbosity { get; set; }

string? ILogger.Parameters { get; set; }
Expand Down Expand Up @@ -115,14 +113,16 @@ void OnTargetFinished(object sender, TargetFinishedEventArgs e)

void OnErrorRaised(object sender, BuildErrorEventArgs e)
{
_errorCount++;
string? targetName = null;

if (TryGetTargetRecord(e.BuildEventContext, out TargetRecord? record))
{
lock (_errorTargets)
{
_errorTargets.Add(GetHashedTargetName(record));
}
targetName = GetHashedTargetName(record);
}

lock (_errors)
{
_errors.Add(new(targetName, e.Code));
}
}

Expand Down Expand Up @@ -155,7 +155,7 @@ void ILogger.Shutdown()
void SendTelemetry()
{
object[][] targetDurations;
string[] errorTargets;
ErrorData[] errors;

lock (_targetRecordById)
{
Expand All @@ -168,16 +168,16 @@ void SendTelemetry()
.Select(record => new object[] { GetHashedTargetName(record), record.Elapsed.Milliseconds })
.ToArray();

errorTargets = _errorTargets.ToArray();
errors = _errors.ToArray();
}

telemetryService.PostProperties(
TelemetryEventName.DesignTimeBuildComplete,
[
(TelemetryPropertyName.DesignTimeBuildComplete.Succeeded, _succeeded),
(TelemetryPropertyName.DesignTimeBuildComplete.Targets, new ComplexPropertyValue(targetDurations)),
(TelemetryPropertyName.DesignTimeBuildComplete.ErrorCount, _errorCount),
(TelemetryPropertyName.DesignTimeBuildComplete.ErrorTargets, errorTargets),
(TelemetryPropertyName.DesignTimeBuildComplete.ErrorCount, errors.Length),
(TelemetryPropertyName.DesignTimeBuildComplete.Errors, new ComplexPropertyValue(errors)),
]);
}

Expand Down Expand Up @@ -232,5 +232,12 @@ private sealed record class TargetRecord(string TargetName, bool IsMicrosoft, Da

public TimeSpan Elapsed => Ended - Started;
}

/// <summary>
/// Data about errors reported during design-time builds.
/// </summary>
/// <param name="TargetName">Names of the targets that reported the error. May be hashed. <see langword="null"/> if the target name could not be identified.</param>
/// <param name="ErrorCode">An error code that identifies the type of error.</param>
private sealed record class ErrorData(string? TargetName, string ErrorCode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,9 @@ public static class DesignTimeBuildComplete
public const string ErrorCount = "vs.projectsystem.managed.designtimebuildcomplete.errorcount";

/// <summary>
/// The names of targets that failed during the design-time build.
/// Data about errors that occur during design-time builds.
/// </summary>
public const string ErrorTargets = "vs.projectsystem.managed.designtimebuildcomplete.errortargets";
public const string Errors = "vs.projectsystem.managed.designtimebuildcomplete.errors";
}

public static class SDKVersion
Expand Down

0 comments on commit 6354040

Please sign in to comment.