lib/httpx/adapters/datadog.rb in httpx-0.24.7 vs lib/httpx/adapters/datadog.rb in httpx-1.0.0
- old
+ new
@@ -1,54 +1,27 @@
# frozen_string_literal: true
-if defined?(DDTrace) && DDTrace::VERSION::STRING >= "1.0.0"
- require "datadog/tracing/contrib/integration"
- require "datadog/tracing/contrib/configuration/settings"
- require "datadog/tracing/contrib/patcher"
+require "datadog/tracing/contrib/integration"
+require "datadog/tracing/contrib/configuration/settings"
+require "datadog/tracing/contrib/patcher"
- TRACING_MODULE = Datadog::Tracing
-else
-
- require "ddtrace/contrib/integration"
- require "ddtrace/contrib/configuration/settings"
- require "ddtrace/contrib/patcher"
-
- TRACING_MODULE = Datadog
-end
-
-module TRACING_MODULE # rubocop:disable Naming/ClassAndModuleCamelCase
+module Datadog::Tracing
module Contrib
module HTTPX
- if defined?(::DDTrace) && ::DDTrace::VERSION::STRING >= "1.0.0"
- METADATA_MODULE = TRACING_MODULE::Metadata
+ METADATA_MODULE = Datadog::Tracing::Metadata
- TYPE_OUTBOUND = TRACING_MODULE::Metadata::Ext::HTTP::TYPE_OUTBOUND
+ TYPE_OUTBOUND = Datadog::Tracing::Metadata::Ext::HTTP::TYPE_OUTBOUND
- TAG_PEER_SERVICE = TRACING_MODULE::Metadata::Ext::TAG_PEER_SERVICE
+ TAG_PEER_SERVICE = Datadog::Tracing::Metadata::Ext::TAG_PEER_SERVICE
- TAG_URL = TRACING_MODULE::Metadata::Ext::HTTP::TAG_URL
- TAG_METHOD = TRACING_MODULE::Metadata::Ext::HTTP::TAG_METHOD
- TAG_TARGET_HOST = TRACING_MODULE::Metadata::Ext::NET::TAG_TARGET_HOST
- TAG_TARGET_PORT = TRACING_MODULE::Metadata::Ext::NET::TAG_TARGET_PORT
+ TAG_URL = Datadog::Tracing::Metadata::Ext::HTTP::TAG_URL
+ TAG_METHOD = Datadog::Tracing::Metadata::Ext::HTTP::TAG_METHOD
+ TAG_TARGET_HOST = Datadog::Tracing::Metadata::Ext::NET::TAG_TARGET_HOST
+ TAG_TARGET_PORT = Datadog::Tracing::Metadata::Ext::NET::TAG_TARGET_PORT
- TAG_STATUS_CODE = TRACING_MODULE::Metadata::Ext::HTTP::TAG_STATUS_CODE
+ TAG_STATUS_CODE = Datadog::Tracing::Metadata::Ext::HTTP::TAG_STATUS_CODE
- else
-
- METADATA_MODULE = Datadog
-
- TYPE_OUTBOUND = TRACING_MODULE::Ext::HTTP::TYPE_OUTBOUND
- TAG_PEER_SERVICE = TRACING_MODULE::Ext::Integration::TAG_PEER_SERVICE
- TAG_URL = TRACING_MODULE::Ext::HTTP::URL
- TAG_METHOD = TRACING_MODULE::Ext::HTTP::METHOD
- TAG_TARGET_HOST = TRACING_MODULE::Ext::NET::TARGET_HOST
- TAG_TARGET_PORT = TRACING_MODULE::Ext::NET::TARGET_PORT
- TAG_STATUS_CODE = Datadog::Ext::HTTP::STATUS_CODE
- PROPAGATOR = TRACING_MODULE::HTTPPropagator
-
- end
-
# HTTPX Datadog Plugin
#
# Enables tracing for httpx requests. A span will be created for each individual requests,
# and it'll trace since the moment it is fed to the connection, until the moment the response is
# fed back to the session.
@@ -62,18 +35,22 @@
def initialize(request)
@request = request
end
def call
- return unless tracing_enabled?
+ return unless Datadog::Tracing.enabled?
@request.on(:response, &method(:finish))
verb = @request.verb
uri = @request.uri
- @span = build_span
+ @span = Datadog::Tracing.trace(
+ SPAN_REQUEST,
+ service: service_name(@request.uri.host, configuration, Datadog.configuration_for(self)),
+ span_type: TYPE_OUTBOUND
+ )
@span.resource = verb
# Add additional request specific tags to the span.
@@ -84,11 +61,12 @@
@span.set_tag(TAG_TARGET_PORT, uri.port.to_s)
# Tag as an external peer service
@span.set_tag(TAG_PEER_SERVICE, @span.service)
- propagate_headers if @configuration[:distributed_tracing]
+ Datadog::Tracing::Propagation::HTTP.inject!(Datadog::Tracing.active_trace,
+ @request.headers) if @configuration[:distributed_tracing]
# Set analytics sample rate
if Contrib::Analytics.enabled?(@configuration[:analytics_enabled])
Contrib::Analytics.set_sample_rate(@span, @configuration[:analytics_sample_rate])
end
@@ -111,52 +89,12 @@
@span.finish
end
private
- if defined?(::DDTrace) && ::DDTrace::VERSION::STRING >= "1.0.0"
-
- def build_span
- TRACING_MODULE.trace(
- SPAN_REQUEST,
- service: service_name(@request.uri.host, configuration, Datadog.configuration_for(self)),
- span_type: TYPE_OUTBOUND
- )
- end
-
- def propagate_headers
- TRACING_MODULE::Propagation::HTTP.inject!(TRACING_MODULE.active_trace, @request.headers)
- end
-
- def configuration
- @configuration ||= Datadog.configuration.tracing[:httpx, @request.uri.host]
- end
-
- def tracing_enabled?
- TRACING_MODULE.enabled?
- end
- else
- def build_span
- service_name = configuration[:split_by_domain] ? @request.uri.host : configuration[:service_name]
- configuration[:tracer].trace(
- SPAN_REQUEST,
- service: service_name,
- span_type: TYPE_OUTBOUND
- )
- end
-
- def propagate_headers
- Datadog::HTTPPropagator.inject!(@span.context, @request.headers)
- end
-
- def configuration
- @configuration ||= Datadog.configuration[:httpx, @request.uri.host]
- end
-
- def tracing_enabled?
- configuration[:tracer].enabled
- end
+ def configuration
+ @configuration ||= Datadog.configuration.tracing[:httpx, @request.uri.host]
end
end
module RequestMethods
def __datadog_enable_trace!
@@ -177,58 +115,38 @@
end
module Configuration
# Default settings for httpx
#
- class Settings < TRACING_MODULE::Contrib::Configuration::Settings
+ class Settings < Datadog::Tracing::Contrib::Configuration::Settings
DEFAULT_ERROR_HANDLER = lambda do |response|
Datadog::Ext::HTTP::ERROR_RANGE.cover?(response.status)
end
option :service_name, default: "httpx"
option :distributed_tracing, default: true
option :split_by_domain, default: false
- if DDTrace::VERSION::STRING >= "1.13.0"
- option :enabled do |o|
- o.type :bool
- o.env "DD_TRACE_HTTPX_ENABLED"
- o.default true
- end
+ option :enabled do |o|
+ o.default { env_to_bool("DD_TRACE_HTTPX_ENABLED", true) }
+ o.lazy
+ end
- option :analytics_enabled do |o|
- o.type :bool
- o.env "DD_TRACE_HTTPX_ANALYTICS_ENABLED"
- o.default false
- end
+ option :analytics_enabled do |o|
+ o.default { env_to_bool(%w[DD_TRACE_HTTPX_ANALYTICS_ENABLED DD_HTTPX_ANALYTICS_ENABLED], false) }
+ o.lazy
+ end
- option :analytics_sample_rate do |o|
- o.type :float
- o.env "DD_TRACE_HTTPX_ANALYTICS_SAMPLE_RATE"
- o.default 1.0
- end
- else
- option :enabled do |o|
- o.default { env_to_bool("DD_TRACE_HTTPX_ENABLED", true) }
- o.lazy
- end
-
- option :analytics_enabled do |o|
- o.default { env_to_bool(%w[DD_TRACE_HTTPX_ANALYTICS_ENABLED DD_HTTPX_ANALYTICS_ENABLED], false) }
- o.lazy
- end
-
- option :analytics_sample_rate do |o|
- o.default { env_to_float(%w[DD_TRACE_HTTPX_ANALYTICS_SAMPLE_RATE DD_HTTPX_ANALYTICS_SAMPLE_RATE], 1.0) }
- o.lazy
- end
+ option :analytics_sample_rate do |o|
+ o.default { env_to_float(%w[DD_TRACE_HTTPX_ANALYTICS_SAMPLE_RATE DD_HTTPX_ANALYTICS_SAMPLE_RATE], 1.0) }
+ o.lazy
end
- if defined?(TRACING_MODULE::Contrib::SpanAttributeSchema)
+ if defined?(Datadog::Tracing::Contrib::SpanAttributeSchema)
option :service_name do |o|
o.default do
- TRACING_MODULE::Contrib::SpanAttributeSchema.fetch_service_name(
+ Datadog::Tracing::Contrib::SpanAttributeSchema.fetch_service_name(
"DD_TRACE_HTTPX_SERVICE_NAME",
"httpx"
)
end
o.lazy
@@ -242,30 +160,18 @@
end
end
option :distributed_tracing, default: true
- if DDTrace::VERSION::STRING >= "1.15.0"
- option :error_handler do |o|
- o.type :proc
- o.default_proc(&DEFAULT_ERROR_HANDLER)
- end
- elsif DDTrace::VERSION::STRING >= "1.13.0"
- option :error_handler do |o|
- o.type :proc
- o.experimental_default_proc(&DEFAULT_ERROR_HANDLER)
- end
- else
- option :error_handler, default: DEFAULT_ERROR_HANDLER
- end
+ option :error_handler, default: DEFAULT_ERROR_HANDLER
end
end
# Patcher enables patching of 'httpx' with datadog components.
#
module Patcher
- include TRACING_MODULE::Contrib::Patcher
+ include Datadog::Tracing::Contrib::Patcher
module_function
def target_version
Integration.version
@@ -284,11 +190,10 @@
# Datadog Integration for HTTPX.
#
class Integration
include Contrib::Integration
- # MINIMUM_VERSION = Gem::Version.new('0.11.0')
MINIMUM_VERSION = Gem::Version.new("0.10.2")
register_as :httpx
def self.version
@@ -301,17 +206,11 @@
def self.compatible?
super && version >= MINIMUM_VERSION
end
- if defined?(::DDTrace) && ::DDTrace::VERSION::STRING >= "1.0.0"
- def new_configuration
- Configuration::Settings.new
- end
- else
- def default_configuration
- Configuration::Settings.new
- end
+ def new_configuration
+ Configuration::Settings.new
end
def patcher
Patcher
end