Skip to content

Commit

Permalink
Create Configuration class
Browse files Browse the repository at this point in the history
  • Loading branch information
p-lambert committed Oct 16, 2017
1 parent 47841f5 commit 0ce3796
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
13 changes: 13 additions & 0 deletions lib/ddtrace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require 'ddtrace/tracer'
require 'ddtrace/error'
require 'ddtrace/pipeline'
require 'ddtrace/configuration'

# \Datadog global namespace that includes all tracing functionality for Tracer and Span classes.
module Datadog
Expand Down Expand Up @@ -30,6 +31,18 @@ def self.tracer
def self.registry
@registry
end

class << self
attr_writer :configuration

def configuration
@configuration ||= Configuration.new
end

def configure
yield(configuration)
end
end
end

require 'ddtrace/monkey'
Expand Down
34 changes: 34 additions & 0 deletions lib/ddtrace/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
require_relative 'configuration/proxy'

module Datadog
# Configuration provides a unique access point for configurations
class Configuration
InvalidIntegrationError = Class.new(StandardError)

def initialize(options = {})
@registry = options.fetch(:registry, Datadog.registry)
end

def [](integration_name)
integration = fetch_integration(integration_name)
Proxy.new(integration)
end

def use(integration_name, options = {})
integration = fetch_integration(integration_name)

options.each_with_object(Proxy.new(integration)) do |(name, value), proxy|
proxy[name] = value
end

integration.patch if integration.respond_to?(:patch)
end

private

def fetch_integration(name)
@registry[name] ||
raise(InvalidIntegrationError, "'#{name}' is not a valid integration.")
end
end
end
28 changes: 28 additions & 0 deletions lib/ddtrace/configuration/proxy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Datadog
class Configuration
# Proxy provides a hash-like interface for fetching/setting configurations
class Proxy
def initialize(integration)
@integration = integration
end

def [](param)
@integration.get_option(param)
end

def []=(param, value)
@integration.set_option(param, value)
end

def to_h
@integration.to_h
end

alias_method :to_hash, :to_h

def reset!
@integration.reset_options!
end
end
end
end
64 changes: 64 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
require 'ddtrace/configuration'
require 'ddtrace/configurable'

module Datadog
class ConfigurationTest < Minitest::Test
def setup
@registry = Registry.new
@configuration = Configuration.new(registry: @registry)
end

def test_use_method
contrib = Minitest::Mock.new
contrib.expect(:patch, true)

@registry.add(:example, contrib)
@configuration.use(:example)

assert_mock(contrib)
end

def test_module_configuration
integration = Module.new do
include Contrib::Base
option :option1
option :option2
end

@registry.add(:example, integration)

@configuration.use(:example, option1: :foo!, option2: :bar!)

assert_equal(:foo!, @configuration[:example][:option1])
assert_equal(:bar!, @configuration[:example][:option2])
end

def test_setting_a_configuration_param
integration = Module.new do
include Contrib::Base
option :option1
end

@registry.add(:example, integration)
@configuration[:example][:option1] = :foo
assert_equal(:foo, @configuration[:example][:option1])
end

def test_invalid_integration
assert_raises(Configuration::InvalidIntegrationError) do
@configuration[:foobar]
end
end

def test_hash_coercion
integration = Module.new do
include Contrib::Base
option :option1, default: :foo
option :option2, default: :bar
end

@registry.add(:example, integration)
assert_equal({ option1: :foo, option2: :bar }, @configuration[:example].to_h)
end
end
end

0 comments on commit 0ce3796

Please sign in to comment.