Skip to content

Commit

Permalink
[sdk-metrics] Refactor duplicated update completion code into a helpe…
Browse files Browse the repository at this point in the history
…r in MetricPoint (#5398)
  • Loading branch information
CodeBlanch authored Feb 28, 2024
1 parent 05b0ca4 commit 7ce0c55
Showing 1 changed file with 26 additions and 68 deletions.
94 changes: 26 additions & 68 deletions src/OpenTelemetry/Metrics/MetricPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -445,23 +445,7 @@ internal void Update(long number)
}
}

// There is a race with Snapshot:
// Update() updates the value
// Snapshot snapshots the value
// Snapshot sets status to NoCollectPending
// Update sets status to CollectPending -- this is not right as the Snapshot
// already included the updated value.
// In the absence of any new Update call until next Snapshot,
// this results in exporting an Update even though
// it had no update.
// TODO: For Delta, this can be mitigated
// by ignoring Zero points
this.MetricPointStatus = MetricPointStatus.CollectPending;

if (this.aggregatorStore.OutputDeltaWithUnusedMetricPointReclaimEnabled)
{
Interlocked.Decrement(ref this.ReferenceCount);
}
this.CompleteUpdate();
}

internal void UpdateWithExemplar(long number, ReadOnlySpan<KeyValuePair<string, object?>> tags, bool isSampled)
Expand Down Expand Up @@ -573,23 +557,7 @@ internal void UpdateWithExemplar(long number, ReadOnlySpan<KeyValuePair<string,
}
}

// There is a race with Snapshot:
// Update() updates the value
// Snapshot snapshots the value
// Snapshot sets status to NoCollectPending
// Update sets status to CollectPending -- this is not right as the Snapshot
// already included the updated value.
// In the absence of any new Update call until next Snapshot,
// this results in exporting an Update even though
// it had no update.
// TODO: For Delta, this can be mitigated
// by ignoring Zero points
this.MetricPointStatus = MetricPointStatus.CollectPending;

if (this.aggregatorStore.OutputDeltaWithUnusedMetricPointReclaimEnabled)
{
Interlocked.Decrement(ref this.ReferenceCount);
}
this.CompleteUpdate();
}

internal void Update(double number)
Expand Down Expand Up @@ -651,23 +619,7 @@ internal void Update(double number)
}
}

// There is a race with Snapshot:
// Update() updates the value
// Snapshot snapshots the value
// Snapshot sets status to NoCollectPending
// Update sets status to CollectPending -- this is not right as the Snapshot
// already included the updated value.
// In the absence of any new Update call until next Snapshot,
// this results in exporting an Update even though
// it had no update.
// TODO: For Delta, this can be mitigated
// by ignoring Zero points
this.MetricPointStatus = MetricPointStatus.CollectPending;

if (this.aggregatorStore.OutputDeltaWithUnusedMetricPointReclaimEnabled)
{
Interlocked.Decrement(ref this.ReferenceCount);
}
this.CompleteUpdate();
}

internal void UpdateWithExemplar(double number, ReadOnlySpan<KeyValuePair<string, object?>> tags, bool isSampled)
Expand Down Expand Up @@ -785,23 +737,7 @@ internal void UpdateWithExemplar(double number, ReadOnlySpan<KeyValuePair<string
}
}

// There is a race with Snapshot:
// Update() updates the value
// Snapshot snapshots the value
// Snapshot sets status to NoCollectPending
// Update sets status to CollectPending -- this is not right as the Snapshot
// already included the updated value.
// In the absence of any new Update call until next Snapshot,
// this results in exporting an Update even though
// it had no update.
// TODO: For Delta, this can be mitigated
// by ignoring Zero points
this.MetricPointStatus = MetricPointStatus.CollectPending;

if (this.aggregatorStore.OutputDeltaWithUnusedMetricPointReclaimEnabled)
{
Interlocked.Decrement(ref this.ReferenceCount);
}
this.CompleteUpdate();
}

internal void TakeSnapshot(bool outputDelta)
Expand Down Expand Up @@ -1495,6 +1431,28 @@ private void UpdateBase2ExponentialHistogramWithMinMax(double number, ReadOnlySp
this.mpComponents.ReleaseLock();
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void CompleteUpdate()
{
// There is a race with Snapshot:
// Update() updates the value
// Snapshot snapshots the value
// Snapshot sets status to NoCollectPending
// Update sets status to CollectPending -- this is not right as the Snapshot
// already included the updated value.
// In the absence of any new Update call until next Snapshot,
// this results in exporting an Update even though
// it had no update.
// TODO: For Delta, this can be mitigated
// by ignoring Zero points
this.MetricPointStatus = MetricPointStatus.CollectPending;

if (this.aggregatorStore.OutputDeltaWithUnusedMetricPointReclaimEnabled)
{
Interlocked.Decrement(ref this.ReferenceCount);
}
}

[MethodImpl(MethodImplOptions.NoInlining)]
private readonly void ThrowNotSupportedMetricTypeException(string methodName)
{
Expand Down

0 comments on commit 7ce0c55

Please sign in to comment.