Skip to content
Merged
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
2 changes: 2 additions & 0 deletions src/Orleans.Core/Core/DefaultClientServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public static void AddDefaultServices(IClientBuilder builder)
// Common services
services.AddLogging();
services.AddOptions();
services.AddMetrics();
services.TryAddSingleton<TimeProvider>(TimeProvider.System);
services.TryAddSingleton<OrleansInstruments>();

// Options logging
services.TryAddSingleton(typeof(IOptionFormatter<>), typeof(DefaultOptionsFormatter<>));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,41 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;

namespace Orleans.Runtime;

internal static class ApplicationRequestInstruments
internal class ApplicationRequestInstruments
{
internal static Counter<long> TimedOutRequestsCounter = Instruments.Meter.CreateCounter<long>(InstrumentNames.APP_REQUESTS_TIMED_OUT);
internal static Counter<long> CanceledRequestsCounter = Instruments.Meter.CreateCounter<long>(InstrumentNames.APP_REQUESTS_CANCELED);
private readonly Counter<long> _timedOutRequestsCounter;
private readonly Counter<long> _canceledRequestsCounter;

private static readonly long[] AppRequestsLatencyHistogramBuckets = new long[] { 1, 2, 4, 6, 8, 10, 50, 100, 200, 400, 800, 1_000, 1_500, 2_000, 5_000, 10_000, 15_000 };
private static readonly HistogramAggregator AppRequestsLatencyHistogramAggregator = new(AppRequestsLatencyHistogramBuckets, Array.Empty<KeyValuePair<string, object>>(), value => new ("duration", $"{value}ms"));
private static readonly ObservableCounter<long> AppRequestsLatencyHistogramBucket = Instruments.Meter.CreateObservableCounter<long>(InstrumentNames.APP_REQUESTS_LATENCY_HISTOGRAM + "-bucket", AppRequestsLatencyHistogramAggregator.CollectBuckets);
private static readonly ObservableCounter<long> AppRequestsLatencyHistogramCount = Instruments.Meter.CreateObservableCounter<long>(InstrumentNames.APP_REQUESTS_LATENCY_HISTOGRAM + "-count", AppRequestsLatencyHistogramAggregator.CollectCount);
private static readonly ObservableCounter<long> AppRequestsLatencyHistogramSum = Instruments.Meter.CreateObservableCounter<long>(InstrumentNames.APP_REQUESTS_LATENCY_HISTOGRAM + "-sum", AppRequestsLatencyHistogramAggregator.CollectSum);
private static readonly long[] AppRequestsLatencyHistogramBuckets = [1, 2, 4, 6, 8, 10, 50, 100, 200, 400, 800, 1_000, 1_500, 2_000, 5_000, 10_000, 15_000];
private readonly HistogramAggregator _appRequestsLatencyHistogramAggregator;
private readonly ObservableCounter<long> _appRequestsLatencyHistogramBucket;
private readonly ObservableCounter<long> _appRequestsLatencyHistogramCount;
private readonly ObservableCounter<long> _appRequestsLatencyHistogramSum;

internal ApplicationRequestInstruments(OrleansInstruments instruments)
{
_timedOutRequestsCounter = instruments.Meter.CreateCounter<long>(InstrumentNames.APP_REQUESTS_TIMED_OUT);
_canceledRequestsCounter = instruments.Meter.CreateCounter<long>(InstrumentNames.APP_REQUESTS_CANCELED);
_appRequestsLatencyHistogramAggregator = new(AppRequestsLatencyHistogramBuckets, [], value => new("duration", $"{value}ms"));
_appRequestsLatencyHistogramBucket = instruments.Meter.CreateObservableCounter(InstrumentNames.APP_REQUESTS_LATENCY_HISTOGRAM + "-bucket", _appRequestsLatencyHistogramAggregator.CollectBuckets);
_appRequestsLatencyHistogramCount = instruments.Meter.CreateObservableCounter(InstrumentNames.APP_REQUESTS_LATENCY_HISTOGRAM + "-count", _appRequestsLatencyHistogramAggregator.CollectCount);
_appRequestsLatencyHistogramSum = instruments.Meter.CreateObservableCounter(InstrumentNames.APP_REQUESTS_LATENCY_HISTOGRAM + "-sum", _appRequestsLatencyHistogramAggregator.CollectSum);
}

internal static void OnAppRequestsEnd(long durationMilliseconds)
internal void OnAppRequestsEnd(long durationMilliseconds)
{
if (AppRequestsLatencyHistogramSum.Enabled)
AppRequestsLatencyHistogramAggregator.Record(durationMilliseconds);
if (_appRequestsLatencyHistogramSum.Enabled)
_appRequestsLatencyHistogramAggregator.Record(durationMilliseconds);
}

internal static void OnAppRequestsTimedOut()
internal void OnAppRequestsTimedOut()
{
TimedOutRequestsCounter.Add(1);
_timedOutRequestsCounter.Add(1);
}

internal static void OnAppRequestsCanceled()
internal void OnAppRequestsCanceled()
{
CanceledRequestsCounter.Add(1);
_canceledRequestsCounter.Add(1);
}
}
16 changes: 16 additions & 0 deletions src/Orleans.Core/Diagnostics/Metrics/OrleansInstruments.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Diagnostics.Metrics;

