-
Notifications
You must be signed in to change notification settings - Fork 373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[OpenTracing] Context propagation implementation #495
Merged
delner
merged 4 commits into
feature/opentracing
from
feature/opentracing_context_propagation
Aug 6, 2018
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
0194c94
Fixed: OpenTracer::Tracer#start_span not using provided SpanContext.
delner 3680786
Added: Propagators to OpenTracer.
delner 54ee584
Added: Specs for OpenTracer::Propagators.
delner 0c4cc15
Added: OpenTracer context propagation integration specs.
delner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module Datadog | ||
module OpenTracer | ||
# OpenTracing propagator for Datadog::OpenTracer::Tracer | ||
module BinaryPropagator | ||
extend Propagator | ||
|
||
# Inject a SpanContext into the given carrier | ||
# | ||
# @param span_context [SpanContext] | ||
# @param carrier [Carrier] A carrier object of Binary type | ||
def self.inject(span_context, carrier) | ||
nil | ||
end | ||
|
||
# Extract a SpanContext in Binary format from the given carrier. | ||
# | ||
# @param carrier [Carrier] A carrier object of Binary type | ||
# @return [SpanContext, nil] the extracted SpanContext or nil if none could be found | ||
def self.extract(carrier) | ||
SpanContext::NOOP_INSTANCE | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
require 'ddtrace/span' | ||
require 'ddtrace/ext/distributed' | ||
|
||
module Datadog | ||
module OpenTracer | ||
# DistributedHeaders provides easy access and validation to headers | ||
class DistributedHeaders | ||
include Datadog::Ext::DistributedTracing | ||
|
||
def initialize(carrier) | ||
@carrier = carrier | ||
end | ||
|
||
def valid? | ||
# Sampling priority is optional. | ||
!trace_id.nil? && !parent_id.nil? | ||
end | ||
|
||
def trace_id | ||
value = @carrier[HTTP_HEADER_TRACE_ID].to_i | ||
return if value <= 0 || value >= Datadog::Span::MAX_ID | ||
value | ||
end | ||
|
||
def parent_id | ||
value = @carrier[HTTP_HEADER_PARENT_ID].to_i | ||
return if value <= 0 || value >= Datadog::Span::MAX_ID | ||
value | ||
end | ||
|
||
def sampling_priority | ||
hdr = @carrier[HTTP_HEADER_SAMPLING_PRIORITY] | ||
# It's important to make a difference between no header, | ||
# and a header defined to zero. | ||
return unless hdr | ||
value = hdr.to_i | ||
return if value < 0 | ||
value | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module Datadog | ||
module OpenTracer | ||
# OpenTracing propagator for Datadog::OpenTracer::Tracer | ||
module Propagator | ||
# Inject a SpanContext into the given carrier | ||
# | ||
# @param span_context [SpanContext] | ||
# @param carrier [Carrier] A carrier object of the type dictated by the specified `format` | ||
def inject(span_context, carrier) | ||
raise NotImplementedError | ||
end | ||
|
||
# Extract a SpanContext in the given format from the given carrier. | ||
# | ||
# @param carrier [Carrier] A carrier object of the type dictated by the specified `format` | ||
# @return [SpanContext, nil] the extracted SpanContext or nil if none could be found | ||
def extract(carrier) | ||
raise NotImplementedError | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
require 'ddtrace/propagation/http_propagator' | ||
|
||
module Datadog | ||
module OpenTracer | ||
# OpenTracing propagator for Datadog::OpenTracer::Tracer | ||
module RackPropagator | ||
extend Propagator | ||
extend Datadog::Ext::DistributedTracing | ||
include Datadog::Ext::DistributedTracing | ||
|
||
BAGGAGE_PREFIX = 'ot-baggage-'.freeze | ||
BAGGAGE_PREFIX_FORMATTED = 'HTTP_OT_BAGGAGE_'.freeze | ||
|
||
class << self | ||
# Inject a SpanContext into the given carrier | ||
# | ||
# @param span_context [SpanContext] | ||
# @param carrier [Carrier] A carrier object of Rack type | ||
def inject(span_context, carrier) | ||
# Inject Datadog trace properties | ||
Datadog::HTTPPropagator.inject!(span_context.datadog_context, carrier) | ||
|
||
# Inject baggage | ||
span_context.baggage.each do |key, value| | ||
carrier[BAGGAGE_PREFIX + key] = value | ||
end | ||
|
||
nil | ||
end | ||
|
||
# Extract a SpanContext in Rack format from the given carrier. | ||
# | ||
# @param carrier [Carrier] A carrier object of Rack type | ||
# @return [SpanContext, nil] the extracted SpanContext or nil if none could be found | ||
def extract(carrier) | ||
# First extract & build a Datadog context | ||
datadog_context = Datadog::HTTPPropagator.extract(carrier) | ||
|
||
# Then extract any other baggage | ||
baggage = {} | ||
carrier.each do |key, value| | ||
baggage[header_to_baggage(key)] = value if baggage_header?(key) | ||
end | ||
|
||
SpanContextFactory.build(datadog_context: datadog_context, baggage: baggage) | ||
end | ||
|
||
private | ||
|
||
def baggage_header?(header) | ||
header.start_with?(BAGGAGE_PREFIX_FORMATTED) | ||
end | ||
|
||
def header_to_baggage(key) | ||
key[BAGGAGE_PREFIX_FORMATTED.length, key.length].downcase | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
require 'ddtrace/ext/distributed' | ||
|
||
module Datadog | ||
module OpenTracer | ||
# OpenTracing propagator for Datadog::OpenTracer::Tracer | ||
module TextMapPropagator | ||
extend Propagator | ||
extend Datadog::Ext::DistributedTracing | ||
include Datadog::Ext::DistributedTracing | ||
|
||
BAGGAGE_PREFIX = 'ot-baggage-'.freeze | ||
|
||
class << self | ||
# Inject a SpanContext into the given carrier | ||
# | ||
# @param span_context [SpanContext] | ||
# @param carrier [Carrier] A carrier object of Rack type | ||
def inject(span_context, carrier) | ||
# Inject Datadog trace properties | ||
span_context.datadog_context.tap do |datadog_context| | ||
carrier[HTTP_HEADER_TRACE_ID] = datadog_context.trace_id | ||
carrier[HTTP_HEADER_PARENT_ID] = datadog_context.span_id | ||
carrier[HTTP_HEADER_SAMPLING_PRIORITY] = datadog_context.sampling_priority | ||
end | ||
|
||
# Inject baggage | ||
span_context.baggage.each do |key, value| | ||
carrier[BAGGAGE_PREFIX + key] = value | ||
end | ||
|
||
nil | ||
end | ||
|
||
# Extract a SpanContext in TextMap format from the given carrier. | ||
# | ||
# @param carrier [Carrier] A carrier object of TextMap type | ||
# @return [SpanContext, nil] the extracted SpanContext or nil if none could be found | ||
def extract(carrier) | ||
# First extract & build a Datadog context | ||
headers = DistributedHeaders.new(carrier) | ||
|
||
datadog_context = if headers.valid? | ||
Datadog::Context.new( | ||
trace_id: headers.trace_id, | ||
span_id: headers.parent_id, | ||
sampling_priority: headers.sampling_priority | ||
) | ||
else | ||
Datadog::Context.new | ||
end | ||
|
||
# Then extract any other baggage | ||
baggage = {} | ||
carrier.each do |key, value| | ||
baggage[item_to_baggage(key)] = value if baggage_item?(key) | ||
end | ||
|
||
SpanContextFactory.build(datadog_context: datadog_context, baggage: baggage) | ||
end | ||
|
||
private | ||
|
||
def baggage_item?(item) | ||
item.start_with?(BAGGAGE_PREFIX) | ||
end | ||
|
||
def item_to_baggage(key) | ||
key[BAGGAGE_PREFIX.length, key.length] | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
require 'spec_helper' | ||
|
||
require 'ddtrace/opentracer' | ||
require 'ddtrace/opentracer/helper' | ||
|
||
if Datadog::OpenTracer.supported? | ||
RSpec.describe Datadog::OpenTracer::BinaryPropagator do | ||
include_context 'OpenTracing helpers' | ||
|
||
describe '#inject' do | ||
subject { described_class.inject(span_context, carrier) } | ||
let(:span_context) { instance_double(Datadog::OpenTracer::SpanContext) } | ||
let(:carrier) { instance_double(Datadog::OpenTracer::Carrier) } | ||
it { is_expected.to be nil } | ||
end | ||
|
||
describe '#extract' do | ||
subject(:span_context) { described_class.extract(carrier) } | ||
let(:carrier) { instance_double(Datadog::OpenTracer::Carrier) } | ||
it { is_expected.to be(Datadog::OpenTracer::SpanContext::NOOP_INSTANCE) } | ||
end | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a possible issue with round-trip propagation here. I'm anticipating there's a case where you could have baggage items with names containing either
-
s or uppercase characters. These should serialize to the carrier without issue, however, they'd probably fail to be extracted back to their original form, because how Rack forces headers to uppercase and underscores.e.g.
My-Baggage-Item
-->#inject
-->HTTP_OT_BAGGAGE_MY_BAGGAGE_ITEM
-->#extract
-->my_baggage_item
.I'm not sure this is possible to correct for, because we lose resolution on whether the key should have these dashes or uppercase characters when it goes through this conversion. Just something to watch out for.