FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

feat: Support OpenFeature tracking by kinyoklion · Pull Request #27 · launchdarkly/openfeature-ruby-server · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .gemspec  (1) .rb  (2) All 2 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion launchdarkly-openfeature-server-sdk.gemspec
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
Expand Up @@ -29,7 +29,7 @@ Gem::Specification.new do |spec|
spec.require_paths = ["lib"]

spec.add_runtime_dependency "launchdarkly-server-sdk", "~> 8.4"
spec.add_runtime_dependency "openfeature-sdk", "~> 0.6.0"
Comment thread
kinyoklion marked this conversation as resolved.
spec.add_runtime_dependency "openfeature-sdk", "~> 0.6.1"

# For more information and examples about making a new gem, check out our
# guide at: https://bundler.io/guides/creating_gem.html
Expand Down
27 changes: 27 additions & 0 deletions lib/ldclient-openfeature/provider.rb
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
Expand Up @@ -33,6 +33,7 @@ class Provider
def initialize(sdk_key, config = LaunchDarkly::Config.default, wait_for_seconds = 5)
@client = LaunchDarkly::LDClient.new(sdk_key, config, wait_for_seconds)

@logger = config.logger
@context_converter = Impl::EvaluationContextConverter.new(config.logger)
@details_converter = Impl::ResolutionDetailsConverter.new

Expand Down Expand Up @@ -63,6 +64,32 @@ def fetch_object_value(flag_key:, default_value:, evaluation_context: nil)
resolve_value(:object, flag_key, default_value, evaluation_context)
end

#
# Track a custom event, which can be used as a metric for an experiment.
#
# @param tracking_event_name [String]
# @param evaluation_context [::OpenFeature::SDK::EvaluationContext, nil]
# @param tracking_event_details [::OpenFeature::SDK::TrackingEventDetails, nil]
#
# @return [void]
#
def track(tracking_event_name, evaluation_context: nil, tracking_event_details: nil)
if evaluation_context.nil?
@logger.warn(
"The track method was called without an evaluation context. No event will be sent to LaunchDarkly, " \
"because a context is required to associate the event with."
)
return
end

ld_context = @context_converter.to_ld_context(evaluation_context)

data = tracking_event_details&.fields
data = nil if data.nil? || data.empty?

@client.track(tracking_event_name, ld_context, data, tracking_event_details&.value)
end

#
# @param flag_type [Symbol]
# @param flag_key [String]
Expand Down
25 changes: 25 additions & 0 deletions spec/provider_spec.rb
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
Expand Up @@ -18,6 +18,31 @@
expect(provider.client).to be_a LaunchDarkly::LDClient
end

it "track sends a custom event for the provided context" do
allow(provider.client).to receive(:track)

provider.track("event-name", evaluation_context: evaluation_context)

expect(provider.client).to have_received(:track).with("event-name", instance_of(LaunchDarkly::LDContext), nil, nil)
end

it "track includes event details" do
details = OpenFeature::SDK::TrackingEventDetails.new(value: 3.14, category: "shopping")
allow(provider.client).to receive(:track)

provider.track("event-name", evaluation_context: evaluation_context, tracking_event_details: details)

expect(provider.client).to have_received(:track).with("event-name", instance_of(LaunchDarkly::LDContext), { "category" => "shopping" }, 3.14)
end

it "track without a context does not send an event" do
allow(provider.client).to receive(:track)

provider.track("event-name")

expect(provider.client).not_to have_received(:track)
end

it "not providing context returns error" do
resolution_details = provider.fetch_boolean_value(flag_key: "flag-key", default_value: true)

Expand Down
Loading

Back | FazBrowse Home | New Git URL