Skip to content

Commit

Permalink
Merge branch 'main' into reyang/refactor-metric-reader
Browse files Browse the repository at this point in the history
  • Loading branch information
cijothomas authored Sep 13, 2021
2 parents 3b95ac8 + e97e50b commit 16dab02
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 70 deletions.
98 changes: 39 additions & 59 deletions src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,74 +89,54 @@ public override ExportResult Export(in Batch<Metric> batch)

var tags = tagsBuilder.ToString();

// Switch would be faster than the if.else ladder
// of try and cast.
switch (metric.MetricType)
var metricType = metric.MetricType;

if (metricType.IsHistogram())
{
case MetricType.LongSum:
var bucketsBuilder = new StringBuilder();
bucketsBuilder.Append($"Sum: {metricPoint.DoubleValue} Count: {metricPoint.LongValue} \n");
for (int i = 0; i < metricPoint.ExplicitBounds.Length + 1; i++)
{
if (i == 0)
{
valueDisplay = metricPoint.LongValue.ToString(CultureInfo.InvariantCulture);
break;
bucketsBuilder.Append("(-Infinity,");
bucketsBuilder.Append(metricPoint.ExplicitBounds[i]);
bucketsBuilder.Append("]");
bucketsBuilder.Append(":");
bucketsBuilder.Append(metricPoint.BucketCounts[i]);
}

case MetricType.DoubleSum:
else if (i == metricPoint.ExplicitBounds.Length)
{
valueDisplay = metricPoint.DoubleValue.ToString(CultureInfo.InvariantCulture);
break;
bucketsBuilder.Append("(");
bucketsBuilder.Append(metricPoint.ExplicitBounds[i - 1]);
bucketsBuilder.Append(",");
bucketsBuilder.Append("+Infinity]");
bucketsBuilder.Append(":");
bucketsBuilder.Append(metricPoint.BucketCounts[i]);
}

case MetricType.LongGauge:
else
{
valueDisplay = metricPoint.LongValue.ToString(CultureInfo.InvariantCulture);
break;
bucketsBuilder.Append("(");
bucketsBuilder.Append(metricPoint.ExplicitBounds[i - 1]);
bucketsBuilder.Append(",");
bucketsBuilder.Append(metricPoint.ExplicitBounds[i]);
bucketsBuilder.Append("]");
bucketsBuilder.Append(":");
bucketsBuilder.Append(metricPoint.BucketCounts[i]);
}

case MetricType.DoubleGauge:
{
valueDisplay = metricPoint.DoubleValue.ToString(CultureInfo.InvariantCulture);
break;
}
bucketsBuilder.AppendLine();
}

case MetricType.Histogram:
{
var bucketsBuilder = new StringBuilder();
bucketsBuilder.Append($"Sum: {metricPoint.DoubleValue} Count: {metricPoint.LongValue} \n");
for (int i = 0; i < metricPoint.ExplicitBounds.Length + 1; i++)
{
if (i == 0)
{
bucketsBuilder.Append("(-Infinity,");
bucketsBuilder.Append(metricPoint.ExplicitBounds[i]);
bucketsBuilder.Append("]");
bucketsBuilder.Append(":");
bucketsBuilder.Append(metricPoint.BucketCounts[i]);
}
else if (i == metricPoint.ExplicitBounds.Length)
{
bucketsBuilder.Append("(");
bucketsBuilder.Append(metricPoint.ExplicitBounds[i - 1]);
bucketsBuilder.Append(",");
bucketsBuilder.Append("+Infinity]");
bucketsBuilder.Append(":");
bucketsBuilder.Append(metricPoint.BucketCounts[i]);
}
else
{
bucketsBuilder.Append("(");
bucketsBuilder.Append(metricPoint.ExplicitBounds[i - 1]);
bucketsBuilder.Append(",");
bucketsBuilder.Append(metricPoint.ExplicitBounds[i]);
bucketsBuilder.Append("]");
bucketsBuilder.Append(":");
bucketsBuilder.Append(metricPoint.BucketCounts[i]);
}

bucketsBuilder.AppendLine();
}

valueDisplay = bucketsBuilder.ToString();
break;
}
valueDisplay = bucketsBuilder.ToString();
}
else if (metricType.IsDouble())
{
valueDisplay = metricPoint.DoubleValue.ToString(CultureInfo.InvariantCulture);
}
else if (metricType.IsLong())
{
valueDisplay = metricPoint.LongValue.ToString(CultureInfo.InvariantCulture);
}

