-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added: Options, Patchable, Registerable, Configurable to Datadog::Con…
…trib.
- Loading branch information
Showing
14 changed files
with
344 additions
and
98 deletions.
There are no files selected for viewing
This file contains 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 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 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,55 @@ | ||
require 'ddtrace/contrib/configuration/resolver' | ||
require 'ddtrace/contrib/configuration/settings' | ||
|
||
module Datadog | ||
module Contrib | ||
# Defines configurable behavior for integrations | ||
module Configurable | ||
def self.included(base) | ||
base.send(:include, InstanceMethods) | ||
end | ||
|
||
# Configurable instance behavior for integrations | ||
module InstanceMethods | ||
def default_configuration | ||
Configuration::Settings.new | ||
end | ||
|
||
def reset_configuration! | ||
@configurations = nil | ||
@resolver = nil | ||
end | ||
|
||
def configuration(name = :default) | ||
name = :default if name.nil? | ||
name = resolver.resolve(name) | ||
return nil unless configurations.key?(name) | ||
configurations[name] | ||
end | ||
|
||
def configurations | ||
@configurations ||= Hash.new { default_configuration }.tap do |configs| | ||
configs[:default] = default_configuration | ||
end | ||
end | ||
|
||
def configure(name = :default, options = {}, &block) | ||
name = resolver.resolve(name) | ||
|
||
configurations[name].tap do |settings| | ||
settings.configure(options, &block) | ||
configurations[name] = settings | ||
end | ||
end | ||
|
||
protected | ||
|
||
attr_writer :resolver | ||
|
||
def resolver | ||
@resolver ||= Configuration::Resolver.new | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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,33 @@ | ||
module Datadog | ||
module Contrib | ||
module Configuration | ||
# Represents an instance of an integration configuration option | ||
class Option | ||
attr_reader \ | ||
:definition | ||
|
||
def initialize(definition, context) | ||
@definition = definition | ||
@context = context | ||
@value = nil | ||
@is_set = false | ||
end | ||
|
||
def set(value) | ||
@value = @context.instance_exec(value, &definition.setter).tap do | ||
@is_set = true | ||
end | ||
end | ||
|
||
def get | ||
return definition.default_value unless @is_set | ||
@value | ||
end | ||
|
||
def reset | ||
set(definition.default_value) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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,29 @@ | ||
module Datadog | ||
module Contrib | ||
module Configuration | ||
# Represents a definition for an integration configuration option | ||
class OptionDefinition | ||
IDENTITY = ->(x) { x } | ||
|
||
attr_reader \ | ||
:default, | ||
:depends_on, | ||
:lazy, | ||
:name, | ||
:setter | ||
|
||
def initialize(name, meta = {}, &block) | ||
@default = meta[:default] | ||
@depends_on = meta[:depends_on] || [] | ||
@lazy = meta[:lazy] || false | ||
@name = name.to_sym | ||
@setter = meta[:setter] || block || IDENTITY | ||
end | ||
|
||
def default_value | ||
lazy ? @default.call : @default | ||
end | ||
end | ||
end | ||
end | ||
end |
20 changes: 20 additions & 0 deletions
20
lib/ddtrace/contrib/configuration/option_definition_set.rb
This file contains 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,20 @@ | ||
require 'ddtrace/configuration/resolver' | ||
|
||
module Datadog | ||
module Contrib | ||
module Configuration | ||
# Represents a set of configuration option definitions for an integration | ||
class OptionDefinitionSet < Hash | ||
def dependency_order | ||
Datadog::Configuration::Resolver.new(dependency_graph).call | ||
end | ||
|
||
def dependency_graph | ||
each_with_object({}) do |(name, option), graph| | ||
graph[name] = option.depends_on | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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,8 @@ | ||
module Datadog | ||
module Contrib | ||
module Configuration | ||
class OptionSet < Hash | ||
end | ||
end | ||
end | ||
end |
This file contains 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,95 @@ | ||
require 'ddtrace/contrib/configuration/option' | ||
require 'ddtrace/contrib/configuration/option_set' | ||
require 'ddtrace/contrib/configuration/option_definition' | ||
require 'ddtrace/contrib/configuration/option_definition_set' | ||
|
||
module Datadog | ||
module Contrib | ||
module Configuration | ||
# Behavior for a configuration object that has options | ||
module Options | ||
def self.included(base) | ||
base.send(:extend, ClassMethods) | ||
base.send(:include, InstanceMethods) | ||
end | ||
|
||
# Class behavior for a configuration object with options | ||
module ClassMethods | ||
def options | ||
@options ||= begin | ||
# Allows for class inheritance of option definitions | ||
superclass <= Options ? superclass.options.dup : OptionDefinitionSet.new | ||
end | ||
end | ||
|
||
protected | ||
|
||
def option(name, meta = {}, &block) | ||
options[name] = OptionDefinition.new(name, meta, &block).tap do | ||
define_option_accessors(name) | ||
end | ||
end | ||
|
||
private | ||
|
||
def define_option_accessors(name) | ||
option_name = name | ||
|
||
define_method(option_name) do | ||
get_option(option_name) | ||
end | ||
|
||
define_method("#{option_name}=") do |value| | ||
set_option(option_name, value) | ||
end | ||
end | ||
end | ||
|
||
# Instance behavior for a configuration object with options | ||
module InstanceMethods | ||
def options | ||
@options ||= OptionSet.new | ||
end | ||
|
||
def set_option(name, value) | ||
add_option(name) unless options.key?(name) | ||
options[name].set(value) | ||
end | ||
|
||
def get_option(name) | ||
add_option(name) unless options.key?(name) | ||
options[name].get | ||
end | ||
|
||
def to_h | ||
options.each_with_object({}) do |(key, _), hash| | ||
hash[key] = get_option(key) | ||
end | ||
end | ||
|
||
def reset_options! | ||
options.values.each(&:reset) | ||
end | ||
|
||
private | ||
|
||
def add_option(name) | ||
assert_valid_option!(name) | ||
definition = self.class.options[name] | ||
Option.new(definition, self).tap do |option| | ||
options[name] = option | ||
end | ||
end | ||
|
||
def assert_valid_option!(name) | ||
unless self.class.options.key?(name) | ||
raise(InvalidOptionError, "#{self.class.name} doesn't define the option: #{name}") | ||
end | ||
end | ||
end | ||
|
||
InvalidOptionError = Class.new(StandardError) | ||
end | ||
end | ||
end | ||
end |
This file contains 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,12 @@ | ||
module Datadog | ||
module Contrib | ||
module Configuration | ||
# Resolves a value to a configuration key | ||
class Resolver | ||
def resolve(name) | ||
name | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Oops, something went wrong.