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

Extract components from configuration #996

Merged
merged 4 commits into from
Apr 9, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 6 additions & 6 deletions lib/ddtrace/buffer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,16 +96,16 @@ def measure_drop(trace)

def measure_pop(traces)
# Accepted
Diagnostics::Health.metrics.queue_accepted(@buffer_accepted)
Diagnostics::Health.metrics.queue_accepted_lengths(@buffer_accepted_lengths)
Datadog.health_metrics.queue_accepted(@buffer_accepted)
Datadog.health_metrics.queue_accepted_lengths(@buffer_accepted_lengths)

# Dropped
Diagnostics::Health.metrics.queue_dropped(@buffer_dropped)
Datadog.health_metrics.queue_dropped(@buffer_dropped)

# Queue gauges
Diagnostics::Health.metrics.queue_max_length(@max_size)
Diagnostics::Health.metrics.queue_spans(@buffer_spans)
Diagnostics::Health.metrics.queue_length(traces.length)
Datadog.health_metrics.queue_max_length(@max_size)
Datadog.health_metrics.queue_spans(@buffer_spans)
Datadog.health_metrics.queue_length(traces.length)

# Reset aggregated metrics
@buffer_accepted = 0
Expand Down
47 changes: 42 additions & 5 deletions lib/ddtrace/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
require 'forwardable'

require 'ddtrace/configuration/pin_setup'
require 'ddtrace/configuration/settings'
require 'ddtrace/configuration/components'

module Datadog
# Configuration provides a unique access point for configurations
module Configuration
extend Forwardable

attr_writer :configuration

def configuration
Expand All @@ -13,18 +18,50 @@ def configuration
def configure(target = configuration, opts = {})
if target.is_a?(Settings)
yield(target) if block_given?

# Rebuild immutable components from settings
rebuild_components!(target)

target
else
PinSetup.new(target, opts).call
marcotc marked this conversation as resolved.
Show resolved Hide resolved
end
end

# Helper methods
def tracer
configuration.tracer
def_delegators \
:components,
:health_metrics,
:runtime_metrics,
:tracer

protected

def components
@components ||= Components.new(configuration)
end

def runtime_metrics
tracer.writer.runtime_metrics
def rebuild_components!(configuration)
# Build new components
new_components = Components.new(configuration)

# Teardown old components if they exist
teardown_components!(@components, new_components) if instance_variable_defined?(:@components)

# Activate new components
@components = new_components
end

def teardown_components!(old, current)
# Shutdown the old tracer, unless it's still being used.
# (e.g. a custom tracer instance passed in.)
old.tracer.shutdown! unless old.tracer == current.tracer

# Shutdown the old metrics, unless they are still being used.
# (e.g. custom Statsd instances.)
old_statsd = [old.runtime_metrics.statsd, old.health_metrics.statsd].uniq
marcotc marked this conversation as resolved.
Show resolved Hide resolved
new_statsd = [current.runtime_metrics.statsd, current.health_metrics.statsd].uniq
unused_statsd = (old_statsd - (old_statsd & new_statsd))
unused_statsd.each(&:close)
end
end
end
94 changes: 94 additions & 0 deletions lib/ddtrace/configuration/components.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
require 'ddtrace/tracer'

module Datadog
module Configuration
# Global components for the trace library.
# rubocop:disable Metrics/LineLength
class Components
def initialize(settings)
# Tracer
@tracer = build_tracer(settings)

# Runtime metrics
build_runtime_metrics(settings)

# Health metrics
@health_metrics = build_health_metrics(settings)
end

attr_reader \
:health_metrics,
:tracer

def runtime_metrics
tracer.writer.runtime_metrics
end

private

def build_tracer(settings)
# If a custom tracer has been provided, use it instead.
# Ignore all other options (they should already be configured.)
return settings.tracer.instance unless settings.tracer.instance.nil?

tracer = Tracer.new(
default_service: settings.service,
enabled: settings.tracer.enabled,
partial_flush: settings.tracer.partial_flush,
tags: build_tracer_tags(settings)
)

# TODO: We reconfigure the tracer here because it has way too many
# options it allows to mutate, and it's overwhelming to rewrite
# tracer initialization for now. Just reconfigure using the
# existing mutable #configure function. Remove when these components
# are extracted.
tracer.configure(build_tracer_options(settings))

tracer
end

def build_tracer_tags(settings)
settings.tags.dup.tap do |tags|
tags['env'] = settings.env unless settings.env.nil?
tags['version'] = settings.version unless settings.version.nil?
end
end

def build_tracer_options(settings)
settings = settings.tracer

{}.tap do |opts|
opts[:hostname] = settings.hostname unless settings.hostname.nil?
opts[:min_spans_before_partial_flush] = settings.partial_flush.min_spans_threshold unless settings.partial_flush.min_spans_threshold.nil?
opts[:partial_flush] = settings.partial_flush.enabled unless settings.partial_flush.enabled.nil?
opts[:port] = settings.port unless settings.port.nil?
opts[:priority_sampling] = settings.priority_sampling unless settings.priority_sampling.nil?
opts[:sampler] = settings.sampler unless settings.sampler.nil?
opts[:transport_options] = settings.transport_options
opts[:writer] = settings.writer unless settings.writer.nil?
opts[:writer_options] = settings.writer_options if settings.writer.nil?
end
end

def build_runtime_metrics(settings)
settings = settings.runtime_metrics
options = { enabled: settings.enabled }
options[:statsd] = settings.statsd unless settings.statsd.nil?

# TODO: We reconfigure runtime metrics here because it is too deeply nested
# within the tracer/writer. Build a new runtime metrics instance when
# runtime metrics are extracted from tracer/writer.
runtime_metrics.configure(options)
end

def build_health_metrics(settings)
settings = settings.diagnostics.health_metrics
options = { enabled: settings.enabled }
options[:statsd] = settings.statsd unless settings.statsd.nil?

Datadog::Diagnostics::Health::Metrics.new(options)
end
end
end
end
Loading