lib/ddtrace/contrib/http/patcher.rb in ddtrace-0.9.2 vs lib/ddtrace/contrib/http/patcher.rb in ddtrace-0.10.0
- old
+ new
@@ -10,17 +10,21 @@
NAME = 'http.request'.freeze
APP = 'net/http'.freeze
SERVICE = 'net/http'.freeze
- @distributed_tracing_enabled = false
+ module_function
- class << self
- attr_accessor :distributed_tracing_enabled
+ # TODO: Remove this once we drop support for legacy configuration
+ def distributed_tracing_enabled
+ Datadog.configuration[:http][:distributed_tracing_enabled]
end
- module_function
+ # TODO: Remove this once we drop support for legacy configuration
+ def distributed_tracing_enabled=(value)
+ Datadog.configuration[:http][:distributed_tracing_enabled] = value
+ end
def should_skip_tracing?(req, address, port, transport, pin)
# we don't want to trace our own call to the API (they use net/http)
# when we know the host & port (from the URI) we use it, else (most-likely
# called with a block) rely on the URL at the end.
@@ -40,19 +44,24 @@
return true if active && (active.name == NAME)
false
end
def should_skip_distributed_tracing?(pin)
+ global_value = Datadog.configuration[:http][:distributed_tracing_enabled]
unless pin.config.nil?
- return !pin.config.fetch(:distributed_tracing_enabled, @distributed_tracing_enabled)
+ return !pin.config.fetch(:distributed_tracing_enabled, global_value)
end
- !@distributed_tracing_enabled
+ !global_value
end
# Patcher enables patching of 'net/http' module.
# This is used in monkey.rb to automatically apply patches
module Patcher
+ include Base
+ register_as :http, auto_patch: true
+ option :distributed_tracing_enabled, default: false
+
@patched = false
module_function
# patch applies our patch if needed
@@ -118,12 +127,18 @@
# Using the method as a resource, as URL/path can trigger
# a possibly infinite number of resources.
span.set_tag(Datadog::Ext::HTTP::URL, req.path)
span.set_tag(Datadog::Ext::HTTP::METHOD, req.method)
- unless Datadog::Contrib::HTTP.should_skip_distributed_tracing?(pin)
+ if pin.tracer.enabled && !Datadog::Contrib::HTTP.should_skip_distributed_tracing?(pin)
req.add_field(Datadog::Ext::DistributedTracing::HTTP_HEADER_TRACE_ID, span.trace_id)
req.add_field(Datadog::Ext::DistributedTracing::HTTP_HEADER_PARENT_ID, span.span_id)
+ if span.context.sampling_priority
+ req.add_field(
+ Datadog::Ext::DistributedTracing::HTTP_HEADER_SAMPLING_PRIORITY,
+ span.context.sampling_priority
+ )
+ end
end
rescue StandardError => e
Datadog::Tracer.log.error("error preparing span for http request: #{e}")
ensure
response = request_without_datadog(req, body, &block)