Skip to content

Propagate sampled flag in-process and improve docs #88

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

Merged
merged 4 commits into from
Oct 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## v0.16.0
### Added
- The tracer now supports B3 context propagation. Propagation can be set by using the `propagator` keyword argument to `Tracer#configure`. Valid values are `:lightstep` (default), and `:b3`.
- The tracer now supports B3 context propagation. Propagation can be set by using the `propagator` keyword argument to `LightStep.configure`. Valid values are `:lightstep` (default), and `:b3`.

## v0.15.0
### Added
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ Or install it yourself as:
# Initialize the singleton tracer
LightStep.configure(component_name: 'lightstep/ruby/example', access_token: 'your_access_token')

# Specify a propagation format (options are :lightstep (default) and :b3)
LightStep.configure(component_name: 'lightstep/ruby/example', access_token: 'your_access_token', propagator: :b3)

# Create a basic span and attach a log to the span
span = LightStep.start_span('my_span')
span.log(event: 'hello world', count: 42)
Expand Down
4 changes: 3 additions & 1 deletion lib/lightstep/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def initialize(
ref = ref.context if (Span === ref)

if SpanContext === ref
@context = SpanContext.new(id: LightStep.guid, trace_id: ref.trace_id)
@context = SpanContext.new(id: LightStep.guid, trace_id: ref.trace_id, sampled: ref.sampled?)
set_baggage(ref.baggage)
set_tag(:parent_span_guid, ref.id)
else
Expand All @@ -83,6 +83,7 @@ def set_baggage_item(key, value)
@context = SpanContext.new(
id: context.id,
trace_id: context.trace_id,
sampled: context.sampled?,
baggage: context.baggage.merge({key => value})
)
self
Expand All @@ -94,6 +95,7 @@ def set_baggage(baggage = {})
@context = SpanContext.new(
id: context.id,
trace_id: context.trace_id,
sampled: context.sampled?,
baggage: baggage
)
end
Expand Down
4 changes: 2 additions & 2 deletions lib/lightstep/span_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ def initialize(id:, trace_id:, sampled: true, baggage: {})

def truncate_id(id)
return id unless id && id.size == 32
id[16..-1]
id[0...16]
end

def pad_id(id)
return id unless id && id.size == 16
"#{ZERO_PADDING}#{id}"
"#{id}#{ZERO_PADDING}"
end
end
end
32 changes: 16 additions & 16 deletions spec/lightstep/propagation/b3_propagator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

describe LightStep::Propagation::B3Propagator, :rack_helpers do
let(:propagator) { subject }
let(:trace_id_high_bytes) { LightStep.guid }
let(:trace_id_low_bytes) { LightStep.guid }
let(:trace_id) { [trace_id_high_bytes, trace_id_low_bytes].join }
let(:trace_id_msb) { LightStep.guid }
let(:trace_id_lsb) { LightStep.guid }
let(:trace_id) { [trace_id_msb, trace_id_lsb].join }
let(:span_id) { LightStep.guid }
let(:baggage) do
{
Expand All @@ -19,10 +19,10 @@
baggage: baggage
)
end
let(:span_context_trace_id_low_bytes) do
let(:span_context_trace_id_8_byte) do
LightStep::SpanContext.new(
id: span_id,
trace_id: trace_id_low_bytes,
trace_id: trace_id_lsb,
baggage: baggage
)
end
Expand Down Expand Up @@ -76,8 +76,8 @@
it 'pads 8 byte trace_ids' do
carrier = {}

propagator.inject(span_context_trace_id_low_bytes, OpenTracing::FORMAT_RACK, carrier)
expect(carrier['x-b3-traceid']).to eq('0' * 16 << trace_id_low_bytes)
propagator.inject(span_context_trace_id_8_byte, OpenTracing::FORMAT_RACK, carrier)
expect(carrier['x-b3-traceid']).to eq(trace_id_lsb + '0' * 16)
expect(carrier['x-b3-spanid']).to eq(span_id)
end
end
Expand All @@ -88,7 +88,7 @@
propagator.inject(span_context, OpenTracing::FORMAT_TEXT_MAP, carrier)
extracted_ctx = propagator.extract(OpenTracing::FORMAT_TEXT_MAP, carrier)

expect(extracted_ctx.trace_id).to eq(trace_id_low_bytes)
expect(extracted_ctx.trace_id).to eq(trace_id_msb)
expect(extracted_ctx.trace_id16).to eq(trace_id)
expect(extracted_ctx.id).to eq(span_id)
expect(extracted_ctx.baggage['footwear']).to eq('cleats')
Expand All @@ -105,7 +105,7 @@
propagator.inject(span_context, OpenTracing::FORMAT_RACK, carrier)
extracted_ctx = propagator.extract(OpenTracing::FORMAT_RACK, to_rack_env(carrier))

expect(extracted_ctx.trace_id).to eq(trace_id_low_bytes)
expect(extracted_ctx.trace_id).to eq(trace_id_msb)
expect(extracted_ctx.trace_id16).to eq(trace_id)
expect(extracted_ctx.id).to eq(span_id)
expect(extracted_ctx.baggage['footwear']).to eq('cleats')
Expand Down Expand Up @@ -135,7 +135,7 @@
)
expect(extracted_ctx.id).to eq(span_id)
expect(extracted_ctx.trace_id16).to eq(trace_id)
expect(extracted_ctx.trace_id).to eq(trace_id_low_bytes)
expect(extracted_ctx.trace_id).to eq(trace_id_msb)
end

