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

feat: Add OpenFeature.Extensions.Hosting package #181

Closed
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
bcf19c4
feat: Add OpenFeature.Extensions.Hosting package
austindrenski Jan 16, 2024
694c0ff
Merge branch 'main' into add-open-feature-extensions-hosting
askpt Apr 10, 2024
0e4fe74
Removed package from merge.
askpt Apr 10, 2024
b39f662
Try to fix build errors.
askpt Apr 10, 2024
85d2b36
Changing namespace.
askpt Apr 10, 2024
c6de9e3
Adding the documentation.
askpt Apr 10, 2024
1fc6d51
Merge branch 'main' into add-open-feature-extensions-hosting
askpt Apr 11, 2024
90f3723
Merge branch 'main' into add-open-feature-extensions-hosting
askpt Apr 11, 2024
ed6fa59
reverted version.
askpt Apr 11, 2024
67b24d9
Merge branch 'main' into add-open-feature-extensions-hosting
askpt Apr 15, 2024
31ba188
Merge branch 'main' into add-open-feature-extensions-hosting
askpt Apr 29, 2024
533aea8
Update README.md
askpt Apr 30, 2024
cb9d0f2
Update README.md
askpt Apr 30, 2024
38ea63e
Adding new example for the DI.
askpt May 14, 2024
942e759
Making class internal.
askpt May 14, 2024
496eabf
Renamed for clarity.
askpt May 15, 2024
00011c3
Moved class.
askpt May 15, 2024
8cd3710
Renamed stuff.
askpt May 15, 2024
a9ec44b
Adding example for global hook.
askpt May 15, 2024
eda289e
Merge branch 'main' into add-open-feature-extensions-hosting
askpt May 15, 2024
c57fb4f
Merge branch 'refs/heads/main' into add-open-feature-extensions-hosting
askpt Jul 1, 2024
a24521e
chore: Update OpenFeatureHostedService to use ShutdownAsync() method.…
askpt Jul 1, 2024
9daa33f
Fix duplicate.
askpt Jul 1, 2024
980f65d
Merge branch 'refs/heads/main' into add-open-feature-extensions-hosting
askpt Jul 4, 2024
4bb4cd1
Merge branch 'refs/heads/main' into add-open-feature-extensions-hosting
askpt Jul 24, 2024
bedb2f1
Fix null refs.
askpt Jul 24, 2024
0b6ee68
Merge branch 'refs/heads/main' into add-open-feature-extensions-hosting
askpt Jul 30, 2024
c9c2141
Merge branch 'refs/heads/main' into add-open-feature-extensions-hosting
askpt Aug 14, 2024
ff2512b
Merge branch 'main' into add-open-feature-extensions-hosting
askpt Aug 23, 2024
4870d9f
Adding extra check.
askpt Aug 23, 2024
cf1f05f
Renamed per suggestion.
askpt Aug 23, 2024
6bd175b
Adding cancellation token check.
askpt Aug 23, 2024
d338265
Merge branch 'main' into add-open-feature-extensions-hosting
askpt Sep 6, 2024
85cc629
Removed duplicated entry.
askpt Sep 6, 2024
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
4 changes: 3 additions & 1 deletion Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

<ItemGroup Label="src">
<PackageVersion Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.1" />
<PackageVersion Include="System.Collections.Immutable" Version="1.7.1" />
<PackageVersion Include="System.Threading.Channels" Version="6.0.0" />
Expand All @@ -19,6 +20,7 @@
<PackageVersion Include="coverlet.msbuild" Version="6.0.2" />
<PackageVersion Include="FluentAssertions" Version="6.12.0" />
<PackageVersion Include="GitHubActionsTestLogger" Version="2.3.3" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="8.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.9.0" />
<PackageVersion Include="NSubstitute" Version="5.1.0" />
<PackageVersion Include="SpecFlow" Version="3.9.74" />
Expand All @@ -32,4 +34,4 @@
<PackageVersion Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.3" />
</ItemGroup>