namespace Orleans.Runtime;

/// <summary>
/// Provides the <see cref="Meter"/> used by Orleans runtime metrics.
/// </summary>
/// <param name="meterFactory">The meter factory used to create the Orleans meter.</param>
public class OrleansInstruments(IMeterFactory meterFactory)
{
Comment thread
ReubenBond marked this conversation as resolved.
/// <summary>
/// Gets the Orleans runtime meter.
/// </summary>
public Meter Meter { get; } = (meterFactory ?? throw new ArgumentNullException(nameof(meterFactory))).Create("Microsoft.Orleans");
}
17 changes: 10 additions & 7 deletions src/Orleans.Core/Runtime/CallbackData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ internal sealed partial class CallbackData
{
private readonly SharedCallbackData shared;
private readonly IResponseCompletionSource context;
private readonly ApplicationRequestInstruments _applicationRequestInstruments;
private int completed;
private StatusResponse? lastKnownStatus;
private ValueStopwatch stopwatch;
Expand All @@ -19,11 +20,13 @@ internal sealed partial class CallbackData
public CallbackData(
SharedCallbackData shared,
IResponseCompletionSource ctx,
Message msg)
Message msg,
ApplicationRequestInstruments applicationRequestInstruments)
{
this.shared = shared;
this.context = ctx;
this.Message = msg;
_applicationRequestInstruments = applicationRequestInstruments;
this.stopwatch = ValueStopwatch.StartNew();
}

Expand Down Expand Up @@ -101,8 +104,8 @@ private void OnCancellation()
stopwatch.Stop();
SignalCancellation();
shared.Unregister(Message);
ApplicationRequestInstruments.OnAppRequestsEnd((long)stopwatch.Elapsed.TotalMilliseconds);
ApplicationRequestInstruments.OnAppRequestsTimedOut();
_applicationRequestInstruments.OnAppRequestsEnd((long)stopwatch.Elapsed.TotalMilliseconds);
_applicationRequestInstruments.OnAppRequestsTimedOut();
OrleansCallBackDataEvent.Log.OnCanceled(Message);
context.Complete(Response.FromException(new OperationCanceledException(_cancellationTokenRegistration.Token)));
_cancellationTokenRegistration.Dispose();
Expand All @@ -123,8 +126,8 @@ public void OnTimeout()

this.shared.Unregister(this.Message);
_cancellationTokenRegistration.Dispose();
ApplicationRequestInstruments.OnAppRequestsEnd((long)this.stopwatch.Elapsed.TotalMilliseconds);
ApplicationRequestInstruments.OnAppRequestsTimedOut();
_applicationRequestInstruments.OnAppRequestsEnd((long)this.stopwatch.Elapsed.TotalMilliseconds);
_applicationRequestInstruments.OnAppRequestsTimedOut();

OrleansCallBackDataEvent.Log.OnTimeout(this.Message);

Expand All @@ -148,7 +151,7 @@ public void OnTargetSiloFail()
this.stopwatch.Stop();
this.shared.Unregister(this.Message);
_cancellationTokenRegistration.Dispose();
ApplicationRequestInstruments.OnAppRequestsEnd((long)this.stopwatch.Elapsed.TotalMilliseconds);
_applicationRequestInstruments.OnAppRequestsEnd((long)this.stopwatch.Elapsed.TotalMilliseconds);

OrleansCallBackDataEvent.Log.OnTargetSiloFail(this.Message);
var msg = this.Message;
Expand All @@ -169,7 +172,7 @@ public void DoCallback(Message response)

this.stopwatch.Stop();
_cancellationTokenRegistration.Dispose();
ApplicationRequestInstruments.OnAppRequestsEnd((long)this.stopwatch.Elapsed.TotalMilliseconds);
_applicationRequestInstruments.OnAppRequestsEnd((long)this.stopwatch.Elapsed.TotalMilliseconds);

