lib/ddtrace/tracer.rb in ddtrace-0.12.0.beta2 vs lib/ddtrace/tracer.rb in ddtrace-0.12.0.rc1

- old
+ new

@@ -3,10 +3,11 @@ require 'logger' require 'pathname' require 'ddtrace/span' require 'ddtrace/context' +require 'ddtrace/context_flush' require 'ddtrace/provider' require 'ddtrace/logger' require 'ddtrace/writer' require 'ddtrace/sampler' @@ -95,10 +96,12 @@ @sampler = options.fetch(:sampler, Datadog::AllSampler.new) @provider = options.fetch(:context_provider, Datadog::DefaultContextProvider.new) @provider ||= Datadog::DefaultContextProvider.new # @provider should never be nil + @context_flush = options[:partial_flush] ? Datadog::ContextFlush.new(options) : nil + @mutex = Mutex.new @services = {} @tags = {} end @@ -115,12 +118,17 @@ # def configure(options = {}) enabled = options.fetch(:enabled, nil) hostname = options.fetch(:hostname, nil) port = options.fetch(:port, nil) + + # Those are rare "power-user" options. sampler = options.fetch(:sampler, nil) priority_sampling = options[:priority_sampling] + max_spans_before_partial_flush = options.fetch(:max_spans_before_partial_flush, nil) + min_spans_before_partial_flush = options.fetch(:min_spans_before_partial_flush, nil) + partial_flush_timeout = options.fetch(:partial_flush_timeout, nil) @enabled = enabled unless enabled.nil? @sampler = sampler unless sampler.nil? if priority_sampling @@ -128,10 +136,14 @@ @writer = Writer.new(priority_sampler: @sampler) end @writer.transport.hostname = hostname unless hostname.nil? @writer.transport.port = port unless port.nil? + + @context_flush = Datadog::ContextFlush.new(options) unless min_spans_before_partial_flush.nil? && + max_spans_before_partial_flush.nil? && + partial_flush_timeout.nil? end # Set the information about the given service. A valid example is: # # tracer.set_service_info('web-application', 'rails', 'web') @@ -293,11 +305,26 @@ # is like trying to record its +context+. def record(context) context = context.context if context.is_a?(Datadog::Span) return if context.nil? trace, sampled = context.get - ready = !trace.nil? && !trace.empty? && sampled - write(trace) if ready + + # If context flushing is configured... + if @context_flush + if sampled + if trace.nil? || trace.empty? + @context_flush.each_partial_trace(context) do |t| + write(t) + end + else + write(trace) + end + end + # Default behavior + else + ready = !trace.nil? && !trace.empty? && sampled + write(trace) if ready + end end # Return the current active span or +nil+. def active_span call_context.current_span