Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging and additional fallback for installation ID #1103

Merged
merged 6 commits into from
Jul 10, 2021
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
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();
}
}
}