-
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.
Merge pull request #714 from DataDog/feature/datadog_global_config_se…
…ttings Implement global configuration using configuration API
- Loading branch information
Showing
43 changed files
with
419 additions
and
762 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,72 +1,26 @@ | ||
require_relative 'configuration/proxy' | ||
require_relative 'configuration/resolver' | ||
require_relative 'configuration/pin_setup' | ||
require 'ddtrace/configuration/pin_setup' | ||
require 'ddtrace/configuration/settings' | ||
|
||
module Datadog | ||
# Configuration provides a unique access point for configurations | ||
class Configuration | ||
ENV_TRACE_ANALYTICS_ENABLED = 'DD_TRACE_ANALYTICS_ENABLED'.freeze | ||
InvalidIntegrationError = Class.new(StandardError) | ||
module Configuration | ||
attr_writer :configuration | ||
|
||
attr_writer :analytics_enabled | ||
|
||
def initialize(options = {}) | ||
@registry = options.fetch(:registry) { Datadog.registry } | ||
@wrapped_registry = {} | ||
@analytics_enabled = nil | ||
end | ||
|
||
def analytics_enabled | ||
@analytics_enabled || begin | ||
if ENV.key?(ENV_TRACE_ANALYTICS_ENABLED) | ||
value = ENV[ENV_TRACE_ANALYTICS_ENABLED] | ||
value.to_s.downcase == 'true' | ||
end | ||
end | ||
def configuration | ||
@configuration ||= Settings.new | ||
end | ||
|
||
def [](integration_name, configuration_name = :default) | ||
integration = fetch_integration(integration_name) | ||
|
||
if integration.class <= Datadog::Contrib::Integration | ||
integration.configuration(configuration_name) | ||
def configure(target = configuration, opts = {}) | ||
if target.is_a?(Settings) | ||
yield(target) | ||
else | ||
@wrapped_registry[integration_name] ||= Proxy.new(integration) | ||
PinSetup.new(target, opts).call | ||
end | ||
end | ||
|
||
def use(integration_name, options = {}, &block) | ||
integration = fetch_integration(integration_name) | ||
|
||
if integration.class <= Datadog::Contrib::Integration | ||
configuration_name = options[:describes] || :default | ||
filtered_options = options.reject { |k, _v| k == :describes } | ||
integration.configure(configuration_name, filtered_options, &block) | ||
else | ||
settings = Proxy.new(integration) | ||
integration.sorted_options.each do |name| | ||
settings[name] = options.fetch(name, settings[name]) | ||
end | ||
end | ||
|
||
integration.patch if integration.respond_to?(:patch) | ||
end | ||
|
||
def tracer(options = {}) | ||
instance = options.fetch(:instance, Datadog.tracer) | ||
|
||
instance.configure(options) | ||
instance.class.log = options[:log] if options[:log] | ||
instance.set_tags(options[:tags]) if options[:tags] | ||
instance.set_tags(env: options[:env]) if options[:env] | ||
instance.class.debug_logging = options.fetch(:debug, false) | ||
end | ||
|
||
private | ||
|
||
def fetch_integration(name) | ||
@registry[name] || | ||
raise(InvalidIntegrationError, "'#{name}' is not a valid integration.") | ||
# Helper methods | ||
def tracer | ||
configuration.tracer | ||
end | ||
end | ||
end |
4 changes: 2 additions & 2 deletions
4
lib/ddtrace/configuration/resolver.rb → ...race/configuration/dependency_resolver.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
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,32 @@ | ||
module Datadog | ||
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 | ||
@is_set = false | ||
@value = nil | ||
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,27 @@ | ||
module Datadog | ||
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 |
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,18 @@ | ||
require 'ddtrace/configuration/dependency_resolver' | ||
|
||
module Datadog | ||
module Configuration | ||
# Represents a set of configuration option definitions for an integration | ||
class OptionDefinitionSet < Hash | ||
def dependency_order | ||
DependencyResolver.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 |
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,6 @@ | ||
module Datadog | ||
module Configuration | ||
class OptionSet < Hash | ||
end | ||
end | ||
end |
Oops, something went wrong.