From 406d8886f02b0695f888b3149d1f70380925b29a Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Thu, 8 Jul 2021 14:49:03 -0700 Subject: [PATCH 1/5] Add handle to instrument on IMetric --- examples/Console/TestMetrics.cs | 2 +- src/OpenTelemetry/Metrics/AggregatorStore.cs | 10 ++++---- .../GaugeMetricAggregator.cs | 8 +++++-- .../HistogramMetricAggregator.cs | 8 +++++-- .../Metrics/MetricAggregators/IMetric.cs | 3 +++ .../MetricAggregators/SumMetricAggregator.cs | 8 +++++-- .../SummaryMetricAggregator.cs | 8 +++++-- .../Processors/MetricConsoleExporter.cs | 24 ++++++++++++++++++- 8 files changed, 56 insertions(+), 15 deletions(-) diff --git a/examples/Console/TestMetrics.cs b/examples/Console/TestMetrics.cs index 3568d4ae833..4a9476fb38d 100644 --- a/examples/Console/TestMetrics.cs +++ b/examples/Console/TestMetrics.cs @@ -41,7 +41,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/Metrics/AggregatorStore.cs b/src/OpenTelemetry/Metrics/AggregatorStore.cs index fafcd6e4ab0..afb8cf3a55e 100644 --- a/src/OpenTelemetry/Metrics/AggregatorStore.cs +++ b/src/OpenTelemetry/Metrics/AggregatorStore.cs @@ -67,20 +67,20 @@ internal MetricAgg[] MapToMetrics(string[] seqKey, object[] seqVal) if (this.instrument.GetType().Name.Contains("Counter")) { - metricpairs.Add(new MetricAgg(timeperiod, new SumMetricAggregator(name, dt, tags, false, true))); - metricpairs.Add(new MetricAgg(timeperiod, new SumMetricAggregator(name, dt, tags, true, true))); + metricpairs.Add(new MetricAgg(timeperiod, new SumMetricAggregator(name, this.instrument, dt, tags, false, true))); + metricpairs.Add(new MetricAgg(timeperiod, new SumMetricAggregator(name, this.instrument, dt, tags, true, true))); } else if (this.instrument.GetType().Name.Contains("Gauge")) { - metricpairs.Add(new MetricAgg(timeperiod, new GaugeMetricAggregator(name, dt, tags))); + metricpairs.Add(new MetricAgg(timeperiod, new GaugeMetricAggregator(name, this.instrument, dt, tags))); } else if (this.instrument.GetType().Name.Contains("Histogram")) { - metricpairs.Add(new MetricAgg(timeperiod, new HistogramMetricAggregator(name, dt, tags, false))); + metricpairs.Add(new MetricAgg(timeperiod, new HistogramMetricAggregator(name, this.instrument, dt, tags, false))); } else { - metricpairs.Add(new MetricAgg(timeperiod, new SummaryMetricAggregator(name, dt, tags, false))); + metricpairs.Add(new MetricAgg(timeperiod, new SummaryMetricAggregator(name, this.instrument, dt, tags, false))); } } diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs index b57f9648880..c76ab41b94b 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,9 +25,10 @@ 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, Instrument instrument, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) { this.Name = name; + this.Instrument = instrument; this.StartTimeExclusive = startTimeExclusive; this.EndTimeInclusive = startTimeExclusive; this.Attributes = attributes; @@ -34,6 +36,8 @@ internal GaugeMetricAggregator(string name, DateTimeOffset startTimeExclusive, K public string Name { get; private set; } + public Instrument Instrument { get; private set; } + public DateTimeOffset StartTimeExclusive { get; private set; } public DateTimeOffset EndTimeInclusive { get; private set; } @@ -56,7 +60,7 @@ public void Update(DateTimeOffset dt, T value) public IMetric Collect(DateTimeOffset dt) { - var cloneItem = new GaugeMetricAggregator(this.Name, this.StartTimeExclusive, this.Attributes); + var cloneItem = new GaugeMetricAggregator(this.Name, this.Instrument, this.StartTimeExclusive, this.Attributes); lock (this.lockUpdate) { diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetricAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetricAggregator.cs index 0d819d32a8e..d800052ded1 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,9 +25,10 @@ internal class HistogramMetricAggregator : IHistogramMetric, IAggregator private readonly object lockUpdate = new object(); private List buckets = new List(); - internal HistogramMetricAggregator(string name, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes, bool isDelta) + internal HistogramMetricAggregator(string name, Instrument instrument, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes, bool isDelta) { this.Name = name; + this.Instrument = instrument; this.StartTimeExclusive = startTimeExclusive; this.EndTimeInclusive = startTimeExclusive; this.Attributes = attributes; @@ -35,6 +37,8 @@ internal HistogramMetricAggregator(string name, DateTimeOffset startTimeExclusiv public string Name { get; private set; } + public Instrument Instrument { get; private set; } + public DateTimeOffset StartTimeExclusive { get; private set; } public DateTimeOffset EndTimeInclusive { get; private set; } @@ -71,7 +75,7 @@ public IMetric Collect(DateTimeOffset dt) return null; } - var cloneItem = new HistogramMetricAggregator(this.Name, this.StartTimeExclusive, this.Attributes, this.IsDeltaTemporality); + var cloneItem = new HistogramMetricAggregator(this.Name, this.Instrument, this.StartTimeExclusive, this.Attributes, this.IsDeltaTemporality); lock (this.lockUpdate) { diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs b/src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs index e8d7d56dc18..4b3a616cb34 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,8 @@ internal interface IMetric { string Name { get; } + Instrument Instrument { get; } + DateTimeOffset StartTimeExclusive { get; } DateTimeOffset EndTimeInclusive { get; } diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs index 8b34263ea84..462de9fd208 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 { @@ -30,9 +31,10 @@ internal class SumMetricAggregator : ISumMetric, IAggregator private double dsumNeg = 0; private long countNeg = 0; - internal SumMetricAggregator(string name, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes, bool isDelta, bool isMonotonic) + internal SumMetricAggregator(string name, Instrument instrument, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes, bool isDelta, bool isMonotonic) { this.Name = name; + this.Instrument = instrument; this.StartTimeExclusive = startTimeExclusive; this.EndTimeInclusive = startTimeExclusive; this.Attributes = attributes; @@ -42,6 +44,8 @@ internal SumMetricAggregator(string name, DateTimeOffset startTimeExclusive, Key public string Name { get; private set; } + public Instrument Instrument { get; private set; } + public DateTimeOffset StartTimeExclusive { get; private set; } public DateTimeOffset EndTimeInclusive { get; private set; } @@ -158,7 +162,7 @@ public void Update(DateTimeOffset dt, T value) public IMetric Collect(DateTimeOffset dt) { - var cloneItem = new SumMetricAggregator(this.Name, this.StartTimeExclusive, this.Attributes, this.IsDeltaTemporality, this.IsMonotonic); + var cloneItem = new SumMetricAggregator(this.Name, this.Instrument, this.StartTimeExclusive, this.Attributes, this.IsDeltaTemporality, this.IsMonotonic); lock (this.lockUpdate) { diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/SummaryMetricAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/SummaryMetricAggregator.cs index d003e0ad8d0..00f28b68d24 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,10 @@ internal class SummaryMetricAggregator : ISummaryMetric, IAggregator private List quantiles = new List(); - internal SummaryMetricAggregator(string name, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes, bool isMonotonic) + internal SummaryMetricAggregator(string name, Instrument instrument, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes, bool isMonotonic) { this.Name = name; + this.Instrument = instrument; this.StartTimeExclusive = startTimeExclusive; this.EndTimeInclusive = startTimeExclusive; this.Attributes = attributes; @@ -36,6 +38,8 @@ internal SummaryMetricAggregator(string name, DateTimeOffset startTimeExclusive, public string Name { get; private set; } + public Instrument Instrument { get; private set; } + public DateTimeOffset StartTimeExclusive { get; private set; } public DateTimeOffset EndTimeInclusive { get; private set; } @@ -97,7 +101,7 @@ public IMetric Collect(DateTimeOffset dt) return null; } - var cloneItem = new SummaryMetricAggregator(this.Name, this.StartTimeExclusive, this.Attributes, this.IsMonotonic); + var cloneItem = new SummaryMetricAggregator(this.Name, this.Instrument, this.StartTimeExclusive, this.Attributes, this.IsMonotonic); lock (this.lockUpdate) { diff --git a/src/OpenTelemetry/Metrics/Processors/MetricConsoleExporter.cs b/src/OpenTelemetry/Metrics/Processors/MetricConsoleExporter.cs index 81bb318d203..ce909c8aa6a 100644 --- a/src/OpenTelemetry/Metrics/Processors/MetricConsoleExporter.cs +++ b/src/OpenTelemetry/Metrics/Processors/MetricConsoleExporter.cs @@ -17,6 +17,7 @@ using System; using System.Globalization; using System.Linq; +using System.Text; namespace OpenTelemetry.Metrics { @@ -73,7 +74,28 @@ public override void OnEnd(MetricItem data) string time = $"{metric.StartTimeExclusive.ToLocalTime().ToString("HH:mm:ss.fff")} {metric.EndTimeInclusive.ToLocalTime().ToString("HH:mm:ss.fff")}"; - var msg = $"Export[{this.name}] {time} {metric.Name} [{string.Join(";", tags)}] {kind} Value: {valueDisplay}, Details: {metric.ToDisplayString()}"; + var msg = new StringBuilder($"Export[{this.name}] {time} {metric.Name} [{string.Join(";", tags)}] {kind} Value: {valueDisplay}, Details: {metric.ToDisplayString()}"); + + if (!string.IsNullOrEmpty(metric.Instrument.Description)) + { + msg.Append($", Description: {metric.Instrument.Description}"); + } + + if (!string.IsNullOrEmpty(metric.Instrument.Unit)) + { + msg.Append($", Unit: {metric.Instrument.Unit}"); + } + + if (!string.IsNullOrEmpty(metric.Instrument.Meter.Name)) + { + msg.Append($", Meter: {metric.Instrument.Meter.Name}"); + + if (!string.IsNullOrEmpty(metric.Instrument.Meter.Version)) + { + msg.Append($"/{metric.Instrument.Meter.Version}"); + } + } + Console.WriteLine(msg); } } From 7cd9d0c74916bd4a9a187d21142a034e2ece8a82 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Fri, 16 Jul 2021 12:38:10 -0700 Subject: [PATCH 2/5] Add reference to Meter on IMetric instead of Instrument --- src/OpenTelemetry/Metrics/AggregatorStore.cs | 8 ++++---- .../Metrics/MetricAggregators/GaugeMetricAggregator.cs | 8 ++++---- .../MetricAggregators/HistogramMetricAggregator.cs | 8 ++++---- src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs | 2 +- .../Metrics/MetricAggregators/SumMetricAggregator.cs | 8 ++++---- .../Metrics/MetricAggregators/SummaryMetricAggregator.cs | 8 ++++---- 6 files changed, 21 insertions(+), 21 deletions(-) diff --git a/src/OpenTelemetry/Metrics/AggregatorStore.cs b/src/OpenTelemetry/Metrics/AggregatorStore.cs index 0730bc54794..1e36eb092e8 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, this.instrument, dt, tags)); + aggregators.Add(new SumMetricAggregator(name, this.instrument.Meter, dt, tags)); } else if (this.instrument.GetType().Name.Contains("Gauge")) { - aggregators.Add(new GaugeMetricAggregator(name, this.instrument, dt, tags)); + aggregators.Add(new GaugeMetricAggregator(name, this.instrument.Meter, dt, tags)); } else if (this.instrument.GetType().Name.Contains("Histogram")) { - aggregators.Add(new HistogramMetricAggregator(name, this.instrument, dt, tags)); + aggregators.Add(new HistogramMetricAggregator(name, this.instrument.Meter, dt, tags)); } else { - aggregators.Add(new SummaryMetricAggregator(name, this.instrument, dt, tags, false)); + aggregators.Add(new SummaryMetricAggregator(name, 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 3064b8e6d93..0395802e1a3 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs @@ -25,17 +25,17 @@ internal class GaugeMetricAggregator : IGaugeMetric, IAggregator private readonly object lockUpdate = new object(); private IDataValue value; - internal GaugeMetricAggregator(string name, Instrument instrument, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) + internal GaugeMetricAggregator(string name, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) { this.Name = name; - this.Instrument = instrument; + this.Meter = meter; this.StartTimeExclusive = startTimeExclusive; this.Attributes = attributes; } public string Name { get; private set; } - public Instrument Instrument { get; private set; } + public Meter Meter { get; private set; } public DateTimeOffset StartTimeExclusive { get; private set; } @@ -58,7 +58,7 @@ public void Update(T value) public IMetric Collect(DateTimeOffset dt, bool isDelta) { - var cloneItem = new GaugeMetricAggregator(this.Name, this.Instrument, this.StartTimeExclusive, this.Attributes); + var cloneItem = new GaugeMetricAggregator(this.Name, 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 8bbbb796fa9..13ea156122e 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetricAggregator.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetricAggregator.cs @@ -25,17 +25,17 @@ internal class HistogramMetricAggregator : IHistogramMetric, IAggregator private readonly object lockUpdate = new object(); private List buckets = new List(); - internal HistogramMetricAggregator(string name, Instrument instrument, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) + internal HistogramMetricAggregator(string name, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) { this.Name = name; - this.Instrument = instrument; + this.Meter = meter; this.StartTimeExclusive = startTimeExclusive; this.Attributes = attributes; } public string Name { get; private set; } - public Instrument Instrument { get; private set; } + public Meter Meter { get; private set; } public DateTimeOffset StartTimeExclusive { get; private set; } @@ -72,7 +72,7 @@ public IMetric Collect(DateTimeOffset dt, bool isDelta) return null; } - var cloneItem = new HistogramMetricAggregator(this.Name, this.Instrument, this.StartTimeExclusive, this.Attributes); + var cloneItem = new HistogramMetricAggregator(this.Name, 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 9755aec473c..fc44c2c23fc 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs @@ -24,7 +24,7 @@ public interface IMetric { string Name { get; } - Instrument Instrument { get; } + Meter Meter { get; } DateTimeOffset StartTimeExclusive { get; } diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs index 49d1935a38d..3ed20826a42 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs @@ -27,10 +27,10 @@ internal class SumMetricAggregator : ISumMetric, IAggregator private long sumLong = 0; private double sumDouble = 0; - internal SumMetricAggregator(string name, Instrument instrument, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) + internal SumMetricAggregator(string name, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) { this.Name = name; - this.Instrument = instrument; + this.Meter = meter; this.StartTimeExclusive = startTimeExclusive; this.Attributes = attributes; this.IsMonotonic = true; @@ -38,7 +38,7 @@ internal SumMetricAggregator(string name, Instrument instrument, DateTimeOffset public string Name { get; private set; } - public Instrument Instrument { get; private set; } + public Meter Meter { get; private set; } public DateTimeOffset StartTimeExclusive { get; private set; } @@ -111,7 +111,7 @@ public void Update(T value) public IMetric Collect(DateTimeOffset dt, bool isDelta) { - var cloneItem = new SumMetricAggregator(this.Name, this.Instrument, this.StartTimeExclusive, this.Attributes); + var cloneItem = new SumMetricAggregator(this.Name, 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 c5df692f113..76f14945199 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/SummaryMetricAggregator.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/SummaryMetricAggregator.cs @@ -26,10 +26,10 @@ internal class SummaryMetricAggregator : ISummaryMetric, IAggregator private List quantiles = new List(); - internal SummaryMetricAggregator(string name, Instrument instrument, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes, bool isMonotonic) + internal SummaryMetricAggregator(string name, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes, bool isMonotonic) { this.Name = name; - this.Instrument = instrument; + this.Meter = meter; this.StartTimeExclusive = startTimeExclusive; this.Attributes = attributes; this.IsMonotonic = isMonotonic; @@ -37,7 +37,7 @@ internal SummaryMetricAggregator(string name, Instrument instrument, DateTimeOff public string Name { get; private set; } - public Instrument Instrument { get; private set; } + public Meter Meter { get; private set; } public DateTimeOffset StartTimeExclusive { get; private set; } @@ -89,7 +89,7 @@ public IMetric Collect(DateTimeOffset dt, bool isDelta) return null; } - var cloneItem = new SummaryMetricAggregator(this.Name, this.Instrument, this.StartTimeExclusive, this.Attributes, this.IsMonotonic); + var cloneItem = new SummaryMetricAggregator(this.Name, this.Meter, this.StartTimeExclusive, this.Attributes, this.IsMonotonic); lock (this.lockUpdate) { From 74940cbecbd10b9a1922d4da91906bc8f710d5e7 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Fri, 16 Jul 2021 13:59:17 -0700 Subject: [PATCH 3/5] Add description and unit to IMeter --- src/OpenTelemetry/Metrics/AggregatorStore.cs | 8 ++++---- .../Metrics/MetricAggregators/GaugeMetricAggregator.cs | 10 ++++++++-- .../MetricAggregators/HistogramMetricAggregator.cs | 10 ++++++++-- src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs | 4 ++++ .../Metrics/MetricAggregators/SumMetricAggregator.cs | 10 ++++++++-- .../MetricAggregators/SummaryMetricAggregator.cs | 10 ++++++++-- 6 files changed, 40 insertions(+), 12 deletions(-) diff --git a/src/OpenTelemetry/Metrics/AggregatorStore.cs b/src/OpenTelemetry/Metrics/AggregatorStore.cs index 1e36eb092e8..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, this.instrument.Meter, 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, this.instrument.Meter, 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, this.instrument.Meter, dt, tags)); + aggregators.Add(new HistogramMetricAggregator(name, this.instrument.Description, this.instrument.Unit, this.instrument.Meter, dt, tags)); } else { - aggregators.Add(new SummaryMetricAggregator(name, this.instrument.Meter, 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 0395802e1a3..ef24ad1bb6a 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs @@ -25,9 +25,11 @@ internal class GaugeMetricAggregator : IGaugeMetric, IAggregator private readonly object lockUpdate = new object(); private IDataValue value; - internal GaugeMetricAggregator(string name, Meter meter, 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; @@ -35,6 +37,10 @@ internal GaugeMetricAggregator(string name, Meter meter, DateTimeOffset startTim 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; } @@ -58,7 +64,7 @@ public void Update(T value) public IMetric Collect(DateTimeOffset dt, bool isDelta) { - var cloneItem = new GaugeMetricAggregator(this.Name, this.Meter, 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 13ea156122e..742c53a7386 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetricAggregator.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/HistogramMetricAggregator.cs @@ -25,9 +25,11 @@ internal class HistogramMetricAggregator : IHistogramMetric, IAggregator private readonly object lockUpdate = new object(); private List buckets = new List(); - internal HistogramMetricAggregator(string name, Meter meter, 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; @@ -35,6 +37,10 @@ internal HistogramMetricAggregator(string name, Meter meter, DateTimeOffset star 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; } @@ -72,7 +78,7 @@ public IMetric Collect(DateTimeOffset dt, bool isDelta) return null; } - var cloneItem = new HistogramMetricAggregator(this.Name, this.Meter, 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 fc44c2c23fc..f77a1599859 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs @@ -24,6 +24,10 @@ public interface IMetric { string Name { get; } + string Description { get; } + + string Unit { get; } + Meter Meter { get; } DateTimeOffset StartTimeExclusive { get; } diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs index 3ed20826a42..eea048b5195 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs @@ -27,9 +27,11 @@ internal class SumMetricAggregator : ISumMetric, IAggregator private long sumLong = 0; private double sumDouble = 0; - internal SumMetricAggregator(string name, Meter meter, 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; @@ -38,6 +40,10 @@ internal SumMetricAggregator(string name, Meter meter, DateTimeOffset startTimeE 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; } @@ -111,7 +117,7 @@ public void Update(T value) public IMetric Collect(DateTimeOffset dt, bool isDelta) { - var cloneItem = new SumMetricAggregator(this.Name, this.Meter, 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 76f14945199..c06874a3c11 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/SummaryMetricAggregator.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/SummaryMetricAggregator.cs @@ -26,9 +26,11 @@ internal class SummaryMetricAggregator : ISummaryMetric, IAggregator private List quantiles = new List(); - internal SummaryMetricAggregator(string name, Meter meter, 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; @@ -37,6 +39,10 @@ internal SummaryMetricAggregator(string name, Meter meter, DateTimeOffset startT 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; } @@ -89,7 +95,7 @@ public IMetric Collect(DateTimeOffset dt, bool isDelta) return null; } - var cloneItem = new SummaryMetricAggregator(this.Name, this.Meter, 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) { From 7dd103b9935b0a272e7fd1da17fe19f3ddcfeab9 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Fri, 16 Jul 2021 14:08:55 -0700 Subject: [PATCH 4/5] ConsoleMetricExporter - include meter, description, and unit --- .../ConsoleMetricExporter.cs | 24 ++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) 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); } } From 7b64de496e3d616c3b00ab14b2636b0ab6e06ba5 Mon Sep 17 00:00:00 2001 From: Alan West <3676547+alanwest@users.noreply.github.com> Date: Fri, 16 Jul 2021 14:16:32 -0700 Subject: [PATCH 5/5] Fix whitespace --- .../Metrics/MetricAggregators/GaugeMetricAggregator.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs b/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs index ef24ad1bb6a..67eadd8286f 100644 --- a/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs +++ b/src/OpenTelemetry/Metrics/MetricAggregators/GaugeMetricAggregator.cs @@ -25,7 +25,7 @@ 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) + internal GaugeMetricAggregator(string name, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair[] attributes) { this.Name = name; this.Description = description;