msg = new StringBuilder();
Expand Down
40 changes: 29 additions & 11 deletions src/OpenTelemetry/Metrics/MetricType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,56 @@
// limitations under the License.
// </copyright>

using System;

namespace OpenTelemetry.Metrics
{
public enum MetricType
[Flags]
public enum MetricType : byte
{
/*
Type:
0x10: Sum
0x20: Gauge
0x30: Histogram
0x40: Summary (reserved)
Point kind:
0x04: I1 (signed 1-byte integer)
0x05: U1 (unsigned 1-byte integer)
0x06: I2 (signed 2-byte integer)
0x07: U2 (unsigned 2-byte integer)
0x08: I4 (signed 4-byte integer)
0x09: U4 (unsigned 4-byte integer)
0x0a: I8 (signed 8-byte integer)
0x0b: U8 (unsigned 8-byte integer)
0x0c: R4 (4-byte floating point)
0x0d: R8 (8-byte floating point)
*/

/// <summary>
/// Sum of Long type.
/// </summary>
LongSum = 0,
LongSum = 0x1a,

/// <summary>
/// Sum of Double type.
/// </summary>
DoubleSum = 1,
DoubleSum = 0x1d,

/// <summary>
/// Gauge of Long type.
/// </summary>
LongGauge = 2,
LongGauge = 0x2a,

/// <summary>
/// Gauge of Double type.
/// </summary>
DoubleGauge = 3,
DoubleGauge = 0x2d,

/// <summary>
/// Histogram.
/// </summary>
Histogram = 4,

/// <summary>
/// Summary.
/// </summary>
Summary = 5,
Histogram = 0x30,
}
}
77 changes: 77 additions & 0 deletions src/OpenTelemetry/Metrics/MetricTypeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// <copyright file="MetricTypeExtensions.cs" company="OpenTelemetry Authors">
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>

using System.Runtime.CompilerServices;

namespace OpenTelemetry.Metrics
{
public static class MetricTypeExtensions
{
#pragma warning disable SA1310 // field should not contain an underscore

internal const MetricType METRIC_TYPE_MASK = (MetricType)0xf0;

internal const MetricType METRIC_TYPE_SUM = (MetricType)0x10;
internal const MetricType METRIC_TYPE_GAUGE = (MetricType)0x20;
internal const MetricType METRIC_TYPE_HISTOGRAM = (MetricType)0x30;
/* internal const byte METRIC_TYPE_SUMMARY = 0x40; // not used */

internal const MetricType POINT_KIND_MASK = (MetricType)0x0f;

internal const MetricType POINT_KIND_I1 = (MetricType)0x04; // signed 1-byte integer
internal const MetricType POINT_KIND_U1 = (MetricType)0x05; // unsigned 1-byte integer
internal const MetricType POINT_KIND_I2 = (MetricType)0x06; // signed 2-byte integer
internal const MetricType POINT_KIND_U2 = (MetricType)0x07; // unsigned 2-byte integer
internal const MetricType POINT_KIND_I4 = (MetricType)0x08; // signed 4-byte integer
internal const MetricType POINT_KIND_U4 = (MetricType)0x09; // unsigned 4-byte integer
internal const MetricType POINT_KIND_I8 = (MetricType)0x0a; // signed 8-byte integer
internal const MetricType POINT_KIND_U8 = (MetricType)0x0b; // unsigned 8-byte integer
internal const MetricType POINT_KIND_R4 = (MetricType)0x0c; // 4-byte floating point
internal const MetricType POINT_KIND_R8 = (MetricType)0x0d; // 8-byte floating point

#pragma warning restore SA1310 // field should not contain an underscore

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsSum(this MetricType self)
{
return (self & METRIC_TYPE_MASK) == METRIC_TYPE_SUM;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsGauge(this MetricType self)
{
return (self & METRIC_TYPE_MASK) == METRIC_TYPE_GAUGE;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsHistogram(this MetricType self)
{
return (self & METRIC_TYPE_MASK) == METRIC_TYPE_HISTOGRAM;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsDouble(this MetricType self)
{
return (self & POINT_KIND_MASK) == POINT_KIND_R8;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsLong(this MetricType self)
{
return (self & POINT_KIND_MASK) == POINT_KIND_I8;
}
}
}

0 comments on commit 16dab02

Please sign in to comment.