</Project>
</Project>
14 changes: 14 additions & 0 deletions OpenFeature.sln
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{65FBA159-2
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenFeature", "src\OpenFeature\OpenFeature.csproj", "{07A6E6BD-FB7E-4B3B-9CBE-65AE9D0EB223}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenFeature.Extensions.Hosting", "src\OpenFeature.Extensions.Hosting\OpenFeature.Extensions.Hosting.csproj", "{F2DB11D0-15E7-4C1F-936A-37D23EECECC0}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenFeature.Tests", "test\OpenFeature.Tests\OpenFeature.Tests.csproj", "{49BB42BA-10A6-4DA3-A7D5-38C968D57837}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenFeature.Extensions.Hosting.Tests", "test\OpenFeature.Extensions.Hosting.Tests\OpenFeature.Extensions.Hosting.Tests.csproj", "{4588FE3C-EB7E-4BA5-BD77-E15131DB3B29}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "OpenFeature.Benchmarks", "test\OpenFeature.Benchmarks\OpenFeature.Benchmarks.csproj", "{90E7EAD3-251E-4490-AF78-E758E33518E5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "OpenFeature.E2ETests", "test\OpenFeature.E2ETests\OpenFeature.E2ETests.csproj", "{7398C446-2630-4F8C-9278-4E807720E9E5}"
Expand All @@ -89,10 +93,18 @@ Global
{07A6E6BD-FB7E-4B3B-9CBE-65AE9D0EB223}.Debug|Any CPU.Build.0 = Debug|Any CPU
{07A6E6BD-FB7E-4B3B-9CBE-65AE9D0EB223}.Release|Any CPU.ActiveCfg = Release|Any CPU
{07A6E6BD-FB7E-4B3B-9CBE-65AE9D0EB223}.Release|Any CPU.Build.0 = Release|Any CPU
{F2DB11D0-15E7-4C1F-936A-37D23EECECC0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F2DB11D0-15E7-4C1F-936A-37D23EECECC0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F2DB11D0-15E7-4C1F-936A-37D23EECECC0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F2DB11D0-15E7-4C1F-936A-37D23EECECC0}.Release|Any CPU.Build.0 = Release|Any CPU
{49BB42BA-10A6-4DA3-A7D5-38C968D57837}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{49BB42BA-10A6-4DA3-A7D5-38C968D57837}.Debug|Any CPU.Build.0 = Debug|Any CPU
{49BB42BA-10A6-4DA3-A7D5-38C968D57837}.Release|Any CPU.ActiveCfg = Release|Any CPU
{49BB42BA-10A6-4DA3-A7D5-38C968D57837}.Release|Any CPU.Build.0 = Release|Any CPU
{4588FE3C-EB7E-4BA5-BD77-E15131DB3B29}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4588FE3C-EB7E-4BA5-BD77-E15131DB3B29}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4588FE3C-EB7E-4BA5-BD77-E15131DB3B29}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4588FE3C-EB7E-4BA5-BD77-E15131DB3B29}.Release|Any CPU.Build.0 = Release|Any CPU
{90E7EAD3-251E-4490-AF78-E758E33518E5}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{90E7EAD3-251E-4490-AF78-E758E33518E5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{90E7EAD3-251E-4490-AF78-E758E33518E5}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -107,7 +119,9 @@ Global
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{07A6E6BD-FB7E-4B3B-9CBE-65AE9D0EB223} = {C97E9975-E10A-4817-AE2C-4DD69C3C02D4}
{F2DB11D0-15E7-4C1F-936A-37D23EECECC0} = {C97E9975-E10A-4817-AE2C-4DD69C3C02D4}
{49BB42BA-10A6-4DA3-A7D5-38C968D57837} = {65FBA159-23E0-4CF9-881B-F78DBFF198E9}
{4588FE3C-EB7E-4BA5-BD77-E15131DB3B29} = {65FBA159-23E0-4CF9-881B-F78DBFF198E9}
{90E7EAD3-251E-4490-AF78-E758E33518E5} = {65FBA159-23E0-4CF9-881B-F78DBFF198E9}
{7398C446-2630-4F8C-9278-4E807720E9E5} = {65FBA159-23E0-4CF9-881B-F78DBFF198E9}
{C4746B8C-FE19-440B-922C-C2377F906FE8} = {2B172AA0-A5A6-4D94-BA1F-B79D59B0C2D8}
Expand Down
25 changes: 24 additions & 1 deletion README.md
Copy link
Member

Choose a reason for hiding this comment

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

We'll need to time this merge closely with the release or move this to a separate PR that can be merged after the 2.0 release.

Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ dotnet add package OpenFeature
public async Task Example()
{
// Register your feature flag provider
await Api.Instance.SetProvider(new InMemoryProvider());
Api.Instance.SetProvider(new InMemoryProvider());

// Create a new client
FeatureClient client = Api.Instance.GetClient();
Expand All @@ -67,6 +67,29 @@ public async Task Example()
}
```

### Dependency Injection Usage

```csharp
// Register your feature flag provider
builder.Services.AddOpenFeature(static builder =>
{
builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<FeatureProvider, SomeFeatureProvider>());
builder.TryAddOpenFeatureClient(SomeFeatureProvider.Name);
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible to do request scoped DI? It would be really great to include an example where you sent request specific context in the client. Since it's a slightly more advanced topic, perhaps it could be included in the evaluation context section later in the doc.

Copy link
Member

Choose a reason for hiding this comment

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

It looks like it should work. Here's an example from .NET FeatureManagement.

https://github.com/microsoft/FeatureManagement-Dotnet?tab=readme-ov-file#using-httpcontext

});

