Update Metric Point for Counter and Gauge (#2667)

This commit is contained in:
Utkarsh Umesan Pillai 2021-11-24 11:46:06 -08:00 committed by GitHub
parent f7c718eae0
commit 9e83587a55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 172 additions and 58 deletions

View File

@ -138,18 +138,32 @@ namespace OpenTelemetry.Exporter
}
else if (metricType.IsDouble())
{
valueDisplay = metricPoint.DoubleValue.ToString(CultureInfo.InvariantCulture);
if (metricType.IsSum())
{
valueDisplay = metricPoint.GetCounterSumDouble().ToString(CultureInfo.InvariantCulture);
}
else
{
valueDisplay = metricPoint.GetGaugeLastValueDouble().ToString(CultureInfo.InvariantCulture);
}
}
else if (metricType.IsLong())
{
valueDisplay = metricPoint.LongValue.ToString(CultureInfo.InvariantCulture);
if (metricType.IsSum())
{
valueDisplay = metricPoint.GetCounterSumLong().ToString(CultureInfo.InvariantCulture);
}
else
{
valueDisplay = metricPoint.GetGaugeLastValueLong().ToString(CultureInfo.InvariantCulture);
}
}
msg = new StringBuilder();
msg.Append('(');
msg.Append(metricPoint.StartTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ", CultureInfo.InvariantCulture));
msg.Append(metricPoint.GetStartTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ", CultureInfo.InvariantCulture));
msg.Append(", ");
msg.Append(metricPoint.EndTime.ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ", CultureInfo.InvariantCulture));
msg.Append(metricPoint.GetEndTime().ToString("yyyy-MM-ddTHH:mm:ss.fffffffZ", CultureInfo.InvariantCulture));
msg.Append("] ");
msg.Append(tags);
if (tags != string.Empty)

View File

@ -152,13 +152,13 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation
{
var dataPoint = new OtlpMetrics.NumberDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
StartTimeUnixNano = (ulong)metricPoint.GetStartTime().ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.GetEndTime().ToUnixTimeNanoseconds(),
};
AddAttributes(metricPoint.Tags, dataPoint.Attributes);
dataPoint.AsInt = metricPoint.LongValue;
dataPoint.AsInt = metricPoint.GetCounterSumLong();
sum.DataPoints.Add(dataPoint);
}
@ -176,13 +176,13 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation
{
var dataPoint = new OtlpMetrics.NumberDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
StartTimeUnixNano = (ulong)metricPoint.GetStartTime().ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.GetEndTime().ToUnixTimeNanoseconds(),
};
AddAttributes(metricPoint.Tags, dataPoint.Attributes);
dataPoint.AsDouble = metricPoint.DoubleValue;
dataPoint.AsDouble = metricPoint.GetCounterSumDouble();
sum.DataPoints.Add(dataPoint);
}
@ -197,13 +197,13 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation
{
var dataPoint = new OtlpMetrics.NumberDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
StartTimeUnixNano = (ulong)metricPoint.GetStartTime().ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.GetEndTime().ToUnixTimeNanoseconds(),
};
AddAttributes(metricPoint.Tags, dataPoint.Attributes);
dataPoint.AsInt = metricPoint.LongValue;
dataPoint.AsInt = metricPoint.GetGaugeLastValueLong();
gauge.DataPoints.Add(dataPoint);
}
@ -218,13 +218,13 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation
{
var dataPoint = new OtlpMetrics.NumberDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
StartTimeUnixNano = (ulong)metricPoint.GetStartTime().ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.GetEndTime().ToUnixTimeNanoseconds(),
};
AddAttributes(metricPoint.Tags, dataPoint.Attributes);
dataPoint.AsDouble = metricPoint.DoubleValue;
dataPoint.AsDouble = metricPoint.GetGaugeLastValueDouble();
gauge.DataPoints.Add(dataPoint);
}
@ -241,8 +241,8 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation
{
var dataPoint = new OtlpMetrics.HistogramDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
StartTimeUnixNano = (ulong)metricPoint.GetStartTime().ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.GetEndTime().ToUnixTimeNanoseconds(),
};
AddAttributes(metricPoint.Tags, dataPoint.Attributes);

View File

