Skip to content

Commit

Permalink
Rafactor OtlpMetricExporter (#5091)
Browse files Browse the repository at this point in the history
  • Loading branch information
utpilla authored Nov 29, 2023
1 parent 97442ef commit 4e390e3
Showing 1 changed file with 32 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
using Google.Protobuf;
using Google.Protobuf.Collections;
using OpenTelemetry.Metrics;
using OpenTelemetry.Proto.Metrics.V1;
using AggregationTemporality = OpenTelemetry.Metrics.AggregationTemporality;
using Metric = OpenTelemetry.Metrics.Metric;
using OtlpCollector = OpenTelemetry.Proto.Collector.Metrics.V1;
using OtlpCommon = OpenTelemetry.Proto.Common.V1;
using OtlpMetrics = OpenTelemetry.Proto.Metrics.V1;
Expand All @@ -28,15 +31,15 @@ namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation;

internal static class MetricItemExtensions
{
private static readonly ConcurrentBag<OtlpMetrics.ScopeMetrics> MetricListPool = new();
private static readonly ConcurrentBag<ScopeMetrics> MetricListPool = new();

internal static void AddMetrics(
this OtlpCollector.ExportMetricsServiceRequest request,
OtlpResource.Resource processResource,
in Batch<Metric> metrics)
{
var metricsByLibrary = new Dictionary<string, OtlpMetrics.ScopeMetrics>();
var resourceMetrics = new OtlpMetrics.ResourceMetrics
var metricsByLibrary = new Dictionary<string, ScopeMetrics>();
var resourceMetrics = new ResourceMetrics
{
Resource = processResource,
};
Expand Down Expand Up @@ -77,19 +80,20 @@ internal static void Return(this OtlpCollector.ExportMetricsServiceRequest reque
return;
}

foreach (var scope in resourceMetrics.ScopeMetrics)
foreach (var scopeMetrics in resourceMetrics.ScopeMetrics)
{
scope.Metrics.Clear();
MetricListPool.Add(scope);
scopeMetrics.Metrics.Clear();
scopeMetrics.Scope.Attributes.Clear();
MetricListPool.Add(scopeMetrics);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static OtlpMetrics.ScopeMetrics GetMetricListFromPool(string name, string version, IEnumerable<KeyValuePair<string, object>> meterTags)
internal static ScopeMetrics GetMetricListFromPool(string name, string version, IEnumerable<KeyValuePair<string, object>> meterTags)
{
if (!MetricListPool.TryTake(out var metrics))
if (!MetricListPool.TryTake(out var scopeMetrics))
{
metrics = new OtlpMetrics.ScopeMetrics
scopeMetrics = new ScopeMetrics
{
Scope = new OtlpCommon.InstrumentationScope
{
Expand All @@ -100,21 +104,20 @@ internal static OtlpMetrics.ScopeMetrics GetMetricListFromPool(string name, stri

if (meterTags != null)
{
AddAttributes(meterTags, metrics.Scope.Attributes);
AddScopeAttributes(meterTags, scopeMetrics.Scope.Attributes);
}
}
else
{
metrics.Scope.Name = name;
metrics.Scope.Version = version ?? string.Empty;
scopeMetrics.Scope.Name = name;
scopeMetrics.Scope.Version = version ?? string.Empty;
if (meterTags != null)
{
metrics.Scope.Attributes.Clear();
AddAttributes(meterTags, metrics.Scope.Attributes);
AddScopeAttributes(meterTags, scopeMetrics.Scope.Attributes);
}
}

return metrics;
return scopeMetrics;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down Expand Up @@ -150,15 +153,15 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
case MetricType.LongSum:
case MetricType.LongSumNonMonotonic:
{
var sum = new OtlpMetrics.Sum
var sum = new Sum
{
IsMonotonic = metric.MetricType == MetricType.LongSum,
AggregationTemporality = temporality,
};

foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
var dataPoint = new OtlpMetrics.NumberDataPoint
var dataPoint = new NumberDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
Expand All @@ -177,15 +180,15 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
case MetricType.DoubleSum:
case MetricType.DoubleSumNonMonotonic:
{
var sum = new OtlpMetrics.Sum
var sum = new Sum
{
IsMonotonic = metric.MetricType == MetricType.DoubleSum,
AggregationTemporality = temporality,
};

foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
var dataPoint = new OtlpMetrics.NumberDataPoint
var dataPoint = new NumberDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
Expand All @@ -203,10 +206,10 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)

case MetricType.LongGauge:
{
var gauge = new OtlpMetrics.Gauge();
var gauge = new Gauge();
foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
var dataPoint = new OtlpMetrics.NumberDataPoint
var dataPoint = new NumberDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
Expand All @@ -224,10 +227,10 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)

case MetricType.DoubleGauge:
{
var gauge = new OtlpMetrics.Gauge();
var gauge = new Gauge();
foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
var dataPoint = new OtlpMetrics.NumberDataPoint
var dataPoint = new NumberDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
Expand All @@ -245,14 +248,14 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)

case MetricType.Histogram:
{
var histogram = new OtlpMetrics.Histogram
var histogram = new Histogram
{
AggregationTemporality = temporality,
};

foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
var dataPoint = new OtlpMetrics.HistogramDataPoint
var dataPoint = new HistogramDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
Expand Down Expand Up @@ -320,14 +323,14 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)

case MetricType.ExponentialHistogram:
{
var histogram = new OtlpMetrics.ExponentialHistogram
var histogram = new ExponentialHistogram
{
AggregationTemporality = temporality,
};

foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
var dataPoint = new OtlpMetrics.ExponentialHistogramDataPoint
var dataPoint = new ExponentialHistogramDataPoint
{
StartTimeUnixNano = (ulong)metricPoint.StartTime.ToUnixTimeNanoseconds(),
TimeUnixNano = (ulong)metricPoint.EndTime.ToUnixTimeNanoseconds(),
Expand All @@ -347,7 +350,7 @@ internal static OtlpMetrics.Metric ToOtlpMetric(this Metric metric)
dataPoint.Scale = exponentialHistogramData.Scale;
dataPoint.ZeroCount = (ulong)exponentialHistogramData.ZeroCount;

dataPoint.Positive = new OtlpMetrics.ExponentialHistogramDataPoint.Types.Buckets();
dataPoint.Positive = new ExponentialHistogramDataPoint.Types.Buckets();
dataPoint.Positive.Offset = exponentialHistogramData.PositiveBuckets.Offset;
foreach (var bucketCount in exponentialHistogramData.PositiveBuckets)
{
Expand Down Expand Up @@ -378,7 +381,7 @@ private static void AddAttributes(ReadOnlyTagCollection tags, RepeatedField<Otlp
}
}

private static void AddAttributes(IEnumerable<KeyValuePair<string, object>> meterTags, RepeatedField<OtlpCommon.KeyValue> attributes)
private static void AddScopeAttributes(IEnumerable<KeyValuePair<string, object>> meterTags, RepeatedField<OtlpCommon.KeyValue> attributes)
{
foreach (var tag in meterTags)
{
Expand Down

0 comments on commit 4e390e3

Please sign in to comment.