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
6 changes: 6 additions & 0 deletions src/Cli/dotnet/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,12 @@ internal static int ProcessArgs(string[] args, TimeSpan startupTime)
}
}

TelemetryClient.TrackEvent("command/finish", properties: new Dictionary<string, string>
{
{ "exitCode", exitCode.ToString() }
},
measurements: new Dictionary<string, double>());

PerformanceLogEventSource.Log.TelemetryClientFlushStart();
TelemetryClient.Flush();
PerformanceLogEventSource.Log.TelemetryClientFlushStop();
Expand Down
2 changes: 1 addition & 1 deletion src/Cli/dotnet/Telemetry/Telemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private void InitializeTelemetry()
_client.Context.Session.Id = CurrentSessionId;
_client.Context.Device.OperatingSystem = CLIRuntimeEnvironment.OperatingSystem;

_commonProperties = new TelemetryCommonProperties().GetTelemetryCommonProperties();
_commonProperties = new TelemetryCommonProperties().GetTelemetryCommonProperties(CurrentSessionId);
_commonMeasurements = FrozenDictionary<string, double>.Empty;
}
catch (Exception e)
Expand Down
6 changes: 4 additions & 2 deletions src/Cli/dotnet/Telemetry/TelemetryCommonProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal class TelemetryCommonProperties(
private const string ProductType = "Product Type";
private const string LibcRelease = "Libc Release";
private const string LibcVersion = "Libc Version";
private const string SessionId = "SessionId";

private const string CI = "Continuous Integration";

Expand All @@ -53,7 +54,7 @@ internal class TelemetryCommonProperties(
private const string MachineIdCacheKey = "MachineId";
private const string IsDockerContainerCacheKey = "IsDockerContainer";

public FrozenDictionary<string, string> GetTelemetryCommonProperties()
public FrozenDictionary<string, string> GetTelemetryCommonProperties(string currentSessionId)
{
return new Dictionary<string, string>
{
Expand Down Expand Up @@ -82,7 +83,8 @@ public FrozenDictionary<string, string> GetTelemetryCommonProperties()
{InstallationType, ExternalTelemetryProperties.GetInstallationType()},
{ProductType, ExternalTelemetryProperties.GetProductType()},
{LibcRelease, ExternalTelemetryProperties.GetLibcRelease()},
{LibcVersion, ExternalTelemetryProperties.GetLibcVersion()}
{LibcVersion, ExternalTelemetryProperties.GetLibcVersion()},
{SessionId, currentSessionId}
}.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase);
}

Expand Down
58 changes: 35 additions & 23 deletions test/dotnet.Tests/TelemetryCommonPropertiesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,35 @@ public TelemetryCommonPropertiesTests(ITestOutputHelper log) : base(log)
public void TelemetryCommonPropertiesShouldContainIfItIsInDockerOrNot()
{
var unitUnderTest = new TelemetryCommonProperties(userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties().Should().ContainKey("Docker Container");
unitUnderTest.GetTelemetryCommonProperties("dummySessionId").Should().ContainKey("Docker Container");
}

[Fact]
public void TelemetryCommonPropertiesShouldReturnHashedPath()
{
var unitUnderTest = new TelemetryCommonProperties(() => "ADirectory", userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["Current Path Hash"].Should().NotBe("ADirectory");
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Current Path Hash"].Should().NotBe("ADirectory");
}

[Fact]
public void TelemetryCommonPropertiesShouldReturnHashedMachineId()
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => "plaintext", userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["Machine ID"].Should().NotBe("plaintext");
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Machine ID"].Should().NotBe("plaintext");
}

[Fact]
public void TelemetryCommonPropertiesShouldReturnDevDeviceId()
{
var unitUnderTest = new TelemetryCommonProperties(getDeviceId: () => "plaintext", userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["devdeviceid"].Should().Be("plaintext");
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["devdeviceid"].Should().Be("plaintext");
}

[Fact]
public void TelemetryCommonPropertiesShouldReturnNewGuidWhenCannotGetMacAddress()
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
var assignedMachineId = unitUnderTest.GetTelemetryCommonProperties()["Machine ID"];
var assignedMachineId = unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Machine ID"];

Guid.TryParse(assignedMachineId, out var _).Should().BeTrue("it should be a guid");
}
Expand All @@ -55,10 +55,10 @@ public void TelemetryCommonPropertiesShouldReturnNewGuidWhenCannotGetMacAddress(
public void TelemetryCommonPropertiesShouldEnsureDevDeviceIDIsCached()
{
var unitUnderTest = new TelemetryCommonProperties(userLevelCacheWriter: new NothingCache());
var assignedMachineId = unitUnderTest.GetTelemetryCommonProperties()["devdeviceid"];
var assignedMachineId = unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["devdeviceid"];

Guid.TryParse(assignedMachineId, out var _).Should().BeTrue("it should be a guid");
var secondAssignedMachineId = unitUnderTest.GetTelemetryCommonProperties()["devdeviceid"];
var secondAssignedMachineId = unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["devdeviceid"];

Guid.TryParse(secondAssignedMachineId, out var _).Should().BeTrue("it should be a guid");
secondAssignedMachineId.Should().Be(assignedMachineId, "it should match the previously assigned guid");
Expand All @@ -68,14 +68,14 @@ public void TelemetryCommonPropertiesShouldEnsureDevDeviceIDIsCached()
public void TelemetryCommonPropertiesShouldReturnHashedMachineIdOld()
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => "plaintext", userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["Machine ID Old"].Should().NotBe("plaintext");
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Machine ID Old"].Should().NotBe("plaintext");
}

[Fact]
public void TelemetryCommonPropertiesShouldReturnNewGuidWhenCannotGetMacAddressOld()
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
var assignedMachineId = unitUnderTest.GetTelemetryCommonProperties()["Machine ID Old"];
var assignedMachineId = unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Machine ID Old"];

Guid.TryParse(assignedMachineId, out var _).Should().BeTrue("it should be a guid");
}
Expand All @@ -84,72 +84,72 @@ public void TelemetryCommonPropertiesShouldReturnNewGuidWhenCannotGetMacAddressO
public void TelemetryCommonPropertiesShouldReturnIsOutputRedirected()
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["Output Redirected"].Should().BeOneOf("True", "False");
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Output Redirected"].Should().BeOneOf("True", "False");
}

[Fact]
public void TelemetryCommonPropertiesShouldReturnIsCIDetection()
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["Continuous Integration"].Should().BeOneOf("True", "False");
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Continuous Integration"].Should().BeOneOf("True", "False");
}

