Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Add reference to Instrument on IMetric #2124

Merged
2 changes: 1 addition & 1 deletion examples/Console/TestMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal static object Run(MetricsOptions options)
Counter<int> counter = null;
if (options.FlagCounter ?? true)
{
counter = meter.CreateCounter<int>("counter");
counter = meter.CreateCounter<int>("counter", "things", "A count of things");
}

Histogram<int> histogram = null;
Expand Down
10 changes: 5 additions & 5 deletions src/OpenTelemetry/Metrics/AggregatorStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;

namespace OpenTelemetry.Metrics
{
Expand All @@ -24,16 +25,19 @@ internal class GaugeMetricAggregator : IGaugeMetric, IAggregator
private readonly object lockUpdate = new object();
private IDataValue value;

internal GaugeMetricAggregator(string name, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] attributes)
internal GaugeMetricAggregator(string name, Instrument instrument, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] attributes)
{
this.Name = name;
this.Instrument = instrument;
this.StartTimeExclusive = startTimeExclusive;
this.EndTimeInclusive = startTimeExclusive;
this.Attributes = attributes;
}

public string Name { get; private set; }

public Instrument Instrument { get; private set; }

public DateTimeOffset StartTimeExclusive { get; private set; }

public DateTimeOffset EndTimeInclusive { get; private set; }
Expand All @@ -56,7 +60,7 @@ public void Update<T>(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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;

namespace OpenTelemetry.Metrics
{
Expand All @@ -24,9 +25,10 @@ internal class HistogramMetricAggregator : IHistogramMetric, IAggregator
private readonly object lockUpdate = new object();
private List<HistogramBucket> buckets = new List<HistogramBucket>();

internal HistogramMetricAggregator(string name, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] attributes, bool isDelta)
internal HistogramMetricAggregator(string name, Instrument instrument, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] attributes, bool isDelta)
{
this.Name = name;
this.Instrument = instrument;
this.StartTimeExclusive = startTimeExclusive;
this.EndTimeInclusive = startTimeExclusive;
this.Attributes = attributes;
Expand All @@ -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; }
Expand Down Expand Up @@ -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)
{
Expand Down
3 changes: 3 additions & 0 deletions src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,16 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;

namespace OpenTelemetry.Metrics
{
internal interface IMetric
{
string Name { get; }

Instrument Instrument { get; }

DateTimeOffset StartTimeExclusive { get; }

DateTimeOffset EndTimeInclusive { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;

namespace OpenTelemetry.Metrics
{
Expand All @@ -30,9 +31,10 @@ internal class SumMetricAggregator : ISumMetric, IAggregator
private double dsumNeg = 0;
private long countNeg = 0;

internal SumMetricAggregator(string name, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] attributes, bool isDelta, bool isMonotonic)
internal SumMetricAggregator(string name, Instrument instrument, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] attributes, bool isDelta, bool isMonotonic)
{
this.Name = name;
this.Instrument = instrument;
this.StartTimeExclusive = startTimeExclusive;
this.EndTimeInclusive = startTimeExclusive;
this.Attributes = attributes;
Expand All @@ -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; }
Expand Down Expand Up @@ -158,7 +162,7 @@ public void Update<T>(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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;

namespace OpenTelemetry.Metrics
{
Expand All @@ -25,9 +26,10 @@ internal class SummaryMetricAggregator : ISummaryMetric, IAggregator

private List<ValueAtQuantile> quantiles = new List<ValueAtQuantile>();

internal SummaryMetricAggregator(string name, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] attributes, bool isMonotonic)
internal SummaryMetricAggregator(string name, Instrument instrument, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] attributes, bool isMonotonic)
{
this.Name = name;
this.Instrument = instrument;
this.StartTimeExclusive = startTimeExclusive;
this.EndTimeInclusive = startTimeExclusive;
this.Attributes = attributes;
Expand All @@ -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; }
Expand Down Expand Up @@ -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)
{
Expand Down
24 changes: 23 additions & 1 deletion src/OpenTelemetry/Metrics/Processors/MetricConsoleExporter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using System;
using System.Globalization;
using System.Linq;
using System.Text;

namespace OpenTelemetry.Metrics
{
Expand Down Expand Up @@ -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);
}
}
Expand Down