Skip to content

Commit f342f22

Browse files
committed
add traces to dotnet new instantiation
1 parent af320e7 commit f342f22

16 files changed

+66
-63
lines changed

Directory.Packages.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,4 @@
161161
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="$(MicrosoftBuildMinimumVersion)" />
162162
<PackageVersion Include="Microsoft.NET.StringTools" Version="$(MicrosoftBuildMinimumVersion)" />
163163
</ItemGroup>
164-
</Project>
164+
</Project>

src/Cli/Microsoft.DotNet.Cli.Utils/Activities.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,17 @@ public static class Activities
1717
/// consumers to easily filter and trace CLI activities.
1818
/// </summary>
1919
public static ActivitySource Source { get; } = new("dotnet-cli", Product.Version);
20+
21+
/// <summary>
22+
/// The environment variable used to transfer the chain of parent activity IDs.
23+
/// This should be used when constructing new sub-processes in order to
24+
/// track spans across calls.
25+
/// </summary>
26+
public const string DOTNET_CLI_TRACEPARENT = nameof(DOTNET_CLI_TRACEPARENT);
27+
/// <summary>
28+
/// The environment variable used to transfer the trace state of the parent activities.
29+
/// This should be used when constructing new sub-processes in order to
30+
/// track spans across calls.
31+
/// </summary>
32+
public const string DOTNET_CLI_TRACESTATE = nameof(DOTNET_CLI_TRACESTATE);
2033
}

src/Cli/Microsoft.DotNet.Cli.Utils/MSBuildForwardingAppWithoutLogging.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private static string GetMSBuildExePath()
199199
MSBuildExeName);
200200
}
201201