[Fact]
public void TelemetryCommonPropertiesShouldContainKernelVersion()
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["Kernel Version"].Should().Be(RuntimeInformation.OSDescription);
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Kernel Version"].Should().Be(RuntimeInformation.OSDescription);
}

[Fact]
public void TelemetryCommonPropertiesShouldContainArchitectureInformation()
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["OS Architecture"].Should().Be(RuntimeInformation.OSArchitecture.ToString());
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["OS Architecture"].Should().Be(RuntimeInformation.OSArchitecture.ToString());
}

[WindowsOnlyFact]
public void TelemetryCommonPropertiesShouldContainWindowsInstallType()
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["Installation Type"].Should().NotBeEmpty();
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Installation Type"].Should().NotBeEmpty();
}

[UnixOnlyFact]
public void TelemetryCommonPropertiesShouldContainEmptyWindowsInstallType()
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["Installation Type"].Should().BeEmpty();
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Installation Type"].Should().BeEmpty();
}

[WindowsOnlyFact]
public void TelemetryCommonPropertiesShouldContainWindowsProductType()
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["Product Type"].Should().NotBeEmpty();
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Product Type"].Should().NotBeEmpty();
}

[UnixOnlyFact]
public void TelemetryCommonPropertiesShouldContainEmptyWindowsProductType()
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["Product Type"].Should().BeEmpty();
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Product Type"].Should().BeEmpty();
}

[WindowsOnlyFact]
public void TelemetryCommonPropertiesShouldContainEmptyLibcReleaseAndVersion()
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["Libc Release"].Should().BeEmpty();
unitUnderTest.GetTelemetryCommonProperties()["Libc Version"].Should().BeEmpty();
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Libc Release"].Should().BeEmpty();
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Libc Version"].Should().BeEmpty();
}

[MacOsOnlyFact]
public void TelemetryCommonPropertiesShouldContainEmptyLibcReleaseAndVersion2()
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["Libc Release"].Should().BeEmpty();
unitUnderTest.GetTelemetryCommonProperties()["Libc Version"].Should().BeEmpty();
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Libc Release"].Should().BeEmpty();
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Libc Version"].Should().BeEmpty();
}

[LinuxOnlyFact]
Expand All @@ -158,8 +158,8 @@ public void TelemetryCommonPropertiesShouldContainLibcReleaseAndVersion()
if (!RuntimeInformation.RuntimeIdentifier.Contains("alpine", StringComparison.OrdinalIgnoreCase))
{
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
unitUnderTest.GetTelemetryCommonProperties()["Libc Release"].Should().NotBeEmpty();
unitUnderTest.GetTelemetryCommonProperties()["Libc Version"].Should().NotBeEmpty();
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Libc Release"].Should().NotBeEmpty();
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Libc Version"].Should().NotBeEmpty();
}
}

Expand All @@ -184,6 +184,18 @@ public void CanDetectCIStatusForEnvVars(Dictionary<string, string> envVars, bool
}
}

[Theory]
[InlineData("dummySessionId")]
[InlineData(null)]
public void TelemetryCommonPropertiesShouldContainSessionId(string sessionId)
{
var unitUnderTest = new TelemetryCommonProperties(userLevelCacheWriter: new NothingCache());
var commonProperties = unitUnderTest.GetTelemetryCommonProperties(sessionId);

commonProperties.Should().ContainKey("SessionId");
commonProperties["SessionId"].Should().Be(sessionId);
}

public static IEnumerable<object[]> CITelemetryTestCases => new List<object[]>{
new object[] { new Dictionary<string, string> { { "TF_BUILD", "true" } }, true },
new object[] { new Dictionary<string, string> { { "GITHUB_ACTIONS", "true" } }, true },
Expand Down