Skip to content

Commit 2ff2429

Browse files
Copilotbaronfel
andcommitted
Address PR feedback: refactor telemetry, fix defaults, use configured hasher
Co-authored-by: baronfel <[email protected]>
1 parent 4576fff commit 2ff2429

File tree

3 files changed

+162
-168
lines changed

3 files changed

+162
-168
lines changed

src/Cli/dotnet/Commands/Run/RunCommand.cs

Lines changed: 94 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -773,91 +773,114 @@ private void SendRunTelemetry(
773773
try
774774
{
775775
bool isFileBased = EntryPointFileFullPath is not null;
776-
string projectIdentifier;
777-
int sdkCount = 1; // Default assumption
778-
int packageReferenceCount = 0;
779-
int projectReferenceCount = 0;
780-
int additionalPropertiesCount = 0;
781-
bool? usedMSBuild = null;
782-
bool? usedRoslynCompiler = null;
783776

784777
if (isFileBased)
785778
{
786-
// File-based app telemetry
787-
projectIdentifier = RunTelemetry.GetFileBasedIdentifier(EntryPointFileFullPath!);
788-
789-
var virtualCommand = CreateVirtualCommand();
790-
var directives = virtualCommand.Directives;
791-
792-
sdkCount = RunTelemetry.CountSdks(directives: directives);
793-
packageReferenceCount = RunTelemetry.CountPackageReferences(directives: directives);
794-
projectReferenceCount = RunTelemetry.CountProjectReferences(directives: directives);
795-
additionalPropertiesCount = RunTelemetry.CountAdditionalProperties(directives);
796-
797-
// Determine if MSBuild or Roslyn compiler was used
798-
if (cachedRunProperties != null)
799-
{
800-
// If we have cached properties, we used the optimized path
801-
usedRoslynCompiler = projectFactory == null;
802-
usedMSBuild = projectFactory != null;
803-
}
804-
else if (ShouldBuild)
805-
{
806-
// Fresh build - check if we used MSBuild optimization
807-
usedMSBuild = !directives.IsDefaultOrEmpty || projectFactory != null;
808-
usedRoslynCompiler = directives.IsDefaultOrEmpty && projectFactory == null;
809-
}
779+
SendFileBasedTelemetry(launchSettings, projectFactory, cachedRunProperties);
810780
}
811781
else
812782
{
813-
// Project-based app telemetry
814-
projectIdentifier = RunTelemetry.GetProjectBasedIdentifier(ProjectFileFullPath!, GetRepositoryRoot());
815-
816-
// Try to get project information for telemetry
817-
// We need to evaluate the project to get accurate counts
818-
if (ShouldBuild)
819-
{
820-
// We built the project, so we can evaluate it for telemetry
821-
try
822-
{
823-
var globalProperties = MSBuildArgs.GlobalProperties?.ToDictionary() ?? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
824-
globalProperties[Constants.EnableDefaultItems] = "false";
825-
globalProperties[Constants.MSBuildExtensionsPath] = AppContext.BaseDirectory;
826-
827-
using var collection = new ProjectCollection(globalProperties: globalProperties);
828-
var project = collection.LoadProject(ProjectFileFullPath!).CreateProjectInstance();
829-
830-
sdkCount = RunTelemetry.CountSdks(project);
831-
packageReferenceCount = RunTelemetry.CountPackageReferences(project);
832-
projectReferenceCount = RunTelemetry.CountProjectReferences(project);
833-
}
834-
catch
835-
{
836-
// If project evaluation fails for telemetry, use defaults
837-
// We don't want telemetry collection to affect the run operation
838-
}
839-
}
783+
SendProjectBasedTelemetry(launchSettings);
840784
}
841-
842-
RunTelemetry.TrackRunEvent(
843-
isFileBased: isFileBased,
844-
launchProfile: LaunchProfile,
845-
noLaunchProfile: NoLaunchProfile,
846-
launchSettings: launchSettings,
847-
projectIdentifier: projectIdentifier,
848-
sdkCount: sdkCount,
849-
packageReferenceCount: packageReferenceCount,
850-
projectReferenceCount: projectReferenceCount,
851-
additionalPropertiesCount: additionalPropertiesCount,
852-
usedMSBuild: usedMSBuild,
853-
usedRoslynCompiler: usedRoslynCompiler);
854785
}
855786
catch
856787
{
857788
// Silently ignore telemetry errors to not affect the run operation
858789
}
859790
}
860791

