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

Measurement type conversion done at the MeterProvider. #2136

Merged
merged 2 commits into from
Jul 14, 2021
Merged
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
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Metrics/AggregatorStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ internal MetricAgg[] MapToMetrics(string[] seqKey, object[] seqVal)

if (this.instrument.GetType().Name.Contains("Counter"))
{
metricpairs.Add(new MetricAgg(timeperiod, new SumMetricAggregator(name, dt, tags, true, true)));
metricpairs.Add(new MetricAgg(timeperiod, new SumMetricAggregator(name, dt, tags, true)));
}
else if (this.instrument.GetType().Name.Contains("Gauge"))
{
Expand Down
11 changes: 7 additions & 4 deletions src/OpenTelemetry/Metrics/MeterProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,15 @@ internal MeterProviderSdk(
MeasurementsCompleted = (instrument, state) => this.MeasurementsCompleted(instrument, state),
};

// Everything double
this.listener.SetMeasurementEventCallback<double>((i, m, l, c) => this.MeasurementRecorded(i, m, l, c));
this.listener.SetMeasurementEventCallback<float>((i, m, l, c) => this.MeasurementRecorded(i, m, l, c));
this.listener.SetMeasurementEventCallback<float>((i, m, l, c) => this.MeasurementRecorded(i, (double)m, l, c));

// Everything long
this.listener.SetMeasurementEventCallback<long>((i, m, l, c) => this.MeasurementRecorded(i, m, l, c));
this.listener.SetMeasurementEventCallback<int>((i, m, l, c) => this.MeasurementRecorded(i, m, l, c));
this.listener.SetMeasurementEventCallback<short>((i, m, l, c) => this.MeasurementRecorded(i, m, l, c));
this.listener.SetMeasurementEventCallback<byte>((i, m, l, c) => this.MeasurementRecorded(i, m, l, c));
this.listener.SetMeasurementEventCallback<int>((i, m, l, c) => this.MeasurementRecorded(i, (long)m, l, c));
this.listener.SetMeasurementEventCallback<short>((i, m, l, c) => this.MeasurementRecorded(i, (long)m, l, c));
this.listener.SetMeasurementEventCallback<byte>((i, m, l, c) => this.MeasurementRecorded(i, (long)m, l, c));

this.listener.Start();

Expand Down
99 changes: 22 additions & 77 deletions src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,17 @@ internal class SumMetricAggregator : ISumMetric, IAggregator
{
private readonly object lockUpdate = new object();
private Type valueType;
private long sumPos = 0;
private double dsumPos = 0;
private long countPos = 0;
private long sumNeg = 0;
private double dsumNeg = 0;
private long countNeg = 0;

internal SumMetricAggregator(string name, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] attributes, bool isDelta, bool isMonotonic)
private long sumLong = 0;
private double sumDouble = 0;

internal SumMetricAggregator(string name, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] attributes, bool isDelta)
{
this.Name = name;
this.StartTimeExclusive = startTimeExclusive;
this.EndTimeInclusive = startTimeExclusive;
this.Attributes = attributes;
this.IsDeltaTemporality = isDelta;
this.IsMonotonic = isMonotonic;
this.IsMonotonic = true;
}

public string Name { get; private set; }
Expand All @@ -60,33 +56,11 @@ public IDataValue Sum
{
if (this.valueType == typeof(long))
{
long sum;

if (this.IsMonotonic)
{
sum = this.sumPos + (long)this.dsumPos;
}
else
{
sum = this.sumPos + (long)this.dsumPos + this.sumNeg + (long)this.dsumNeg;
}

return new DataValue(sum);
return new DataValue(this.sumLong);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the public IDataValue Sum is not thread-safe. We may avoid that issue if we refactor to avoid SumMetricAggregator implementing both ISumMetric and IAggregator intefaces. The latter represents the thing which does the aggregation, and former is the "aggregation output". Will tackle this in another PR.

}
else if (this.valueType == typeof(double))
{
double sum;

if (this.IsMonotonic)
{
sum = this.dsumPos + (double)this.sumPos;
}
else
{
sum = this.dsumPos + (double)this.sumPos + this.dsumNeg + (double)this.sumNeg;
}

return new DataValue(sum);
return new DataValue(this.sumDouble);
}

throw new Exception("Unsupported Type");
Expand All @@ -100,53 +74,32 @@ public void Update<T>(DateTimeOffset dt, T value)
{
this.EndTimeInclusive = dt;

if (typeof(T) == typeof(int))
{
// Promote to Long
this.valueType = typeof(long);
var val = (long)(int)(object)value;

if (val >= 0)
{
this.sumPos += val;
this.countPos++;
}
else
{
this.sumNeg += val;
this.countNeg++;
}
}
else if (typeof(T) == typeof(long))
if (typeof(T) == typeof(long))
{
this.valueType = typeof(T);
var val = (long)(object)value;

if (val >= 0)
if (val < 0)
{
this.sumPos += val;
this.countPos++;
// TODO: log?
// Also, this validation can be done in earlier stage.
}
else
{
this.sumNeg += val;
this.countNeg++;
this.sumLong += val;
}
}
else if (typeof(T) == typeof(double))
{
this.valueType = typeof(T);
var val = (double)(object)value;

if (val >= 0)
if (val < 0)
{
this.dsumPos += val;
this.countPos++;
// TODO: log?
// Also, this validation can be done in earlier stage.
}
else
{
this.dsumNeg += val;
this.countNeg++;
this.sumDouble += val;
}
}
else
Expand All @@ -158,29 +111,21 @@ public void Update<T>(DateTimeOffset dt, T value)

public IMetric Collect(DateTimeOffset dt)
{
var cloneItem = new SumMetricAggregator(this.Name, this.StartTimeExclusive, this.Attributes, this.IsDeltaTemporality, this.IsMonotonic);
var cloneItem = new SumMetricAggregator(this.Name, this.StartTimeExclusive, this.Attributes, this.IsDeltaTemporality);

lock (this.lockUpdate)
{
cloneItem.Exemplars = this.Exemplars;
cloneItem.EndTimeInclusive = dt;
cloneItem.valueType = this.valueType;
cloneItem.countPos = this.countPos;
cloneItem.sumPos = this.sumPos;
cloneItem.dsumPos = this.dsumPos;
cloneItem.countNeg = this.countNeg;
cloneItem.sumNeg = this.sumNeg;
cloneItem.dsumNeg = this.dsumNeg;
cloneItem.sumLong = this.sumLong;
cloneItem.sumDouble = this.sumDouble;

if (this.IsDeltaTemporality)
{
this.StartTimeExclusive = dt;
this.countPos = 0;
this.sumPos = 0;
this.dsumPos = 0;
this.countNeg = 0;
this.sumNeg = 0;
this.dsumNeg = 0;
this.sumLong = 0;
this.sumDouble = 0;
}
}

Expand All @@ -189,7 +134,7 @@ public IMetric Collect(DateTimeOffset dt)

public string ToDisplayString()
{
return $"Delta={this.IsDeltaTemporality},Mon={this.IsMonotonic},Count={this.countPos},Sum={this.Sum.Value}";
return $"Delta={this.IsDeltaTemporality},Monotonic={this.IsMonotonic},Sum={this.Sum.Value}";
}
}
}