Skip to content

Conversation

@kylejuliandev
Copy link
Contributor

@kylejuliandev kylejuliandev commented May 7, 2025

This PR

  • Introduces a new extension method in the OpenFeature.DependencyInjection package which allows consumers to add, at a global/api level, a handler.

Example usage:

builder.Services.AddOpenFeature(feature =>
{
    feature.AddHostedFeatureLifecycle()
        .AddInMemoryProvider()
        .AddHandler(ProviderEventTypes.ProviderReady, (@event) => { Console.WriteLine("{0}", @event!.ProviderName); })
        .AddHandler(ProviderEventTypes.ProviderReady, sp => (@event) =>
        {
            var logger = sp.GetRequiredService<ILogger<Program>>();
            logger.LogInformation("Provider Ready");
        });
});

Related Issues

Fixes #457

Notes

Follow-up Tasks

How to test

@kylejuliandev kylejuliandev requested a review from a team as a code owner May 7, 2025 19:24
@codecov
Copy link

codecov bot commented May 7, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 86.87%. Comparing base (0a5ab0c) to head (51645c2).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #462      +/-   ##
==========================================
+ Coverage   86.72%   86.87%   +0.14%     
==========================================
  Files          42       43       +1     
  Lines        1703     1722      +19     
  Branches      179      180       +1     
==========================================
+ Hits         1477     1496      +19     
  Misses        187      187              
  Partials       39       39              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Member

@beeme1mr beeme1mr left a comment

Choose a reason for hiding this comment

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

Looks good to me. I just had a question about the random name generator but it isn't a blocker.

Copy link
Member

@askpt askpt left a comment

Choose a reason for hiding this comment

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

Looks good to me. Great work. Added a couple of suggestions 👍

* Add check if passed in handler name is an empty or null string

Signed-off-by: Kyle Julian <[email protected]>
Copy link
Member

@askpt askpt left a comment

Choose a reason for hiding this comment

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

It is a very minor request! We should also validate for inputs like whitespace.

Also, @arttonoyan, would you mind double-checking?

* Ensure that calling AddHandler multiple times will actually add all
  instances to the underlying OpenFeature API
* Add unit tests to cover this behaviour

Signed-off-by: Kyle Julian <[email protected]>
Copy link
Member

@askpt askpt left a comment

Choose a reason for hiding this comment

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

LGTM. Just a minor question, and should be ready to merge! Thanks!

* Throw ArgumentException when HandlerName is null or empty
* Use primary constructor in EventHandlerDelegateWrapper

Signed-off-by: Kyle Julian <[email protected]>
@askpt
Copy link
Member

askpt commented May 10, 2025

LGTM! If @arttonoyan is happy with the changes @kylejuliandev made, we can merge it 👍
Thanks for your efforts!

* Ensure when multiple handlers are registered that they are both
  registered
* Tweak CreateServerAsync to make it easier to extend dependency
  injection tests

Signed-off-by: Kyle Julian <[email protected]>
* Remove any unnecessary unit tests
* Update integration tests to use openFeatureBuilder for easily
  adjusting the Arrange part of each test

Signed-off-by: Kyle Julian <[email protected]>
@askpt askpt added this pull request to the merge queue May 12, 2025
Merged via the queue into open-feature:main with commit ff414b8 May 12, 2025
18 checks passed
@kylejuliandev
Copy link
Contributor Author

Thank you @askpt @arttonoyan @beeme1mr! This ended up being a code review and API review all in one; I appreciate the collaboration!

@kylejuliandev kylejuliandev deleted the feat/add-eventhandler-via-dependencyinjection branch May 12, 2025 07:58
WeihanLi pushed a commit to WeihanLi/openfeature-dotnet-sdk that referenced this pull request May 14, 2025
…open-feature#462)

<!-- Please use this template for your pull request. -->
<!-- Please use the sections that you need and delete other sections -->

## This PR
<!-- add the description of the PR here -->

- Introduces a new extension method in the
OpenFeature.DependencyInjection package which allows consumers to add,
at a global/api level, a handler.

Example usage:

```csharp
builder.Services.AddOpenFeature(feature =>
{
    feature.AddHostedFeatureLifecycle()
        .AddInMemoryProvider()
        .AddHandler(ProviderEventTypes.ProviderReady, (@event) => { Console.WriteLine("{0}", @event!.ProviderName); })
        .AddHandler(ProviderEventTypes.ProviderReady, sp => (@event) =>
        {
            var logger = sp.GetRequiredService<ILogger<Program>>();
            logger.LogInformation("Provider Ready");
        });
});
```

### Related Issues
<!-- add here the GitHub issue that this PR resolves if applicable -->

Fixes open-feature#457

### Notes
<!-- any additional notes for this PR -->

### Follow-up Tasks
<!-- anything that is related to this PR but not done here should be
noted under this section -->
<!-- if there is a need for a new issue, please link it here -->

### How to test
<!-- if applicable, add testing instructions under this section -->

---------

Signed-off-by: Kyle Julian <[email protected]>
Signed-off-by: Weihan Li <[email protected]>
@askpt askpt mentioned this pull request May 14, 2025
@askpt askpt mentioned this pull request May 23, 2025
github-merge-queue bot pushed a commit that referenced this pull request May 23, 2025
🤖 I have created a release *beep* *boop*
---


##
[2.6.0](v2.5.0...v2.6.0)
(2025-05-23)


### ✨ New Features

* add AddHandler extension method to Dependency Injection package
([#462](#462))
([ff414b8](ff414b8))
* Add Extension Method for adding global Hook via DependencyInjection
([#459](#459))
([9b04485](9b04485))
* Add OTEL compatible telemetry object builder
([#397](#397))
([6c44db9](6c44db9))


### 🧹 Chore

* Cleanup .props file
([#476](#476))
([6d7a535](6d7a535))
* **deps:** update actions/attest-build-provenance action to v2.3.0
([#464](#464))
([0a5ab0c](0a5ab0c))
* **deps:** update codecov/codecov-action action to v5.4.3
([#475](#475))
([fbcf3a4](fbcf3a4))
* **deps:** update github/codeql-action digest to 60168ef
([#463](#463))
([ea76351](ea76351))
* **deps:** update github/codeql-action digest to ff0a06e
([#473](#473))
([af1b20f](af1b20f))
* **deps:** update spec digest to edf0deb
([#474](#474))
([fc3bdfe](fc3bdfe))


### 📚 Documentation

* Add AspNetCore sample app
([#477](#477))
([9742a0d](9742a0d))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

---------

Signed-off-by: OpenFeature Bot <[email protected]>
Signed-off-by: André Silva <[email protected]>
Co-authored-by: André Silva <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FEATURE] Add Events to the global instance of OpenFeature using DI syntax

4 participants