diff --git a/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterExtensions.cs b/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterExtensions.cs index d1e3c5032c..4877302dba 100644 --- a/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterExtensions.cs +++ b/src/OpenTelemetry.Exporter.Prometheus/PrometheusExporterExtensions.cs @@ -128,6 +128,23 @@ public static void WriteMetricsCollection(this PrometheusExporter exporter, Stre case MetricType.Histogram: { + /* + * For Histogram we emit one row for Sum, Count and as + * many rows as number of buckets. + * myHistogram_sum{tag1="value1",tag2="value2"} 258330 1629860660991 + * myHistogram_count{tag1="value1",tag2="value2"} 355 1629860660991 + * myHistogram_bucket{tag1="value1",tag2="value2",le="0"} 0 1629860660991 + * myHistogram_bucket{tag1="value1",tag2="value2",le="5"} 2 1629860660991 + * myHistogram_bucket{tag1="value1",tag2="value2",le="10"} 4 1629860660991 + * myHistogram_bucket{tag1="value1",tag2="value2",le="25"} 6 1629860660991 + * myHistogram_bucket{tag1="value1",tag2="value2",le="50"} 12 1629860660991 + * myHistogram_bucket{tag1="value1",tag2="value2",le="75"} 19 1629860660991 + * myHistogram_bucket{tag1="value1",tag2="value2",le="100"} 26 1629860660991 + * myHistogram_bucket{tag1="value1",tag2="value2",le="250"} 65 1629860660991 + * myHistogram_bucket{tag1="value1",tag2="value2",le="500"} 128 1629860660991 + * myHistogram_bucket{tag1="value1",tag2="value2",le="1000"} 241 1629860660991 + * myHistogram_bucket{tag1="value1",tag2="value2",le="+Inf"} 355 1629860660991 + */ builder = builder.WithType(PrometheusHistogramType); foreach (var metricPoint in metric.GetMetricPoints()) { @@ -187,68 +204,5 @@ public static string GetMetricsCollection(this PrometheusExporter exporter) return Encoding.UTF8.GetString(stream.ToArray(), 0, (int)stream.Length); } - - private static void WriteHistogram( - StreamWriter writer, - PrometheusMetricBuilder builder, - IEnumerable> labels, - string metricName, - double sum, - long count, - IEnumerable buckets) - { - /* For Histogram we emit one row for Sum, Count and as - * many rows as number of buckets. - * myHistogram_sum{tag1="value1",tag2="value2"} 258330 1629860660991 - * myHistogram_count{tag1="value1",tag2="value2"} 355 1629860660991 - * myHistogram_bucket{tag1="value1",tag2="value2",le="0"} 0 1629860660991 - * myHistogram_bucket{tag1="value1",tag2="value2",le="5"} 2 1629860660991 - * myHistogram_bucket{tag1="value1",tag2="value2",le="10"} 4 1629860660991 - * myHistogram_bucket{tag1="value1",tag2="value2",le="25"} 6 1629860660991 - * myHistogram_bucket{tag1="value1",tag2="value2",le="50"} 12 1629860660991 - * myHistogram_bucket{tag1="value1",tag2="value2",le="75"} 19 1629860660991 - * myHistogram_bucket{tag1="value1",tag2="value2",le="100"} 26 1629860660991 - * myHistogram_bucket{tag1="value1",tag2="value2",le="250"} 65 1629860660991 - * myHistogram_bucket{tag1="value1",tag2="value2",le="500"} 128 1629860660991 - * myHistogram_bucket{tag1="value1",tag2="value2",le="1000"} 241 1629860660991 - * myHistogram_bucket{tag1="value1",tag2="value2",le="+Inf"} 355 1629860660991 - */ - - builder = builder.WithType(PrometheusHistogramType); - var metricValueBuilderSum = builder.AddValue(); - metricValueBuilderSum.WithName(metricName + PrometheusHistogramSumPostFix); - metricValueBuilderSum = metricValueBuilderSum.WithValue(sum); - foreach (var label in labels) - { - metricValueBuilderSum.WithLabel(label.Key, label.Value.ToString()); - } - - var metricValueBuilderCount = builder.AddValue(); - metricValueBuilderCount.WithName(metricName + PrometheusHistogramCountPostFix); - metricValueBuilderCount = metricValueBuilderCount.WithValue(count); - foreach (var label in labels) - { - metricValueBuilderCount.WithLabel(label.Key, label.Value.ToString()); - } - - long totalCount = 0; - foreach (var bucket in buckets) - { - totalCount += bucket.Count; - var metricValueBuilderBuckets = builder.AddValue(); - metricValueBuilderBuckets.WithName(metricName + PrometheusHistogramBucketPostFix); - metricValueBuilderBuckets = metricValueBuilderBuckets.WithValue(totalCount); - foreach (var label in labels) - { - metricValueBuilderBuckets.WithLabel(label.Key, label.Value.ToString()); - } - - var bucketName = double.IsPositiveInfinity(bucket.HighBoundary) ? - PrometheusHistogramBucketLabelPositiveInfinity : bucket.HighBoundary.ToString(CultureInfo.InvariantCulture); - metricValueBuilderBuckets.WithLabel(PrometheusHistogramBucketLabelLessThan, bucketName); - } - - builder.Write(writer); - } } } diff --git a/src/OpenTelemetry/CHANGELOG.md b/src/OpenTelemetry/CHANGELOG.md index db7fb988fb..4a220a9c72 100644 --- a/src/OpenTelemetry/CHANGELOG.md +++ b/src/OpenTelemetry/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +* Metrics perf improvements, bug fixes. + Replace MetricProcessor with MetricReader. + ([#2306](https://github.com/open-telemetry/opentelemetry-dotnet/pull/2306)) + * Add `BatchExportActivityProcessorOptions` which supports field value overriding using `OTEL_BSP_SCHEDULE_DELAY`, `OTEL_BSP_EXPORT_TIMEOUT`, `OTEL_BSP_MAX_QUEUE_SIZE`, `OTEL_BSP_MAX_EXPORT_BATCH_SIZE` diff --git a/src/OpenTelemetry/Metrics/Processors/AggregationTemporality.cs b/src/OpenTelemetry/Metrics/AggregationTemporality.cs similarity index 93% rename from src/OpenTelemetry/Metrics/Processors/AggregationTemporality.cs rename to src/OpenTelemetry/Metrics/AggregationTemporality.cs index b30dfe7d3c..5634897d70 100644 --- a/src/OpenTelemetry/Metrics/Processors/AggregationTemporality.cs +++ b/src/OpenTelemetry/Metrics/AggregationTemporality.cs @@ -14,10 +14,6 @@ // limitations under the License. // -using System; -using System.Collections.Generic; -using System.Text; - namespace OpenTelemetry.Metrics { public enum AggregationTemporality diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs deleted file mode 100644 index 2a7848a2b5..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs +++ /dev/null @@ -1,89 +0,0 @@ -// -// 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. -// - -using System; -using System.Collections.Generic; -using System.Diagnostics.Metrics; - -namespace OpenTelemetry.Metrics -{ - internal class GaugeMetricAggregator : IGaugeMetric, IAggregator - { - private readonly object lockUpdate = new object(); - private IDataValue value; - - internal GaugeMetricAggregator(string name, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) - { - this.Name = name; - this.Description = description; - this.Unit = unit; - this.Meter = meter; - this.StartTimeExclusive = startTimeExclusive; - this.Attributes = attributes; - - // TODO: Split this class into two or leverage generic - this.MetricType = MetricType.LongGauge; - } - - public string Name { get; private set; } - - public string Description { get; private set; } - - public string Unit { get; private set; } - - public Meter Meter { get; private set; } - - public DateTimeOffset StartTimeExclusive { get; private set; } - - public DateTimeOffset EndTimeInclusive { get; private set; } - - public KeyValuePair[] Attributes { get; private set; } - - public IEnumerable Exemplars { get; private set; } = new List(); - - public IDataValue LastValue => this.value; - - public MetricType MetricType { get; private set; } - - public void Update(T value) - where T : struct - { - lock (this.lockUpdate) - { - this.value = new DataValue(value); - } - } - - public IMetric Collect(DateTimeOffset dt, bool isDelta) - { - var cloneItem = new GaugeMetricAggregator(this.Name, this.Description, this.Unit, this.Meter, this.StartTimeExclusive, this.Attributes); - - lock (this.lockUpdate) - { - cloneItem.Exemplars = this.Exemplars; - cloneItem.EndTimeInclusive = dt; - cloneItem.value = this.LastValue; - } - - return cloneItem; - } - - public string ToDisplayString() - { - return $"Last={this.LastValue.Value}"; - } - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/HistogramBucket.cs b/src/OpenTelemetry/Metrics/MetricAggregators/HistogramBucket.cs deleted file mode 100644 index b54ae5e9dd..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/HistogramBucket.cs +++ /dev/null @@ -1,25 +0,0 @@ -// -// 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. -// - -namespace OpenTelemetry.Metrics -{ - public struct HistogramBucket - { - public double LowBoundary; - public double HighBoundary; - public long Count; - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetric.cs b/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetric.cs deleted file mode 100644 index cf74009387..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetric.cs +++ /dev/null @@ -1,70 +0,0 @@ -// -// 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. -// - -using System; -using System.Collections.Generic; -using System.Diagnostics.Metrics; - -namespace OpenTelemetry.Metrics -{ - internal class HistogramMetric : IHistogramMetric - { - internal HistogramMetric(string name, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes, int bucketCount) - { - this.Name = name; - this.Description = description; - this.Unit = unit; - this.Meter = meter; - this.StartTimeExclusive = startTimeExclusive; - this.Attributes = attributes; - this.MetricType = MetricType.Histogram; - this.BucketsArray = new HistogramBucket[bucketCount]; - } - - public string Name { get; private set; } - - public string Description { get; private set; } - - public string Unit { get; private set; } - - public Meter Meter { get; private set; } - - public DateTimeOffset StartTimeExclusive { get; internal set; } - - public DateTimeOffset EndTimeInclusive { get; internal set; } - - public KeyValuePair[] Attributes { get; private set; } - - public bool IsDeltaTemporality { get; internal set; } - - public IEnumerable Exemplars { get; private set; } = new List(); - - public long PopulationCount { get; internal set; } - - public double PopulationSum { get; internal set; } - - public IEnumerable Buckets => this.BucketsArray; - - public MetricType MetricType { get; private set; } - - internal HistogramBucket[] BucketsArray { get; set; } - - public string ToDisplayString() - { - return $"Count={this.PopulationCount},Sum={this.PopulationSum}"; - } - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetricAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetricAggregator.cs deleted file mode 100644 index 7b5229fdbd..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetricAggregator.cs +++ /dev/null @@ -1,176 +0,0 @@ -// -// 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. -// - -using System; -using System.Collections.Generic; -using System.Diagnostics.Metrics; - -namespace OpenTelemetry.Metrics -{ - internal class HistogramMetricAggregator : IAggregator - { - private static readonly double[] DefaultBoundaries = new double[] { 0, 5, 10, 25, 50, 75, 100, 250, 500, 1000 }; - - private readonly object lockUpdate = new object(); - private HistogramBucket[] buckets; - private long populationCount; - private double populationSum; - private double[] boundaries; - private DateTimeOffset startTimeExclusive; - private HistogramMetric histogramMetric; - - internal HistogramMetricAggregator(string name, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) - : this(name, description, unit, meter, startTimeExclusive, attributes, DefaultBoundaries) - { - } - - internal HistogramMetricAggregator(string name, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes, double[] boundaries) - { - this.startTimeExclusive = startTimeExclusive; - this.histogramMetric = new HistogramMetric(name, description, unit, meter, startTimeExclusive, attributes, boundaries.Length + 1); - - if (boundaries.Length == 0) - { - boundaries = DefaultBoundaries; - } - - this.boundaries = boundaries; - this.buckets = this.InitializeBucket(boundaries); - } - - public void Update(T value) - where T : struct - { - // promote value to be a double - - double val; - if (typeof(T) == typeof(long)) - { - val = (long)(object)value; - } - else if (typeof(T) == typeof(double)) - { - val = (double)(object)value; - } - else - { - throw new Exception("Unsupported Type!"); - } - - // Determine the bucket index - - int i; - for (i = 0; i < this.boundaries.Length; i++) - { - if (val < this.boundaries[i]) - { - break; - } - } - - lock (this.lockUpdate) - { - this.populationCount++; - this.populationSum += val; - this.buckets[i].Count++; - } - } - - public IMetric Collect(DateTimeOffset dt, bool isDelta) - { - if (this.populationCount == 0) - { - // TODO: Output stale markers - return null; - } - - lock (this.lockUpdate) - { - this.histogramMetric.StartTimeExclusive = this.startTimeExclusive; - this.histogramMetric.EndTimeInclusive = dt; - this.histogramMetric.PopulationCount = this.populationCount; - this.histogramMetric.PopulationSum = this.populationSum; - this.buckets.CopyTo(this.histogramMetric.BucketsArray, 0); - this.histogramMetric.IsDeltaTemporality = isDelta; - - if (isDelta) - { - this.startTimeExclusive = dt; - this.populationCount = 0; - this.populationSum = 0; - for (int i = 0; i < this.buckets.Length; i++) - { - this.buckets[i].Count = 0; - } - } - } - - // TODO: Confirm that this approach of - // re-using the same instance is correct. - // This avoids allocating a new instance. - // It is read only for Exporters, - // and also there is no parallel - // Collect allowed. - return this.histogramMetric; - } - - private HistogramBucket[] InitializeBucket(double[] boundaries) - { - var buckets = new HistogramBucket[boundaries.Length + 1]; - - var lastBoundary = boundaries[0]; - for (int i = 0; i < buckets.Length; i++) - { - if (i == 0) - { - // LowBoundary is inclusive - buckets[i].LowBoundary = double.NegativeInfinity; - - // HighBoundary is exclusive - buckets[i].HighBoundary = boundaries[i]; - } - else if (i < boundaries.Length) - { - // LowBoundary is inclusive - buckets[i].LowBoundary = lastBoundary; - - // HighBoundary is exclusive - buckets[i].HighBoundary = boundaries[i]; - } - else - { - // LowBoundary and HighBoundary are inclusive - buckets[i].LowBoundary = lastBoundary; - buckets[i].HighBoundary = double.PositiveInfinity; - } - - buckets[i].Count = 0; - - if (i < boundaries.Length) - { - if (boundaries[i] < lastBoundary) - { - throw new ArgumentException("Boundary values must be increasing.", nameof(boundaries)); - } - - lastBoundary = boundaries[i]; - } - } - - return buckets; - } - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/IAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/IAggregator.cs deleted file mode 100644 index da9f3ab54f..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/IAggregator.cs +++ /dev/null @@ -1,28 +0,0 @@ -// -// 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. -// - -using System; - -namespace OpenTelemetry.Metrics -{ - internal interface IAggregator - { - void Update(T value) - where T : struct; - - IMetric Collect(DateTimeOffset dt, bool isDelta); - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/IGaugeMetric.cs b/src/OpenTelemetry/Metrics/MetricAggregators/IGaugeMetric.cs deleted file mode 100644 index afb67d7959..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/IGaugeMetric.cs +++ /dev/null @@ -1,27 +0,0 @@ -// -// 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. -// - -using System.Collections.Generic; - -namespace OpenTelemetry.Metrics -{ - public interface IGaugeMetric : IMetric - { - IEnumerable Exemplars { get; } - - IDataValue LastValue { get; } - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/IHistogramMetric.cs b/src/OpenTelemetry/Metrics/MetricAggregators/IHistogramMetric.cs deleted file mode 100644 index ad79e52db8..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/IHistogramMetric.cs +++ /dev/null @@ -1,33 +0,0 @@ -// -// 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. -// - -using System.Collections.Generic; - -namespace OpenTelemetry.Metrics -{ - public interface IHistogramMetric : IMetric - { - bool IsDeltaTemporality { get; } - - IEnumerable Exemplars { get; } - - long PopulationCount { get; } - - double PopulationSum { get; } - - IEnumerable Buckets { get; } - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs b/src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs deleted file mode 100644 index 079172dd6c..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs +++ /dev/null @@ -1,43 +0,0 @@ -// -// 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. -// - -using System; -using System.Collections.Generic; -using System.Diagnostics.Metrics; - -namespace OpenTelemetry.Metrics -{ - public interface IMetric - { - string Name { get; } - - string Description { get; } - - string Unit { get; } - - Meter Meter { get; } - - DateTimeOffset StartTimeExclusive { get; } - - DateTimeOffset EndTimeInclusive { get; } - - KeyValuePair[] Attributes { get; } - - MetricType MetricType { get; } - - string ToDisplayString(); - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/ISumMetric.cs b/src/OpenTelemetry/Metrics/MetricAggregators/ISumMetric.cs deleted file mode 100644 index 36d2f4f648..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/ISumMetric.cs +++ /dev/null @@ -1,29 +0,0 @@ -// -// 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. -// - -using System.Collections.Generic; - -namespace OpenTelemetry.Metrics -{ - public interface ISumMetric : IMetric - { - bool IsDeltaTemporality { get; } - - bool IsMonotonic { get; } - - IEnumerable Exemplars { get; } - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/ISumMetricDouble.cs b/src/OpenTelemetry/Metrics/MetricAggregators/ISumMetricDouble.cs deleted file mode 100644 index 5b04e1d9db..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/ISumMetricDouble.cs +++ /dev/null @@ -1,23 +0,0 @@ -// -// 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. -// - -namespace OpenTelemetry.Metrics -{ - public interface ISumMetricDouble : ISumMetric - { - double DoubleSum { get; } - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/ISumMetricLong.cs b/src/OpenTelemetry/Metrics/MetricAggregators/ISumMetricLong.cs deleted file mode 100644 index 033956fdf8..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/ISumMetricLong.cs +++ /dev/null @@ -1,23 +0,0 @@ -// -// 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. -// - -namespace OpenTelemetry.Metrics -{ - public interface ISumMetricLong : ISumMetric - { - long LongSum { get; } - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/ISummaryMetric.cs b/src/OpenTelemetry/Metrics/MetricAggregators/ISummaryMetric.cs deleted file mode 100644 index fce320de83..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/ISummaryMetric.cs +++ /dev/null @@ -1,29 +0,0 @@ -// -// 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. -// - -using System.Collections.Generic; - -namespace OpenTelemetry.Metrics -{ - public interface ISummaryMetric : IMetric - { - long PopulationCount { get; } - - double PopulationSum { get; } - - IEnumerable Quantiles { get; } - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregatorDouble.cs b/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregatorDouble.cs deleted file mode 100644 index d266973c3a..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregatorDouble.cs +++ /dev/null @@ -1,93 +0,0 @@ -// -// 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. -// - -using System; -using System.Collections.Generic; -using System.Diagnostics.Metrics; - -namespace OpenTelemetry.Metrics -{ - internal class SumMetricAggregatorDouble : IAggregator - { - private readonly object lockUpdate = new object(); - private double sumDouble = 0; - private SumMetricDouble sumMetricDouble; - private DateTimeOffset startTimeExclusive; - - internal SumMetricAggregatorDouble(string name, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) - { - this.startTimeExclusive = startTimeExclusive; - this.sumMetricDouble = new SumMetricDouble(name, description, unit, meter, startTimeExclusive, attributes); - } - - public void Update(T value) - where T : struct - { - // TODO: Replace Lock with - // TryAdd..{Spin..TryAdd..Repeat} if "lost race to another thread" - lock (this.lockUpdate) - { - if (typeof(T) == typeof(double)) - { - // TODO: Confirm this doesn't cause boxing. - var val = (double)(object)value; - if (val < 0) - { - // TODO: log? - // Also, this validation can be done in earlier stage. - } - else - { - this.sumDouble += val; - } - } - else - { - throw new Exception("Unsupported Type"); - } - } - } - - public IMetric Collect(DateTimeOffset dt, bool isDelta) - { - lock (this.lockUpdate) - { - this.sumMetricDouble.StartTimeExclusive = this.startTimeExclusive; - this.sumMetricDouble.EndTimeInclusive = dt; - this.sumMetricDouble.DoubleSum = this.sumDouble; - this.sumMetricDouble.IsDeltaTemporality = isDelta; - if (isDelta) - { - this.startTimeExclusive = dt; - this.sumDouble = 0; - } - } - - // TODO: Confirm that this approach of - // re-using the same instance is correct. - // This avoids allocating a new instance. - // It is read only for Exporters, - // and also there is no parallel - // Collect allowed. - return this.sumMetricDouble; - } - - public string ToDisplayString() - { - return $"Sum={this.sumDouble}"; - } - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregatorLong.cs b/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregatorLong.cs deleted file mode 100644 index 75438dd559..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregatorLong.cs +++ /dev/null @@ -1,106 +0,0 @@ -// -// 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. -// - -using System; -using System.Collections.Generic; -using System.Diagnostics.Metrics; - -namespace OpenTelemetry.Metrics -{ - internal class SumMetricAggregatorLong : IAggregator - { - private readonly object lockUpdate = new object(); - private long sumLong = 0; - private long lastSumLong = 0; - private bool treatIncomingMeasurementAsDelta; - private SumMetricLong sumMetricLong; - private DateTimeOffset startTimeExclusive; - - internal SumMetricAggregatorLong(string name, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes, bool treatIncomingMeasurementAsDelta) - { - this.startTimeExclusive = startTimeExclusive; - this.treatIncomingMeasurementAsDelta = treatIncomingMeasurementAsDelta; - this.sumMetricLong = new SumMetricLong(name, description, unit, meter, startTimeExclusive, attributes); - } - - public void Update(T value) - where T : struct - { - // TODO: Replace Lock with Interlocked.Add - lock (this.lockUpdate) - { - if (typeof(T) == typeof(long)) - { - // TODO: Confirm this doesn't cause boxing. - var val = (long)(object)value; - if (val < 0) - { - // TODO: log? - // Also, this validation can be done in earlier stage. - } - else - { - if (this.treatIncomingMeasurementAsDelta) - { - this.sumLong += val; - } - else - { - this.sumLong = val; - } - } - } - else - { - throw new Exception("Unsupported Type"); - } - } - } - - public IMetric Collect(DateTimeOffset dt, bool isDelta) - { - lock (this.lockUpdate) - { - this.sumMetricLong.StartTimeExclusive = this.startTimeExclusive; - this.sumMetricLong.EndTimeInclusive = dt; - this.sumMetricLong.IsDeltaTemporality = isDelta; - if (isDelta) - { - this.startTimeExclusive = dt; - this.sumMetricLong.LongSum = this.sumLong - this.lastSumLong; - this.lastSumLong = this.sumLong; - } - else - { - this.sumMetricLong.LongSum = this.sumLong; - } - } - - // TODO: Confirm that this approach of - // re-using the same instance is correct. - // This avoids allocating a new instance. - // It is read only for Exporters, - // and also there is no parallel - // Collect allowed. - return this.sumMetricLong; - } - - public string ToDisplayString() - { - return $"Sum={this.sumLong}"; - } - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricDouble.cs b/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricDouble.cs deleted file mode 100644 index 3b5486ad6d..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricDouble.cs +++ /dev/null @@ -1,66 +0,0 @@ -// -// 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. -// - -using System; -using System.Collections.Generic; -using System.Diagnostics.Metrics; - -namespace OpenTelemetry.Metrics -{ - internal class SumMetricDouble : ISumMetricDouble - { - internal SumMetricDouble(string name, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) - { - this.Name = name; - this.Description = description; - this.Unit = unit; - this.Meter = meter; - this.StartTimeExclusive = startTimeExclusive; - this.Attributes = attributes; - this.IsMonotonic = true; - this.MetricType = MetricType.DoubleSum; - } - - public string Name { get; private set; } - - public string Description { get; private set; } - - public string Unit { get; private set; } - - public Meter Meter { get; private set; } - - public DateTimeOffset StartTimeExclusive { get; internal set; } - - public DateTimeOffset EndTimeInclusive { get; internal set; } - - public KeyValuePair[] Attributes { get; private set; } - - public bool IsDeltaTemporality { get; internal set; } - - public bool IsMonotonic { get; } - - public IEnumerable Exemplars { get; private set; } = new List(); - - public double DoubleSum { get; internal set; } - - public MetricType MetricType { get; private set; } - - public string ToDisplayString() - { - return $"Sum={this.DoubleSum}"; - } - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricLong.cs b/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricLong.cs deleted file mode 100644 index 1398cb694a..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricLong.cs +++ /dev/null @@ -1,66 +0,0 @@ -// -// 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. -// - -using System; -using System.Collections.Generic; -using System.Diagnostics.Metrics; - -namespace OpenTelemetry.Metrics -{ - internal class SumMetricLong : ISumMetricLong - { - internal SumMetricLong(string name, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) - { - this.Name = name; - this.Description = description; - this.Unit = unit; - this.Meter = meter; - this.StartTimeExclusive = startTimeExclusive; - this.Attributes = attributes; - this.IsMonotonic = true; - this.MetricType = MetricType.LongSum; - } - - public string Name { get; private set; } - - public string Description { get; private set; } - - public string Unit { get; private set; } - - public Meter Meter { get; private set; } - - public DateTimeOffset StartTimeExclusive { get; internal set; } - - public DateTimeOffset EndTimeInclusive { get; internal set; } - - public KeyValuePair[] Attributes { get; private set; } - - public bool IsDeltaTemporality { get; internal set; } - - public bool IsMonotonic { get; } - - public IEnumerable Exemplars { get; private set; } = new List(); - - public long LongSum { get; internal set; } - - public MetricType MetricType { get; private set; } - - public string ToDisplayString() - { - return $"Sum={this.LongSum}"; - } - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/SummaryMetricAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/SummaryMetricAggregator.cs deleted file mode 100644 index 0e7fe40063..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/SummaryMetricAggregator.cs +++ /dev/null @@ -1,123 +0,0 @@ -// -// 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. -// - -using System; -using System.Collections.Generic; -using System.Diagnostics.Metrics; - -namespace OpenTelemetry.Metrics -{ - internal class SummaryMetricAggregator : ISummaryMetric, IAggregator - { - private readonly object lockUpdate = new object(); - - private List quantiles = new List(); - - internal SummaryMetricAggregator(string name, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes, bool isMonotonic) - { - this.Name = name; - this.Description = description; - this.Unit = unit; - this.Meter = meter; - this.StartTimeExclusive = startTimeExclusive; - this.Attributes = attributes; - this.IsMonotonic = isMonotonic; - this.MetricType = MetricType.Summary; - } - - public string Name { get; private set; } - - public string Description { get; private set; } - - public string Unit { get; private set; } - - public Meter Meter { get; private set; } - - public DateTimeOffset StartTimeExclusive { get; private set; } - - public DateTimeOffset EndTimeInclusive { get; private set; } - - public KeyValuePair[] Attributes { get; private set; } - - public bool IsMonotonic { get; } - - public long PopulationCount { get; private set; } - - public double PopulationSum { get; private set; } - - public IEnumerable Quantiles => this.quantiles; - - public MetricType MetricType { get; private set; } - - public void Update(T value) - where T : struct - { - // TODO: Implement Summary! - - lock (this.lockUpdate) - { - if (typeof(T) == typeof(long)) - { - var val = (long)(object)value; - if (val > 0 || !this.IsMonotonic) - { - this.PopulationSum += (double)val; - this.PopulationCount++; - } - } - else if (typeof(T) == typeof(double)) - { - var val = (double)(object)value; - if (val > 0 || !this.IsMonotonic) - { - this.PopulationSum += (double)val; - this.PopulationCount++; - } - } - } - } - - public IMetric Collect(DateTimeOffset dt, bool isDelta) - { - if (this.PopulationCount == 0) - { - // TODO: Output stale markers - return null; - } - - var cloneItem = new SummaryMetricAggregator(this.Name, this.Description, this.Unit, this.Meter, this.StartTimeExclusive, this.Attributes, this.IsMonotonic); - - lock (this.lockUpdate) - { - cloneItem.EndTimeInclusive = dt; - cloneItem.PopulationCount = this.PopulationCount; - cloneItem.PopulationSum = this.PopulationSum; - cloneItem.quantiles = this.quantiles; - - this.StartTimeExclusive = dt; - this.PopulationCount = 0; - this.PopulationSum = 0; - } - - return cloneItem; - } - - public string ToDisplayString() - { - return $"Count={this.PopulationCount},Sum={this.PopulationSum}"; - } - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/ValueAtQuantile.cs b/src/OpenTelemetry/Metrics/MetricAggregators/ValueAtQuantile.cs deleted file mode 100644 index 6b43f98e15..0000000000 --- a/src/OpenTelemetry/Metrics/MetricAggregators/ValueAtQuantile.cs +++ /dev/null @@ -1,24 +0,0 @@ -// -// 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. -// - -namespace OpenTelemetry.Metrics -{ - public struct ValueAtQuantile - { - internal double Quantile; - internal double Value; - } -} diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/MetricType.cs b/src/OpenTelemetry/Metrics/MetricType.cs similarity index 100% rename from src/OpenTelemetry/Metrics/MetricAggregators/MetricType.cs rename to src/OpenTelemetry/Metrics/MetricType.cs diff --git a/src/OpenTelemetry/Metrics/Processors/MetricItem.cs b/src/OpenTelemetry/Metrics/Processors/MetricItem.cs deleted file mode 100644 index 8dd9a450ad..0000000000 --- a/src/OpenTelemetry/Metrics/Processors/MetricItem.cs +++ /dev/null @@ -1,29 +0,0 @@ -// -// 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. -// - -using System.Collections.Generic; - -namespace OpenTelemetry.Metrics -{ - public class MetricItem - { - public List Metrics = new List(); - - internal MetricItem() - { - } - } -} diff --git a/src/OpenTelemetry/Metrics/Processors/MetricProcessor.cs b/src/OpenTelemetry/Metrics/Processors/MetricProcessor.cs deleted file mode 100644 index 8acb863383..0000000000 --- a/src/OpenTelemetry/Metrics/Processors/MetricProcessor.cs +++ /dev/null @@ -1,46 +0,0 @@ -// -// 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. -// - -using System; - -namespace OpenTelemetry.Metrics -{ - public abstract class MetricProcessor : BaseProcessor - { - protected readonly BaseExporter exporter; - - protected MetricProcessor(BaseExporter exporter) - { - this.exporter = exporter ?? throw new ArgumentNullException(nameof(exporter)); - } - - public virtual AggregationTemporality GetAggregationTemporality() - { - return AggregationTemporality.Cumulative; - } - - // GetMetric or GetMemoryState or GetAggregatedMetrics.. - // ...or some other names - public abstract void SetGetMetricFunction(Func> getMetrics); - - internal override void SetParentProvider(BaseProvider parentProvider) - { - base.SetParentProvider(parentProvider); - - this.exporter.ParentProvider = parentProvider; - } - } -} diff --git a/src/OpenTelemetry/Metrics/Processors/PullMetricProcessor.cs b/src/OpenTelemetry/Metrics/Processors/PullMetricProcessor.cs deleted file mode 100644 index a10bbf1da8..0000000000 --- a/src/OpenTelemetry/Metrics/Processors/PullMetricProcessor.cs +++ /dev/null @@ -1,72 +0,0 @@ -// -// 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. -// - -using System; - -namespace OpenTelemetry.Metrics -{ - public class PullMetricProcessor : MetricProcessor, IDisposable - { - private Func> getMetrics; - private bool disposed; - private AggregationTemporality aggTemporality; - - public PullMetricProcessor(BaseExporter exporter, bool isDelta) - : base(exporter) - { - this.aggTemporality = isDelta ? AggregationTemporality.Delta : AggregationTemporality.Cumulative; - } - - public override AggregationTemporality GetAggregationTemporality() - { - return this.aggTemporality; - } - - public override void SetGetMetricFunction(Func> getMetrics) - { - this.getMetrics = getMetrics; - } - - public void PullRequest() - { - if (this.getMetrics != null) - { - var metricsToExport = this.getMetrics(); - this.exporter.Export(metricsToExport); - } - } - - /// - protected override void Dispose(bool disposing) - { - base.Dispose(disposing); - - if (disposing && !this.disposed) - { - try - { - this.exporter.Dispose(); - } - catch (Exception) - { - // TODO: Log - } - - this.disposed = true; - } - } - } -} diff --git a/src/OpenTelemetry/Metrics/Processors/PushMetricProcessor.cs b/src/OpenTelemetry/Metrics/Processors/PushMetricProcessor.cs deleted file mode 100644 index 377510fc40..0000000000 --- a/src/OpenTelemetry/Metrics/Processors/PushMetricProcessor.cs +++ /dev/null @@ -1,91 +0,0 @@ -// -// 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. -// - -using System; -using System.Threading; -using System.Threading.Tasks; - -namespace OpenTelemetry.Metrics -{ - public class PushMetricProcessor : MetricProcessor, IDisposable - { - private Task exportTask; - private CancellationTokenSource token; - private int exportIntervalMs; - private Func> getMetrics; - private bool disposed; - private AggregationTemporality aggTemporality; - - public PushMetricProcessor(BaseExporter exporter, int exportIntervalMs, bool isDelta) - : base(exporter) - { - this.exportIntervalMs = exportIntervalMs; - this.token = new CancellationTokenSource(); - this.exportTask = new Task(() => - { - while (!this.token.IsCancellationRequested) - { - Task.Delay(this.exportIntervalMs).Wait(); - this.Export(); - } - }); - - this.exportTask.Start(); - this.aggTemporality = isDelta ? AggregationTemporality.Delta : AggregationTemporality.Cumulative; - } - - public override AggregationTemporality GetAggregationTemporality() - { - return this.aggTemporality; - } - - public override void SetGetMetricFunction(Func> getMetrics) - { - this.getMetrics = getMetrics; - } - - /// - protected override void Dispose(bool disposing) - { - base.Dispose(disposing); - - if (disposing && !this.disposed) - { - try - { - this.token.Cancel(); - this.exporter.Dispose(); - this.exportTask.Wait(); - } - catch (Exception) - { - // TODO: Log - } - - this.disposed = true; - } - } - - private void Export() - { - if (this.getMetrics != null) - { - var metricsToExport = this.getMetrics(); - this.exporter.Export(metricsToExport); - } - } - } -} diff --git a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/MetricTests.cs b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/MetricTests.cs index 3ad4bb89ba..df0dce743e 100644 --- a/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/MetricTests.cs +++ b/test/OpenTelemetry.Instrumentation.AspNetCore.Tests/MetricTests.cs @@ -138,16 +138,5 @@ public void Dispose() { this.meterProvider?.Dispose(); } - - private static void WaitForMetricItems(List metricItems, int count) - { - Assert.True(SpinWait.SpinUntil( - () => - { - Thread.Sleep(10); - return metricItems.Count >= count; - }, - TimeSpan.FromSeconds(1))); - } } } diff --git a/test/OpenTelemetry.Tests/Metrics/AggregatorTest.cs b/test/OpenTelemetry.Tests/Metrics/AggregatorTest.cs index 9b8c8e57a6..7a12642368 100644 --- a/test/OpenTelemetry.Tests/Metrics/AggregatorTest.cs +++ b/test/OpenTelemetry.Tests/Metrics/AggregatorTest.cs @@ -57,67 +57,5 @@ public void HistogramDistributeToAllBuckets() Assert.Equal(2, histogramPoint.BucketCounts[i]); } } - - [Fact] - public void HistogramCustomBoundaries() - { - using var meter = new Meter("TestMeter", "0.0.1"); - - var hist = new HistogramMetricAggregator("test", "desc", "1", meter, DateTimeOffset.UtcNow, new KeyValuePair[0], new double[] { 0 }); - - hist.Update(-1); - hist.Update(0); - var metric = hist.Collect(DateTimeOffset.UtcNow, false); - - Assert.NotNull(metric); - Assert.IsType(metric); - - if (metric is HistogramMetric agg) - { - int len = 0; - foreach (var bucket in agg.Buckets) - { - Assert.Equal(1, bucket.Count); - len++; - } - - Assert.Equal(2, len); - } - } - - [Fact] - public void HistogramWithEmptyBuckets() - { - using var meter = new Meter("TestMeter", "0.0.1"); - - var hist = new HistogramMetricAggregator("test", "desc", "1", meter, DateTimeOffset.UtcNow, new KeyValuePair[0], new double[] { 0, 5, 10 }); - - hist.Update(-3); - hist.Update(-2); - hist.Update(-1); - hist.Update(6); - hist.Update(7); - hist.Update(12); - var metric = hist.Collect(DateTimeOffset.UtcNow, false); - - Assert.NotNull(metric); - Assert.IsType(metric); - - if (metric is HistogramMetric agg) - { - var expectedCounts = new int[] { 3, 0, 2, 1 }; - int len = 0; - foreach (var bucket in agg.Buckets) - { - if (len < expectedCounts.Length) - { - Assert.Equal(expectedCounts[len], bucket.Count); - len++; - } - } - - Assert.Equal(4, len); - } - } } }