// Inject the client
app.MapGet("/flag", async ([FromServices]IFeatureClient client) =>
{
// Evaluate your feature flag
var flag = await client.GetBooleanValue("some_flag", true).ConfigureAwait(true);

if (flag)
{
// Do some work
}
})
```

## 🌟 Features

| Status | Features | Description |
Expand Down
3 changes: 3 additions & 0 deletions build/Common.prod.props
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@
<None Include="$(MSBuildThisFileDirectory)openfeature-icon.png" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<Compile Include="$(MSBuildThisFileDirectory)../src/Shared/**" LinkBase="Shared" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;

namespace OpenFeature.Internal;

/// <summary>
///
/// </summary>
public sealed class OpenFeatureHostedService(Api api, IEnumerable<FeatureProvider> providers) : IHostedLifecycleService
{
readonly Api _api = Check.NotNull(api);
readonly IEnumerable<FeatureProvider> _providers = Check.NotNull(providers);

async Task IHostedLifecycleService.StartingAsync(CancellationToken cancellationToken)
{
askpt marked this conversation as resolved.
Show resolved Hide resolved
foreach (var provider in this._providers)
{
await this._api.SetProviderAsync(provider.GetMetadata().Name ?? string.Empty, provider).ConfigureAwait(false);
Copy link
Member

Choose a reason for hiding this comment

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

I don't think the meta-data name for a provider is an appropriate name here. We probably need a way to make providers that encapsulates that data.

Simple example is using one provider with two different environments.

But more than that the provider name is a different domain of names. It is the domain of the provider implementation, where client names are in the domain of the application. An application developer needs to know and assign that name.

Copy link
Member

Choose a reason for hiding this comment

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

💯


if (this._api.GetProviderMetadata() is { Name: "No-op Provider" })
await this._api.SetProviderAsync(provider).ConfigureAwait(false);
}
}

Task IHostedService.StartAsync(CancellationToken cancellationToken) => Task.CompletedTask;

Task IHostedLifecycleService.StartedAsync(CancellationToken cancellationToken) => Task.CompletedTask;

Task IHostedLifecycleService.StoppingAsync(CancellationToken cancellationToken) => Task.CompletedTask;

Task IHostedService.StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;

Task IHostedLifecycleService.StoppedAsync(CancellationToken cancellationToken) => this._api.Shutdown();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Nullable>enable</Nullable>
<TargetFrameworks>netstandard2.0;net6.0;net7.0;net8.0;net462</TargetFrameworks>

Choose a reason for hiding this comment

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

Should .NET 7 be included as TFM? .NET 7 is out of official support

</PropertyGroup>

<PropertyGroup>
<PackageReadmeFile>README.md</PackageReadmeFile>
<RootNamespace>OpenFeature</RootNamespace>
</PropertyGroup>

<ItemGroup>
<None Include="../../README.md" Pack="true" PackagePath="/" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="../OpenFeature/OpenFeature.csproj" />
</ItemGroup>

</Project>
9 changes: 9 additions & 0 deletions src/OpenFeature.Extensions.Hosting/OpenFeatureBuilder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Microsoft.Extensions.DependencyInjection;

namespace OpenFeature;

/// <summary>
/// Describes a <see cref="OpenFeatureBuilder"/> backed by an <see cref="IServiceCollection"/>.
/// </summary>
/// <param name="Services"><see cref="IServiceCollection"/></param>
public sealed record OpenFeatureBuilder(IServiceCollection Services);
Copy link
Member

Choose a reason for hiding this comment

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

What is the reason to go for a record and the extension methods here? I am not too deep into .NET but I have not seen that pattern formerly.

145 changes: 145 additions & 0 deletions src/OpenFeature.Extensions.Hosting/OpenFeatureBuilderExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Logging;
using OpenFeature.Internal;
using OpenFeature.Model;

namespace OpenFeature;

/// <summary>
/// Contains extension methods for the <see cref="OpenFeatureBuilder"/> class.
/// </summary>
public static class OpenFeatureBuilderExtensions
{
/// <summary>
/// This method is used to add a new context to the service collection.
/// </summary>
/// <param name="builder"><see cref="OpenFeatureBuilder"/></param>
/// <param name="configure">the desired configuration</param>
/// <returns>
/// the <see cref="OpenFeatureBuilder"/> instance
/// </returns>
public static OpenFeatureBuilder AddContext(
this OpenFeatureBuilder builder,
Action<EvaluationContextBuilder> configure)
{
Check.NotNull(builder);
Check.NotNull(configure);

AddContext(builder, null, (b, _, _) => configure(b));

return builder;
}

/// <summary>
/// This method is used to add a new context to the service collection.
/// </summary>
/// <param name="builder"><see cref="OpenFeatureBuilder"/></param>
/// <param name="configure">the desired configuration</param>
/// <returns>
/// the <see cref="OpenFeatureBuilder"/> instance
/// </returns>
public static OpenFeatureBuilder AddContext(
this OpenFeatureBuilder builder,
Action<EvaluationContextBuilder, IServiceProvider> configure)
{
Check.NotNull(builder);
Check.NotNull(configure);

AddContext(builder, null, (b, _, s) => configure(b, s));

return builder;
}

/// <summary>
/// This method is used to add a new context to the service collection.
/// </summary>
/// <param name="builder"><see cref="OpenFeatureBuilder"/></param>
/// <param name="providerName">the name of the provider</param>
/// <param name="configure">the desired configuration</param>
/// <returns>
/// the <see cref="OpenFeatureBuilder"/> instance
/// </returns>
public static OpenFeatureBuilder AddContext(
this OpenFeatureBuilder builder,
string? providerName,
Action<EvaluationContextBuilder, string?, IServiceProvider> configure)
{
Check.NotNull(builder);
Check.NotNull(configure);

builder.Services.AddKeyedSingleton(providerName, (services, key) =>
{
var b = EvaluationContext.Builder();

configure(b, key as string, services);

return b.Build();
});

return builder;
}

/// <summary>
/// This method is used to add a new feature client to the service collection.
/// </summary>
/// <param name="builder"><see cref="OpenFeatureBuilder"/></param>
/// <param name="providerName">the name of the provider</param>
public static void TryAddOpenFeatureClient(this OpenFeatureBuilder builder, string? providerName = null)
{
Check.NotNull(builder);

builder.Services.AddHostedService<OpenFeatureHostedService>();

builder.Services.TryAddKeyedSingleton(providerName, static (services, providerName) =>
{
var api = providerName switch
{
null => Api.Instance,
not null => services.GetRequiredKeyedService<Api>(null)
};

api.AddHooks(services.GetKeyedServices<Hook>(providerName));
api.SetContext(services.GetRequiredKeyedService<EvaluationContextBuilder>(providerName).Build());

return api;
});

builder.Services.TryAddKeyedSingleton(providerName, static (services, providerName) => providerName switch
{
null => services.GetRequiredService<ILogger<FeatureClient>>(),
not null => services.GetRequiredService<ILoggerFactory>().CreateLogger($"OpenFeature.FeatureClient.{providerName}")
});

builder.Services.TryAddKeyedTransient(providerName, static (services, providerName) =>
{
var builder = providerName switch
{
null => EvaluationContext.Builder(),
not null => services.GetRequiredKeyedService<EvaluationContextBuilder>(null)
};

foreach (var c in services.GetKeyedServices<EvaluationContext>(providerName))
{
builder.Merge(c);
}

return builder;
});

builder.Services.TryAddKeyedTransient<IFeatureClient>(providerName, static (services, providerName) =>
{
var api = services.GetRequiredService<Api>();

return api.GetClient(
api.GetProviderMetadata(providerName as string ?? string.Empty).Name,
null,
services.GetRequiredKeyedService<ILogger>(providerName),
services.GetRequiredKeyedService<EvaluationContextBuilder>(providerName).Build());
});

if (providerName is not null)
builder.Services.Replace(ServiceDescriptor.Transient(services => services.GetRequiredKeyedService<IFeatureClient>(providerName)));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using OpenFeature;

#pragma warning disable IDE0130 // Namespace does not match folder structure
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection;

/// <summary>
/// Contains extension methods for the <see cref="IServiceCollection"/> class.
/// </summary>
public static class OpenFeatureServiceCollectionExtensions
{
/// <summary>
/// This method is used to add OpenFeature to the service collection.
/// OpenFeature will be registered as a singleton.
/// </summary>
/// <param name="services"><see cref="IServiceCollection"/></param>
/// <param name="configure">the desired configuration</param>
/// <returns>the current <see cref="IServiceCollection"/> instance</returns>
public static IServiceCollection AddOpenFeature(this IServiceCollection services, Action<OpenFeatureBuilder> configure)
{
Check.NotNull(services);
Check.NotNull(configure);

configure(AddOpenFeature(services));

return services;
}

/// <summary>
/// This method is used to add OpenFeature to the service collection.
/// OpenFeature will be registered as a singleton.
/// </summary>
/// <param name="services"><see cref="IServiceCollection"/></param>
/// <returns>the current <see cref="IServiceCollection"/> instance</returns>
public static OpenFeatureBuilder AddOpenFeature(this IServiceCollection services)
{
Check.NotNull(services);

var builder = new OpenFeatureBuilder(services);

builder.TryAddOpenFeatureClient();

return builder;
}
}
Loading