792+
/// <summary>
793+
/// Builds and sends telemetry data for file-based app runs.
794+
/// </summary>
795+
private void SendFileBasedTelemetry(
796+
ProjectLaunchSettingsModel? launchSettings,
797+
Func<ProjectCollection, ProjectInstance>? projectFactory,
798+
RunProperties? cachedRunProperties)
799+
{
800+
var projectIdentifier = RunTelemetry.GetFileBasedIdentifier(EntryPointFileFullPath!, Sha256Hasher.Hash);
801+
802+
var virtualCommand = CreateVirtualCommand();
803+
var directives = virtualCommand.Directives;
804+
805+
var sdkCount = RunTelemetry.CountSdks(directives);
806+
var packageReferenceCount = RunTelemetry.CountPackageReferences(directives);
807+
var projectReferenceCount = RunTelemetry.CountProjectReferences(directives);
808+
var additionalPropertiesCount = RunTelemetry.CountAdditionalProperties(directives);
809+
810+
// Determine if MSBuild or Roslyn compiler was used
811+
bool? usedMSBuild = null;
812+
bool? usedRoslynCompiler = null;
813+
814+
if (cachedRunProperties != null)
815+
{
816+
// If we have cached properties, we used the optimized path
817+
usedRoslynCompiler = projectFactory == null;
818+
usedMSBuild = projectFactory != null;
819+
}
820+
else if (ShouldBuild)
821+
{
822+
// Fresh build - check if we used MSBuild optimization
823+
usedMSBuild = !directives.IsDefaultOrEmpty || projectFactory != null;
824+
usedRoslynCompiler = directives.IsDefaultOrEmpty && projectFactory == null;
825+
}
826+
827+
RunTelemetry.TrackRunEvent(
828+
isFileBased: true,
829+
projectIdentifier: projectIdentifier,
830+
launchProfile: LaunchProfile,
831+
noLaunchProfile: NoLaunchProfile,
832+
launchSettings: launchSettings,
833+
sdkCount: sdkCount,
834+
packageReferenceCount: packageReferenceCount,
835+
projectReferenceCount: projectReferenceCount,
836+
additionalPropertiesCount: additionalPropertiesCount,
837+
usedMSBuild: usedMSBuild,
838+
usedRoslynCompiler: usedRoslynCompiler);
839+
}
840+
841+
/// <summary>
842+
/// Builds and sends telemetry data for project-based app runs.
843+
/// </summary>
844+
private void SendProjectBasedTelemetry(ProjectLaunchSettingsModel? launchSettings)
845+
{
846+
var projectIdentifier = RunTelemetry.GetProjectBasedIdentifier(ProjectFileFullPath!, GetRepositoryRoot(), Sha256Hasher.Hash);
847+
848+
// Get package and project reference counts for project-based apps
849+
int packageReferenceCount = 0;
850+
int projectReferenceCount = 0;
851+
852+
// Try to get project information for telemetry if we built the project
853+
if (ShouldBuild)
854+
{
855+
try
856+
{
857+
var globalProperties = MSBuildArgs.GlobalProperties?.ToDictionary() ?? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
858+
globalProperties[Constants.EnableDefaultItems] = "false";
859+
globalProperties[Constants.MSBuildExtensionsPath] = AppContext.BaseDirectory;
860+
861+
using var collection = new ProjectCollection(globalProperties: globalProperties);
862+
var project = collection.LoadProject(ProjectFileFullPath!).CreateProjectInstance();
863+
864+
packageReferenceCount = RunTelemetry.CountPackageReferences(project);
865+
projectReferenceCount = RunTelemetry.CountProjectReferences(project);
866+
}
867+
catch
868+
{
869+
// If project evaluation fails for telemetry, use defaults
870+
// We don't want telemetry collection to affect the run operation
871+
}
872+
}
873+
874+
RunTelemetry.TrackRunEvent(
875+
isFileBased: false,
876+
projectIdentifier: projectIdentifier,
877+
launchProfile: LaunchProfile,
878+
noLaunchProfile: NoLaunchProfile,
879+
launchSettings: launchSettings,
880+
packageReferenceCount: packageReferenceCount,
881+
projectReferenceCount: projectReferenceCount);
882+
}
883+
861884
/// <summary>
862885
/// Attempts to find the repository root directory.
863886
/// </summary>

0 commit comments

Comments
 (0)