@ -40,7 +40,7 @@ namespace OpenTelemetry.Exporter.Prometheus
foreach (ref var metricPoint in metric.GetMetricPoints())
{
var tags = metricPoint.Tags;
var timestamp = metricPoint.EndTime.ToUnixTimeMilliseconds();
var timestamp = metricPoint.GetEndTime().ToUnixTimeMilliseconds();
// Counter and Gauge
cursor = WriteMetricName(buffer, cursor, metric.Name, metric.Unit);
@ -70,11 +70,25 @@ namespace OpenTelemetry.Exporter.Prometheus
// for each MetricPoint
if (((int)metric.MetricType & 0b_0000_1111) == 0x0a /* I8 */)
{
cursor = WriteLong(buffer, cursor, metricPoint.LongValue);
if (metric.MetricType.IsSum())
{
cursor = WriteLong(buffer, cursor, metricPoint.GetCounterSumLong());
}
else
{
cursor = WriteLong(buffer, cursor, metricPoint.GetGaugeLastValueLong());
}
}
else
{
cursor = WriteDouble(buffer, cursor, metricPoint.DoubleValue);
if (metric.MetricType.IsSum())
{
cursor = WriteDouble(buffer, cursor, metricPoint.GetCounterSumDouble());
}
else
{
cursor = WriteDouble(buffer, cursor, metricPoint.GetGaugeLastValueDouble());
}
}
buffer[cursor++] = unchecked((byte)' ');
@ -89,7 +103,7 @@ namespace OpenTelemetry.Exporter.Prometheus
foreach (ref var metricPoint in metric.GetMetricPoints())
{
var tags = metricPoint.Tags;
var timestamp = metricPoint.EndTime.ToUnixTimeMilliseconds();
var timestamp = metricPoint.GetEndTime().ToUnixTimeMilliseconds();
long totalCount = 0;
foreach (var histogramMeasurement in metricPoint.GetHistogramBuckets())

View File

@ -21,7 +21,13 @@ OpenTelemetry.Metrics.HistogramBuckets.Enumerator.Current.get -> OpenTelemetry.M
OpenTelemetry.Metrics.HistogramBuckets.Enumerator.Enumerator() -> void
OpenTelemetry.Metrics.HistogramBuckets.Enumerator.MoveNext() -> bool
OpenTelemetry.Metrics.HistogramBuckets.GetEnumerator() -> OpenTelemetry.Metrics.HistogramBuckets.Enumerator
OpenTelemetry.Metrics.MetricPoint.GetCounterSumDouble() -> double
OpenTelemetry.Metrics.MetricPoint.GetCounterSumLong() -> long
OpenTelemetry.Metrics.MetricPoint.GetEndTime() -> System.DateTimeOffset
OpenTelemetry.Metrics.MetricPoint.GetGaugeLastValueDouble() -> double
OpenTelemetry.Metrics.MetricPoint.GetGaugeLastValueLong() -> long
OpenTelemetry.Metrics.MetricPoint.GetHistogramBuckets() -> OpenTelemetry.Metrics.HistogramBuckets
OpenTelemetry.Metrics.MetricPoint.GetStartTime() -> System.DateTimeOffset
OpenTelemetry.Metrics.MetricPointsAccessor
OpenTelemetry.Metrics.MetricPointsAccessor.MetricPointsAccessor() -> void
OpenTelemetry.Metrics.MetricPointsAccessor.Dispose() -> void
@ -59,13 +65,9 @@ OpenTelemetry.Metrics.Metric.Name.get -> string
OpenTelemetry.Metrics.Metric.Temporality.get -> OpenTelemetry.Metrics.AggregationTemporality
OpenTelemetry.Metrics.Metric.Unit.get -> string
OpenTelemetry.Metrics.MetricPoint
OpenTelemetry.Metrics.MetricPoint.DoubleValue.get -> double
OpenTelemetry.Metrics.MetricPoint.EndTime.get -> System.DateTimeOffset
OpenTelemetry.Metrics.MetricPoint.GetHistogramCount() -> long
OpenTelemetry.Metrics.MetricPoint.GetHistogramSum() -> double
OpenTelemetry.Metrics.MetricPoint.LongValue.get -> long
OpenTelemetry.Metrics.MetricPoint.MetricPoint() -> void
OpenTelemetry.Metrics.MetricPoint.StartTime.get -> System.DateTimeOffset
OpenTelemetry.Metrics.MetricPoint.Tags.get -> OpenTelemetry.ReadOnlyTagCollection
OpenTelemetry.Metrics.MetricReader
OpenTelemetry.Metrics.MetricReader.Collect(int timeoutMilliseconds = -1) -> bool

View File

@ -21,7 +21,13 @@ OpenTelemetry.Metrics.HistogramBuckets.Enumerator.Current.get -> OpenTelemetry.M
OpenTelemetry.Metrics.HistogramBuckets.Enumerator.Enumerator() -> void
OpenTelemetry.Metrics.HistogramBuckets.Enumerator.MoveNext() -> bool
OpenTelemetry.Metrics.HistogramBuckets.GetEnumerator() -> OpenTelemetry.Metrics.HistogramBuckets.Enumerator
OpenTelemetry.Metrics.MetricPoint.GetCounterSumDouble() -> double
OpenTelemetry.Metrics.MetricPoint.GetCounterSumLong() -> long
OpenTelemetry.Metrics.MetricPoint.GetEndTime() -> System.DateTimeOffset
OpenTelemetry.Metrics.MetricPoint.GetGaugeLastValueDouble() -> double
OpenTelemetry.Metrics.MetricPoint.GetGaugeLastValueLong() -> long
OpenTelemetry.Metrics.MetricPoint.GetHistogramBuckets() -> OpenTelemetry.Metrics.HistogramBuckets
OpenTelemetry.Metrics.MetricPoint.GetStartTime() -> System.DateTimeOffset
OpenTelemetry.Metrics.MetricPointsAccessor
OpenTelemetry.Metrics.MetricPointsAccessor.MetricPointsAccessor() -> void
OpenTelemetry.Metrics.MetricPointsAccessor.Dispose() -> void
@ -59,13 +65,9 @@ OpenTelemetry.Metrics.Metric.Name.get -> string
OpenTelemetry.Metrics.Metric.Temporality.get -> OpenTelemetry.Metrics.AggregationTemporality
OpenTelemetry.Metrics.Metric.Unit.get -> string
OpenTelemetry.Metrics.MetricPoint
OpenTelemetry.Metrics.MetricPoint.DoubleValue.get -> double
OpenTelemetry.Metrics.MetricPoint.EndTime.get -> System.DateTimeOffset
OpenTelemetry.Metrics.MetricPoint.GetHistogramCount() -> long
OpenTelemetry.Metrics.MetricPoint.GetHistogramSum() -> double
OpenTelemetry.Metrics.MetricPoint.LongValue.get -> long
OpenTelemetry.Metrics.MetricPoint.MetricPoint() -> void
OpenTelemetry.Metrics.MetricPoint.StartTime.get -> System.DateTimeOffset
OpenTelemetry.Metrics.MetricPoint.Tags.get -> OpenTelemetry.ReadOnlyTagCollection
OpenTelemetry.Metrics.MetricReader
OpenTelemetry.Metrics.MetricReader.Collect(int timeoutMilliseconds = -1) -> bool

View File

@ -24,6 +24,12 @@
* Refactored temporality setting to align with the latest spec.
([#2666](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2666))
* Removed the public properties `LongValue`, `DoubleValue`, `StartTime`, and
`EndTime` in favor of their counterpart public methods `GetCounterSumLong`,
`GetCounterSumDouble`, `GetGaugeLastValueLong`, `GetGaugeLastValueDouble`,
`GetStartTime`, and `GetEndTime`.
([#2667](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2667))
## 1.2.0-beta2
Released 2021-Nov-19

View File

@ -22,11 +22,16 @@ namespace OpenTelemetry.Metrics
{
public struct MetricPoint
{
internal DateTimeOffset StartTime;
internal DateTimeOffset EndTime;
private readonly AggregationType aggType;
private readonly HistogramBuckets histogramBuckets;
private long longVal;
private long longValue;
private long lastLongSum;
private double doubleVal;
private double doubleValue;
private double lastDoubleSum;
internal MetricPoint(
@ -42,10 +47,10 @@ namespace OpenTelemetry.Metrics
this.StartTime = startTime;
this.Tags = new ReadOnlyTagCollection(keys, values);
this.EndTime = default;
this.LongValue = default;
this.longValue = default;
this.longVal = default;
this.lastLongSum = default;
this.DoubleValue = default;
this.doubleValue = default;
this.doubleVal = default;
this.lastDoubleSum = default;
this.MetricPointStatus = MetricPointStatus.NoCollectPending;
@ -69,16 +74,66 @@ namespace OpenTelemetry.Metrics
/// </summary>
public ReadOnlyTagCollection Tags { get; }
public DateTimeOffset StartTime { get; internal set; }
public DateTimeOffset EndTime { get; internal set; }
public long LongValue { get; internal set; }
public double DoubleValue { get; internal set; }
internal MetricPointStatus MetricPointStatus { get; private set; }
public DateTimeOffset GetStartTime()
{
return this.StartTime;
}
public DateTimeOffset GetEndTime()
{
return this.EndTime;
}
public long GetCounterSumLong()
{
if (this.aggType == AggregationType.LongSumIncomingDelta || this.aggType == AggregationType.LongSumIncomingCumulative)
{
return this.longValue;
}
else
{
throw new NotSupportedException($"{nameof(this.GetCounterSumLong)} is not supported for this metric type.");
}
}
public double GetCounterSumDouble()
{
if (this.aggType == AggregationType.DoubleSumIncomingDelta || this.aggType == AggregationType.DoubleSumIncomingCumulative)
{
return this.doubleValue;
}
else
{
throw new NotSupportedException($"{nameof(this.GetCounterSumDouble)} is not supported for this metric type.");
}
}
public long GetGaugeLastValueLong()
{
if (this.aggType == AggregationType.LongGauge)
{
return this.longValue;
}
else
{
throw new NotSupportedException($"{nameof(this.GetGaugeLastValueLong)} is not supported for this metric type.");
}
}
public double GetGaugeLastValueDouble()
{
if (this.aggType == AggregationType.DoubleGauge)
{
return this.doubleValue;
}
else
{
throw new NotSupportedException($"{nameof(this.GetGaugeLastValueDouble)} is not supported for this metric type.");
}
}
public long GetHistogramCount()
{
if (this.aggType == AggregationType.Histogram || this.aggType == AggregationType.HistogramSumCount)
@ -245,7 +300,7 @@ namespace OpenTelemetry.Metrics
if (outputDelta)
{
long initValue = Interlocked.Read(ref this.longVal);
this.LongValue = initValue - this.lastLongSum;
this.longValue = initValue - this.lastLongSum;
this.lastLongSum = initValue;
this.MetricPointStatus = MetricPointStatus.NoCollectPending;
@ -258,7 +313,7 @@ namespace OpenTelemetry.Metrics
}
else
{
this.LongValue = Interlocked.Read(ref this.longVal);
this.longValue = Interlocked.Read(ref this.longVal);
}
break;
@ -275,7 +330,7 @@ namespace OpenTelemetry.Metrics
// the exchange (to 0.0) will never occur,
// but we get the original value atomically.
double initValue = Interlocked.CompareExchange(ref this.doubleVal, 0.0, double.NegativeInfinity);
this.DoubleValue = initValue - this.lastDoubleSum;
this.doubleValue = initValue - this.lastDoubleSum;
this.lastDoubleSum = initValue;
this.MetricPointStatus = MetricPointStatus.NoCollectPending;
@ -293,7 +348,7 @@ namespace OpenTelemetry.Metrics
// As long as the value is not -ve infinity,
// the exchange (to 0.0) will never occur,
// but we get the original value atomically.
this.DoubleValue = Interlocked.CompareExchange(ref this.doubleVal, 0.0, double.NegativeInfinity);
this.doubleValue = Interlocked.CompareExchange(ref this.doubleVal, 0.0, double.NegativeInfinity);
}
break;
@ -301,12 +356,12 @@ namespace OpenTelemetry.Metrics
case AggregationType.LongGauge:
{
this.LongValue = Interlocked.Read(ref this.longVal);
this.longValue = Interlocked.Read(ref this.longVal);
this.MetricPointStatus = MetricPointStatus.NoCollectPending;
// Check again if value got updated, if yes reset status.
// This ensures no Updates get Lost.
if (this.LongValue != Interlocked.Read(ref this.longVal))
if (this.longValue != Interlocked.Read(ref this.longVal))
{
this.MetricPointStatus = MetricPointStatus.CollectPending;
}
@ -321,12 +376,12 @@ namespace OpenTelemetry.Metrics
// As long as the value is not -ve infinity,
// the exchange (to 0.0) will never occur,
// but we get the original value atomically.
this.DoubleValue = Interlocked.CompareExchange(ref this.doubleVal, 0.0, double.NegativeInfinity);
this.doubleValue = Interlocked.CompareExchange(ref this.doubleVal, 0.0, double.NegativeInfinity);
this.MetricPointStatus = MetricPointStatus.NoCollectPending;
// Check again if value got updated, if yes reset status.
// This ensures no Updates get Lost.
if (this.DoubleValue != Interlocked.CompareExchange(ref this.doubleVal, 0.0, double.NegativeInfinity))
if (this.doubleValue != Interlocked.CompareExchange(ref this.doubleVal, 0.0, double.NegativeInfinity))
{
this.MetricPointStatus = MetricPointStatus.CollectPending;
}

View File

@ -71,7 +71,7 @@ namespace Benchmarks.Metrics
// The performant way of iterating.
foreach (ref var metricPoint in metric.GetMetricPoints())
{
sum += metricPoint.LongValue;
sum += metricPoint.GetCounterSumDouble();
}
}
else
@ -80,7 +80,7 @@ namespace Benchmarks.Metrics
// This is still "correct", but less performant.
foreach (var metricPoint in metric.GetMetricPoints())
{
sum += metricPoint.LongValue;
sum += metricPoint.GetCounterSumDouble();
}
}
}

View File

@ -49,7 +49,7 @@ namespace OpenTelemetry.Metrics.Tests
var metricPointsEnumerator = metric.GetMetricPoints().GetEnumerator();
Assert.True(metricPointsEnumerator.MoveNext()); // One MetricPoint is emitted for the Metric
ref var metricPointForFirstExport = ref metricPointsEnumerator.Current;
Assert.Equal(10, metricPointForFirstExport.LongValue);
Assert.Equal(10, metricPointForFirstExport.GetCounterSumLong());
// Emit 25 for the MetricPoint with a single key-vaue pair: ("tag1", "value1")
counter.Add(25, new KeyValuePair<string, object>("tag1", "value1"));
@ -60,10 +60,10 @@ namespace OpenTelemetry.Metrics.Tests
metricPointsEnumerator = metric.GetMetricPoints().GetEnumerator();
Assert.True(metricPointsEnumerator.MoveNext()); // One MetricPoint is emitted for the Metric
var metricPointForSecondExport = metricPointsEnumerator.Current;
Assert.Equal(25, metricPointForSecondExport.LongValue);
Assert.Equal(25, metricPointForSecondExport.GetCounterSumLong());
// MetricPoint.LongValue for the first exporter metric should still be 10
Assert.Equal(10, metricPointForFirstExport.LongValue);
Assert.Equal(10, metricPointForFirstExport.GetCounterSumLong());
}
}
}

View File

@ -65,7 +65,7 @@ namespace OpenTelemetry.Metrics.Tests
Assert.Single(metricPoints);
var metricPoint = metricPoints[0];
Assert.Equal(100, metricPoint.LongValue);
Assert.Equal(100, metricPoint.GetGaugeLastValueLong());
Assert.True(metricPoint.Tags.Count > 0);
}
@ -95,7 +95,7 @@ namespace OpenTelemetry.Metrics.Tests
Assert.Single(metricPoints);
var metricPoint = metricPoints[0];
Assert.Equal(100, metricPoint.LongValue);
Assert.Equal(100, metricPoint.GetGaugeLastValueLong());
Assert.True(metricPoint.Tags.Count > 0);
}
@ -698,7 +698,14 @@ namespace OpenTelemetry.Metrics.Tests
{
foreach (ref var metricPoint in metric.GetMetricPoints())
{
sum += metricPoint.LongValue;
if (metric.MetricType.IsSum())
{
sum += metricPoint.GetCounterSumLong();
}
else
{
sum += metricPoint.GetGaugeLastValueLong();
}
}
}
@ -712,7 +719,14 @@ namespace OpenTelemetry.Metrics.Tests
{
foreach (ref var metricPoint in metric.GetMetricPoints())
{
sum += metricPoint.DoubleValue;
if (metric.MetricType.IsSum())
{
sum += metricPoint.GetCounterSumDouble();
}
else
{
sum += metricPoint.GetGaugeLastValueDouble();
}
}
}

View File

@ -113,7 +113,14 @@ namespace OpenTelemetry.Metrics.Tests
var metricPointsEnumerator = metricPoints.GetEnumerator();
Assert.True(metricPointsEnumerator.MoveNext()); // One MetricPoint is emitted for the Metric
ref var metricPointForFirstExport = ref metricPointsEnumerator.Current;
Assert.Equal(value, metricPointForFirstExport.LongValue);
if (metric.MetricType.IsSum())
{
Assert.Equal(value, metricPointForFirstExport.GetCounterSumLong());
}
else
{
Assert.Equal(value, metricPointForFirstExport.GetGaugeLastValueLong());
}
}
}
}