-
-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1f9d4f2
commit e0ed84c
Showing
19 changed files
with
391 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
namespace Sentry.Internal; | ||
|
||
/// <summary> | ||
/// FNV is a non-cryptographic hash. | ||
/// | ||
/// See https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function#FNV_hash_parameters | ||
/// </summary> | ||
/// <remarks> | ||
/// We use a struct to avoid heap allocations. | ||
/// </remarks> | ||
internal struct FnvHash | ||
{ | ||
public FnvHash() | ||
{ | ||
} | ||
|
||
private const int Offset = unchecked((int)2166136261); | ||
private const int Prime = 16777619; | ||
|
||
private int HashCode { get; set; } = Offset; | ||
|
||
private void Combine(byte data) | ||
{ | ||
unchecked | ||
{ | ||
HashCode ^= data; | ||
HashCode *= Prime; | ||
} | ||
} | ||
|
||
private static int ComputeHash(byte[] data) | ||
{ | ||
var result = new FnvHash(); | ||
foreach (var b in data) | ||
{ | ||
result.Combine(b); | ||
} | ||
|
||
return result.HashCode; | ||
} | ||
|
||
public static int ComputeHash(string data) => ComputeHash(Encoding.UTF8.GetBytes(data)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Sentry.Internal; | ||
|
||
internal static class SampleRandHelper | ||
{ | ||
internal static double GenerateSampleRand(string traceId) | ||
=> new Random(FnvHash.ComputeHash(traceId)).NextDouble(); | ||
|
||
internal static bool IsSampled(double sampleRand, double rate) => rate switch | ||
{ | ||
>= 1 => true, | ||
<= 0 => false, | ||
_ => sampleRand < rate | ||
}; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.