From 57f18fa6ac8dd1ab9f273530e9aea01052e0ca0a Mon Sep 17 00:00:00 2001 From: Pedro Lambert Date: Thu, 2 Nov 2017 18:47:36 -0400 Subject: [PATCH] [WIP] Propagate `sampling_priority` --- lib/ddtrace/contrib/faraday/middleware.rb | 10 ++++++---- lib/ddtrace/contrib/http/patcher.rb | 3 +++ lib/ddtrace/span.rb | 2 ++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/lib/ddtrace/contrib/faraday/middleware.rb b/lib/ddtrace/contrib/faraday/middleware.rb index a5bc3545b56..9d7ff64eeef 100644 --- a/lib/ddtrace/contrib/faraday/middleware.rb +++ b/lib/ddtrace/contrib/faraday/middleware.rb @@ -8,6 +8,8 @@ module Contrib module Faraday # Middleware implements a faraday-middleware for ddtrace instrumentation class Middleware < ::Faraday::Middleware + include Ext::DistributedTracing + DEFAULT_ERROR_HANDLER = lambda do |env| Ext::HTTP::ERROR_RANGE.cover?(env[:status]) end @@ -54,10 +56,10 @@ def handle_response(span, env) end def propagate!(span, env) - env[:request_headers].merge!( - Ext::DistributedTracing::HTTP_HEADER_TRACE_ID => span.trace_id.to_s, - Ext::DistributedTracing::HTTP_HEADER_PARENT_ID => span.span_id.to_s - ) + env[:request_headers][HTTP_HEADER_TRACE_ID] = span.trace_id.to_s + env[:request_headers][HTTP_HEADER_PARENT_ID] = span.span_id.to_s + return unless span.sampling_priority + env[:request_headers][HTTP_HEADER_SAMPLING_PRIORITY] = span.sampling_priority.to_s end def dd_pin diff --git a/lib/ddtrace/contrib/http/patcher.rb b/lib/ddtrace/contrib/http/patcher.rb index 09d6dc3ed71..6dfed3aba2f 100644 --- a/lib/ddtrace/contrib/http/patcher.rb +++ b/lib/ddtrace/contrib/http/patcher.rb @@ -132,6 +132,9 @@ def request(req, body = nil, &block) # :yield: +response+ unless 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.sampling_priority + req.add_field(Datadog::Ext::DistributedTracing::HTTP_HEADER_SAMPLING_PRIORITY, span.sampling_priority) + end end rescue StandardError => e Datadog::Tracer.log.error("error preparing span for http request: #{e}") diff --git a/lib/ddtrace/span.rb b/lib/ddtrace/span.rb index 8c4cdb07483..fecd6010d9f 100644 --- a/lib/ddtrace/span.rb +++ b/lib/ddtrace/span.rb @@ -46,6 +46,7 @@ def initialize(tracer, name, options = {}) @span_id = Datadog::Utils.next_id @parent_id = options.fetch(:parent_id, 0) @trace_id = options.fetch(:trace_id, Datadog::Utils.next_id) + @sampling_priority = options[:sampling_priority] @context = options.fetch(:context, nil) @@ -160,6 +161,7 @@ def parent=(parent) @parent_id = parent.span_id @service ||= parent.service @sampled = parent.sampled + @sampling_priority = parent.sampling_priority end end