From a1f6f420ee52fefb83c39f80118d5a3d01b75036 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Fri, 16 Jul 2021 15:28:36 -0700 Subject: [PATCH] Add reference to Instrument on IMetric (#2124) --- examples/Console/TestMetrics.cs | 2 +- .../ConsoleMetricExporter.cs | 24 ++++++++++++++++++- src/OpenTelemetry/Metrics/AggregatorStore.cs | 8 +++---- .../GaugeMetricAggregator.cs | 14 +++++++++-- .../HistogramMetricAggregator.cs | 14 +++++++++-- .../Metrics/MetricAggregators/IMetric.cs | 7 ++++++ .../MetricAggregators/SumMetricAggregator.cs | 14 +++++++++-- .../SummaryMetricAggregator.cs | 14 +++++++++-- 8 files changed, 83 insertions(+), 14 deletions(-) diff --git a/examples/Console/TestMetrics.cs b/examples/Console/TestMetrics.cs index 0a7c8d975ae..d851b218930 100644 --- a/examples/Console/TestMetrics.cs +++ b/examples/Console/TestMetrics.cs @@ -42,7 +42,7 @@ internal static object Run(MetricsOptions options) Counter counter = null; if (options.FlagCounter ?? true) { - counter = meter.CreateCounter("counter"); + counter = meter.CreateCounter("counter", "things", "A count of things"); } Histogram histogram = null; diff --git a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs index d16fc41f5e6..82c3f9b1e88 100644 --- a/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs +++ b/src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.cs @@ -17,6 +17,7 @@ using System; using System.Globalization; using System.Linq; +using System.Text; using OpenTelemetry.Metrics; namespace OpenTelemetry.Exporter @@ -74,7 +75,28 @@ public override ExportResult Export(in Batch batch) string time = $"{metric.StartTimeExclusive.ToLocalTime().ToString("HH:mm:ss.fff")} {metric.EndTimeInclusive.ToLocalTime().ToString("HH:mm:ss.fff")}"; - var msg = $"Export {time} {metric.Name} [{string.Join(";", tags)}] {kind} Value: {valueDisplay}"; + var msg = new StringBuilder($"Export {time} {metric.Name} [{string.Join(";", tags)}] {kind} Value: {valueDisplay}"); + + if (!string.IsNullOrEmpty(metric.Description)) + { + msg.Append($", Description: {metric.Description}"); + } + + if (!string.IsNullOrEmpty(metric.Unit)) + { + msg.Append($", Unit: {metric.Unit}"); + } + + if (!string.IsNullOrEmpty(metric.Meter.Name)) + { + msg.Append($", Meter: {metric.Meter.Name}"); + + if (!string.IsNullOrEmpty(metric.Meter.Version)) + { + msg.Append($"/{metric.Meter.Version}"); + } + } + Console.WriteLine(msg); } } diff --git a/src/OpenTelemetry/Metrics/AggregatorStore.cs b/src/OpenTelemetry/Metrics/AggregatorStore.cs index ac966485fc0..7e516e00fc8 100644 --- a/src/OpenTelemetry/Metrics/AggregatorStore.cs +++ b/src/OpenTelemetry/Metrics/AggregatorStore.cs @@ -56,19 +56,19 @@ internal IAggregator[] MapToMetrics(string[] seqKey, object[] seqVal) // TODO: Need to map each instrument to metrics (based on View API) if (this.instrument.GetType().Name.Contains("Counter")) { - aggregators.Add(new SumMetricAggregator(name, dt, tags)); + aggregators.Add(new SumMetricAggregator(name, this.instrument.Description, this.instrument.Unit, this.instrument.Meter, dt, tags)); } else if (this.instrument.GetType().Name.Contains("Gauge")) { - aggregators.Add(new GaugeMetricAggregator(name, dt, tags)); + aggregators.Add(new GaugeMetricAggregator(name, this.instrument.Description, this.instrument.Unit, this.instrument.Meter, dt, tags)); } else if (this.instrument.GetType().Name.Contains("Histogram")) { - aggregators.Add(new HistogramMetricAggregator(name, dt, tags)); + aggregators.Add(new HistogramMetricAggregator(name, this.instrument.Description, this.instrument.Unit, this.instrument.Meter, dt, tags)); } else { - aggregators.Add(new SummaryMetricAggregator(name, dt, tags, false)); + aggregators.Add(new SummaryMetricAggregator(name, this.instrument.Description, this.instrument.Unit, this.instrument.Meter, dt, tags, false)); } return aggregators.ToArray(); diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs index e89e8798756..67eadd8286f 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs @@ -16,6 +16,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Metrics; namespace OpenTelemetry.Metrics { @@ -24,15 +25,24 @@ internal class GaugeMetricAggregator : IGaugeMetric, IAggregator private readonly object lockUpdate = new object(); private IDataValue value; - internal GaugeMetricAggregator(string name, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) + 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; } 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; } @@ -54,7 +64,7 @@ public void Update(T value) public IMetric Collect(DateTimeOffset dt, bool isDelta) { - var cloneItem = new GaugeMetricAggregator(this.Name, this.StartTimeExclusive, this.Attributes); + var cloneItem = new GaugeMetricAggregator(this.Name, this.Description, this.Unit, this.Meter, this.StartTimeExclusive, this.Attributes); lock (this.lockUpdate) { diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetricAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetricAggregator.cs index b7686b22b45..742c53a7386 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetricAggregator.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetricAggregator.cs @@ -16,6 +16,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Metrics; namespace OpenTelemetry.Metrics { @@ -24,15 +25,24 @@ internal class HistogramMetricAggregator : IHistogramMetric, IAggregator private readonly object lockUpdate = new object(); private List buckets = new List(); - internal HistogramMetricAggregator(string name, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) + internal HistogramMetricAggregator(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; } 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; } @@ -68,7 +78,7 @@ public IMetric Collect(DateTimeOffset dt, bool isDelta) return null; } - var cloneItem = new HistogramMetricAggregator(this.Name, this.StartTimeExclusive, this.Attributes); + var cloneItem = new HistogramMetricAggregator(this.Name, this.Description, this.Unit, this.Meter, this.StartTimeExclusive, this.Attributes); lock (this.lockUpdate) { diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs b/src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs index 4880d59b5c6..f77a1599859 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs @@ -16,6 +16,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Metrics; namespace OpenTelemetry.Metrics { @@ -23,6 +24,12 @@ public interface IMetric { string Name { get; } + string Description { get; } + + string Unit { get; } + + Meter Meter { get; } + DateTimeOffset StartTimeExclusive { get; } DateTimeOffset EndTimeInclusive { get; } diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs index 41cac109c4c..eea048b5195 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs @@ -16,6 +16,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Metrics; namespace OpenTelemetry.Metrics { @@ -26,9 +27,12 @@ internal class SumMetricAggregator : ISumMetric, IAggregator private long sumLong = 0; private double sumDouble = 0; - internal SumMetricAggregator(string name, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) + internal SumMetricAggregator(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; @@ -36,6 +40,12 @@ internal SumMetricAggregator(string name, DateTimeOffset startTimeExclusive, Key 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; } @@ -107,7 +117,7 @@ public void Update(T value) public IMetric Collect(DateTimeOffset dt, bool isDelta) { - var cloneItem = new SumMetricAggregator(this.Name, this.StartTimeExclusive, this.Attributes); + var cloneItem = new SumMetricAggregator(this.Name, this.Description, this.Unit, this.Meter, this.StartTimeExclusive, this.Attributes); lock (this.lockUpdate) { diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/SummaryMetricAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/SummaryMetricAggregator.cs index ee5cd60dd87..c06874a3c11 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/SummaryMetricAggregator.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/SummaryMetricAggregator.cs @@ -16,6 +16,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics.Metrics; namespace OpenTelemetry.Metrics { @@ -25,9 +26,12 @@ internal class SummaryMetricAggregator : ISummaryMetric, IAggregator private List quantiles = new List(); - internal SummaryMetricAggregator(string name, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes, bool isMonotonic) + 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; @@ -35,6 +39,12 @@ internal SummaryMetricAggregator(string name, DateTimeOffset startTimeExclusive, 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; } @@ -85,7 +95,7 @@ public IMetric Collect(DateTimeOffset dt, bool isDelta) return null; } - var cloneItem = new SummaryMetricAggregator(this.Name, this.StartTimeExclusive, this.Attributes, this.IsMonotonic); + var cloneItem = new SummaryMetricAggregator(this.Name, this.Description, this.Unit, this.Meter, this.StartTimeExclusive, this.Attributes, this.IsMonotonic); lock (this.lockUpdate) {