Skip to content

Commit

Permalink
Use global Random instance instead of ThreadLocal (open-telemetry#380)
Browse files Browse the repository at this point in the history
Co-authored-by: srprash <srprash@amazon.com>

Co-authored-by: srprash <srprash@amazon.com>
Co-authored-by: Prashant Srivastava <50466688+srprash@users.noreply.github.com>
  • Loading branch information
3 people committed Jun 16, 2022
1 parent 3b3299f commit 08b52d5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
30 changes: 13 additions & 17 deletions src/OpenTelemetry.Contrib.Extensions.AWSXRay/AWSXRayIdGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Threading;
using OpenTelemetry.Internal;
using OpenTelemetry.Trace;

Expand All @@ -38,16 +37,7 @@ public static class AWSXRayIdGenerator
private static readonly DateTime EpochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private static readonly long UnixEpochMicroseconds = EpochStart.Ticks / TicksPerMicrosecond;
private static readonly Random Global = new Random();
private static readonly ThreadLocal<Random> Local = new ThreadLocal<Random>(() =>
{
int seed;
lock (Global)
{
seed = Global.Next();
}
return new Random(seed);
});
private static object randLock = new object();

internal static void ReplaceTraceId(Sampler sampler = null)
{
Expand Down Expand Up @@ -129,11 +119,17 @@ private static string GenerateHexNumber(int digits)
Guard.ThrowIfOutOfRange(digits, min: 0);

byte[] bytes = new byte[digits / 2];
NextBytes(bytes);
string hexNumber = string.Concat(bytes.Select(x => x.ToString("x2", CultureInfo.InvariantCulture)).ToArray());
if (digits % 2 != 0)

string hexNumber;

lock (randLock)
{
hexNumber += Next(16).ToString("x", CultureInfo.InvariantCulture);
NextBytes(bytes);
hexNumber = string.Concat(bytes.Select(x => x.ToString("x2", CultureInfo.InvariantCulture)).ToArray());
if (digits % 2 != 0)
{
hexNumber += Next(16).ToString("x", CultureInfo.InvariantCulture);
}
}

return hexNumber;
Expand All @@ -145,7 +141,7 @@ private static string GenerateHexNumber(int digits)
/// <param name="buffer">An array of bytes to contain random numbers.</param>
private static void NextBytes(byte[] buffer)
{
Local.Value.NextBytes(buffer);
Global.NextBytes(buffer);
}

/// <summary>
Expand All @@ -155,7 +151,7 @@ private static void NextBytes(byte[] buffer)
/// <returns>A 32-bit signed integer that is greater than or equal to 0, and less than maxValue.</returns>
private static int Next(int maxValue)
{
return Local.Value.Next(maxValue);
return Global.Next(maxValue);
}

private static ActivitySamplingResult ComputeRootActivitySamplingResult(
Expand Down
6 changes: 6 additions & 0 deletions src/OpenTelemetry.Contrib.Extensions.AWSXRay/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog - OpenTelemetry.Contrib.Extensions.AWSXRay

## 1.3.0 [TBD]

* Enhancement - AWSXRayIdGenerator - Generate X-Ray IDs with global Random
instance instead of recreating with ThreadLocal
([#380](https://github.com/open-telemetry/opentelemetry-dotnet-contrib/pull/380))

## 1.2.0 [2022-May-18]

* Enhancement - AWSEKSResourceDetector - Validate ClusterName/ContainerID
Expand Down

0 comments on commit 08b52d5

Please sign in to comment.