// do callback outside the CallbackData lock. Just not a good practice to hold a lock for this unrelated operation.
ResponseCallback(response, this.context);
Expand Down
7 changes: 5 additions & 2 deletions src/Orleans.Core/Runtime/OutsideRuntimeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ internal partial class OutsideRuntimeClient : IRuntimeClient, IDisposable, IClus

private readonly MessagingTrace messagingTrace;
private readonly InterfaceToImplementationMappingCache _interfaceToImplementationMapping;
private readonly ApplicationRequestInstruments _applicationRequestInstruments;
private IGrainCallCancellationManager _cancellationManager;
private IClusterConnectionStatusObserver[] _statusObservers;

Expand Down Expand Up @@ -71,10 +72,12 @@ public OutsideRuntimeClient(
MessagingTrace messagingTrace,
IServiceProvider serviceProvider,
TimeProvider timeProvider,
InterfaceToImplementationMappingCache interfaceToImplementationMapping)
InterfaceToImplementationMappingCache interfaceToImplementationMapping,
OrleansInstruments orleansInstruments)
{
TimeProvider = timeProvider;
_interfaceToImplementationMapping = interfaceToImplementationMapping;
_applicationRequestInstruments = new(orleansInstruments);
this.ServiceProvider = serviceProvider;
_localClientDetails = localClientDetails;
this.loggerFactory = loggerFactory;
Expand Down Expand Up @@ -281,7 +284,7 @@ public void SendRequest(GrainReference target, IInvokable request, IResponseComp

if (!oneWay)
{
var callbackData = new CallbackData(this.sharedCallbackData, context, message);
var callbackData = new CallbackData(this.sharedCallbackData, context, message, _applicationRequestInstruments);
callbackData.SubscribeForCancellation(cancellationToken);
callbacks.TryAdd(message.Id, callbackData);
}
Expand Down
7 changes: 5 additions & 2 deletions src/Orleans.Runtime/Core/InsideRuntimeClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ internal sealed partial class InsideRuntimeClient : IRuntimeClient, ILifecyclePa
private MessageCenter messageCenter;
private List<IIncomingGrainCallFilter> grainCallFilters;
private readonly DeepCopier _deepCopier;
private readonly ApplicationRequestInstruments _applicationRequestInstruments;
private IGrainCallCancellationManager _cancellationManager;
private HostedClient hostedClient;

Expand All @@ -63,11 +64,13 @@ public InsideRuntimeClient(
GrainInterfaceTypeToGrainTypeResolver interfaceToTypeResolver,
DeepCopier deepCopier,
TimeProvider timeProvider,
InterfaceToImplementationMappingCache interfaceToImplementationMapping)
InterfaceToImplementationMappingCache interfaceToImplementationMapping,
OrleansInstruments orleansInstruments)
{
TimeProvider = timeProvider;
this.interfaceToImplementationMapping = interfaceToImplementationMapping;
this._deepCopier = deepCopier;
this._applicationRequestInstruments = new(orleansInstruments);
this.ServiceProvider = serviceProvider;
this.MySilo = siloDetails.SiloAddress;
this.callbacks = new ConcurrentDictionary<(GrainId, CorrelationId), CallbackData>();
Expand Down Expand Up @@ -173,7 +176,7 @@ public void SendRequest(
Debug.Assert(context is not null);

// Register a callback for the request.
var callbackData = new CallbackData(sharedData, context, message);
var callbackData = new CallbackData(sharedData, context, message, _applicationRequestInstruments);
callbacks.TryAdd((message.SendingGrain, message.Id), callbackData);
callbackData.SubscribeForCancellation(cancellationToken);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Orleans.Runtime/Hosting/DefaultSiloServices.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ internal static void AddDefaultServices(ISiloBuilder builder)
// Common services
services.AddLogging();
services.AddOptions();
services.AddMetrics();
services.TryAddSingleton<TimeProvider>(TimeProvider.System);
services.TryAddSingleton<OrleansInstruments>();

services.TryAddSingleton(typeof(IOptionFormatter<>), typeof(DefaultOptionsFormatter<>));
services.TryAddSingleton(typeof(IOptionFormatterResolver<>), typeof(DefaultOptionsFormatterResolver<>));
Expand Down
2 changes: 1 addition & 1 deletion src/api/Orleans.Core/Orleans.Core.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4513,4 +4513,4 @@ public override void SetArgument(int index, object value) { }

public override void SetTarget(global::Orleans.Serialization.Invocation.ITargetHolder holder) { }
}
}
}
Loading