Skip to content

Commit

Permalink
[Sampler.AWS] Use DateTimeOffset to beter handle timezones. (#1682)
Browse files Browse the repository at this point in the history
  • Loading branch information
ppittle authored Apr 23, 2024
1 parent 99f4fb1 commit 10ff408
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
4 changes: 2 additions & 2 deletions src/OpenTelemetry.Sampler.AWS/AWSXRayRemoteSampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ private async void GetAndUpdateTargets(object? state)

if (response.LastRuleModification > 0)
{
DateTime lastRuleModificationTime = this.Clock.ToDateTime(response.LastRuleModification);
var lastRuleModificationTime = this.Clock.ToDateTime(response.LastRuleModification);

if (lastRuleModificationTime > this.RulesCache.GetUpdatedAt())
{
Expand All @@ -167,7 +167,7 @@ private async void GetAndUpdateTargets(object? state)
}

// schedule next target poll
DateTime nextTargetFetchTime = this.RulesCache.NextTargetFetchTime();
var nextTargetFetchTime = this.RulesCache.NextTargetFetchTime();
TimeSpan nextTargetFetchInterval = nextTargetFetchTime.Subtract(this.Clock.Now());
if (nextTargetFetchInterval < TimeSpan.Zero)
{
Expand Down
6 changes: 3 additions & 3 deletions src/OpenTelemetry.Sampler.AWS/Clock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ public static Clock GetDefault()
return SystemClock.GetInstance();
}

public abstract DateTime Now();
public abstract DateTimeOffset Now();

public abstract long NowInMilliSeconds();

public abstract DateTime ToDateTime(double seconds);
public abstract DateTimeOffset ToDateTime(double seconds);

public abstract double ToDouble(DateTime dateTime);
public abstract double ToDouble(DateTimeOffset dateTime);
}
8 changes: 4 additions & 4 deletions src/OpenTelemetry.Sampler.AWS/RulesCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public RulesCache(Clock clock, string clientId, Resource resource, Trace.Sampler

internal List<SamplingRuleApplier> RuleAppliers { get; set; }

internal DateTime UpdatedAt { get; set; }
internal DateTimeOffset UpdatedAt { get; set; }

public bool Expired()
{
Expand Down Expand Up @@ -96,7 +96,7 @@ public SamplingResult ShouldSample(in SamplingParameters samplingParameters)
return this.FallbackSampler.ShouldSample(in samplingParameters);
}

public List<SamplingStatisticsDocument> Snapshot(DateTime now)
public List<SamplingStatisticsDocument> Snapshot(DateTimeOffset now)
{
List<SamplingStatisticsDocument> snapshots = new List<SamplingStatisticsDocument>();
foreach (var ruleApplier in this.RuleAppliers)
Expand Down Expand Up @@ -135,7 +135,7 @@ public void UpdateTargets(Dictionary<string, SamplingTargetDocument> targets)
}
}

public DateTime NextTargetFetchTime()
public DateTimeOffset NextTargetFetchTime()
{
var defaultPollingTime = this.Clock.Now().AddSeconds(AWSXRayRemoteSampler.DefaultTargetInterval.TotalSeconds);

Expand All @@ -162,7 +162,7 @@ public void Dispose()
GC.SuppressFinalize(this);
}

internal DateTime GetUpdatedAt()
internal DateTimeOffset GetUpdatedAt()
{
this.rwLock.EnterReadLock();
try
Expand Down
16 changes: 8 additions & 8 deletions src/OpenTelemetry.Sampler.AWS/SamplingRuleApplier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ private SamplingRuleApplier(
Trace.Sampler fixedRateSampler,
bool borrowing,
Statistics statistics,
DateTime reservoirEndTime,
DateTime nextSnapshotTime)
DateTimeOffset reservoirEndTime,
DateTimeOffset nextSnapshotTime)
{
this.ClientId = clientId;
this.Rule = rule;
Expand Down Expand Up @@ -81,9 +81,9 @@ private SamplingRuleApplier(

internal bool Borrowing { get; set; }

internal DateTime ReservoirEndTime { get; set; }
internal DateTimeOffset ReservoirEndTime { get; set; }

internal DateTime NextSnapshotTime { get; set; }
internal DateTimeOffset NextSnapshotTime { get; set; }

// check if this rule applier matches the request
public bool Matches(SamplingParameters samplingParameters, Resource resource)
Expand Down Expand Up @@ -179,7 +179,7 @@ public SamplingResult ShouldSample(in SamplingParameters samplingParameters)
}

// take the snapshot and reset the statistics.
public SamplingStatisticsDocument Snapshot(DateTime now)
public SamplingStatisticsDocument Snapshot(DateTimeOffset now)
{
double timestamp = this.Clock.ToDouble(now);

Expand All @@ -198,14 +198,14 @@ public SamplingStatisticsDocument Snapshot(DateTime now)
return statiscticsDocument;
}

public SamplingRuleApplier WithTarget(SamplingTargetDocument target, DateTime now)
public SamplingRuleApplier WithTarget(SamplingTargetDocument target, DateTimeOffset now)
{
Trace.Sampler newFixedRateSampler = target.FixedRate != null
? new ParentBasedSampler(new TraceIdRatioBasedSampler(target.FixedRate.Value))
: this.FixedRateSampler;

Trace.Sampler newReservoirSampler = new AlwaysOffSampler();
DateTime newReservoirEndTime = DateTime.MaxValue;
DateTimeOffset newReservoirEndTime = DateTimeOffset.MaxValue;
if (target.ReservoirQuota != null && target.ReservoirQuotaTTL != null)
{
if (target.ReservoirQuota > 0)
Expand All @@ -220,7 +220,7 @@ public SamplingRuleApplier WithTarget(SamplingTargetDocument target, DateTime no
newReservoirEndTime = this.Clock.ToDateTime(target.ReservoirQuotaTTL.Value);
}

DateTime newNextSnapshotTime = target.Interval != null
DateTimeOffset newNextSnapshotTime = target.Interval != null
? now.AddSeconds(target.Interval.Value)
: now.Add(AWSXRayRemoteSampler.DefaultTargetInterval);

Expand Down
10 changes: 5 additions & 5 deletions src/OpenTelemetry.Sampler.AWS/SystemClock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ internal class SystemClock : Clock
{
private static readonly SystemClock Instance = new SystemClock();

private static readonly DateTime EpochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
private static readonly DateTimeOffset EpochStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

private SystemClock()
{
Expand All @@ -21,22 +21,22 @@ public static Clock GetInstance()
return Instance;
}

public override DateTime Now()
public override DateTimeOffset Now()
{
return DateTime.UtcNow;
return DateTimeOffset.UtcNow;
}

public override long NowInMilliSeconds()
{
return (long)this.Now().ToUniversalTime().Subtract(EpochStart).TotalMilliseconds;
}

public override DateTime ToDateTime(double seconds)
public override DateTimeOffset ToDateTime(double seconds)
{
return EpochStart.AddSeconds(seconds);
}

public override double ToDouble(DateTime dateTime)
public override double ToDouble(DateTimeOffset dateTime)
{
var current = new TimeSpan(dateTime.ToUniversalTime().Ticks - EpochStart.Ticks);
double timestamp = Math.Round(current.TotalMilliseconds, 0) / 1000.0;
Expand Down
6 changes: 3 additions & 3 deletions test/OpenTelemetry.Sampler.AWS.Tests/TestClock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public TestClock(DateTime time)
this.nowTime = time;
}

public override DateTime Now()
public override DateTimeOffset Now()
{
return this.nowTime;
}
Expand All @@ -30,12 +30,12 @@ public override long NowInMilliSeconds()
return (long)this.nowTime.ToUniversalTime().Subtract(EpochStart).TotalMilliseconds;
}

public override DateTime ToDateTime(double seconds)
public override DateTimeOffset ToDateTime(double seconds)
{
return EpochStart.AddSeconds(seconds);
}

public override double ToDouble(DateTime dateTime)
public override double ToDouble(DateTimeOffset dateTime)
{
TimeSpan current = new TimeSpan(dateTime.ToUniversalTime().Ticks - EpochStart.Ticks);
double timestamp = Math.Round(current.TotalMilliseconds, 0) / 1000.0;
Expand Down

0 comments on commit 10ff408

Please sign in to comment.