Skip to content

Commit

Permalink
Add shared
Browse files Browse the repository at this point in the history
  • Loading branch information
ykitamura-mdsol committed Mar 8, 2019
1 parent d4b6254 commit 1139cc5
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 29 deletions.
8 changes: 5 additions & 3 deletions lib/zipkin-tracer/rack/zipkin_env.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ def initialize(env, config)
end

def trace_id(default_flags = Trace::Flags::EMPTY)
trace_id, span_id, parent_span_id = retrieve_or_generate_ids
trace_id, span_id, parent_span_id, shared = retrieve_or_generate_ids
sampled = sampled_header_value(@env['HTTP_X_B3_SAMPLED'])
flags = (@env['HTTP_X_B3_FLAGS'] || default_flags).to_i
Trace::TraceId.new(trace_id, parent_span_id, span_id, sampled, flags)
Trace::TraceId.new(trace_id, parent_span_id, span_id, sampled, flags, shared)
end

def called_with_zipkin_headers?
Expand All @@ -33,12 +33,14 @@ def retrieve_or_generate_ids
if called_with_zipkin_headers?
trace_id, span_id = @env.values_at(*B3_REQUIRED_HEADERS)
parent_span_id = @env['HTTP_X_B3_PARENTSPANID']
shared = true
else
span_id = TraceGenerator.new.generate_id
trace_id = TraceGenerator.new.generate_id_from_span_id(span_id)
parent_span_id = nil
shared = false
end
[trace_id, span_id, parent_span_id]
[trace_id, span_id, parent_span_id, shared]
end

def new_sampled_header_value(sampled)
Expand Down
11 changes: 7 additions & 4 deletions lib/zipkin-tracer/trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def default_endpoint

class Annotation
attr_reader :value, :timestamp

def initialize(value)
@timestamp = (Time.now.to_f * 1000 * 1000).to_i # micros
@value = value
Expand Down Expand Up @@ -101,14 +101,15 @@ def to_i; @i64; end

# A TraceId contains all the information of a given trace id
class TraceId
attr_reader :trace_id, :parent_id, :span_id, :sampled, :flags
attr_reader :trace_id, :parent_id, :span_id, :sampled, :flags, :shared

def initialize(trace_id, parent_id, span_id, sampled, flags)
def initialize(trace_id, parent_id, span_id, sampled, flags, shared = false)
@trace_id = Trace.trace_id_128bit ? TraceId128Bit.from_value(trace_id) : SpanId.from_value(trace_id)
@parent_id = parent_id.nil? ? nil : SpanId.from_value(parent_id)
@span_id = SpanId.from_value(span_id)
@sampled = sampled
@flags = flags
@shared = shared
end

def next_id
Expand All @@ -125,7 +126,8 @@ def sampled?
end

def to_s
"TraceId(trace_id = #{@trace_id.to_s}, parent_id = #{@parent_id.to_s}, span_id = #{@span_id.to_s}, sampled = #{@sampled.to_s}, flags = #{@flags.to_s})"
"TraceId(trace_id = #{@trace_id.to_s}, parent_id = #{@parent_id.to_s}, span_id = #{@span_id.to_s}," \
" sampled = #{@sampled.to_s}, flags = #{@flags.to_s}, shared = #{@shared.to_s})"
end
end

Expand Down Expand Up @@ -209,6 +211,7 @@ def to_h
h[:remoteEndpoint] = @remote_endpoint.to_h unless @remote_endpoint.nil?
h[:annotations] = @annotations.map(&:to_h) unless @annotations.empty?
h[:tags] = @tags unless @tags.empty?
h[:shared] = true if @span_id.shared
h
end

Expand Down
8 changes: 8 additions & 0 deletions spec/lib/rack/zipkin_env_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ def mock_env(params = {}, path = '/')
expect(zipkin_env.trace_id.sampled?).to eq(true)
end

it 'shared is false' do
expect(zipkin_env.trace_id.shared).to eq(false)
end

context 'trace_id_128bit is true' do
before do
allow(Trace).to receive(:trace_id_128bit).and_return(true)
Expand All @@ -103,6 +107,10 @@ def mock_env(params = {}, path = '/')
expect(zipkin_env.called_with_zipkin_headers?).to eq(true)
end

it 'shared is true' do
expect(zipkin_env.trace_id.shared).to eq(true)
end