202-
private static string GetMSBuildSDKsPath()
202+
public static string GetMSBuildSDKsPath()
203203
{
204204
var envMSBuildSDKsPath = Environment.GetEnvironmentVariable("MSBuildSDKsPath");
205205

src/Cli/Microsoft.TemplateEngine.Cli/Commands/create/InstantiateCommand.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ internal static async Task<IEnumerable<TemplateGroup>> GetTemplateGroupsAsync(
7474
HostSpecificDataLoader hostSpecificDataLoader,
7575
CancellationToken cancellationToken)
7676
{
77+
using var createTemplateGroupsActivity = Activities.Source.StartActivity("create-template-groups");
7778
IReadOnlyList<ITemplateInfo> templates = await templatePackageManager.GetTemplatesAsync(cancellationToken).ConfigureAwait(false);
7879
return TemplateGroup.FromTemplateList(CliTemplateInfo.FromTemplateInfo(templates, hostSpecificDataLoader));
7980
}
@@ -84,6 +85,7 @@ internal static HashSet<TemplateCommand> GetTemplateCommand(
8485
TemplatePackageManager templatePackageManager,
8586
TemplateGroup templateGroup)
8687
{
88+
using var getTemplateActivity = Activities.Source.StartActivity("get-template-command");
8789
//groups templates in the group by precedence
8890
foreach (IGrouping<int, CliTemplateInfo> templateGrouping in templateGroup.Templates.GroupBy(g => g.Precedence).OrderByDescending(g => g.Key))
8991
{
@@ -204,6 +206,8 @@ private static async Task<NewCommandStatus> ExecuteIntAsync(
204206

205207
return await templateListCoordinator.DisplayCommandDescriptionAsync(instantiateArgs, cancellationToken).ConfigureAwait(false);
206208
}
209+
using var createActivity = Activities.Source.StartActivity("instantiate-command");
210+
createActivity?.DisplayName = $"Invoke '{instantiateArgs.ShortName}'";
207211

208212
IEnumerable<TemplateGroup> allTemplateGroups = await GetTemplateGroupsAsync(
209213
templatePackageManager,
@@ -273,10 +277,11 @@ private static async Task<NewCommandStatus> HandleTemplateInstantiationAsync(
273277
{
274278
TemplateCommand templateCommandToRun = candidates.Single();
275279
args.Command.Subcommands.Add(templateCommandToRun);
276-
280+
var templateParseActivity = Activities.Source.StartActivity("reparse-for-template");
277281
ParseResult updatedParseResult = args.ParseResult.RootCommandResult.Command.Parse(
278282
args.ParseResult.Tokens.Select(t => t.Value).ToArray(),
279283
args.ParseResult.Configuration);
284+
templateParseActivity?.Stop();
280285
return await candidates.Single().InvokeAsync(updatedParseResult, cancellationToken).ConfigureAwait(false);
281286
}
282287
else if (candidates.Any())

src/Cli/Microsoft.TemplateEngine.Cli/Commands/create/TemplateCommand.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,19 +146,23 @@ internal static async Task<IReadOnlyList<TemplateConstraintResult>> ValidateCons
146146

147147
internal async Task<NewCommandStatus> InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken)
148148
{
149+
using var templateInvocationActivity = Activities.Source.StartActivity("invoke-template");
149150
TemplateCommandArgs args = new(this, _instantiateCommand, parseResult);
150151
TemplateInvoker invoker = new(_environmentSettings, () => Console.ReadLine() ?? string.Empty);
151152
TemplatePackageCoordinator packageCoordinator = new(_environmentSettings, _templatePackageManager);
152-
TemplateConstraintManager constraintManager = new(_environmentSettings);
153+
using TemplateConstraintManager constraintManager = new(_environmentSettings);
153154
TemplatePackageDisplay templatePackageDisplay = new(Reporter.Output, Reporter.Error);
154155

155156
CancellationTokenSource cancellationTokenSource = new();
156157
cancellationTokenSource.CancelAfter(ConstraintEvaluationTimeout);
157158

159+
#pragma warning disable CA2025 // Do not pass 'IDisposable' instances into unawaited tasks
158160
Task<IReadOnlyList<TemplateConstraintResult>> constraintsEvaluation = ValidateConstraintsAsync(constraintManager, args.Template, args.IsForceFlagSpecified ? cancellationTokenSource.Token : cancellationToken);
161+
#pragma warning restore CA2025 // Do not pass 'IDisposable' instances into unawaited tasks
159162

160163
if (!args.IsForceFlagSpecified)
161164
{
165+
using var constraintResultsActivity = Activities.Source.StartActivity("validate-constraints");
162166
var constraintResults = await constraintsEvaluation.ConfigureAwait(false);
163167
if (constraintResults.Any())
164168
{

src/Cli/Microsoft.TemplateEngine.Cli/TemplateInvoker.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ internal TemplateInvoker(
3737

3838
internal async Task<NewCommandStatus> InvokeTemplateAsync(TemplateCommandArgs templateArgs, CancellationToken cancellationToken)
3939
{
40+
using var invokerActivity = Activities.Source.StartActivity("invoker-invoking");
4041
cancellationToken.ThrowIfCancellationRequested();
4142

4243
CliTemplateInfo templateToRun = templateArgs.Template;
@@ -158,6 +159,7 @@ private async Task<NewCommandStatus> CreateTemplateAsync(TemplateCommandArgs tem
158159

159160
try
160161
{
162+
using var templateCreationActivity = Activities.Source.StartActivity("actual-instantiate-template");
161163
instantiateResult = await _templateCreator.InstantiateAsync(
162164
templateArgs.Template,
163165
templateArgs.Name,
@@ -306,6 +308,7 @@ private async Task<NewCommandStatus> CreateTemplateAsync(TemplateCommandArgs tem
306308

307309
private NewCommandStatus HandlePostActions(ITemplateCreationResult creationResult, TemplateCommandArgs args)
308310
{
311+
using var postActionActivity = Activities.Source.StartActivity("post-actions");
309312
PostActionExecutionStatus result = _postActionDispatcher.Process(creationResult, args.IsDryRun, args.AllowScripts ?? AllowRunScripts.Prompt);
310313

311314
return result switch

src/Cli/Microsoft.TemplateEngine.Cli/TemplateListCoordinator.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ internal TemplateListCoordinator(
3030
_templatePackageManager = templatePackageManager ?? throw new ArgumentNullException(nameof(templatePackageManager));
3131
_hostSpecificDataLoader = hostSpecificDataLoader ?? throw new ArgumentNullException(nameof(hostSpecificDataLoader));
3232
_defaultLanguage = engineEnvironmentSettings.GetDefaultLanguage();
33+
using var constraintManagerActivity = Activities.Source.StartActivity("create-constraints");
3334
_constraintManager = new TemplateConstraintManager(_engineEnvironmentSettings);
3435
}
3536

src/Cli/dotnet/Activities.cs

Lines changed: 0 additions & 13 deletions
This file was deleted.

src/Cli/dotnet/Commands/New/BuiltInTemplatePackageProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#nullable disable
55

6+
using Microsoft.DotNet.Cli.Utils;
67
using Microsoft.TemplateEngine.Abstractions;
78
using Microsoft.TemplateEngine.Abstractions.TemplatePackage;
89
using NuGet.Versioning;
@@ -44,7 +45,7 @@ private static IEnumerable<string> GetTemplateFolders(IEngineEnvironmentSettings
4445
{
4546
var templateFoldersToInstall = new List<string>();
4647

47-
var sdksDirectory = new DirectoryInfo(environmentSettings.Environment.GetEnvironmentVariable("MSBuildSDKsPath"));
48+
var sdksDirectory = new DirectoryInfo(MSBuildForwardingAppWithoutLogging.GetMSBuildSDKsPath());
4849
var sdkDirectory = sdksDirectory.Parent!;
4950
var sdkVersion = sdkDirectory.Name;
5051
var dotnetRootPath = sdkDirectory.Parent!.Parent!;

src/Cli/dotnet/Commands/New/OptionalWorkloadProvider.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
#nullable disable
55

6+
using Microsoft.DotNet.Cli.Commands.MSBuild;
67
using Microsoft.DotNet.Cli.Utils;
78
using Microsoft.DotNet.Configurer;
89
using Microsoft.TemplateEngine.Abstractions;
@@ -33,7 +34,7 @@ public Task<IReadOnlyList<ITemplatePackage>> GetAllTemplatePackagesAsync(Cancell
3334
{
3435
var list = new List<TemplatePackage>();
3536
var optionalWorkloadLocator = new TemplateLocator.TemplateLocator();
36-
var sdksDirectory = new DirectoryInfo(_environmentSettings.Environment.GetEnvironmentVariable("MSBuildSDKsPath"));
37+
var sdksDirectory = new DirectoryInfo(MSBuildForwardingAppWithoutLogging.GetMSBuildSDKsPath());
3738
var sdkDirectory = sdksDirectory.Parent!;
3839
var sdkVersion = sdkDirectory.Name;
3940
var dotnetRootPath = sdkDirectory.Parent!.Parent!;

0 commit comments

Comments
 (0)