lib/ddtrace/tracer.rb in ddtrace-0.4.3 vs lib/ddtrace/tracer.rb in ddtrace-0.5.0
- old
+ new
@@ -3,19 +3,20 @@
require 'logger'
require 'ddtrace/span'
require 'ddtrace/buffer'
require 'ddtrace/writer'
+require 'ddtrace/sampler'
# \Datadog global namespace that includes all tracing functionality for Tracer and Span classes.
module Datadog
# A \Tracer keeps track of the time spent by an application processing a single operation. For
# 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.
class Tracer
- attr_reader :writer, :services
+ attr_reader :writer, :sampler, :services
attr_accessor :enabled
# 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
@@ -42,10 +43,12 @@
# * +enabled+: set if the tracer submits or not spans to the local agent. It's enabled
# by default.
def initialize(options = {})
@enabled = options.fetch(:enabled, true)
@writer = options.fetch(:writer, Datadog::Writer.new)
+ @sampler = options.fetch(:sampler, Datadog::AllSampler.new)
+
@buffer = Datadog::SpanBuffer.new()
@mutex = Mutex.new
@spans = []
@services = {}
@@ -64,14 +67,16 @@
#
def configure(options = {})
enabled = options.fetch(:enabled, nil)
hostname = options.fetch(:hostname, nil)
port = options.fetch(:port, nil)
+ sampler = options.fetch(:sampler, nil)
@enabled = enabled unless enabled.nil?
@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')
@@ -119,10 +124,17 @@
# set up inheritance
parent = @buffer.get()
span.set_parent(parent)
@buffer.set(span)
+ # sampling
+ if parent.nil?
+ @sampler.sample(span)
+ else
+ span.sampled = span.parent.sampled
+ end
+
# call the finish only if a block is given; this ensures
# that a call to tracer.trace() without a block, returns
# a span that should be manually finished.
if block_given?
begin
@@ -153,10 +165,10 @@
return unless parent.nil?
spans = @spans
@spans = []
end
- return if spans.empty?
+ return if spans.empty? || !span.sampled
write(spans)
end
# Return the current active span or +nil+.
def active_span