-
Notifications
You must be signed in to change notification settings - Fork 237
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 TraceFlags class #57
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
fe512a6
Add TraceFlags class
fbogsany f35ffbf
Ruby 2.4 doesn't have Integer#anybits?
fbogsany 078ca1b
Stringify IDs
fbogsany 5d523e3
Move ID generation to Trace module
fbogsany a96186b
Add tests, fix errors & rubocop warnings
fbogsany d5857cb
Restore constant
fbogsany d88136b
Freeze strings, prematurely optimize
fbogsany ade516c
Rubocop
fbogsany 3287bdb
Ignore Rubocop: x == 0 is faster than x.zero?
fbogsany 326f889
Merge branch 'master' into trace-flags
fbogsany File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright 2019 OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module Trace | ||
# TraceFlags contain details about the trace. Unlike Tracestate values, | ||
# TraceFlags are present in all traces. Currently, the only TraceFlag is a | ||
# boolean {sampled?} {https://www.w3.org/TR/trace-context/#trace-flags flag}. | ||
class TraceFlags | ||
class << self | ||
private :new # rubocop:disable Style/AccessModifierDeclarations | ||
|
||
# Returns a newly created {TraceFlags} with the specified flags. | ||
# | ||
# @param [Integer] flags 8-bit byte of bit flags | ||
mwear marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# @raise [ArgumentError] If flags is not an 8-bit byte | ||
# @return [TraceFlags] | ||
def from_byte(flags) | ||
raise ArgumentError, 'flags must be an 8-bit byte' unless (0..255).include?(flags) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it more efficient to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bitwise check is a clear winner: require 'benchmark/ipsa'
num = 144
Benchmark.ipsa do |x|
x.report 'range check' do
(0..255).include?(num)
end
x.report 'bitwise check' do
num & ~0xFF == 0
end
x.compare!
end Results:
|
||
|
||
new(flags) | ||
end | ||
end | ||
|
||
# @api private | ||
# The constructor is private and only for use internally by the class. | ||
# Users should use the {from_byte} factory method to obtain a {TraceFlags} | ||
# instance. | ||
# | ||
# @param [Integer] flags 8-bit byte of bit flags | ||
# @return [TraceFlags] | ||
def initialize(flags) | ||
@flags = flags | ||
end | ||
|
||
# Returns whether the caller may have recorded trace data. When false, | ||
# the caller did not record trace data out-of-band. | ||
# | ||
# @return [Boolean] | ||
def sampled? | ||
(@flags & 1) != 0 | ||
end | ||
|
||
DEFAULT = from_byte(0) | ||
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,62 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
describe OpenTelemetry::Trace::SpanContext do | ||
let(:span_context) { OpenTelemetry::Trace::SpanContext.new } | ||
let(:invalid_context) { OpenTelemetry::Trace::SpanContext::INVALID } | ||
|
||
describe '#initialize' do | ||
it 'must generate valid span_id and trace_id by default' do | ||
span_context.trace_id.wont_equal(OpenTelemetry::Trace::INVALID_TRACE_ID) | ||
span_context.span_id.wont_equal(OpenTelemetry::Trace::INVALID_SPAN_ID) | ||
end | ||
end | ||
|
||
describe '#valid?' do | ||
it 'is true by default' do | ||
span_context.must_be(:valid?) | ||
end | ||
|
||
it 'is false for invalid context' do | ||
invalid_context.wont_be(:valid?) | ||
end | ||
end | ||
|
||
describe '#trace_id' do | ||
it 'reflects the value passed in' do | ||
trace_id = OpenTelemetry::Trace.generate_trace_id | ||
context = OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id) | ||
context.trace_id.must_equal(trace_id) | ||
end | ||
|
||
it 'is invalid for invalid context' do | ||
invalid_context.trace_id | ||
.must_equal(OpenTelemetry::Trace::INVALID_TRACE_ID) | ||
end | ||
end | ||
|
||
describe '#span_id' do | ||
it 'reflects the value passed in' do | ||
span_id = OpenTelemetry::Trace.generate_span_id | ||
context = OpenTelemetry::Trace::SpanContext.new(span_id: span_id) | ||
context.span_id.must_equal(span_id) | ||
end | ||
|
||
it 'is invalid for invalid context' do | ||
invalid_context.span_id.must_equal(OpenTelemetry::Trace::INVALID_SPAN_ID) | ||
end | ||
end | ||
|
||
describe '#trace_flags' do | ||
it 'is unsampled by default' do | ||
span_context.trace_flags.wont_be(:sampled?) | ||
end | ||
|
||
it 'reflects the value passed in' do | ||
flags = OpenTelemetry::Trace::TraceFlags.from_byte(1) | ||
context = OpenTelemetry::Trace::SpanContext.new(trace_flags: flags) | ||
context.trace_flags.must_equal(flags) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'test_helper' | ||
|
||
describe OpenTelemetry::Trace::TraceFlags do | ||
describe '.new' do | ||
it 'is private' do | ||
-> { OpenTelemetry::Trace::TraceFlags.new(0) }\ | ||
.must_raise(NoMethodError) | ||
end | ||
end | ||
describe '.from_byte' do | ||
it 'can be initialized with a byte' do | ||
flags = OpenTelemetry::Trace::TraceFlags.from_byte(0) | ||
flags.sampled?.must_equal(false) | ||
end | ||
|
||
it 'enforces flags is an 8-bit byte' do | ||
-> { OpenTelemetry::Trace::TraceFlags.from_byte(256) }\ | ||
.must_raise(ArgumentError) | ||
end | ||
end | ||
|
||
describe '#sampled?' do | ||
it 'reflects the least-significant bit in the flags' do | ||
sampled = OpenTelemetry::Trace::TraceFlags.from_byte(1) | ||
not_sampled = OpenTelemetry::Trace::TraceFlags.from_byte(0) | ||
|
||
sampled.sampled?.must_equal(true) | ||
not_sampled.sampled?.must_equal(false) | ||
end | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we freeze this string (and INVALID_SPAN_ID below)?