it 'handles carriers with string keys' do
Expand All @@ -148,7 +148,7 @@

expect(string_ctx).not_to be_nil
expect(string_ctx.trace_id16).to eq(trace_id)
expect(string_ctx.trace_id).to eq(trace_id_low_bytes)
expect(string_ctx.trace_id).to eq(trace_id_msb)
expect(string_ctx).to be_sampled
expect(string_ctx.id).to eq(span_id)
end
Expand All @@ -163,21 +163,21 @@

expect(symbol_ctx).not_to be_nil
expect(symbol_ctx.trace_id16).to eq(trace_id)
expect(symbol_ctx.trace_id).to eq(trace_id_low_bytes)
expect(symbol_ctx.trace_id).to eq(trace_id_msb)
expect(symbol_ctx).to be_sampled
expect(symbol_ctx.id).to eq(span_id)
end

it 'pads 8 byte trace_ids' do
carrier = {
'x-b3-traceid' => trace_id_low_bytes,
'x-b3-traceid' => trace_id_lsb,
'x-b3-spanid' => span_id,
'x-b3-sampled' => '1'
}

extracted_ctx = propagator.extract(OpenTracing::FORMAT_TEXT_MAP, carrier)
expect(extracted_ctx.trace_id16).to eq('0' * 16 << trace_id_low_bytes)
expect(extracted_ctx.trace_id).to eq(trace_id_low_bytes)
expect(extracted_ctx.trace_id16).to eq(trace_id_lsb + '0' * 16)
expect(extracted_ctx.trace_id).to eq(trace_id_lsb)
end

it 'interprets a true sampled flag properly' do
Expand Down Expand Up @@ -209,7 +209,7 @@
extracted_ctx = propagator.extract(OpenTracing::FORMAT_TEXT_MAP, carrier)
expect(extracted_ctx.trace_id16).to eq(trace_id)
expect(extracted_ctx.trace_id16.size).to eq(32)
expect(extracted_ctx.trace_id).to eq(trace_id_low_bytes)
expect(extracted_ctx.trace_id).to eq(trace_id_msb)
expect(extracted_ctx.trace_id.size).to eq(16)
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/lightstep/propagation/lightstep_propagator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe LightStep::Propagation::LightStepPropagator, :rack_helpers do
let(:propagator) { subject }
let(:trace_id) { LightStep.guid }
let(:padded_trace_id) { '0' * 16 << trace_id }
let(:padded_trace_id) { trace_id + '0' * 16 }
let(:span_id) { LightStep.guid }
let(:baggage) do
{
Expand Down Expand Up @@ -153,7 +153,7 @@
end

it 'maintains 8 and 16 byte trace ids' do
trace_id16 = [LightStep.guid, trace_id].join
trace_id16 = [trace_id, LightStep.guid].join

carrier = {
'ot-tracer-traceid' => trace_id16,
Expand Down
14 changes: 14 additions & 0 deletions spec/lightstep_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ def init_callback_tracer(callback)
expect(child_span.span_context.baggage).to eq(parent_span.span_context.baggage)
end

it 'should inherit true sampled flag from parent span' do
tracer = init_test_tracer
parent_ctx = LightStep::SpanContext.new(id: LightStep.guid, trace_id: LightStep.guid, sampled: true)
child_span = tracer.start_span('child_span', child_of: parent_ctx)
expect(child_span.span_context).to be_sampled
end

it 'should inherit false sampled flag from parent span' do
tracer = init_test_tracer
parent_ctx = LightStep::SpanContext.new(id: LightStep.guid, trace_id: LightStep.guid, sampled: false)
child_span = tracer.start_span('child_span', child_of: parent_ctx)
expect(child_span.span_context).not_to be_sampled
end

it 'should allow operation_name updates' do
tracer = init_test_tracer
span = tracer.start_span('original')
Expand Down