Skip to content

Commit

Permalink
Update Metric Point for Counter and Gauge (#2667)
Browse files Browse the repository at this point in the history
  • Loading branch information
utpilla authored Nov 24, 2021
1 parent f7c718e commit 9e83587
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 55 deletions.
22 changes: 18 additions & 4 deletions src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,32 @@ public override ExportResult Export(in Batch<Metric> batch)
}
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,13 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
{
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);
}

Expand All @@ -176,13 +176,13 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
{
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);
}

Expand All @@ -197,13 +197,13 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
{
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);
}

Expand All @@ -218,13 +218,13 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
{
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);
}

Expand All @@ -241,8 +241,8 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
{
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric)
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);
Expand Down Expand Up @@ -70,11 +70,25 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric)
// 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)' ');
Expand All @@ -89,7 +103,7 @@ public static int WriteMetric(byte[] buffer, int cursor, Metric metric)
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())
Expand Down
10 changes: 6 additions & 4 deletions src/OpenTelemetry/.publicApi/net461/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading

0 comments on commit 9e83587

Please sign in to comment.