diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ff7864..50f63d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/README.md b/README.md index ef0cfdb..3e32962 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/lib/lightstep/span.rb b/lib/lightstep/span.rb index ab50c8e..14679eb 100644 --- a/lib/lightstep/span.rb +++ b/lib/lightstep/span.rb @@ -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 @@ -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 @@ -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 diff --git a/lib/lightstep/span_context.rb b/lib/lightstep/span_context.rb index cbbf986..573b45a 100644 --- a/lib/lightstep/span_context.rb +++ b/lib/lightstep/span_context.rb @@ -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 diff --git a/spec/lightstep/propagation/b3_propagator_spec.rb b/spec/lightstep/propagation/b3_propagator_spec.rb index be0b75d..76345d1 100644 --- a/spec/lightstep/propagation/b3_propagator_spec.rb +++ b/spec/lightstep/propagation/b3_propagator_spec.rb @@ -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 { @@ -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 @@ -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 @@ -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') @@ -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') @@ -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 @@ -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 @@ -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 @@ -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 diff --git a/spec/lightstep/propagation/lightstep_propagator_spec.rb b/spec/lightstep/propagation/lightstep_propagator_spec.rb index c49e630..5af0d1a 100644 --- a/spec/lightstep/propagation/lightstep_propagator_spec.rb +++ b/spec/lightstep/propagation/lightstep_propagator_spec.rb @@ -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 { @@ -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, diff --git a/spec/lightstep_spec.rb b/spec/lightstep_spec.rb index f3f7a81..c424df8 100644 --- a/spec/lightstep_spec.rb +++ b/spec/lightstep_spec.rb @@ -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')