Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Move MeterProvider to API #592

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@

namespace OpenTelemetry.Metrics.Aggregators
{
public abstract class Aggregator<T> where T : struct
public abstract class Aggregator<T>
where T : struct
{
public abstract void Update(T value);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public CounterSumAggregator()
}
}

/// <inheritdoc/>
public override void Checkpoint()
{
// checkpoints the current running sum into checkpoint, and starts counting again.
Expand All @@ -52,6 +53,7 @@ public override void Checkpoint()
}
}

/// <inheritdoc/>
public override MetricData<T> ToMetricData()
{
var sumData = new SumData<T>();
Expand All @@ -60,6 +62,7 @@ public override MetricData<T> ToMetricData()
return sumData;
}

/// <inheritdoc/>
public override AggregationType GetAggregationType()
{
if (typeof(T) == typeof(double))
Expand All @@ -69,9 +72,10 @@ public override AggregationType GetAggregationType()
else
{
return AggregationType.LongSum;
}
}
}

/// <inheritdoc/>
public override void Update(T value)
{
// Adds value to the running total in a thread safe manner.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@ public LastValueAggregator()
}
}

/// <inheritdoc/>
public override void Checkpoint()
{
this.checkpoint = this.value;
}

/// <inheritdoc/>
public override MetricData<T> ToMetricData()
{
var sumData = new SumData<T>();
Expand All @@ -50,6 +52,7 @@ public override MetricData<T> ToMetricData()
return sumData;
}

/// <inheritdoc/>
public override AggregationType GetAggregationType()
{
if (typeof(T) == typeof(double))
Expand All @@ -62,6 +65,7 @@ public override AggregationType GetAggregationType()
}
}

/// <inheritdoc/>
public override void Update(T newValue)
{
this.value = newValue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace OpenTelemetry.Metrics.Aggregators
/// Aggregator which calculates summary (Min,Max,Sum,Count) from measures.
/// </summary>
/// <typeparam name="T">Type of measure instrument.</typeparam>
public class MeasureMinMaxSumCountAggregator<T> : Aggregator<T>
public class MeasureMinMaxSumCountAggregator<T> : Aggregator<T>
where T : struct
{
private Summary<T> summary;
Expand All @@ -42,16 +42,19 @@ public MeasureMinMaxSumCountAggregator()
this.checkPoint = new Summary<T>();
}

/// <inheritdoc/>
public override void Checkpoint()
{
this.checkPoint = Interlocked.Exchange(ref this.summary, new Summary<T>());
}

/// <inheritdoc/>
public override AggregationType GetAggregationType()
{
return AggregationType.Summary;
}

/// <inheritdoc/>
public override MetricData<T> ToMetricData()
{
var summaryData = new SummaryData<T>();
Expand All @@ -63,11 +66,12 @@ public override MetricData<T> ToMetricData()
return summaryData;
}

/// <inheritdoc/>
public override void Update(T value)
{
lock (this.updateLock)
{
this.summary.Count++;
this.summary.Count++;
if (typeof(T) == typeof(double))
{
this.summary.Sum = (T)(object)((double)(object)this.summary.Sum + (double)(object)value);
Expand All @@ -83,7 +87,8 @@ public override void Update(T value)
}
}

private class Summary<Type> where Type : struct
private class Summary<Type>
where Type : struct
{
public long Count;
public Type Min;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ internal BoundCounterMetricSdk()
}
}

internal BoundCounterMetricSdk(RecordStatus recordStatus) : this()
internal BoundCounterMetricSdk(RecordStatus recordStatus)
: this()
{
this.Status = recordStatus;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,16 +36,16 @@ private MeterFactory(MetricProcessor metricProcessor)
{
this.metricProcessor = metricProcessor;
}

this.defaultMeter = new MeterSdk(string.Empty,
this.metricProcessor);

this.defaultMeter = new MeterSdk(string.Empty, this.metricProcessor);
}

public static MeterFactory Create(MetricProcessor metricProcessor)
{
return new MeterFactory(metricProcessor);
}

/// <inheritdoc/>
public override Meter GetMeter(string name, string version = null)
{
if (string.IsNullOrEmpty(name))
Expand All @@ -58,8 +58,7 @@ public override Meter GetMeter(string name, string version = null)
var key = new MeterRegistryKey(name, version);
if (!this.meterRegistry.TryGetValue(key, out var meter))
{
meter = this.defaultMeter = new MeterSdk(name,
this.metricProcessor);
meter = this.defaultMeter = new MeterSdk(name, this.metricProcessor);

this.meterRegistry.Add(key, meter);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ internal class CounterMetricSdk<T> : CounterMetric<T>
{
private readonly IDictionary<LabelSet, BoundCounterMetricSdk<T>> counterBoundInstruments = new ConcurrentDictionary<LabelSet, BoundCounterMetricSdk<T>>();
private string metricName;

// Lock used to sync with Bind/UnBind.
private object bindUnbindLock = new object();

Expand All @@ -38,7 +39,8 @@ public CounterMetricSdk()
}
}

public CounterMetricSdk(string name) : this()
public CounterMetricSdk(string name)
: this()
{
this.metricName = name;
}
Expand Down Expand Up @@ -106,7 +108,7 @@ internal BoundCounterMetric<T> Bind(LabelSet labelset, bool isShortLived)
* But it gets added again by Bind() so no record is lost.
* If Bind method gets this lock first, it'd promote record to UpdatePending, so that
* Unbind will leave this record untouched.
*
* Additional notes:
* This lock is never taken for bound instruments, and they offer the fastest performance.
* This lock is only taken for those labelsets which are marked CandidateForRemoval.
Expand All @@ -119,7 +121,7 @@ internal BoundCounterMetric<T> Bind(LabelSet labelset, bool isShortLived)
*
* Its important to note that, for a brand new LabelSet being encountered for the 1st time, lock is not
* taken. Lock is taken only during the 1st re-appearance of a LabelSet after a Collect period.
*
*
*/

lock (this.bindUnbindLock)
Expand Down Expand Up @@ -148,7 +150,7 @@ internal void UnBind(LabelSet labelSet)
this.counterBoundInstruments.Remove(labelSet);
}
}
}
}
}

