Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 13 additions & 0 deletions src/OpenTelemetry/Internal/OpenTelemetrySdkEventSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ public void MetricViewException(string source, Exception ex)
}
}

[NonEvent]
public void ExemplarReservoirException(Exception ex)
{
if (this.IsEnabled(EventLevel.Warning, EventKeywords.All))
{
this.ExemplarReservoirException(ex.ToInvariantString());
}
}

[Event(4, Message = "Unknown error in SpanProcessor event '{0}': '{1}'.", Level = EventLevel.Error)]
public void SpanProcessorException(string evnt, string ex)
=> this.WriteEvent(4, evnt, ex);
Expand Down Expand Up @@ -310,6 +319,10 @@ public void TracesSamplerArgConfigInvalid(string configValue)
public void MetricViewException(string source, string ex)
=> this.WriteEvent(56, source, ex);

[Event(57, Message = "Exception thrown by user-supplied ExemplarReservoir.Offer implementation: '{0}'.", Level = EventLevel.Warning)]
public void ExemplarReservoirException(string ex)
=> this.WriteEvent(57, ex);

void IConfigurationExtensionsLogger.LogInvalidConfigurationValue(string key, string value)
=> this.InvalidConfigurationValue(key, value);

Expand Down
2 changes: 1 addition & 1 deletion src/OpenTelemetry/Metrics/MeterProviderSdk.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ internal static void MeasurementsCompleted(Instrument instrument, object? state)
{
if (state is not MetricState metricState)
{
// todo: Log
OpenTelemetrySdkEventSource.Log.MeterProviderSdkEvent($"MeasurementsCompleted called with unexpected state type '{state?.GetType().Name ?? "null"}'.");
return;
}

Expand Down
30 changes: 26 additions & 4 deletions src/OpenTelemetry/Metrics/MetricPoint/MetricPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1041,9 +1041,7 @@ private readonly void UpdateExemplar(long number, ReadOnlySpan<KeyValuePair<stri
{
if (offerExemplar)
{
// TODO: A custom implementation of `ExemplarReservoir.Offer` might throw an exception.
this.mpComponents!.ExemplarReservoir!.Offer(
new ExemplarMeasurement<long>(number, tags));
this.OfferExemplarSafe(number, tags);
Comment thread
martincostello marked this conversation as resolved.
}
}

Expand All @@ -1052,10 +1050,34 @@ private readonly void UpdateExemplar(double number, ReadOnlySpan<KeyValuePair<st
{
if (offerExemplar)
{
// TODO: A custom implementation of `ExemplarReservoir.Offer` might throw an exception.
this.OfferExemplarSafe(number, tags, explicitBucketHistogramBucketIndex);
}
}

private readonly void OfferExemplarSafe(long number, ReadOnlySpan<KeyValuePair<string, object?>> tags)
{
try
{
this.mpComponents!.ExemplarReservoir!.Offer(
new ExemplarMeasurement<long>(number, tags));
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.ExemplarReservoirException(ex);
}
}

private readonly void OfferExemplarSafe(double number, ReadOnlySpan<KeyValuePair<string, object?>> tags, int explicitBucketHistogramBucketIndex)
{
try
{
this.mpComponents!.ExemplarReservoir!.Offer(
new ExemplarMeasurement<double>(number, tags, explicitBucketHistogramBucketIndex));
}
catch (Exception ex)
{
OpenTelemetrySdkEventSource.Log.ExemplarReservoirException(ex);
}
Comment thread
nimanikoo marked this conversation as resolved.
}
Comment thread
nimanikoo marked this conversation as resolved.

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
46 changes: 36 additions & 10 deletions src/OpenTelemetry/Metrics/Reader/MetricReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,47 @@ public abstract partial class MetricReader : IDisposable
static (_) => AggregationTemporality.Cumulative;

private static readonly Func<Type, AggregationTemporality> MonotonicDeltaTemporalityPreferenceFunc =
static (instrumentType) => instrumentType.GetGenericTypeDefinition() switch
static (instrumentType) =>
Comment thread
martincostello marked this conversation as resolved.
{
var type when type == typeof(Counter<>) => AggregationTemporality.Delta,
var type when type == typeof(ObservableCounter<>) => AggregationTemporality.Delta,
var type when type == typeof(Histogram<>) => AggregationTemporality.Delta,
var genericType = instrumentType.GetGenericTypeDefinition();
if (genericType == typeof(Counter<>))
{
return AggregationTemporality.Delta;
}

if (genericType == typeof(ObservableCounter<>))
{
return AggregationTemporality.Delta;
}

if (genericType == typeof(Histogram<>))
{
return AggregationTemporality.Delta;
}

// 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,
if (genericType == typeof(ObservableGauge<>))
{
return AggregationTemporality.Delta;
}

var type when type == typeof(UpDownCounter<>) => AggregationTemporality.Cumulative,
var type when type == typeof(ObservableUpDownCounter<>) => AggregationTemporality.Cumulative,
if (genericType == typeof(Gauge<>))
{
return AggregationTemporality.Delta;
}

if (genericType == typeof(UpDownCounter<>))
{
return AggregationTemporality.Cumulative;
}

if (genericType == typeof(ObservableUpDownCounter<>))
{
return AggregationTemporality.Cumulative;
}

// TODO: Consider logging here because we should not fall through to this case.
_ => AggregationTemporality.Delta,
OpenTelemetrySdkEventSource.Log.MeterProviderSdkEvent($"Unexpected instrument type '{instrumentType.FullName}' encountered in temporality preference. Defaulting to Delta.");
Comment thread
martincostello marked this conversation as resolved.
Outdated
return AggregationTemporality.Delta;
};

private static readonly Func<Type, AggregationTemporality> LowMemoryTemporalityPreferenceFunc =
Expand Down
9 changes: 4 additions & 5 deletions src/OpenTelemetry/Metrics/Reader/MetricReaderExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ internal virtual List<Metric> AddMetricWithViews(Instrument instrument, List<Met
var maxCountMetricsToBeCreated = metricStreamConfigs.Count;

// Create list with initial capacity as the max metric count.
// Due to duplicate/max limit, we may not end up using them
// all, and that memory is wasted until Meter disposed.
// TODO: Revisit to see if we need to do metrics.TrimExcess()
// Due to duplicate/max limit, we may not end up using them all.
var metrics = new List<Metric>(maxCountMetricsToBeCreated);
lock (this.instrumentCreationLock)
{
Expand Down Expand Up @@ -175,9 +173,10 @@ internal virtual List<Metric> AddMetricWithViews(Instrument instrument, List<Met
this.CreateOrUpdateMetricStreamRegistration(in metricStreamIdentity);
}
}

return metrics;
}

metrics.TrimExcess();
return metrics;
}

internal void ApplyParentProviderSettings(
Expand Down
Loading