Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move env vars to configuration settings #985

Merged
merged 3 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion docs/GettingStarted.md
Original file line number Diff line number Diff line change
Expand Up @@ -1576,7 +1576,10 @@ These values can also be overridden at the tracer level:

```ruby
Datadog.configure do |c|
c.tracer env: 'test', tags: { 'team' => 'qa' }
c.service = 'billing-api'
c.env = 'test'
c.tags = { 'team' => 'qa' }
c.version = '1.3-alpha'
end
```

Expand Down
3 changes: 2 additions & 1 deletion lib/ddtrace/configuration/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Configuration
module Base
def self.included(base)
base.send(:extend, Datadog::Environment::Helpers)
base.send(:include, Datadog::Environment::Helpers)
base.send(:include, Options)

base.send(:extend, ClassMethods)
Expand All @@ -23,7 +24,7 @@ def settings(name, &block)
settings_class = new_settings_class(&block)

option(name) do |o|
o.default -> { settings_class.new }
o.default { settings_class.new }
o.lazy
o.resetter do |value|
value.reset! if value.respond_to?(:reset!)
Expand Down
10 changes: 9 additions & 1 deletion lib/ddtrace/configuration/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def get
elsif definition.delegate_to
context_eval(&definition.delegate_to)
else
set(definition.default_value)
set(default_value)
end
end

Expand All @@ -42,6 +42,14 @@ def reset
end
end

def default_value
if definition.lazy
context_eval(&definition.default)
else
definition.default
end
end

private

def context_exec(*args, &block)
Expand Down
4 changes: 0 additions & 4 deletions lib/ddtrace/configuration/option_definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ def initialize(name, meta = {}, &block)
@setter = meta[:setter] || block || IDENTITY
end

def default_value
lazy ? @default.call : @default
end

# Creates a new Option, bound to the context provided.
def build(context)
Option.new(self, context)
Expand Down
101 changes: 78 additions & 23 deletions lib/ddtrace/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@ class Settings
# Configuration options
#
option :analytics_enabled do |o|
# TODO: Raise deprecation warning
o.default { env_to_bool(Ext::Analytics::ENV_TRACE_ANALYTICS_ENABLED, nil) }
o.lazy
end

option :report_hostname do |o|
o.default { env_to_bool(Ext::NET::ENV_REPORT_HOSTNAME, false) }
o.lazy
end
settings :diagnostics do
option :health_metrics do |o|
o.default do
Datadog::Diagnostics::Health::Metrics.new(
enabled: env_to_bool(Datadog::Ext::Diagnostics::Health::Metrics::ENV_ENABLED, false)
)
end

option :runtime_metrics_enabled do |o|
o.default { env_to_bool(Ext::Runtime::Metrics::ENV_ENABLED, false) }
o.lazy
o.lazy
end
end

settings :distributed_tracing do
Expand All @@ -57,6 +60,31 @@ class Settings
end
end

option :env do |o|
o.default { ENV.fetch(Ext::Environment::ENV_ENVIRONMENT, nil) }
o.lazy
end

option :report_hostname do |o|
o.default { env_to_bool(Ext::NET::ENV_REPORT_HOSTNAME, false) }
o.lazy
end

# Backwards compatibility for configuring runtime metrics e.g. `c.runtime_metrics enabled: true`
def runtime_metrics(options = nil)
runtime_metrics = get_option(:tracer).writer.runtime_metrics
return runtime_metrics if options.nil?

# TODO: Raise deprecation warning
runtime_metrics.configure(options)
end

option :runtime_metrics_enabled do |o|
# TODO: Raise deprecation warning
o.default { env_to_bool(Ext::Runtime::Metrics::ENV_ENABLED, false) }
o.lazy
end

settings :sampling do
option :default_rate do |o|
o.default { env_to_float(Ext::Sampling::ENV_SAMPLE_RATE, nil) }
Expand All @@ -69,16 +97,29 @@ class Settings
end
end

