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 Sep 29, 2017
1 parent 83a1d11 commit 7feb1d7
Show file tree
Hide file tree
Showing 3 changed files with 94 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 @@ -2,6 +2,7 @@
require 'ddtrace/pin'
require 'ddtrace/tracer'
require 'ddtrace/error'
require 'ddtrace/configuration'

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

class << self
attr_writer :configuration

def configuration
@configuration ||= Configuration.new
end

def configure
configuration.configure
end
end
end

require 'ddtrace/monkey'
Expand Down
30 changes: 30 additions & 0 deletions lib/ddtrace/configuration.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
module Datadog
# Configuration provides a unique access point for configurations
class Configuration
def initialize(options = {})
@registry = options.fetch(:registry, Datadog.registry)
end

def configure
yield(self)
end

def get(contrib, param)
registry[contrib].get_option(param)
end

def use(integration_name, options = {})
integration = registry[integration_name]

options.each_with_object(integration) do |(name, value), klass|
klass.set_option(name, value)
end

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

private

attr_reader :registry
end
end
51 changes: 51 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
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.configure do |c|
c.use :example, option1: :foo!, option2: :bar!
end

assert_equal(:foo!, integration.get_option(:option1))
assert_equal(:bar!, integration.get_option(:option2))
end

def test_get
integration = Module.new do
include Contrib::Base
option :option1, default: :value1
option :option2, default: :value2
end

@registry.add(:example, integration)

assert_equal(:value1, @configuration.get(:example, :option1))
assert_equal(:value2, @configuration.get(:example, :option2))
end
end
end

0 comments on commit 7feb1d7

Please sign in to comment.