Skip to content

OTel experimentation #49409

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

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft
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
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,10 @@ dotnet_diagnostic.IDE0200.severity = none
dotnet_diagnostic.IDE0240.severity = warning

# Additional rules for template engine source code

# Default severity for analyzer diagnostics with category 'StyleCop.CSharp.SpacingRules'
dotnet_analyzer_diagnostic.category-StyleCop.CSharp.SpacingRules.severity = none

[{src,test}/**{Microsoft.TemplateEngine.*,dotnet-new?*}/**.cs]
# Default analyzed API surface = 'public' (public APIs)
dotnet_code_quality.api_surface = public
Expand Down
8 changes: 6 additions & 2 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<NoWarn>$(NoWarn);NU1507</NoWarn>
</PropertyGroup>
<ItemGroup>
<PackageVersion Include="Azure.Monitor.OpenTelemetry.Exporter" Version="1.4.0" />
<PackageVersion Include="Basic.CompilerLog.Util" Version="0.9.9" />
<PackageVersion Include="AwesomeAssertions" Version="$(AwesomeAssertionsVersion)" />
<PackageVersion Include="BenchmarkDotNet" Version="$(BenchmarkDotNetPackageVersion)" />
Expand Down Expand Up @@ -95,6 +96,9 @@
<PackageVersion Include="NuGet.ProjectModel" Version="$(NuGetProjectModelPackageVersion)" />
<PackageVersion Include="NuGet.Protocol" Version="$(NuGetBuildTasksPackageVersion)" />
<PackageVersion Include="NuGet.Versioning" Version="$(NuGetVersioningPackageVersion)" />
<PackageVersion Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Http" Version="1.12.0" />
<PackageVersion Include="OpenTelemetry.Instrumentation.Runtime" Version="1.12.0" />
<PackageVersion Include="runtime.linux-musl-x64.Microsoft.NETCore.DotNetHostResolver" Version="$(MicrosoftNETCoreDotNetHostResolverPackageVersion)" />
<PackageVersion Include="runtime.linux-x64.Microsoft.NETCore.DotNetHostResolver" Version="$(MicrosoftNETCoreDotNetHostResolverPackageVersion)" />
<PackageVersion Include="runtime.osx-x64.Microsoft.NETCore.DotNetHostResolver" Version="$(MicrosoftNETCoreDotNetHostResolverPackageVersion)" />
Expand All @@ -108,6 +112,7 @@
<PackageVersion Include="System.Composition.Runtime" Version="$(SystemCompositionRuntimePackageVersion)" />
<PackageVersion Include="System.Composition.TypedParts" Version="$(SystemCompositionTypedPartsPackageVersion)" />
<PackageVersion Include="System.Configuration.ConfigurationManager" Version="$(SystemConfigurationConfigurationManagerPackageVersion)" />
<PackageVersion Include="System.Diagnostics.DiagnosticSource" Version="$(SystemDiagnosticsDiagnosticSourcePackageVersion)" />
<PackageVersion Include="System.Formats.Asn1" Version="$(SystemFormatsAsn1Version)" />
<PackageVersion Include="System.IO.Hashing" Version="$(SystemIOHashingPackageVersion)" />
<!-- System.Reflection.Metadata and System.Collections.Immutable cannot be pinned here because of hard dependencies within Roslyn on specific versions that have to work both here and in VS -->
Expand All @@ -126,7 +131,6 @@
<PackageVersion Include="Xunit.Combinatorial" Version="$(XunitCombinatorialVersion)" />
<PackageVersion Include="xunit.console" Version="$(XUnitVersion)" />
</ItemGroup>

<!-- Use different versions of Microsoft.Build.* depending on whether the output will be used in
.NET Framework (VS) or only in the .NET SDK.

Expand Down Expand Up @@ -158,4 +162,4 @@
<PackageVersion Include="Microsoft.Build.Utilities.Core" Version="$(MicrosoftBuildMinimumVersion)" />
<PackageVersion Include="Microsoft.NET.StringTools" Version="$(MicrosoftBuildMinimumVersion)" />
</ItemGroup>
</Project>
</Project>
1 change: 1 addition & 0 deletions eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<SystemCompositionHostingPackageVersion>10.0.0-preview.7.25359.101</SystemCompositionHostingPackageVersion>
<SystemCompositionRuntimePackageVersion>10.0.0-preview.7.25359.101</SystemCompositionRuntimePackageVersion>
<SystemCompositionTypedPartsPackageVersion>10.0.0-preview.7.25359.101</SystemCompositionTypedPartsPackageVersion>
<SystemDiagnosticsDiagnosticSourcePackageVersion>10.0.0-preview.7.25359.101</SystemDiagnosticsDiagnosticSourcePackageVersion>
<SystemConfigurationConfigurationManagerPackageVersion>10.0.0-preview.7.25359.101</SystemConfigurationConfigurationManagerPackageVersion>
<SystemReflectionMetadataLoadContextVersion>10.0.0-preview.7.25359.101</SystemReflectionMetadataLoadContextVersion>
<SystemResourcesExtensionsPackageVersion>10.0.0-preview.7.25359.101</SystemResourcesExtensionsPackageVersion>
Expand Down
32 changes: 32 additions & 0 deletions src/Cli/Microsoft.DotNet.Cli.Utils/Activities.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;

namespace Microsoft.DotNet.Cli.Utils;

/// <summary>
/// Contains helpers for working with <see cref="System.Diagnostics.Activity">Activities</see> in the .NET CLI.
/// </summary>
public static class Activities
{
/// <summary>
/// The main entrypoint for creating <see cref="Activity">Activities</see> in the .NET CLI.
/// All activities created in the CLI should use this <see cref="ActivitySource"/>, to allow
/// consumers to easily filter and trace CLI activities.
/// </summary>
public static ActivitySource Source { get; } = new("dotnet-cli", Product.Version);

/// <summary>
/// The environment variable used to transfer the chain of parent activity IDs.
/// This should be used when constructing new sub-processes in order to
/// track spans across calls.
/// </summary>
public const string DOTNET_CLI_TRACEPARENT = nameof(DOTNET_CLI_TRACEPARENT);
/// <summary>
/// The environment variable used to transfer the trace state of the parent activities.
/// This should be used when constructing new sub-processes in order to
/// track spans across calls.
/// </summary>
public const string DOTNET_CLI_TRACESTATE = nameof(DOTNET_CLI_TRACESTATE);
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ internal class ForwardingAppImplementation
private readonly string? _depsFile;
private readonly string? _runtimeConfig;
private readonly string? _additionalProbingPath;
private Dictionary<string, string> _environmentVariables;
private Dictionary<string, string?> _environmentVariables;

private readonly string[] _allArgs;

Expand All @@ -29,7 +29,7 @@ public ForwardingAppImplementation(
string? depsFile = null,
string? runtimeConfig = null,
string? additionalProbingPath = null,
Dictionary<string, string>? environmentVariables = null)
Dictionary<string, string?>? environmentVariables = null)
{
_forwardApplicationPath = forwardApplicationPath;
_argsToForward = argsToForward;
Expand Down Expand Up @@ -86,7 +86,7 @@ public ProcessStartInfo GetProcessStartInfo()
return processInfo;
}

public ForwardingAppImplementation WithEnvironmentVariable(string name, string value)
public ForwardingAppImplementation WithEnvironmentVariable(string name, string? value)
{
_environmentVariables.Add(name, value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static string MSBuildVersion
// True if, given current state of the class, MSBuild would be executed in its own process.
public bool ExecuteMSBuildOutOfProc => _forwardingApp != null;

private readonly Dictionary<string, string> _msbuildRequiredEnvironmentVariables = GetMSBuildRequiredEnvironmentVariables();
private readonly Dictionary<string, string?> _msbuildRequiredEnvironmentVariables = GetMSBuildRequiredEnvironmentVariables();

private readonly List<string> _msbuildRequiredParameters = [ "-maxcpucount", "--verbosity:m" ];

Expand Down Expand Up @@ -112,7 +112,7 @@ private static string EmitProperty(KeyValuePair<string, string> property, string
: $"--{label}:{property.Key}={property.Value}";
}

public void EnvironmentVariable(string name, string value)
public void EnvironmentVariable(string name, string? value)
{
if (_forwardingApp != null)
{
Expand Down Expand Up @@ -153,7 +153,7 @@ public int ExecuteInProc(string[] arguments)
Dictionary<string, string?> savedEnvironmentVariables = [];
try
{
foreach (KeyValuePair<string, string> kvp in _msbuildRequiredEnvironmentVariables)
foreach (KeyValuePair<string, string?> kvp in _msbuildRequiredEnvironmentVariables)
{
savedEnvironmentVariables[kvp.Key] = Environment.GetEnvironmentVariable(kvp.Key);
Environment.SetEnvironmentVariable(kvp.Key, kvp.Value);
Expand Down Expand Up @@ -199,7 +199,7 @@ private static string GetMSBuildExePath()
MSBuildExeName);
}

private static string GetMSBuildSDKsPath()
public static string GetMSBuildSDKsPath()
{
var envMSBuildSDKsPath = Environment.GetEnvironmentVariable("MSBuildSDKsPath");

Expand All @@ -218,7 +218,7 @@ private static string GetDotnetPath()
return new Muxer().MuxerPath;
}

internal static Dictionary<string, string> GetMSBuildRequiredEnvironmentVariables()
internal static Dictionary<string, string?> GetMSBuildRequiredEnvironmentVariables()
{
return new()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
<PackageReference Include="Microsoft.Build" ExcludeAssets="runtime" PrivateAssets="all" />
<PackageReference Include="Microsoft.Build.Utilities.Core" ExcludeAssets="runtime" PrivateAssets="all" />
<PackageReference Include="System.CommandLine" />
<PackageReference Include="System.Diagnostics.DiagnosticSource" />
<PackageReference Include="System.IO.Hashing" />
</ItemGroup>

Expand Down
18 changes: 9 additions & 9 deletions src/Cli/Microsoft.DotNet.Cli.Utils/TelemetryEventEntry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public static class TelemetryEventEntry
public static ITelemetryFilter TelemetryFilter { get; set; } = new BlockFilter();

public static void TrackEvent(
string? eventName = null,
string eventName,
IDictionary<string, string?>? properties = null,
IDictionary<string, double>? measurements = null)
{
Expand All @@ -32,7 +32,7 @@ public static void SendFiltered(object? o = null)
}
}

public static void Subscribe(Action<string?, IDictionary<string, string?>?, IDictionary<string, double>?> subscriber)
public static void Subscribe(Action<string, IDictionary<string, string?>?, IDictionary<string, double>?> subscriber)
{
void Handler(object? sender, InstrumentationEventArgs eventArgs)
{
Expand All @@ -47,24 +47,24 @@ public sealed class PerformanceMeasurement : IDisposable
{
private readonly Stopwatch? _timer;
private readonly Dictionary<string, double>? _data;
private readonly string? _name;
private readonly string _name;

public PerformanceMeasurement(Dictionary<string, double>? data, string name)
{
_name = name;
// Measurement is a no-op if we don't have a dictionary to store the entry.
if (data == null)
{
return;
}

_data = data;
_name = name;
_timer = Stopwatch.StartNew();
}

public void Dispose()
{
if (_name is not null && _timer is not null)
if (_timer is not null)
{
_data?.Add(_name, _timer.Elapsed.TotalMilliseconds);
}
Expand All @@ -82,7 +82,7 @@ public IEnumerable<ApplicationInsightsEntryFormat> Filter(object o)
public class InstrumentationEventArgs : EventArgs
{
internal InstrumentationEventArgs(
string? eventName,
string eventName,
IDictionary<string, string?>? properties,
IDictionary<string, double>? measurements)
{
Expand All @@ -91,17 +91,17 @@ internal InstrumentationEventArgs(
Measurements = measurements;
}

public string? EventName { get; }
public string EventName { get; }
public IDictionary<string, string?>? Properties { get; }
public IDictionary<string, double>? Measurements { get; }
}

public class ApplicationInsightsEntryFormat(
string? eventName = null,
string eventName,
IDictionary<string, string?>? properties = null,
IDictionary<string, double>? measurements = null)
{
public string? EventName { get; } = eventName;
public string EventName { get; } = eventName;
public IDictionary<string, string?>? Properties { get; } = properties;
public IDictionary<string, double>? Measurements { get; } = measurements;

Expand Down
45 changes: 22 additions & 23 deletions src/Cli/Microsoft.DotNet.InternalAbstractions/FilePath.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Microsoft.Extensions.EnvironmentAbstractions
namespace Microsoft.Extensions.EnvironmentAbstractions;

public readonly struct FilePath
{
public struct FilePath
{
public string Value { get; }
public string Value { get; }

/// <summary>
/// Create FilePath to represent an absolute file path. Note it may not exist.
/// </summary>
/// <param name="value">If the value is not rooted. Path.GetFullPath will be called during the constructor.</param>
public FilePath(string value)
/// <summary>
/// Create FilePath to represent an absolute file path. Note: It may not exist.
/// </summary>
/// <param name="value">If the value is not rooted. Path.GetFullPath will be called during the constructor.</param>
public FilePath(string value)
{
if (!Path.IsPathRooted(value))
{
if (!Path.IsPathRooted(value))
{
value = Path.GetFullPath(value);
}

Value = value;
value = Path.GetFullPath(value);
}

public override string ToString()
{
return Value;
}
Value = value;
}

public DirectoryPath GetDirectoryPath()
{
return new DirectoryPath(Path.GetDirectoryName(Value)!);
}
public override string ToString()
{
return Value;
}

public DirectoryPath GetDirectoryPath()
{
return new DirectoryPath(Path.GetDirectoryName(Value)!);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ internal InstantiateCommand(
Arity = new ArgumentArity(0, 999)
};

internal IReadOnlyList<Option> PassByOptions { get; } = new Option[]
{
internal IReadOnlyList<Option> PassByOptions { get; } =
[
SharedOptions.ForceOption,
SharedOptions.NameOption,
SharedOptions.DryRunOption,
SharedOptions.NoUpdateCheckOption
};
];

internal static Task<NewCommandStatus> ExecuteAsync(
NewCommandArgs newCommandArgs,
Expand All @@ -74,6 +74,7 @@ internal static async Task<IEnumerable<TemplateGroup>> GetTemplateGroupsAsync(
HostSpecificDataLoader hostSpecificDataLoader,
CancellationToken cancellationToken)
{
using var createTemplateGroupsActivity = Activities.Source.StartActivity("create-template-groups");
IReadOnlyList<ITemplateInfo> templates = await templatePackageManager.GetTemplatesAsync(cancellationToken).ConfigureAwait(false);
return TemplateGroup.FromTemplateList(CliTemplateInfo.FromTemplateInfo(templates, hostSpecificDataLoader));
}
Expand All @@ -84,6 +85,7 @@ internal static HashSet<TemplateCommand> GetTemplateCommand(
TemplatePackageManager templatePackageManager,
TemplateGroup templateGroup)
{
using var getTemplateActivity = Activities.Source.StartActivity("get-template-command");
//groups templates in the group by precedence
foreach (IGrouping<int, CliTemplateInfo> templateGrouping in templateGroup.Templates.GroupBy(g => g.Precedence).OrderByDescending(g => g.Key))
{
Expand Down Expand Up @@ -114,7 +116,7 @@ internal static HashSet<TemplateCommand> GetTemplateCommand(
templateGroup,
candidates);
}
return new HashSet<TemplateCommand>();
return [];
}

internal static void HandleNoMatchingTemplateGroup(InstantiateCommandArgs instantiateArgs, IEnumerable<TemplateGroup> templateGroups, IReporter reporter)
Expand Down Expand Up @@ -204,6 +206,8 @@ private static async Task<NewCommandStatus> ExecuteIntAsync(

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

IEnumerable<TemplateGroup> allTemplateGroups = await GetTemplateGroupsAsync(
templatePackageManager,
Expand Down Expand Up @@ -273,10 +277,11 @@ private static async Task<NewCommandStatus> HandleTemplateInstantiationAsync(
{
TemplateCommand templateCommandToRun = candidates.Single();
args.Command.Subcommands.Add(templateCommandToRun);

var templateParseActivity = Activities.Source.StartActivity("reparse-for-template");
ParseResult updatedParseResult = args.ParseResult.RootCommandResult.Command.Parse(
args.ParseResult.Tokens.Select(t => t.Value).ToArray(),
args.ParseResult.Configuration);
templateParseActivity?.Stop();
return await candidates.Single().InvokeAsync(updatedParseResult, cancellationToken).ConfigureAwait(false);
}
else if (candidates.Any())
Expand Down
Loading
Loading