Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: open-telemetry/opentelemetry-dotnet
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: main
Choose a base ref
...
head repository: CodeBlanch/opentelemetry-dotnet
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: inmemory-metrics-copy
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 3 commits
  • 4 files changed
  • 1 contributor

Commits on Mar 22, 2022

  1. Copy the full SHA
    35cae08 View commit details
  2. Tweaks.

    CodeBlanch committed Mar 22, 2022
    Copy the full SHA
    fa3047d View commit details
  3. Tweak.

    CodeBlanch committed Mar 22, 2022
    Copy the full SHA
    3fb38e8 View commit details
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(in metricPoint));
}

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

return ExportResult.Success;
}

public class 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
@@ -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
@@ -14,6 +14,8 @@
// limitations under the License.
// </copyright>

using System;

namespace OpenTelemetry.Metrics
{
/// <summary>
@@ -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>
11 changes: 10 additions & 1 deletion src/OpenTelemetry/Metrics/MetricPoint.cs
Original file line number Diff line number Diff line change
@@ -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;
@@ -541,6 +541,15 @@ internal void TakeSnapshot(bool outputDelta)
}
}

internal static MetricPoint Copy(in MetricPoint metricPoint)
{
MetricPoint copy = metricPoint;

copy.histogramBuckets = metricPoint.GetHistogramBuckets().Copy();

return copy;
}

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