Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
bf8b6dd
WIP to update exmplar code.
CodeBlanch Feb 13, 2024
daf57cd
First compiling version.
CodeBlanch Feb 14, 2024
c23e7bf
Tweak.
CodeBlanch Feb 14, 2024
feb1c7e
Improvements.
CodeBlanch Feb 14, 2024
4e97f10
API updates.
CodeBlanch Feb 14, 2024
05e058a
Tweak.
CodeBlanch Feb 14, 2024
4542e83
Tweaks.
CodeBlanch Feb 14, 2024
82e6fa6
Better concurrency. Bug fixes.
CodeBlanch Feb 14, 2024
6b20ac3
Tweaks.
CodeBlanch Feb 14, 2024
4c849be
Tweaks.
CodeBlanch Feb 14, 2024
de1856a
Tweaks.
CodeBlanch Feb 14, 2024
ea58f75
Tweak.
CodeBlanch Feb 15, 2024
369738b
Tweak.
CodeBlanch Feb 15, 2024
645bb75
Merge from main.
CodeBlanch Feb 15, 2024
17cbe69
Cleanup.
CodeBlanch Feb 15, 2024
39230c9
Cleanup.
CodeBlanch Feb 15, 2024
7a8669c
Tweaks.
CodeBlanch Feb 15, 2024
65e87eb
CHANGELOG patch.
CodeBlanch Feb 15, 2024
aa0b3b5
Faster init for FixedSizeExemplarReservoir buffers.
CodeBlanch Feb 16, 2024
b21ba8d
Merge from main.
CodeBlanch Feb 16, 2024
ea2f149
Tweaks.
CodeBlanch Feb 16, 2024
ee7a978
Tweak.
CodeBlanch Feb 18, 2024
6ccfd4d
Tweaks.
CodeBlanch Feb 18, 2024
b2b327a
Tweak.
CodeBlanch Feb 18, 2024
04d56ca
Merge from main.
CodeBlanch Feb 20, 2024
a6011de
WIP
CodeBlanch Feb 20, 2024
015670a
Tweaks.
CodeBlanch Feb 21, 2024
fb521aa
Test improvements and tweaks.
CodeBlanch Feb 21, 2024
8bde052
Code review.
CodeBlanch Feb 21, 2024
c2c7f0a
Code review.
CodeBlanch Feb 21, 2024
0ffba7d
Code review.
CodeBlanch Feb 21, 2024
fb9b07a
Code review.
CodeBlanch Feb 21, 2024
4512b49
Merge remote-tracking branch 'upstream/main' into sdk-exemplar-spec-i…
CodeBlanch Feb 22, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Tweaks.
  • Loading branch information
CodeBlanch committed Feb 16, 2024
commit ea2f149d84f6dfaa5d669eaba85f7089b8dfd3e6
16 changes: 9 additions & 7 deletions src/OpenTelemetry/Metrics/Exemplar/Exemplar.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics;
#if EXPOSE_EXPERIMENTAL_FEATURES && NET8_0_OR_GREATER
using System.Diagnostics.CodeAnalysis;
#endif
using System.Runtime.CompilerServices;
#if EXPOSE_EXPERIMENTAL_FEATURES && NET8_0_OR_GREATER
using OpenTelemetry.Internal;
#endif

using System.Diagnostics;

namespace OpenTelemetry.Metrics;

#if EXPOSE_EXPERIMENTAL_FEATURES
Expand All @@ -32,7 +34,7 @@ struct Exemplar
private int tagCount;
private KeyValuePair<string, object?>[]? tagStorage;
private MetricPointValueStorage valueStorage;
private volatile int isCriticalSectionOccupied;
private int isCriticalSectionOccupied;

/// <summary>
/// Gets the timestamp.
Expand Down Expand Up @@ -76,7 +78,7 @@ public readonly ReadOnlyFilteredTagCollection FilteredTags
internal void Update<T>(in ExemplarMeasurement<T> measurement)
where T : struct
{
if (Interlocked.CompareExchange(ref this.isCriticalSectionOccupied, 1, 0) != 0)
if (Interlocked.Exchange(ref this.isCriticalSectionOccupied, 1) != 0)
{
// Some other thread is already writing, abort.
return;
Expand Down Expand Up @@ -112,7 +114,7 @@ internal void Update<T>(in ExemplarMeasurement<T> measurement)

this.StoreRawTags(measurement.Tags);

this.isCriticalSectionOccupied = 0;
Volatile.Write(ref this.isCriticalSectionOccupied, 0);
}

internal void Reset()
Expand All @@ -122,7 +124,7 @@ internal void Reset()

internal readonly bool IsUpdated()
{
if (this.isCriticalSectionOccupied != 0)
if (Volatile.Read(ref Unsafe.AsRef(in this.isCriticalSectionOccupied)) != 0)
{
this.WaitForUpdateToCompleteRare();
return true;
Expand Down Expand Up @@ -171,6 +173,6 @@ private readonly void WaitForUpdateToCompleteRare()
{
spinWait.SpinOnce();
}
while (this.isCriticalSectionOccupied != 0);
while (Volatile.Read(ref Unsafe.AsRef(in this.isCriticalSectionOccupied)) != 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ internal abstract class FixedSizeExemplarReservoir : ExemplarReservoir
{
private readonly Exemplar[] bufferA;
private readonly Exemplar[] bufferB;
private volatile Exemplar[]? activeBuffer;
private Exemplar[]? activeBuffer;

protected FixedSizeExemplarReservoir(int capacity)
{
Expand All @@ -34,11 +34,11 @@ protected FixedSizeExemplarReservoir(int capacity)
/// <returns><see cref="ReadOnlyExemplarCollection"/>.</returns>
public sealed override ReadOnlyExemplarCollection Collect()
{
var currentBuffer = this.activeBuffer;
var currentBuffer = Volatile.Read(ref this.activeBuffer);

Debug.Assert(currentBuffer != null, "currentBuffer was null");

this.activeBuffer = null;
Volatile.Write(ref this.activeBuffer, null);

var inactiveBuffer = currentBuffer == this.bufferA
? this.bufferB
Expand All @@ -54,7 +54,7 @@ public sealed override ReadOnlyExemplarCollection Collect()

this.OnCollectionCompleted();

this.activeBuffer = inactiveBuffer;
Volatile.Write(ref this.activeBuffer, inactiveBuffer);

return new(currentBuffer!);
}
Expand Down Expand Up @@ -95,7 +95,7 @@ protected virtual void OnCollectionCompleted()
protected void UpdateExemplar<T>(int exemplarIndex, in ExemplarMeasurement<T> measurement)
where T : struct
{
var activeBuffer = this.activeBuffer;
var activeBuffer = Volatile.Read(ref this.activeBuffer);
if (activeBuffer == null)
{
// Note: This is expected to happen when we race with a collection.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace OpenTelemetry.Metrics;
/// </remarks>
internal sealed class SimpleFixedSizeExemplarReservoir : FixedSizeExemplarReservoir
{
private volatile int measurementsSeen;
private int measurementsSeen;

public SimpleFixedSizeExemplarReservoir()
: this(Environment.ProcessorCount)
Expand All @@ -41,7 +41,7 @@ protected override void OnCollectionCompleted()
// Reset internal state irrespective of temporality.
// This ensures incoming measurements have fair chance
// of making it to the reservoir.
this.measurementsSeen = 0;
Volatile.Write(ref this.measurementsSeen, 0);
}

private void Offer<T>(in ExemplarMeasurement<T> measurement)
Expand Down