Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Copy methods to MetricPoint and HistogramBuckets to facilitate deep-copying #3113

Merged
merged 23 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f944a1c
Merge pull request #1 from open-telemetry/main
TimothyMothra Jan 28, 2022
ba726c5
Merge branch 'open-telemetry:main' into main
TimothyMothra Feb 10, 2022
68d1977
Merge branch 'open-telemetry:main' into main
TimothyMothra Feb 15, 2022
9dcb839
poc for fix to InMemoryExporter
TimothyMothra Feb 16, 2022
34d15ec
Revert "poc for fix to InMemoryExporter"
TimothyMothra Feb 22, 2022
436f0dc
Merge branch 'open-telemetry:main' into main
TimothyMothra Feb 22, 2022
1c7390c
Merge branch 'open-telemetry:main' into main
TimothyMothra Mar 4, 2022
9d2453f
Merge branch 'open-telemetry:main' into main
TimothyMothra Mar 14, 2022
752f6fd
includescopes
TimothyMothra Mar 14, 2022
4e9fe78
Merge branch 'open-telemetry:main' into main
TimothyMothra Mar 15, 2022
03fe223
REVERT
TimothyMothra Mar 17, 2022
c980b29
Merge branch 'open-telemetry:main' into main
TimothyMothra Mar 17, 2022
2cb16ad
Merge branch 'open-telemetry:main' into main
TimothyMothra Mar 23, 2022
bd3e535
cloning the MetricPoint and HistogramBuckets
TimothyMothra Mar 30, 2022
42a3b63
changelog
TimothyMothra Mar 30, 2022
09b821d
Merge branch 'main' into 2361_cloning_methods
TimothyMothra Mar 30, 2022
aeea5cf
code review comments
TimothyMothra Mar 31, 2022
b14e151
Merge branch '2361_cloning_methods' of https://github.com/TimothyMoth…
TimothyMothra Mar 31, 2022
358c4f3
Merge branch 'main' into 2361_cloning_methods
TimothyMothra Mar 31, 2022
c2db474
Merge branch 'main' into 2361_cloning_methods
TimothyMothra Mar 31, 2022
2e124d2
remove changelog
TimothyMothra Mar 31, 2022
7b7a7b6
fix placement of using
TimothyMothra Mar 31, 2022
46ef282
Merge branch 'main' into 2361_cloning_methods
TimothyMothra Mar 31, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
9 changes: 8 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 @@ -239,6 +239,13 @@ public readonly HistogramBuckets GetHistogramBuckets()
return this.histogramBuckets;
}

internal readonly MetricPoint Copy()
{
MetricPoint copy = this;
copy.histogramBuckets = this.histogramBuckets?.Copy();
return copy;
}

internal void Update(long number)
{
switch (this.aggType)
Expand Down
104 changes: 104 additions & 0 deletions test/OpenTelemetry.Tests/Metrics/MetricPointTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// <copyright file="MetricPointTests.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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.Metrics;
using System.Threading;
using OpenTelemetry.Tests;
using Xunit;
using Xunit.Abstractions;

namespace OpenTelemetry.Metrics.Tests
{
public class MetricPointTests : IDisposable
{
private Meter meter;
private MeterProvider provider;
private Metric metric;
private MetricPoint metricPoint;

private Histogram<long> histogram;
private double[] bounds;

public MetricPointTests()
{
this.meter = new Meter(Utils.GetCurrentMethodName());
this.histogram = this.meter.CreateHistogram<long>("histogram");

// Evenly distribute the bound values over the range [0, MaxValue)
this.bounds = new double[10];
for (int i = 0; i < this.bounds.Length; i++)
{
this.bounds[i] = i * 1000 / this.bounds.Length;
}

var exportedItems = new List<Metric>();

this.provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(this.meter.Name)
.AddInMemoryExporter(exportedItems)
.AddView(this.histogram.Name, new ExplicitBucketHistogramConfiguration() { Boundaries = this.bounds })
.Build();

this.histogram.Record(500);

this.provider.ForceFlush();

this.metric = exportedItems[0];
var metricPointsEnumerator = this.metric.GetMetricPoints().GetEnumerator();
metricPointsEnumerator.MoveNext();
this.metricPoint = metricPointsEnumerator.Current;
}

public void Dispose()
{
this.meter?.Dispose();
this.provider?.Dispose();
}

[Fact]
public void VerifyMetricPointCopy()
{
var copy = this.metricPoint.Copy();

// Verify these structs are unique instances.
Assert.NotEqual(copy, this.metricPoint);

// Verify properties are copied.
Assert.Equal(copy.Tags, this.metricPoint.Tags);
Assert.Equal(copy.StartTime, this.metricPoint.StartTime);
Assert.Equal(copy.EndTime, this.metricPoint.EndTime);
}

[Fact]
public void VerifyHistogramBucketsCopy()
{
var histogramBuckets = this.metricPoint.GetHistogramBuckets();
var copy = histogramBuckets.Copy();

// Verify these are unique instances.
Assert.False(ReferenceEquals(copy, histogramBuckets));
Assert.NotSame(copy, histogramBuckets);

// Verify fields are copied
Assert.NotSame(copy.SnapshotBucketCounts, histogramBuckets.SnapshotBucketCounts);
Assert.Equal(copy.SnapshotBucketCounts, histogramBuckets.SnapshotBucketCounts);
Assert.Equal(copy.SnapshotSum, histogramBuckets.SnapshotSum);
}
}
}