-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for reading B3 single header (#165)
* Add support for reading B3 single header * Update lib/zipkin-tracer/zipkin_b3_single_header_format.rb Co-Authored-By: Adrian Cole <[email protected]> * Add magic comment about frozen strings * Avoid instantiating
- Loading branch information
1 parent
a1b1523
commit 0a8be52
Showing
7 changed files
with
213 additions
and
11 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
# 0.40.0 | ||
* Add support for reading B3 single header. | ||
|
||
# 0.39.2 | ||
* Make Faraday 0.13 the minimum requirement. | ||
|
||
|
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
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module ZipkinTracer | ||
VERSION = '0.39.2'.freeze | ||
VERSION = '0.40.0'.freeze | ||
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,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module ZipkinTracer | ||
# This format corresponds to the propagation key "b3" (or "B3"). | ||
# b3: {x-b3-traceid}-{x-b3-spanid}-{if x-b3-flags 'd' else x-b3-sampled}-{x-b3-parentspanid} | ||
# For details, see: https://github.com/openzipkin/b3-propagation | ||
class B3SingleHeaderFormat | ||
def self.parse_from_header(b3_single_header) | ||
if b3_single_header.size == 1 | ||
flag = b3_single_header | ||
else | ||
trace_id, span_id, flag, parent_span_id = b3_single_header.split('-') | ||
end | ||
[trace_id, span_id, parent_span_id, parse_sampled(flag), parse_flags(flag)] | ||
end | ||
|
||
def self.parse_sampled(flag) | ||
case flag | ||
when '1', '0' | ||
flag | ||
end | ||
end | ||
|
||
def self.parse_flags(flag) | ||
flag == 'd' ? Trace::Flags::DEBUG : Trace::Flags::EMPTY | ||
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,73 @@ | ||
require 'spec_helper' | ||
|
||
describe ZipkinTracer::B3SingleHeaderFormat do | ||
let(:b3_single_header_format) { described_class.parse_from_header(b3_single_header) } | ||
|
||
context 'child span' do | ||
let(:b3_single_header) { '80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1-05e3ac9a4f6e3b90' } | ||
|
||
it 'has all fields' do | ||
expect(b3_single_header_format.to_a).to eq( | ||
['80f198ee56343ba864fe8b2a57d3eff7', 'e457b5a2e4d86bd1', '05e3ac9a4f6e3b90', '1', 0] | ||
) | ||
end | ||
end | ||
|
||
context 'sampled root span' do | ||
let(:b3_single_header) { '80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-1' } | ||
|
||
it 'does not have parent_span_id' do | ||
expect(b3_single_header_format.to_a).to eq(['80f198ee56343ba864fe8b2a57d3eff7', 'e457b5a2e4d86bd1', nil, '1', 0]) | ||
end | ||
end | ||
|
||
context 'not yet sampled root span' do | ||
let(:b3_single_header) { '80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1' } | ||
|
||
it 'does not have parent_span_id and sampled' do | ||
expect(b3_single_header_format.to_a).to eq(['80f198ee56343ba864fe8b2a57d3eff7', 'e457b5a2e4d86bd1', nil, nil, 0]) | ||
end | ||
end | ||
|
||
context 'debug RPC child span' do | ||
let(:b3_single_header) { '80f198ee56343ba864fe8b2a57d3eff7-e457b5a2e4d86bd1-d-05e3ac9a4f6e3b90' } | ||
|
||
it 'has debug flag' do | ||
expect(b3_single_header_format.to_a).to eq( | ||
['80f198ee56343ba864fe8b2a57d3eff7', 'e457b5a2e4d86bd1', '05e3ac9a4f6e3b90', nil, 1] | ||
) | ||
end | ||
end | ||
|
||
context 'do not sample flag only' do | ||
let(:b3_single_header) { '0' } | ||
|
||
it 'has do not sample flag only' do | ||
expect(b3_single_header_format.to_a).to eq([nil, nil, nil, '0', 0]) | ||
end | ||
end | ||
|
||
context 'sampled flag only' do | ||
let(:b3_single_header) { '1' } | ||
|
||
it 'has sampled flag only' do | ||
expect(b3_single_header_format.to_a).to eq([nil, nil, nil, '1', 0]) | ||
end | ||
end | ||
|
||
context 'debug flag only' do | ||
let(:b3_single_header) { 'd' } | ||
|
||
it 'has debug flag only' do | ||
expect(b3_single_header_format.to_a).to eq([nil, nil, nil, nil, 1]) | ||
end | ||
end | ||
|
||
context 'unknown flag only' do | ||
let(:b3_single_header) { 'u' } | ||
|
||
it 'has nothing' do | ||
expect(b3_single_header_format.to_a).to eq([nil, nil, nil, nil, 0]) | ||
end | ||
end | ||
end |