settings :diagnostics do
option :health_metrics do |o|
o.default do
Datadog::Diagnostics::Health::Metrics.new(
enabled: env_to_bool(Datadog::Ext::Diagnostics::Health::Metrics::ENV_ENABLED, false)
)
option :service do |o|
o.default { ENV.fetch(Ext::Environment::ENV_SERVICE, nil) }
o.lazy
end

option :tags do |o|
o.default do
tags = {}

# Parse tags from environment
env_to_list(Ext::Environment::ENV_TAGS).each do |tag|
pair = tag.split(':')
tags[pair.first] = pair.last if pair.length == 2
end

o.lazy
# Override tags if defined
tags[Ext::Environment::TAG_ENV] = env unless env.nil?
tags[Ext::Environment::TAG_VERSION] = version unless version.nil?

tags
end

o.lazy
end

option :tracer do |o|
Expand All @@ -99,20 +140,34 @@ class Settings
tracer.tap do |t|
unless options.nil?
t.configure(options)
Datadog::Logger.log = options[:log] if options[:log]
t.set_tags(options[:tags]) if options[:tags]
t.set_tags(env: options[:env]) if options[:env]
Datadog::Logger.debug_logging = options.fetch(:debug, false)

if options[:log]
# TODO: Raise deprecation warning
Datadog::Logger.log = options[:log]
end

if options[:tags]
# TODO: Raise deprecation warning
t.set_tags(options[:tags])
end

if options[:env]
# TODO: Raise deprecation warning
t.set_tags(env: options[:env])
end

if options.key?(:debug)
# TODO: Raise deprecation warning
Datadog::Logger.debug_logging = options[:debug]
end
end
end
end
end

def runtime_metrics(options = nil)
runtime_metrics = get_option(:tracer).writer.runtime_metrics
return runtime_metrics if options.nil?

runtime_metrics.configure(options)
option :version do |o|
o.default { ENV.fetch(Ext::Environment::ENV_VERSION, nil) }
o.lazy
end
end
end
Expand Down
14 changes: 8 additions & 6 deletions lib/ddtrace/contrib/presto/configuration/settings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ module Presto
module Configuration
# Custom settings for the Presto integration
class Settings < Contrib::Configuration::Settings
option :analytics_enabled,
default: -> { env_to_bool(Ext::ENV_ANALYTICS_ENABLED, false) },
lazy: true
option :analytics_enabled do |o|
o.default { env_to_bool(Ext::ENV_ANALYTICS_ENABLED, false) }
o.lazy
end

option :analytics_sample_rate,
default: -> { env_to_float(Ext::ENV_ANALYTICS_SAMPLE_RATE, 1.0) },
lazy: true
option :analytics_sample_rate do |o|
o.default { env_to_float(Ext::ENV_ANALYTICS_SAMPLE_RATE, 1.0) }
o.lazy
end

option :service_name, default: Ext::SERVICE_NAME
end
Expand Down
4 changes: 2 additions & 2 deletions lib/ddtrace/correlation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ def initialize(*args)
super
self.trace_id = trace_id || 0
self.span_id = span_id || 0
self.env = env || Datadog::Environment.env
self.version = version || Datadog::Environment.version
self.env = env || Datadog.configuration.env
self.version = version || Datadog.configuration.version
end

def to_s
Expand Down
29 changes: 0 additions & 29 deletions lib/ddtrace/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,6 @@
module Datadog
# Namespace for handling application environment
module Environment
# TODO: Extract to Datadog::Configuration::Settings
def self.env
ENV[Ext::Environment::ENV_ENVIRONMENT]
end

# TODO: Extract to Datadog::Configuration::Settings
def self.service
ENV[Ext::Environment::ENV_SERVICE]
end

# TODO: Extract to Datadog::Configuration::Settings
def self.tags
tags = {}

env_to_list(Ext::Environment::ENV_TAGS).each do |tag|
pair = tag.split(':')
tags[pair.first] = pair.last if pair.length == 2
end

tags[Ext::Environment::TAG_ENV] = env unless env.nil?
tags[Ext::Environment::TAG_VERSION] = version unless version.nil?

tags
end

def self.version
ENV[Ext::Environment::ENV_VERSION]
end

