-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Emit provider events from LaunchDarkly client status #29
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
Open
kinyoklion
wants to merge
1
commit into
main
Choose a base branch
from
devin/ruby-provider-events
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require 'ldclient-rb' | ||
| require 'open_feature/sdk' | ||
|
|
||
| module LaunchDarkly | ||
| module OpenFeature | ||
| module Impl | ||
| # | ||
| # Translates LaunchDarkly data source status changes into OpenFeature provider events. | ||
| # | ||
| class DataSourceStatusListener | ||
| # | ||
| # @param provider [LaunchDarkly::OpenFeature::Provider] | ||
| # | ||
| def initialize(provider) | ||
| @provider = provider | ||
| end | ||
|
|
||
| # | ||
| # @param status [LaunchDarkly::Interfaces::DataSource::Status] | ||
| # | ||
| # @return [void] | ||
| # | ||
| def update(status) | ||
| case status.state | ||
| when ::LaunchDarkly::Interfaces::DataSource::Status::VALID | ||
| @provider.emit_event(::OpenFeature::SDK::ProviderEvent::PROVIDER_READY) | ||
| when ::LaunchDarkly::Interfaces::DataSource::Status::INTERRUPTED | ||
| @provider.emit_event( | ||
| ::OpenFeature::SDK::ProviderEvent::PROVIDER_STALE, | ||
| message: message(status, "the data source has been interrupted") | ||
| ) | ||
| when ::LaunchDarkly::Interfaces::DataSource::Status::OFF | ||
| @provider.emit_event( | ||
| ::OpenFeature::SDK::ProviderEvent::PROVIDER_ERROR, | ||
| error_code: ::OpenFeature::SDK::Provider::ErrorCode::GENERAL, | ||
| message: message(status, "the data source has been permanently shut down") | ||
| ) | ||
| end | ||
| end | ||
|
|
||
| # | ||
| # @param status [LaunchDarkly::Interfaces::DataSource::Status] | ||
| # @param fallback [String] | ||
| # | ||
| # @return [String] | ||
| # | ||
| private def message(status, fallback) | ||
| error = status.last_error | ||
| return fallback if error.nil? | ||
|
|
||
| "#{fallback}: #{error.kind} #{error.status_code} #{error.message}".strip | ||
| end | ||
| end | ||
|
|
||
| # | ||
| # Translates LaunchDarkly flag change events into OpenFeature configuration changed events. | ||
| # | ||
| class FlagChangeListener | ||
| # | ||
| # @param provider [LaunchDarkly::OpenFeature::Provider] | ||
| # | ||
| def initialize(provider) | ||
| @provider = provider | ||
| end | ||
|
|
||
| # | ||
| # @param flag_change [LaunchDarkly::Interfaces::FlagChange] | ||
| # | ||
| # @return [void] | ||
| # | ||
| def update(flag_change) | ||
| @provider.emit_event( | ||
| ::OpenFeature::SDK::ProviderEvent::PROVIDER_CONFIGURATION_CHANGED, | ||
| flags_changed: [flag_change.key] | ||
| ) | ||
| end | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe LaunchDarkly::OpenFeature::Impl::DataSourceStatusListener do | ||
| let(:provider) { double(emit_event: nil) } | ||
| let(:listener) { described_class.new(provider) } | ||
|
|
||
| def status(state, error = nil) | ||
| LaunchDarkly::Interfaces::DataSource::Status.new(state, Time.now, error) | ||
| end | ||
|
|
||
| it "a valid data source is ready" do | ||
| listener.update(status(LaunchDarkly::Interfaces::DataSource::Status::VALID)) | ||
|
|
||
| expect(provider).to have_received(:emit_event).with(OpenFeature::SDK::ProviderEvent::PROVIDER_READY) | ||
| end | ||
|
|
||
| it "an interrupted data source is stale" do | ||
| listener.update(status(LaunchDarkly::Interfaces::DataSource::Status::INTERRUPTED)) | ||
|
|
||
| expect(provider).to have_received(:emit_event) | ||
| .with(OpenFeature::SDK::ProviderEvent::PROVIDER_STALE, hash_including(:message)) | ||
| end | ||
|
|
||
| it "a shut down data source is an error" do | ||
| error = LaunchDarkly::Interfaces::DataSource::ErrorInfo.new( | ||
| LaunchDarkly::Interfaces::DataSource::ErrorInfo::ERROR_RESPONSE, 401, "Unauthorized", Time.now | ||
| ) | ||
|
|
||
| listener.update(status(LaunchDarkly::Interfaces::DataSource::Status::OFF, error)) | ||
|
|
||
| expect(provider).to have_received(:emit_event).with( | ||
| OpenFeature::SDK::ProviderEvent::PROVIDER_ERROR, | ||
| hash_including(error_code: OpenFeature::SDK::Provider::ErrorCode::GENERAL, message: /401/) | ||
| ) | ||
| end | ||
|
|
||
| it "an initializing data source does not emit an event" do | ||
| listener.update(status(LaunchDarkly::Interfaces::DataSource::Status::INITIALIZING)) | ||
|
|
||
| expect(provider).not_to have_received(:emit_event) | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| RSpec.describe LaunchDarkly::OpenFeature::Impl::FlagChangeListener do | ||
| let(:provider) { double(emit_event: nil) } | ||
|
|
||
| it "a flag change is a configuration change" do | ||
| described_class.new(provider).update(LaunchDarkly::Interfaces::FlagChange.new("flag-key")) | ||
|
|
||
| expect(provider).to have_received(:emit_event).with( | ||
| OpenFeature::SDK::ProviderEvent::PROVIDER_CONFIGURATION_CHANGED, | ||
| flags_changed: ["flag-key"] | ||
| ) | ||
| end | ||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
aside: We don't say fatal, because then OF short-circuits evaluations.