context 'parent_id is not provided' do
it 'uses the trace_id and span_id' do
trace_id = zipkin_env.trace_id
Expand Down
94 changes: 72 additions & 22 deletions spec/lib/trace_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
let(:parent_id) { 'f0e71086411b1445' }
let(:sampled) { true }
let(:flags) { Trace::Flags::EMPTY }
let(:trace_id) { Trace::TraceId.new(traceid, parent_id, span_id, sampled, flags) }
let(:shared) { false }
let(:trace_id) { Trace::TraceId.new(traceid, parent_id, span_id, sampled, flags, shared) }

it 'is not a debug trace' do
expect(trace_id.debug?).to eq(false)
Expand Down Expand Up @@ -60,6 +61,13 @@
end
end

context 'shared value is true' do
let(:shared) { true }
it 'is shared' do
expect(trace_id.shared).to eq(true)
end
end

context 'trace_id_128bit is false' do
let(:traceid) { '5af30660491a5a27234555b04cf7e099' }

Expand All @@ -76,6 +84,15 @@
expect(trace_id.trace_id.to_s).to eq(traceid)
end
end

describe '#to_s' do
it 'returns all information' do
expect(trace_id.to_s).to eq(
'TraceId(trace_id = 234555b04cf7e099, parent_id = f0e71086411b1445, span_id = c3a555b04cf7e099,' \
' sampled = true, flags = 0, shared = false)'
)
end
end
end

describe Trace::TraceId128Bit do
Expand Down Expand Up @@ -118,17 +135,17 @@
describe Trace::Span do
let(:span_id) { 'c3a555b04cf7e099' }
let(:parent_id) { 'f0e71086411b1445' }
let(:timestamp) { 1452987900000000 }
let(:duration) { 0 }
let(:key) { 'key' }
let(:value) { 'value' }
let(:numeric_value) { 123 }
let(:span_without_parent) do
Trace::Span.new('get', Trace::TraceId.new(span_id, nil, span_id, true, Trace::Flags::EMPTY))
end
let(:span_with_parent) do
Trace::Span.new('get', Trace::TraceId.new(span_id, parent_id, span_id, true, Trace::Flags::EMPTY))
end
let(:timestamp) { 1452987900000000 }
let(:duration) { 0 }
let(:key) { 'key' }
let(:value) { 'value' }
let(:numeric_value) { 123 }

before do
Timecop.freeze(Time.utc(2016, 1, 16, 23, 45))
Expand All @@ -142,22 +159,55 @@
end

describe '#to_h' do
it 'returns a hash representation of a span' do
expected_hash = {
name: 'get',
kind: 'CLIENT',
traceId: span_id,
localEndpoint: dummy_endpoint.to_h,
remoteEndpoint: dummy_endpoint.to_h,
id: span_id,
debug: false,
timestamp: timestamp,
duration: duration,
annotations: [{ timestamp: timestamp, value: "value" }],
tags: { "key" => "value" }
}
expect(span_without_parent.to_h).to eq(expected_hash)
expect(span_with_parent.to_h).to eq(expected_hash.merge(parentId: parent_id))
context 'client span' do
let(:expected_hash) do
{
name: 'get',
kind: 'CLIENT',
traceId: span_id,
localEndpoint: dummy_endpoint.to_h,
remoteEndpoint: dummy_endpoint.to_h,
id: span_id,
debug: false,
timestamp: timestamp,
duration: duration,
annotations: [{ timestamp: timestamp, value: "value" }],
tags: { "key" => "value" }
}
end

it 'returns a hash representation of a span' do
expect(span_without_parent.to_h).to eq(expected_hash)
expect(span_with_parent.to_h).to eq(expected_hash.merge(parentId: parent_id))
end
end

context 'server span' do
let(:shared_server_span) do
Trace::Span.new('get', Trace::TraceId.new(span_id, nil, span_id, true, Trace::Flags::EMPTY, true))
end
let(:expected_hash) do
{
name: 'get',
kind: 'SERVER',
traceId: span_id,
localEndpoint: dummy_endpoint.to_h,
id: span_id,
debug: false,
timestamp: timestamp,
duration: duration,
shared: true
}
end

before do
shared_server_span.kind = Trace::Span::Kind::SERVER
shared_server_span.local_endpoint = dummy_endpoint
end

it 'returns a hash representation of a span' do
expect(shared_server_span.to_h).to eq(expected_hash)
end
end
end

Expand Down

0 comments on commit 1139cc5

Please sign in to comment.