lib/ddtrace/tracer.rb in ddtrace-0.9.2 vs lib/ddtrace/tracer.rb in ddtrace-0.10.0

- old
+ new

@@ -16,12 +16,12 @@ # example, a trace can be used to track the entire time spent processing a complicated web request. # Even though the request may require multiple resources and machines to handle the request, all # of these function calls and sub-requests would be encapsulated within a single trace. # rubocop:disable Metrics/ClassLength class Tracer - attr_reader :writer, :sampler, :services, :tags, :provider - attr_accessor :enabled + attr_reader :sampler, :services, :tags, :provider + attr_accessor :enabled, :writer attr_writer :default_service # Global, memoized, lazy initialized instance of a logger that is used within the the Datadog # namespace. This logger outputs to +STDOUT+ by default, and is considered thread-safe. def self.log @@ -69,11 +69,11 @@ # # tracer.shutdown! # def shutdown! return if !@enabled || @writer.worker.nil? - @writer.worker.shutdown! + @writer.worker.stop end # Return the current active \Context for this traced execution. This method is # automatically called when calling Tracer.trace or Tracer.start_span, # but it can be used in the application code during manual instrumentation. @@ -116,15 +116,22 @@ def configure(options = {}) enabled = options.fetch(:enabled, nil) hostname = options.fetch(:hostname, nil) port = options.fetch(:port, nil) sampler = options.fetch(:sampler, nil) + priority_sampling = options[:priority_sampling] @enabled = enabled unless enabled.nil? + @sampler = sampler unless sampler.nil? + + if priority_sampling + @sampler = PrioritySampler.new(base_sampler: @sampler) + @writer = Writer.new(priority_sampler: @sampler) + end + @writer.transport.hostname = hostname unless hostname.nil? @writer.transport.port = port unless port.nil? - @sampler = sampler unless sampler.nil? end # Set the information about the given service. A valid example is: # # tracer.set_service_info('web-application', 'rails', 'web') @@ -160,28 +167,19 @@ def set_tags(tags) @tags.update(tags) end # Guess context and parent from child_of entry. - def guess_context_and_parent(options = {}) - child_of = options.fetch(:child_of, nil) # can be context or span + def guess_context_and_parent(child_of) + # call_context should not be in this code path, as start_span + # should never try and pick an existing context, but only get + # it from the parameters passed to it (child_of) + return [Datadog::Context.new, nil] unless child_of - ctx = nil - parent = nil - unless child_of.nil? - if child_of.respond_to?(:current_span) - ctx = child_of - parent = child_of.current_span - elsif child_of.is_a?(Datadog::Span) - parent = child_of - ctx = child_of.context - end - end + return [child_of, child_of.current_span] if child_of.is_a?(Context) - ctx ||= call_context - - [ctx, parent] + [child_of.context, child_of] end # Return a span that will trace an operation called \name. This method allows # parenting passing \child_of as an option. If it's missing, the newly created span is a # root span. Available options are: @@ -200,17 +198,21 @@ # Filter options, we want no side effects with unexpected args. # Plus, this documents the code (Ruby 2 named args would be better but we're Ruby 1.9 compatible) [:service, :resource, :span_type].include?(k) end - ctx, parent = guess_context_and_parent(options) + ctx, parent = guess_context_and_parent(options[:child_of]) opts[:context] = ctx unless ctx.nil? span = Span.new(self, name, opts) if parent.nil? # root span @sampler.sample(span) span.set_tag('system.pid', Process.pid) + if ctx && ctx.trace_id && ctx.span_id + span.trace_id = ctx.trace_id + span.parent_id = ctx.span_id + end else # child span span.parent = parent # sets service, trace_id, parent_id, sampled end tags.each { |k, v| span.set_tag(k, v) } unless tags.empty?