Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
OpenTelemetry.Metrics.MetricReaderTemporalityPreference.LowMemory = 3 -> OpenTelemetry.Metrics.MetricReaderTemporalityPreference
3 changes: 3 additions & 0 deletions src/OpenTelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ Notes](../../RELEASENOTES.md).

## Unreleased

* Added `LowMemory` temporality as an option in the OTLP metrics exporter.
([#6648](https://github.com/open-telemetry/opentelemetry-dotnet/pull/6648))

## 1.14.0-rc.1

Released 2025-Oct-21
Expand Down
16 changes: 16 additions & 0 deletions src/OpenTelemetry/Metrics/Reader/MetricReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,21 @@ public abstract partial class MetricReader : IDisposable
};
};

private static readonly Func<Type, AggregationTemporality> LowMemoryTemporalityPreferenceFunc = (instrumentType) =>
{
return instrumentType.GetGenericTypeDefinition() switch
{
var type when type == typeof(Counter<>) => AggregationTemporality.Delta,
var type when type == typeof(Histogram<>) => AggregationTemporality.Delta,

var type when type == typeof(UpDownCounter<>) => AggregationTemporality.Cumulative,
var type when type == typeof(ObservableCounter<>) => AggregationTemporality.Cumulative,
var type when type == typeof(ObservableUpDownCounter<>) => AggregationTemporality.Cumulative,

_ => AggregationTemporality.Cumulative,
};
};
Comment on lines +40 to +53
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if using TypeHandle property would be more performant here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless there's a huge benefit, I think it's better to make the code obvious by using ==. It's also not obvious from the source code what that property does differently as it just throws an exception.


private readonly Lock newTaskLock = new();
private readonly Lock onCollectLock = new();
private readonly TaskCompletionSource<bool> shutdownTcs = new();
Expand Down Expand Up @@ -72,6 +87,7 @@ public MetricReaderTemporalityPreference TemporalityPreference
this.temporalityFunc = value switch
{
MetricReaderTemporalityPreference.Delta => MonotonicDeltaTemporalityPreferenceFunc,
MetricReaderTemporalityPreference.LowMemory => LowMemoryTemporalityPreferenceFunc,
_ => CumulativeTemporalityPreferenceFunc,
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ public enum MetricReaderTemporalityPreference
/// Aggregations of non-monotonic measurements use cumulative temporality.
/// </summary>
Delta = 2,

/// <summary>
/// Uses delta temporality for synchronous Counter and Histogram instruments and
/// cumulative temporality for synchronous UpDownCounter, ObservableCounter and
/// ObservableUpDownCounter instruments. This mode reduces SDK memory usage by avoiding
/// the need to store both cumulative and delta states for temporality conversion.
/// </summary>
LowMemory = 3,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically, all these 3 options are defined only for OTLP exporter https://github.com/open-telemetry/opentelemetry-specification/blob/7f6d35f758bb5d92e354460d040974665a29ba32/specification/metrics/sdk_exporters/otlp.md?plain=1#L55 and it shouldn't be part of the SDK.

Based on current design, I do not see any better option to put in the current class design.
@alanwest, do you remember any historical reasons for keeping it here?

}
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,7 @@ public void TestHistogramToOtlpMetric(string name, string? description, string?
[InlineData("cuMulative", MetricReaderTemporalityPreference.Cumulative)]
[InlineData("DeltA", MetricReaderTemporalityPreference.Delta)]
[InlineData("invalid", MetricReaderTemporalityPreference.Cumulative)]
[InlineData("lowmemory", MetricReaderTemporalityPreference.LowMemory)]
public void TestTemporalityPreferenceUsingConfiguration(string configValue, MetricReaderTemporalityPreference expectedTemporality)
{
var testExecuted = false;
Expand Down Expand Up @@ -757,6 +758,7 @@ public void TestTemporalityPreferenceUsingConfiguration(string configValue, Metr
[InlineData("cuMulative", MetricReaderTemporalityPreference.Cumulative)]
[InlineData("DeltA", MetricReaderTemporalityPreference.Delta)]
[InlineData("invalid", MetricReaderTemporalityPreference.Cumulative)]
[InlineData("lowmemory", MetricReaderTemporalityPreference.LowMemory)]
public void TestTemporalityPreferenceUsingEnvVar(string configValue, MetricReaderTemporalityPreference expectedTemporality)
{
Environment.SetEnvironmentVariable(OtlpSpecConfigDefinitionTests.MetricsData.TemporalityKeyName, configValue);
Expand Down
103 changes: 103 additions & 0 deletions test/OpenTelemetry.Tests/Metrics/MetricReaderTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

using System.Diagnostics.Metrics;
using OpenTelemetry.Tests;
using Xunit;

namespace OpenTelemetry.Metrics.Tests;

public class MetricReaderTests
{
[Theory]
[InlineData("counter", typeof(long), AggregationTemporality.Delta)]
[InlineData("counter", typeof(double), AggregationTemporality.Delta)]
[InlineData("histogram", typeof(long), AggregationTemporality.Delta)]
[InlineData("histogram", typeof(double), AggregationTemporality.Delta)]
[InlineData("updowncounter", typeof(long), AggregationTemporality.Cumulative)]
[InlineData("updowncounter", typeof(double), AggregationTemporality.Cumulative)]
[InlineData("observablecounter", typeof(long), AggregationTemporality.Cumulative)]
[InlineData("observablecounter", typeof(double), AggregationTemporality.Cumulative)]
[InlineData("observableupdowncounter", typeof(long), AggregationTemporality.Cumulative)]
[InlineData("observableupdowncounter", typeof(double), AggregationTemporality.Cumulative)]
public void LowMemoryTemporality_UsesCorrectAggregationTemporality(string instrumentName, Type valueType, AggregationTemporality expectedTemporality)
{
var metrics = new List<Metric>();
using var meter = new Meter(Utils.GetCurrentMethodName());
using var provider = Sdk.CreateMeterProviderBuilder()
.AddMeter(meter.Name)
.AddInMemoryExporter(metrics, metricReaderOptions =>
{
metricReaderOptions.TemporalityPreference = MetricReaderTemporalityPreference.LowMemory;
})
.Build();

switch (instrumentName)
{
case "counter":
if (valueType == typeof(long))
{
meter.CreateCounter<long>("test_counter").Add(1);
}
else
{
meter.CreateCounter<double>("test_counter").Add(1);
}

break;

case "histogram":
if (valueType == typeof(long))
{
meter.CreateHistogram<long>("test_histogram").Record(1);
}
else
{
meter.CreateHistogram<double>("test_histogram").Record(1);
}

break;

case "updowncounter":
if (valueType == typeof(long))
{
meter.CreateUpDownCounter<long>("test_updown").Add(1);
}
else
{
meter.CreateUpDownCounter<double>("test_updown").Add(1);
}

break;

case "observablecounter":
if (valueType == typeof(long))
{
meter.CreateObservableCounter("test_observable_counter", () => new Measurement<long>(1));
}
else
{
meter.CreateObservableCounter("test_observable_counter", () => new Measurement<double>(1));
}

break;

case "observableupdowncounter":
if (valueType == typeof(long))
{
meter.CreateObservableUpDownCounter("test_observable_updown", () => new Measurement<long>(1));
}
else
{
meter.CreateObservableUpDownCounter("test_observable_updown", () => new Measurement<double>(1));
}

break;
}

provider.ForceFlush();

var metric = Assert.Single(metrics);
Assert.Equal(expectedTemporality, metric.Temporality);
}
}
Loading