Skip to content

Commit

Permalink
Store immutable copies of Metric/MetricPoint inside in-memory exporter.
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch committed Mar 22, 2022
1 parent bdcf942 commit 35cae08
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 1 deletion.
78 changes: 78 additions & 0 deletions src/OpenTelemetry.Exporter.InMemory/InMemoryMetricExporter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// <copyright file="InMemoryMetricExporter.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.Collections.Generic;
using OpenTelemetry.Metrics;

namespace OpenTelemetry.Exporter
{
public class InMemoryMetricExporter : BaseExporter<Metric>
{
private readonly ICollection<ExportedMetric> exportedItems;

public InMemoryMetricExporter(ICollection<ExportedMetric> exportedItems)
{
this.exportedItems = exportedItems;
}

public override ExportResult Export(in Batch<Metric> batch)
{
if (this.exportedItems == null)
{
return ExportResult.Failure;
}

foreach (var metric in batch)
{
List<MetricPoint> metricPoints = new();

foreach (ref readonly var metricPoint in metric.GetMetricPoints())
{
metricPoints.Add(metricPoint.Copy());
}

this.exportedItems.Add(new ExportedMetric(metric, metricPoints));
}

return ExportResult.Success;
}

public readonly struct ExportedMetric
{
private readonly InstrumentIdentity instrumentIdentity;

internal ExportedMetric(
Metric metric,
IReadOnlyList<MetricPoint> metricPoints)
{
this.instrumentIdentity = metric.InstrumentIdentity;
this.MetricPoints = metricPoints;
}

public string Name => this.instrumentIdentity.InstrumentName;

public string Description => this.instrumentIdentity.Description;

public string Unit => this.instrumentIdentity.Unit;

public string MeterName => this.instrumentIdentity.MeterName;

public string MeterVersion => this.instrumentIdentity.MeterVersion;

public IReadOnlyList<MetricPoint> MetricPoints { get; }
}
}
}
1 change: 1 addition & 0 deletions src/OpenTelemetry/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@

[assembly: InternalsVisibleTo("OpenTelemetry.Tests" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("OpenTelemetry.Extensions.Hosting.Tests" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("OpenTelemetry.Exporter.InMemory" + AssemblyInfo.PublicKey)]
[assembly: InternalsVisibleTo("DynamicProxyGenAssembly2" + AssemblyInfo.MoqPublicKey)]
[assembly: InternalsVisibleTo("Benchmarks" + AssemblyInfo.PublicKey)]
12 changes: 12 additions & 0 deletions src/OpenTelemetry/Metrics/HistogramBuckets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

using System;

namespace OpenTelemetry.Metrics
{
/// <summary>
Expand Down Expand Up @@ -45,6 +47,16 @@ internal HistogramBuckets(double[] explicitBounds)

public Enumerator GetEnumerator() => new(this);

internal HistogramBuckets Copy()
{
HistogramBuckets copy = new HistogramBuckets(this.ExplicitBounds);

Array.Copy(this.SnapshotBucketCounts, copy.SnapshotBucketCounts, this.SnapshotBucketCounts.Length);
copy.SnapshotSum = this.SnapshotSum;

return copy;
}

/// <summary>
/// Enumerates the elements of a <see cref="HistogramBuckets"/>.
/// </summary>
Expand Down
11 changes: 10 additions & 1 deletion src/OpenTelemetry/Metrics/MetricPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct MetricPoint
{
private readonly AggregationType aggType;

private readonly HistogramBuckets histogramBuckets;
private HistogramBuckets histogramBuckets;

// Represents temporality adjusted "value" for double/long metric types or "count" when histogram
private MetricPointValueStorage runningValue;
Expand Down Expand Up @@ -541,6 +541,15 @@ internal void TakeSnapshot(bool outputDelta)
}
}

internal MetricPoint Copy()
{
MetricPoint copy = this;

copy.histogramBuckets = this.histogramBuckets.Copy();

return copy;
}

[MethodImpl(MethodImplOptions.NoInlining)]
private readonly void ThrowNotSupportedMetricTypeException(string methodName)
{
Expand Down

0 comments on commit 35cae08

Please sign in to comment.