Skip to content

Commit

Permalink
logging and additional fallback for installation ID (#1103)
Browse files Browse the repository at this point in the history
Co-authored-by: Bruno Garcia <bruno@brunogarcia.com>
  • Loading branch information
Tyrrrz and bruno-garcia committed Jul 10, 2021
1 parent 4fe2084 commit 733245e
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
17 changes: 15 additions & 2 deletions src/Sentry/GlobalSessionManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Sentry.Extensibility;
using Sentry.Infrastructure;
using Sentry.Internal;
using Sentry.Internal.Extensions;

namespace Sentry
{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -94,6 +100,7 @@ public GlobalSessionManager(SentryOptions options)
{
try
{
// Get MAC address of the first network adapter
var installationId = NetworkInterface
.GetAllNetworkInterfaces()
.Where(nic =>
Expand All @@ -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;
Expand All @@ -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
Expand All @@ -145,7 +157,8 @@ public GlobalSessionManager(SentryOptions options)

var id =
TryGetPersistentInstallationId() ??
TryGetHardwareInstallationId();
TryGetHardwareInstallationId() ??
GetMachineNameInstallationId();

if (!string.IsNullOrWhiteSpace(id))
{
Expand Down
30 changes: 30 additions & 0 deletions test/Sentry.Tests/GlobalSessionManagerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}

0 comments on commit 733245e

Please sign in to comment.