# Defines helper methods for environment
module Helpers
def env_to_bool(var, default = nil)
Expand Down
9 changes: 6 additions & 3 deletions lib/ddtrace/metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,12 @@ def default_metric_options
# and defaults are unfrozen for mutation in Statsd.
DEFAULT.dup.tap do |options|
options[:tags] = options[:tags].dup
# rubocop:disable Metrics/LineLength
options[:tags] << "#{Datadog::Ext::Environment::TAG_ENV}:#{Datadog::Environment.env}" unless Datadog::Environment.env.nil?
options[:tags] << "#{Datadog::Ext::Environment::TAG_VERSION}:#{Datadog::Environment.version}" unless Datadog::Environment.version.nil?

env = Datadog.configuration.env
options[:tags] << "#{Datadog::Ext::Environment::TAG_ENV}:#{env}" unless env.nil?

version = Datadog.configuration.version
options[:tags] << "#{Datadog::Ext::Environment::TAG_VERSION}:#{version}" unless version.nil?
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/ddtrace/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ def finish(finish_time = nil)
# spans without a service would be dropped, so here we provide a default.
# This should really never happen with integrations in contrib, as a default
# service is always set. It's only for custom instrumentation.
@service ||= (Datadog::Environment.service || (@tracer && @tracer.default_service))
@service ||= (Datadog.configuration.service || (@tracer && @tracer.default_service))

begin
@context.close_span(self)
Expand Down
4 changes: 2 additions & 2 deletions lib/ddtrace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def initialize(options = {})
end

@mutex = Mutex.new
@tags = Datadog::Environment.tags
@tags = options.fetch(:tags, Datadog.configuration.tags)

# Enable priority sampling by default
activate_priority_sampling!(@sampler)
Expand Down Expand Up @@ -156,7 +156,7 @@ def default_service
# tracer.set_tags('env' => 'prod', 'component' => 'core')
def set_tags(tags)
string_tags = Hash[tags.collect { |k, v| [k.to_s, v] }]
@tags.update(string_tags)
@tags = @tags.merge(string_tags)
end

# Guess context and parent from child_of entry.
Expand Down
17 changes: 11 additions & 6 deletions spec/ddtrace/configuration/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
let(:name) { :debug }
let(:block) { proc { option :enabled } }

it 'adds a settings option' do
settings
describe 'defines a settings option' do
subject(:definition) { base_class.options[name] }
before { settings }

base_class.options[name].tap do |option|
expect(option).to be_a_kind_of(Datadog::Configuration::OptionDefinition)
expect(option.default_value).to be_a_kind_of(described_class)
expect(option.default_value.option_defined?(:enabled)).to be true
it { is_expected.to be_a_kind_of(Datadog::Configuration::OptionDefinition) }

describe 'when instantiated' do
subject(:option) { Datadog::Configuration::Option.new(definition, self) }
it { expect(option.default_value).to be_a_kind_of(described_class) }
it { expect(option.default_value.option_defined?(:enabled)).to be true }
end
end
end
Expand All @@ -34,6 +37,8 @@
describe 'instance behavior' do
subject(:base_object) { base_class.new }

it { is_expected.to be_a_kind_of(Datadog::Environment::Helpers) }

describe '#initialize' do
subject(:base_object) { base_class.new(options) }
let(:options) { { foo: :bar } }
Expand Down
18 changes: 0 additions & 18 deletions spec/ddtrace/configuration/option_definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,24 +126,6 @@
end
end

describe '#default_value' do
subject(:result) { definition.default_value }
let(:meta) { { default: default } }
let(:default) { double('default') }

context 'when lazy is true' do
let(:meta) { super().merge(lazy: true) }
let(:default_value) { double('default_value') }
before(:each) { expect(default).to receive(:call).and_return(default_value) }
it { is_expected.to be default_value }
end

context 'when lazy is false' do
let(:meta) { super().merge(lazy: false) }
it { is_expected.to be default }
end
end

describe '#build' do
subject(:build) { definition.build(context) }
let(:context) { double('context') }
Expand Down
Loading