lib/ddtrace/contrib/http/patcher.rb in ddtrace-0.16.1 vs lib/ddtrace/contrib/http/patcher.rb in ddtrace-0.17.0
- old
+ new
@@ -1,88 +1,40 @@
-# requirements should be kept minimal as Patcher is a shared requirement.
+require 'ddtrace/contrib/patcher'
+require 'ddtrace/contrib/http/ext'
module Datadog
module Contrib
# Datadog Net/HTTP integration.
module HTTP
- URL = 'http.url'.freeze
- METHOD = 'http.method'.freeze
- BODY = 'http.body'.freeze
-
- NAME = 'http.request'.freeze
- APP = 'net/http'.freeze
- SERVICE = 'net/http'.freeze
-
- module_function
-
- 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.
- if req.respond_to?(:uri) && req.uri
- if req.uri.host.to_s == transport.hostname.to_s &&
- req.uri.port.to_i == transport.port.to_i
- return true
- end
- elsif address && port &&
- address.to_s == transport.hostname.to_s &&
- port.to_i == transport.port.to_i
- return true
- end
- # we don't want a "shotgun" effect with two nested traces for one
- # logical get, and request is likely to call itself recursively
- active = pin.tracer.active_span()
- return true if active && (active.name == NAME)
- false
- end
-
- def should_skip_distributed_tracing?(pin)
- if pin.config && pin.config.key?(:distributed_tracing)
- return !pin.config[:distributed_tracing]
- end
-
- !Datadog.configuration[:http][:distributed_tracing]
- end
-
# Patcher enables patching of 'net/http' module.
module Patcher
- include Base
- register_as :http, auto_patch: true
- option :distributed_tracing, default: false
- option :service_name, default: SERVICE
- option :tracer, default: Datadog.tracer
+ include Contrib::Patcher
- @patched = false
-
module_function
+ def patched?
+ done?(:http)
+ end
+
# patch applies our patch if needed
def patch
- unless @patched
+ do_once(:http) do
begin
require 'uri'
require 'ddtrace/pin'
require 'ddtrace/ext/app_types'
require 'ddtrace/ext/http'
require 'ddtrace/ext/net'
require 'ddtrace/ext/distributed'
patch_http
-
- @patched = true
rescue StandardError => e
Datadog::Tracer.log.error("Unable to apply net/http integration: #{e}")
end
end
- @patched
end
- # patched? tells whether patch has been successfully applied
- def patched?
- @patched
- end
-
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/BlockLength
# rubocop:disable Metrics/AbcSize
def patch_http
::Net::HTTP.class_eval do
@@ -92,11 +44,11 @@
def datadog_pin
@datadog_pindatadog_pin ||= begin
service = Datadog.configuration[:http][:service_name]
tracer = Datadog.configuration[:http][:tracer]
- Datadog::Pin.new(service, app: APP, app_type: Datadog::Ext::AppTypes::WEB, tracer: tracer)
+ Datadog::Pin.new(service, app: Ext::APP, app_type: Datadog::Ext::AppTypes::WEB, tracer: tracer)
end
end
def request(req, body = nil, &block) # :yield: +response+
pin = datadog_pin
@@ -106,10 +58,10 @@
if Datadog::Contrib::HTTP.should_skip_tracing?(req, @address, @port, transport, pin)
return request_without_datadog(req, body, &block)
end
- pin.tracer.trace(NAME) do |span|
+ pin.tracer.trace(Ext::SPAN_REQUEST) do |span|
begin
span.service = pin.service
span.span_type = Datadog::Ext::HTTP::TYPE
span.resource = req.method