Skip to content

Commit

Permalink
Add reference to Instrument on IMetric (#2124)
Browse files Browse the repository at this point in the history
  • Loading branch information
alanwest authored Jul 16, 2021
1 parent 8577221 commit a1f6f42
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 14 deletions.
2 changes: 1 addition & 1 deletion examples/Console/TestMetrics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,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
24 changes: 23 additions & 1 deletion src/OpenTelemetry.Exporter.Console/ConsoleMetricExporter.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;
using OpenTelemetry.Metrics;

namespace OpenTelemetry.Exporter
Expand Down Expand Up @@ -74,7 +75,28 @@ public override ExportResult Export(in Batch<MetricItem> 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);
}
}
Expand Down
8 changes: 4 additions & 4 deletions src/OpenTelemetry/Metrics/AggregatorStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
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,15 +25,24 @@ 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, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] 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; }
Expand All @@ -54,7 +64,7 @@ public void Update<T>(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)
{
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,15 +25,24 @@ 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)
internal HistogramMetricAggregator(string name, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] 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; }
Expand Down Expand Up @@ -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)
{
Expand Down
7 changes: 7 additions & 0 deletions src/OpenTelemetry/Metrics/MetricAggregators/IMetric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,20 @@

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; }
Expand Down
14 changes: 12 additions & 2 deletions src/OpenTelemetry/Metrics/MetricAggregators/SumMetricAggregator.cs
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 @@ -26,16 +27,25 @@ internal class SumMetricAggregator : ISumMetric, IAggregator
private long sumLong = 0;
private double sumDouble = 0;

internal SumMetricAggregator(string name, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] attributes)
internal SumMetricAggregator(string name, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] attributes)
{
this.Name = name;
this.Description = description;
this.Unit = unit;
this.Meter = meter;
this.StartTimeExclusive = startTimeExclusive;
this.Attributes = attributes;
this.IsMonotonic = true;
}

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; }
Expand Down Expand Up @@ -107,7 +117,7 @@ public void Update<T>(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)
{
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,16 +26,25 @@ 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, string description, string unit, Meter meter, DateTimeOffset startTimeExclusive, KeyValuePair<string, object>[] attributes, bool isMonotonic)
{
this.Name = name;
this.Description = description;
this.Unit = unit;
this.Meter = meter;
this.StartTimeExclusive = startTimeExclusive;
this.Attributes = attributes;
this.IsMonotonic = isMonotonic;
}

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

0 comments on commit a1f6f42

Please sign in to comment.