Skip to content

Commit

Permalink
Isolate 'DefaultMeterFactory' singleton per tenant
Browse files Browse the repository at this point in the history
  • Loading branch information
jtkech committed Aug 18, 2023
1 parent a1900d0 commit 2be899f
Showing 1 changed file with 40 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Metrics;
using System.IO;
using System.Linq;
using Microsoft.AspNetCore.Authentication;
Expand Down Expand Up @@ -63,6 +64,15 @@ public static class ServiceCollectionExtensions
.Select(sd => sd.GetImplementationType()))
.ToArray();

/// <summary>
/// Metrics singletons used to isolate tenants from the host.
/// </summary>
private static readonly Type[] _metricsTypesToIsolate = new ServiceCollection()
.AddMetrics()

Check failure on line 71 in src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-latest)

'ServiceCollection' does not contain a definition for 'AddMetrics' and no accessible extension method 'AddMetrics' accepting a first argument of type 'ServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 71 in src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-latest)

'ServiceCollection' does not contain a definition for 'AddMetrics' and no accessible extension method 'AddMetrics' accepting a first argument of type 'ServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 71 in src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest)

'ServiceCollection' does not contain a definition for 'AddMetrics' and no accessible extension method 'AddMetrics' accepting a first argument of type 'ServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 71 in src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest)

'ServiceCollection' does not contain a definition for 'AddMetrics' and no accessible extension method 'AddMetrics' accepting a first argument of type 'ServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 71 in src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest)

'ServiceCollection' does not contain a definition for 'AddMetrics' and no accessible extension method 'AddMetrics' accepting a first argument of type 'ServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 71 in src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest)

'ServiceCollection' does not contain a definition for 'AddMetrics' and no accessible extension method 'AddMetrics' accepting a first argument of type 'ServiceCollection' could be found (are you missing a using directive or an assembly reference?)
.Where(sd => sd.Lifetime == ServiceLifetime.Singleton)
.Select(sd => sd.GetImplementationType())
.ToArray();

/// <summary>
/// Adds OrchardCore services to the host service collection.
/// </summary>
Expand All @@ -89,6 +99,7 @@ public static OrchardCoreBuilder AddOrchardCore(this IServiceCollection services
AddExtensionServices(builder);
AddStaticFiles(builder);

AddMetrics(builder);
AddRouting(builder);
IsolateHttpClient(builder);
AddEndpointsApiExplorer(builder);
Expand Down Expand Up @@ -270,6 +281,35 @@ private static void AddStaticFiles(OrchardCoreBuilder builder)
});
}

/// <summary>
/// Adds isolated tenant level routing services.
/// </summary>
private static void AddMetrics(OrchardCoreBuilder builder)
{
// 'AddMetrics()' is called by the host.

builder.ConfigureServices(collection =>
{
// The 'DefaultMeterFactory' caches 'Meters' in a non thread safe dictionary.
// So, we need to register an isolated 'IMeterFactory' singleton per tenant.
var descriptorsToRemove = collection
.Where(sd =>
sd is ClonedSingletonDescriptor &&
_metricsTypesToIsolate.Contains(sd.GetImplementationType()))
.ToArray();
// Isolate each tenant from the host.
foreach (var descriptor in descriptorsToRemove)
{
collection.Remove(descriptor);
}
collection.AddMetrics();

Check failure on line 308 in src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-latest)

'IServiceCollection' does not contain a definition for 'AddMetrics' and no accessible extension method 'AddMetrics' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 308 in src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / Build & Test (ubuntu-latest)

'IServiceCollection' does not contain a definition for 'AddMetrics' and no accessible extension method 'AddMetrics' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 308 in src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest)

'IServiceCollection' does not contain a definition for 'AddMetrics' and no accessible extension method 'AddMetrics' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 308 in src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest)

'IServiceCollection' does not contain a definition for 'AddMetrics' and no accessible extension method 'AddMetrics' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 308 in src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest)

'IServiceCollection' does not contain a definition for 'AddMetrics' and no accessible extension method 'AddMetrics' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)

Check failure on line 308 in src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs

View workflow job for this annotation

GitHub Actions / Build & Test (windows-latest)

'IServiceCollection' does not contain a definition for 'AddMetrics' and no accessible extension method 'AddMetrics' accepting a first argument of type 'IServiceCollection' could be found (are you missing a using directive or an assembly reference?)
},
order: Int32.MinValue + 100);
}

/// <summary>
/// Adds isolated tenant level routing services.
/// </summary>
Expand Down

0 comments on commit 2be899f

Please sign in to comment.