internal IDictionary<LabelSet, BoundCounterMetricSdk<T>> GetAllBoundInstruments()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public override void Observe(double value, LabelSet labelset)
if (!this.observerHandles.TryGetValue(labelset, out var boundInstrument))
{
boundInstrument = new DoubleObserverMetricHandleSdk();

// TODO cleanup of handle/aggregator. Issue #530
this.observerHandles.Add(labelset, boundInstrument);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@ public abstract class MetricProcessor
/// <param name="metricName">the name of the instrument.</param>
/// <param name="labelSet">the labelSet associated with the instrument.</param>
/// <param name="aggregator">the aggregator used.</param>
public abstract void Process(string meterName, string metricName, LabelSet labelSet, Aggregator<double> aggregator);
public abstract void Process(string meterName, string metricName, LabelSet labelSet, Aggregator<double> aggregator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ public override void Observe(long value)
internal LastValueAggregator<long> GetAggregator()
{
return this.aggregator;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public override void Observe(long value, LabelSet labelset)
if (!this.observerHandles.TryGetValue(labelset, out var boundInstrument))
{
boundInstrument = new Int64ObserverMetricHandleSdk();

// TODO cleanup of handle/aggregator. Issue #530
this.observerHandles.Add(labelset, boundInstrument);
}
Expand All @@ -57,6 +58,6 @@ public void InvokeCallback()
internal IDictionary<LabelSet, Int64ObserverMetricHandleSdk> GetAllHandles()
{
return this.observerHandles;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal class LabelSetSdk : LabelSet
private string labelSetEncoded;

/// <summary>
/// Initializes a new instance of the <see cref="LabelSet"/> class.
/// Initializes a new instance of the <see cref="LabelSetSdk"/> class.
/// </summary>
/// <param name="labels">labels from which labelset should be constructed.</param>
internal LabelSetSdk(IEnumerable<KeyValuePair<string, string>> labels)
Expand Down Expand Up @@ -74,7 +74,7 @@ private IEnumerable<KeyValuePair<string, string>> SortAndDedup(IEnumerable<KeyVa
int dedupedListIndex = 0;
dedupedList.Add(orderedList[dedupedListIndex]);
for (int i = 1; i < orderedList.Count; i++)
{
{
if (orderedList[i].Key.Equals(orderedList[i - 1].Key))
{
dedupedList[dedupedListIndex] = orderedList[i];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public MeasureMetricSdk()
}
}

public MeasureMetricSdk(string name) : this()
public MeasureMetricSdk(string name)
: this()
{
this.metricName = name;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal MeterSdk(string meterName, MetricProcessor metricProcessor)
this.metricProcessor = metricProcessor;
}

/// <inheritdoc/>
public override LabelSet GetLabelSet(IEnumerable<KeyValuePair<string, string>> labels)
{
return new LabelSetSdk(labels);
Expand Down Expand Up @@ -70,7 +71,7 @@ public void Collect()
// NoPendingUpdate, to CandidateForRemoval, to physical removal.
// i.e UpdatePending->NoPendingUpdate->CandidateForRemoval->removal
if (handle.Value.Status == RecordStatus.CandidateForRemoval)
{
{
// The actual removal doesn't occur here as we are still
// iterating the dictionary.
boundInstrumentsToRemove.Add(labelSet);
Expand Down Expand Up @@ -213,6 +214,7 @@ public void Collect()
}
}

/// <inheritdoc/>
public override CounterMetric<long> CreateInt64Counter(string name, bool monotonic = true)
{
if (!this.longCounters.TryGetValue(name, out var counter))
Expand All @@ -225,6 +227,7 @@ public override CounterMetric<long> CreateInt64Counter(string name, bool monoton
return counter;
}

/// <inheritdoc/>
public override CounterMetric<double> CreateDoubleCounter(string name, bool monotonic = true)
{
if (!this.doubleCounters.TryGetValue(name, out var counter))
Expand All @@ -236,6 +239,7 @@ public override CounterMetric<double> CreateDoubleCounter(string name, bool mono
return counter;
}

/// <inheritdoc/>
public override MeasureMetric<double> CreateDoubleMeasure(string name, bool absolute = true)
{
if (!this.doubleMeasures.TryGetValue(name, out var measure))
Expand All @@ -248,6 +252,7 @@ public override MeasureMetric<double> CreateDoubleMeasure(string name, bool abso
return measure;
}

/// <inheritdoc/>
public override MeasureMetric<long> CreateInt64Measure(string name, bool absolute = true)
{
if (!this.longMeasures.TryGetValue(name, out var measure))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ internal enum RecordStatus
UpdatePending,

/// <summary>
/// This status is applied to UpdatePending instruments after Collect() is done.
/// This status is applied to UpdatePending instruments after Collect() is done.
/// This will be moved to CandidateForRemoval during the next Collect() cycle.
/// If an update occurs, the instrument promotes them to UpdatePending.
/// </summary>
Expand Down