Skip to content

Commit 0f27f9b

Browse files
authored
Add exit code telemetry and add session id (#50763)
1 parent d9cf033 commit 0f27f9b

File tree

4 files changed

+46
-26
lines changed

4 files changed

+46
-26
lines changed

src/Cli/dotnet/Program.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,12 @@ internal static int ProcessArgs(string[] args, TimeSpan startupTime)
285285
}
286286
}
287287

288+
TelemetryClient.TrackEvent("command/finish", properties: new Dictionary<string, string>
289+
{
290+
{ "exitCode", exitCode.ToString() }
291+
},
292+
measurements: new Dictionary<string, double>());
293+
288294
PerformanceLogEventSource.Log.TelemetryClientFlushStart();
289295
TelemetryClient.Flush();
290296
PerformanceLogEventSource.Log.TelemetryClientFlushStop();

src/Cli/dotnet/Telemetry/Telemetry.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private void InitializeTelemetry()
154154
_client.Context.Session.Id = CurrentSessionId;
155155
_client.Context.Device.OperatingSystem = CLIRuntimeEnvironment.OperatingSystem;
156156

157-
_commonProperties = new TelemetryCommonProperties().GetTelemetryCommonProperties();
157+
_commonProperties = new TelemetryCommonProperties().GetTelemetryCommonProperties(CurrentSessionId);
158158
_commonMeasurements = FrozenDictionary<string, double>.Empty;
159159
}
160160
catch (Exception e)

src/Cli/dotnet/Telemetry/TelemetryCommonProperties.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ internal class TelemetryCommonProperties(
4444
private const string ProductType = "Product Type";
4545
private const string LibcRelease = "Libc Release";
4646
private const string LibcVersion = "Libc Version";
47+
private const string SessionId = "SessionId";
4748

4849
private const string CI = "Continuous Integration";
4950

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

56-
public FrozenDictionary<string, string> GetTelemetryCommonProperties()
57+
public FrozenDictionary<string, string> GetTelemetryCommonProperties(string currentSessionId)
5758
{
5859
return new Dictionary<string, string>
5960
{
@@ -82,7 +83,8 @@ public FrozenDictionary<string, string> GetTelemetryCommonProperties()
8283
{InstallationType, ExternalTelemetryProperties.GetInstallationType()},
8384
{ProductType, ExternalTelemetryProperties.GetProductType()},
8485
{LibcRelease, ExternalTelemetryProperties.GetLibcRelease()},
85-
{LibcVersion, ExternalTelemetryProperties.GetLibcVersion()}
86+
{LibcVersion, ExternalTelemetryProperties.GetLibcVersion()},
87+
{SessionId, currentSessionId}
8688
}.ToFrozenDictionary(StringComparer.OrdinalIgnoreCase);
8789
}
8890

test/dotnet.Tests/TelemetryCommonPropertiesTests.cs

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,35 @@ public TelemetryCommonPropertiesTests(ITestOutputHelper log) : base(log)
1818
public void TelemetryCommonPropertiesShouldContainIfItIsInDockerOrNot()
1919
{
2020
var unitUnderTest = new TelemetryCommonProperties(userLevelCacheWriter: new NothingCache());
21-
unitUnderTest.GetTelemetryCommonProperties().Should().ContainKey("Docker Container");
21+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId").Should().ContainKey("Docker Container");
2222
}
2323

2424
[Fact]
2525
public void TelemetryCommonPropertiesShouldReturnHashedPath()
2626
{
2727
var unitUnderTest = new TelemetryCommonProperties(() => "ADirectory", userLevelCacheWriter: new NothingCache());
28-
unitUnderTest.GetTelemetryCommonProperties()["Current Path Hash"].Should().NotBe("ADirectory");
28+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Current Path Hash"].Should().NotBe("ADirectory");
2929
}
3030

3131
[Fact]
3232
public void TelemetryCommonPropertiesShouldReturnHashedMachineId()
3333
{
3434
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => "plaintext", userLevelCacheWriter: new NothingCache());
35-
unitUnderTest.GetTelemetryCommonProperties()["Machine ID"].Should().NotBe("plaintext");
35+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Machine ID"].Should().NotBe("plaintext");
3636
}
3737

3838
[Fact]
3939
public void TelemetryCommonPropertiesShouldReturnDevDeviceId()
4040
{
4141
var unitUnderTest = new TelemetryCommonProperties(getDeviceId: () => "plaintext", userLevelCacheWriter: new NothingCache());
42-
unitUnderTest.GetTelemetryCommonProperties()["devdeviceid"].Should().Be("plaintext");
42+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["devdeviceid"].Should().Be("plaintext");
4343
}
4444

4545
[Fact]
4646
public void TelemetryCommonPropertiesShouldReturnNewGuidWhenCannotGetMacAddress()
4747
{
4848
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
49-
var assignedMachineId = unitUnderTest.GetTelemetryCommonProperties()["Machine ID"];
49+
var assignedMachineId = unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Machine ID"];
5050

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

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

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

7474
[Fact]
7575
public void TelemetryCommonPropertiesShouldReturnNewGuidWhenCannotGetMacAddressOld()
7676
{
7777
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
78-
var assignedMachineId = unitUnderTest.GetTelemetryCommonProperties()["Machine ID Old"];
78+
var assignedMachineId = unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Machine ID Old"];
7979

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

9090
[Fact]
9191
public void TelemetryCommonPropertiesShouldReturnIsCIDetection()
9292
{
9393
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
94-
unitUnderTest.GetTelemetryCommonProperties()["Continuous Integration"].Should().BeOneOf("True", "False");
94+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Continuous Integration"].Should().BeOneOf("True", "False");
9595
}
9696

9797
[Fact]
9898
public void TelemetryCommonPropertiesShouldContainKernelVersion()
9999
{
100100
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
101-
unitUnderTest.GetTelemetryCommonProperties()["Kernel Version"].Should().Be(RuntimeInformation.OSDescription);
101+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Kernel Version"].Should().Be(RuntimeInformation.OSDescription);
102102
}
103103

104104
[Fact]
105105
public void TelemetryCommonPropertiesShouldContainArchitectureInformation()
106106
{
107107
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
108-
unitUnderTest.GetTelemetryCommonProperties()["OS Architecture"].Should().Be(RuntimeInformation.OSArchitecture.ToString());
108+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["OS Architecture"].Should().Be(RuntimeInformation.OSArchitecture.ToString());
109109
}
110110

111111
[WindowsOnlyFact]
112112
public void TelemetryCommonPropertiesShouldContainWindowsInstallType()
113113
{
114114
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
115-
unitUnderTest.GetTelemetryCommonProperties()["Installation Type"].Should().NotBeEmpty();
115+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Installation Type"].Should().NotBeEmpty();
116116
}
117117

118118
[UnixOnlyFact]
119119
public void TelemetryCommonPropertiesShouldContainEmptyWindowsInstallType()
120120
{
121121
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
122-
unitUnderTest.GetTelemetryCommonProperties()["Installation Type"].Should().BeEmpty();
122+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Installation Type"].Should().BeEmpty();
123123
}
124124

125125
[WindowsOnlyFact]
126126
public void TelemetryCommonPropertiesShouldContainWindowsProductType()
127127
{
128128
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
129-
unitUnderTest.GetTelemetryCommonProperties()["Product Type"].Should().NotBeEmpty();
129+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Product Type"].Should().NotBeEmpty();
130130
}
131131

132132
[UnixOnlyFact]
133133
public void TelemetryCommonPropertiesShouldContainEmptyWindowsProductType()
134134
{
135135
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
136-
unitUnderTest.GetTelemetryCommonProperties()["Product Type"].Should().BeEmpty();
136+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Product Type"].Should().BeEmpty();
137137
}
138138

139139
[WindowsOnlyFact]
140140
public void TelemetryCommonPropertiesShouldContainEmptyLibcReleaseAndVersion()
141141
{
142142
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
143-
unitUnderTest.GetTelemetryCommonProperties()["Libc Release"].Should().BeEmpty();
144-
unitUnderTest.GetTelemetryCommonProperties()["Libc Version"].Should().BeEmpty();
143+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Libc Release"].Should().BeEmpty();
144+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Libc Version"].Should().BeEmpty();
145145
}
146146

147147
[MacOsOnlyFact]
148148
public void TelemetryCommonPropertiesShouldContainEmptyLibcReleaseAndVersion2()
149149
{
150150
var unitUnderTest = new TelemetryCommonProperties(getMACAddress: () => null, userLevelCacheWriter: new NothingCache());
151-
unitUnderTest.GetTelemetryCommonProperties()["Libc Release"].Should().BeEmpty();
152-
unitUnderTest.GetTelemetryCommonProperties()["Libc Version"].Should().BeEmpty();
151+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Libc Release"].Should().BeEmpty();
152+
unitUnderTest.GetTelemetryCommonProperties("dummySessionId")["Libc Version"].Should().BeEmpty();
153153
}
154154

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

@@ -184,6 +184,18 @@ public void CanDetectCIStatusForEnvVars(Dictionary<string, string> envVars, bool
184184
}
185185
}
186186

187+
[Theory]
188+
[InlineData("dummySessionId")]
189+
[InlineData(null)]
190+
public void TelemetryCommonPropertiesShouldContainSessionId(string sessionId)
191+
{
192+
var unitUnderTest = new TelemetryCommonProperties(userLevelCacheWriter: new NothingCache());
193+
var commonProperties = unitUnderTest.GetTelemetryCommonProperties(sessionId);
194+
195+
commonProperties.Should().ContainKey("SessionId");
196+
commonProperties["SessionId"].Should().Be(sessionId);
197+
}
198+
187199
public static IEnumerable<object[]> CITelemetryTestCases => new List<object[]>{
188200
new object[] { new Dictionary<string, string> { { "TF_BUILD", "true" } }, true },
189201
new object[] { new Dictionary<string, string> { { "GITHUB_ACTIONS", "true" } }, true },

0 commit comments

Comments
 (0)