diff --git a/CHANGELOG.md b/CHANGELOG.md index 2f355776e3..de44e1d8ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ - Add HTTP request breadcrumb ([#1113](https://github.com/getsentry/sentry-dotnet/pull/1113)) - Integration for Google Cloud Functions ([#1085](https://github.com/getsentry/sentry-dotnet/pull/1085)) - Add ClearAttachments to Scope ([#1104](https://github.com/getsentry/sentry-dotnet/pull/1104)) +- Add additional logging and additional fallback for installation ID ([#1103](https://github.com/getsentry/sentry-dotnet/pull/1103)) ## 3.6.1 diff --git a/src/Sentry/GlobalSessionManager.cs b/src/Sentry/GlobalSessionManager.cs index bb77dd0448..df00f3f752 100644 --- a/src/Sentry/GlobalSessionManager.cs +++ b/src/Sentry/GlobalSessionManager.cs @@ -6,6 +6,7 @@ using Sentry.Extensibility; using Sentry.Infrastructure; using Sentry.Internal; +using Sentry.Internal.Extensions; namespace Sentry { @@ -51,6 +52,11 @@ public GlobalSessionManager(SentryOptions options) Directory.CreateDirectory(directoryPath); + _options.DiagnosticLogger?.LogDebug( + "Created directory for installation ID file ({0}).", + directoryPath + ); + var filePath = Path.Combine(directoryPath, ".installation"); // Read installation ID stored in a file @@ -94,6 +100,7 @@ public GlobalSessionManager(SentryOptions options) { try { + // Get MAC address of the first network adapter var installationId = NetworkInterface .GetAllNetworkInterfaces() .Where(nic => @@ -105,7 +112,7 @@ public GlobalSessionManager(SentryOptions options) if (string.IsNullOrWhiteSpace(installationId)) { _options.DiagnosticLogger?.LogError( - "Failed to resolve hardware installation ID." + "Failed to find an appropriate network interface for installation ID." ); return null; @@ -124,6 +131,11 @@ public GlobalSessionManager(SentryOptions options) } } + // Internal for testing + internal string GetMachineNameInstallationId() => + // Never fails + Environment.MachineName.GetHashString(); + private string? TryGetInstallationId() { // Installation ID could have already been resolved by this point @@ -145,7 +157,8 @@ public GlobalSessionManager(SentryOptions options) var id = TryGetPersistentInstallationId() ?? - TryGetHardwareInstallationId(); + TryGetHardwareInstallationId() ?? + GetMachineNameInstallationId(); if (!string.IsNullOrWhiteSpace(id)) { diff --git a/test/Sentry.Tests/GlobalSessionManagerTests.cs b/test/Sentry.Tests/GlobalSessionManagerTests.cs index 84313e2de9..56d2c6e388 100644 --- a/test/Sentry.Tests/GlobalSessionManagerTests.cs +++ b/test/Sentry.Tests/GlobalSessionManagerTests.cs @@ -185,5 +185,35 @@ public void EndSession_ActiveSessionDoesNotExist_DoesNothing() e.Level == SentryLevel.Debug ); } + + [Fact] + public void GetMachineNameInstallationId_Hashed() + { + // Arrange + using var fixture = new Fixture(); + + // Act + var installationId = fixture.SessionManager.GetMachineNameInstallationId(); + + // Assert + installationId.Should().NotBeNullOrWhiteSpace(); + installationId.Should().NotBeEquivalentTo(Environment.MachineName); + } + + [Fact] + public void GetMachineNameInstallationId_Idempotent() + { + // Arrange + using var fixture = new Fixture(); + + // Act + var installationIds = Enumerable + .Range(0, 10) + .Select(_ => fixture.SessionManager.GetMachineNameInstallationId()) + .ToArray(); + + // Assert + installationIds.Distinct().Should().ContainSingle(); + } } }