-
Notifications
You must be signed in to change notification settings - Fork 250
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
199 additions
and
14 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
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 | ||
# @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 flags & ~0xFF == 0 # rubocop:disable Style/NumericPredicate | ||
|
||
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 |