Skip to content
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

Add support for reading B3 single header #165

Merged

Conversation

ykitamura-mdsol
Copy link
Contributor

This PR is just to support the "reading" part of B3 single header (#126).
The writing part is not included.

@adriancole sorry for taking a long time but I like the single header!

@jcarres-mdsol @jfeltesse-mdsol

return new(parse_sampled_flags(b3_single_header)) if b3_single_header.size == 1

trace_id, span_id, flag, parent_span_id = b3_single_header.split('-')
new(trace_id: trace_id, span_id: span_id, parent_span_id: parent_span_id, **parse_sampled_flags(flag))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's a bit wasteful to have this method create a new array (by calling split), then a new object and then in the caller call to_a which is going to create yet another array.

How about having a method getting the header as an input and return an array directly, without instantiating?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though I would say this is the least code approach I've ever seen :P

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated in 2fef91b

@@ -0,0 +1,38 @@
module ZipkinTracer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about adding the magic comment about frozen strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added in f8d6a89

Copy link
Member

@codefromthecrypt codefromthecrypt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lookin good!

agree some performance work could be done, but I like it anyway

def called_with_zipkin_headers?
@called_with_zipkin_headers ||= B3_REQUIRED_HEADERS.all? { |key| @env.key?(key) }
end

private

B3_REQUIRED_HEADERS = %w(HTTP_X_B3_TRACEID HTTP_X_B3_SPANID).freeze
B3_OPT_HEADERS = %w(HTTP_X_B3_PARENTSPANID HTTP_X_B3_SAMPLED HTTP_X_B3_FLAGS).freeze
B3_SINGLE_HEADER = 'HTTP_B3'.freeze
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

easier to quote, ain't it :)

lib/zipkin-tracer/zipkin_b3_single_header_format.rb Outdated Show resolved Hide resolved
return new(parse_sampled_flags(b3_single_header)) if b3_single_header.size == 1

trace_id, span_id, flag, parent_span_id = b3_single_header.split('-')
new(trace_id: trace_id, span_id: span_id, parent_span_id: parent_span_id, **parse_sampled_flags(flag))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

though I would say this is the least code approach I've ever seen :P

describe ZipkinTracer::B3SingleHeaderFormat do
let(:b3_single_header_format) { described_class.parse_from_header(b3_single_header) }

context 'child span' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add some malformed tests? Ex in brave we use

"not-a-tumor"
"b970dafd-0d95-40aa-95d8-1d8725aebe40"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adriancole In ruby client, we are doing malformed check in another class:

class TraceId128Bit < SpanId
HEX_REGEX_16 = /^[a-f0-9]{16}$/i
HEX_REGEX_32 = /^[a-f0-9]{32}$/i
MAX_SIGNED_I128 = (2 ** 128 / 2) -1
MASK = (2 ** 128) - 1
def self.from_value(v)
if v.is_a?(String) && v =~ HEX_REGEX_16
SpanId.new(v.hex)
elsif v.is_a?(String) && v =~ HEX_REGEX_32
new(v.hex)
elsif v.is_a?(Numeric)
new(v)
elsif v.is_a?(SpanId)
v
end
end

I think we don't need to have malformed tests here but what do you think?

@codefromthecrypt
Copy link
Member

codefromthecrypt commented Oct 9, 2019 via email

@jcarres-mdsol jcarres-mdsol merged commit 0a8be52 into openzipkin:master Oct 9, 2019
@ykitamura-mdsol ykitamura-mdsol deleted the feature/read_b3_single_header branch October 10, 2019 17:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants