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

[sdk-metrics] Support .NET 9 Synchronous Gauge #5867

5 changes: 5 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ Notes](../../RELEASENOTES.md).
See [#5854](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5854)
for details.

* Add support for collecting metrics from
[Gauge](https://learn.microsoft.com/dotnet/api/system.diagnostics.metrics.gauge-1)
API
([#5867](https://github.com/open-telemetry/opentelemetry-dotnet/pull/5867))

## 1.9.0

Released 2024-Jun-14
Expand Down
14 changes: 14 additions & 0 deletions src/OpenTelemetry/Metrics/Metric.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ internal Metric(
aggType = AggregationType.DoubleGauge;
this.MetricType = MetricType.DoubleGauge;
}
else if (instrumentIdentity.InstrumentType == typeof(Gauge<double>)
|| instrumentIdentity.InstrumentType == typeof(Gauge<float>))
{
aggType = AggregationType.DoubleGauge;
this.MetricType = MetricType.DoubleGauge;
}
else if (instrumentIdentity.InstrumentType == typeof(ObservableGauge<long>)
|| instrumentIdentity.InstrumentType == typeof(ObservableGauge<int>)
|| instrumentIdentity.InstrumentType == typeof(ObservableGauge<short>)
Expand All @@ -125,6 +131,14 @@ internal Metric(
aggType = AggregationType.LongGauge;
this.MetricType = MetricType.LongGauge;
}
else if (instrumentIdentity.InstrumentType == typeof(Gauge<long>)
|| instrumentIdentity.InstrumentType == typeof(Gauge<int>)
|| instrumentIdentity.InstrumentType == typeof(Gauge<short>)
|| instrumentIdentity.InstrumentType == typeof(Gauge<byte>))
{
aggType = AggregationType.LongGauge;
this.MetricType = MetricType.LongGauge;
}
else if (instrumentIdentity.IsHistogram)
{
var explicitBucketBounds = instrumentIdentity.HistogramBucketBounds;
Expand Down
1 change: 1 addition & 0 deletions src/OpenTelemetry/Metrics/Reader/MetricReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public abstract partial class MetricReader : IDisposable

// Temporality is not defined for gauges, so this does not really affect anything.
var type when type == typeof(ObservableGauge<>) => AggregationTemporality.Delta,
var type when type == typeof(Gauge<>) => AggregationTemporality.Delta,

var type when type == typeof(UpDownCounter<>) => AggregationTemporality.Cumulative,
var type when type == typeof(ObservableUpDownCounter<>) => AggregationTemporality.Cumulative,
Expand Down
28 changes: 28 additions & 0 deletions test/OpenTelemetry.Tests/Metrics/MetricApiTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1613,6 +1613,34 @@ public void UnsupportedMetricInstrument()
Assert.Empty(exportedItems);
}

[Fact]
public void GaugeIsExportedCorrectly()
cijothomas marked this conversation as resolved.
Show resolved Hide resolved
{
var exportedItems = new List<Metric>();

using var meter = new Meter($"{Utils.GetCurrentMethodName()}");

using var container = this.BuildMeterProvider(out var meterProvider, builder => builder
.AddMeter(meter.Name)
.AddInMemoryExporter(exportedItems));

var gauge = meter.CreateGauge<long>(name: "NoiseLevel", unit: "dB", description: "Background Noise Level");
gauge.Record(10);
meterProvider.ForceFlush(MaxTimeToAllowForFlush);
Assert.Single(exportedItems);
var metric = exportedItems[0];
Assert.Equal("Background Noise Level", metric.Description);
List<MetricPoint> metricPoints = new List<MetricPoint>();
foreach (ref readonly var mp in metric.GetMetricPoints())
{
metricPoints.Add(mp);
}

var histogramPoint = metricPoints[0];
rajkumar-rangaraj marked this conversation as resolved.
Show resolved Hide resolved
var lastValue = metricPoints[0].GetGaugeLastValueLong();
Assert.Equal(10, lastValue);
}

internal static IConfiguration BuildConfiguration(bool emitOverflowAttribute, bool shouldReclaimUnusedMetricPoints)
{
var configurationData = new Dictionary<string, string>();
Expand Down
Loading