Skip to content
Merged
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
13 changes: 12 additions & 1 deletion src/Cli/dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,18 @@ internal static int ProcessArgs(string[] args, TimeSpan startupTime)
}
PerformanceLogEventSource.Log.TelemetrySaveIfEnabledStart();
performanceData.Add("Startup Time", startupTime.TotalMilliseconds);
TelemetryEventEntry.SendFiltered(Tuple.Create(parseResult, performanceData));

string globalJsonState = string.Empty;
if (TelemetryClient.Enabled)
{
// Get the global.json state to report in telemetry along with this command invocation.
// We don't care about the actual SDK resolution, just the global.json information,
// so just pass empty string as executable directory for resolution.
NativeWrapper.SdkResolutionResult result = NativeWrapper.NETCoreSdkResolverNativeWrapper.ResolveSdk(string.Empty, Environment.CurrentDirectory);
globalJsonState = result.GlobalJsonState;
}

TelemetryEventEntry.SendFiltered(Tuple.Create(parseResult, performanceData, globalJsonState));
PerformanceLogEventSource.Log.TelemetrySaveIfEnabledStop();

int exitCode;
Expand Down
24 changes: 20 additions & 4 deletions src/Cli/dotnet/Telemetry/TelemetryFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,40 @@ public IEnumerable<ApplicationInsightsEntryFormat> Filter(object objectToFilter)
{
var result = new List<ApplicationInsightsEntryFormat>();
Dictionary<string, double> measurements = null;
string globalJsonState = string.Empty;
if (objectToFilter is Tuple<ParseResult, Dictionary<string, double>> parseResultWithMeasurements)
{
objectToFilter = parseResultWithMeasurements.Item1;
measurements = parseResultWithMeasurements.Item2;
measurements = RemoveZeroTimes(measurements);
}
else if (objectToFilter is Tuple<ParseResult, Dictionary<string, double>, string> parseResultWithMeasurementsAndGlobalJsonState)
{
objectToFilter = parseResultWithMeasurementsAndGlobalJsonState.Item1;
measurements = parseResultWithMeasurementsAndGlobalJsonState.Item2;
measurements = RemoveZeroTimes(measurements);
globalJsonState = parseResultWithMeasurementsAndGlobalJsonState.Item3;
}

if (objectToFilter is ParseResult parseResult)
{
var topLevelCommandName = parseResult.RootSubCommandResult();
if (topLevelCommandName != null)
{
Dictionary<string, string> properties = new()
{
["verb"] = topLevelCommandName
};
if (!string.IsNullOrEmpty(globalJsonState))
{
properties["globalJson"] = globalJsonState;
}

result.Add(new ApplicationInsightsEntryFormat(
"toplevelparser/command",
new Dictionary<string, string>()
{{ "verb", topLevelCommandName }}
, measurements
));
properties,
measurements
));

LogVerbosityForAllTopLevelCommand(result, parseResult, topLevelCommandName, measurements);

Expand Down
1 change: 1 addition & 0 deletions src/Resolvers/Microsoft.DotNet.NativeWrapper/Interop.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ internal enum hostfxr_resolve_sdk2_result_key_t : int
resolved_sdk_dir = 0,
global_json_path = 1,
requested_version = 2,
global_json_state = 3,
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ public class SdkResolutionResult
/// </summary>
public string? RequestedVersion;

/// <summary>
/// Result of the global.json search
/// </summary>
public string? GlobalJsonState;

/// <summary>
/// True if a global.json was found but there was no compatible SDK, so it was ignored.
/// </summary>
Expand All @@ -38,6 +43,9 @@ internal void Initialize(Interop.hostfxr_resolve_sdk2_result_key_t key, string v
case Interop.hostfxr_resolve_sdk2_result_key_t.requested_version:
RequestedVersion = value;
break;
case Interop.hostfxr_resolve_sdk2_result_key_t.global_json_state:
GlobalJsonState = value;
break;
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions test/dotnet.Tests/TelemetryFilterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,20 @@ public void TopLevelCommandNameShouldBeSentToTelemetryWithPerformanceData()
e.Measurement["Startup Time"] == 12345);
}

[Fact]
public void TopLevelCommandNameShouldBeSentToTelemetryWithGlobalJsonState()
{
string globalJsonState = "invalid_data";
var parseResult = Parser.Parse(["build"]);
TelemetryEventEntry.SendFiltered(Tuple.Create(parseResult, new Dictionary<string, double>(), globalJsonState));
_fakeTelemetry.LogEntries.Should().Contain(e => e.EventName == "toplevelparser/command" &&
e.Properties.ContainsKey("verb") &&
e.Properties["verb"] == Sha256Hasher.Hash("BUILD") &&
e.Measurement == null &&
e.Properties.ContainsKey("globalJson") &&
e.Properties["globalJson"] == Sha256Hasher.HashWithNormalizedCasing(globalJsonState));
}

[Fact]
public void TopLevelCommandNameShouldBeSentToTelemetryWithZeroPerformanceData()
{
Expand Down