Skip to content

Commit

Permalink
Make it possible to set instrumenter and wrap http adapter
Browse files Browse the repository at this point in the history
Instrumentation is important in production (for metrics) and dev (for
logging).

Wrapping the adapter is important because it allows for caching,
instrumentation and more.

Also added docs for the cloud method.
  • Loading branch information
jnunemaker committed May 6, 2017
1 parent 8178f84 commit 4b10e4d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
28 changes: 25 additions & 3 deletions lib/flipper/cloud.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,39 @@

module Flipper
module Cloud
def self.new(token, options = {})
url = options.fetch(:url, "https://www.featureflipper.com/adapter")
instrumenter = options.fetch(:instrumenter, Flipper::Instrumenters::Noop)
# The default adapter wrapper which doesn't wrap at all.
DEFAULT_ADAPTER_WRAPPER_BLOCK = ->(adapter) { adapter }

# The default url should be the one, the only, the website™.
DEFAULT_URL = "https://www.featureflipper.com/adapter"

# Public: Returns a new Flipper instance with an http adapter correctly
# configured for flipper cloud.
#
# token - The String token for the environment from the website.
# options - The Hash of options.
# # :url - The url to point at (defaults to DEFAULT_URL).
# # :adapter_wrapper - The adapter wrapper block. Block should
# receive an adapter and return an adapter.
# Allows you to wrap the http adapter with
# other adapters to make instrumentation and
# caching easy.
# # :instrumenter - The optional instrumenter to use for the
# Flipper::DSL instance (defaults to Noop).
def self.new(token, options = {})
url = options.fetch(:url, DEFAULT_URL)
http_options = {
uri: URI(url),
headers: {
"Feature-Flipper-Token" => token,
},
}
adapter = Flipper::Adapters::Http.new(http_options)

adapter_wrapper = options.fetch(:adapter_wrapper, DEFAULT_ADAPTER_WRAPPER_BLOCK)
adapter = adapter_wrapper.call(adapter)

instrumenter = options.fetch(:instrumenter, Flipper::Instrumenters::Noop)
Flipper.new(adapter, instrumenter: instrumenter)
end
end
Expand Down
16 changes: 16 additions & 0 deletions spec/flipper/cloud_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'helper'
require 'flipper/cloud'
require 'flipper/adapters/instrumented'

RSpec.describe Flipper::Cloud do
context "initialize with token" do
Expand Down Expand Up @@ -52,4 +53,19 @@
expect(uri.path).to eq('/sadpanda')
end
end

it 'can set instrumenter' do
instrumenter = Object.new
instance = described_class.new('asdf', instrumenter: instrumenter)
expect(instance.instrumenter).to be(instrumenter)
end

it 'allows wrapping adapter with another adapter like the instrumenter' do
adapter_wrapper = ->(adapter) {
Flipper::Adapters::Instrumented.new(adapter)
}
instance = described_class.new('asdf', adapter_wrapper: adapter_wrapper)
# instance.adapter is memoizable adapter instance
expect(instance.adapter.adapter).to be_instance_of(Flipper::Adapters::Instrumented)
end
end

0 comments on commit 4b10e4d

Please sign in to comment.