Skip to content

Generate valid machine id #245

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

Merged
merged 7 commits into from
Apr 9, 2025
Merged

Generate valid machine id #245

merged 7 commits into from
Apr 9, 2025

Conversation

konraddysput
Copy link
Collaborator

Why

In the past, we used Unity ID to generate a user GUID attribute. If the Unity identifier was unavailable for any reason, we fall back to the network identifier. If both methods failed, we generated a random guid. Later, we also introduced storing the guid in the session memory.

Recently we noticed, the user identifier provided by Unity is not a valid guid. We thought it is based on the quick tests and results in Backtrace, but it looks like it's not on Windows at least.

To mitigate the issue:

  • When a user identifier is read, we verify if its a valid uuid. If not, we're converting a string into a uuid representation.
  • The code was changed to verify potential tests cases and strategies.

Testing strategy:

  • unity tests
  • manual tests

Expected results:

  • if the guid is invalid, always generate a valid guid based on the string we stored in memory or we got from Unity,\

ref: INT-93

@konraddysput konraddysput added the bug Something isn't working label Apr 7, 2025
@konraddysput konraddysput requested review from perf2711 and Copilot April 7, 2025 08:05
@konraddysput konraddysput self-assigned this Apr 7, 2025
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 10 out of 17 changed files in this pull request and generated no comments.

Files not reviewed (7)
  • Runtime/Model/DataProvider.meta: Language not supported
  • Runtime/Model/DataProvider/IMachineIdentifierDataProvider.cs.meta: Language not supported
  • Runtime/Model/DataProvider/ISessionStorageDataProvider.cs.meta: Language not supported
  • Runtime/Model/DataProvider/NetworkIdentifierDataProvider.cs.meta: Language not supported
  • Runtime/Model/DataProvider/SessionStorageDataProvider.cs.meta: Language not supported
  • Runtime/Model/DataProvider/UnityMachineIdentifierProvider.cs.meta: Language not supported
  • Tests/Runtime/Client/Mocks/MachineIdStorageMock.cs.meta: Language not supported
Comments suppressed due to low confidence (4)

Runtime/Model/MachineIdStorage.cs:41

  • Consider retrieving an existing machine ID from storage (possibly via FetchMachineIdFromStorage) before generating a new identifier to ensure consistency across sessions.
internal string GenerateMachineId()

Tests/Runtime/Client/BacktraceAttributeMachineIdTests.cs:28

  • Correct the spelling of 'Netowrking' to 'Networking' in the test method name.
public void TestMachineAttributes_ShouldUseMac_ShouldReturnNetowrkingIdentifier()

Tests/Runtime/Client/BacktraceAttributeMachineIdTests.cs:76

  • [nitpick] Consider correcting the method name to 'IdentifiersAreTheSame' for grammatical accuracy.
public void TestMachineAttributes_ShouldAlwaysReturnTheSameValueUnityId_IdentifierAreTheSame()

Tests/Runtime/Client/BacktraceAttributeMachineIdTests.cs:85

  • [nitpick] Correct the spelling mistakes: 'Untiy' should be 'Unity' and 'Identitfier' should be 'Identifier'.
public void TestMachineAttributes_ShouldAlwaysGenerateTheSameUntiyAttribute_ShouldReturnTheSameUnityIdentitfier()

/// <summary>
/// Converts a random string into a guid representation.
/// </summary>
public static Guid FromString(string value)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we rename this to FromMD5String? FromString suggests that a GUID string can be provided here.

Copy link
Collaborator Author

@konraddysput konraddysput Apr 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

MD5 is an algorithm behind the scene that we're using to convert string into bytes that later we can use to generate GUID. It doesn't make sense to include MD5 in the name in my opinion. It's string and md5 is an implementation detail.

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Backtrace.Unity.Tests.Runtime")]
namespace Backtrace.Unity.Model.DataProvider
{
internal interface IMachineIdentifierDataProvider
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This only provides the machine identifier. Why not name this IMachineIdentifierProvider?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Comment on lines +25 to +29
if (Guid.TryParse(_deviceUniqueIdentifier, out Guid unityUuidGuid))
{
return unityUuidGuid.ToString();
}
return GuidHelper.FromString(_deviceUniqueIdentifier).ToString();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How often the ID will be generated? Maybe it's a good idea to generate this in the constructor, or cache the value somewhere?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I see that in MachineIdStorage it is retrieved only once? If so, feel free to discard this comment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to confirm - yes. we're doing it only once.

@konraddysput konraddysput merged commit 59c90be into master Apr 9, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants