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 remote? to SpanContext #76

Merged
merged 4 commits into from
Sep 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions api/lib/opentelemetry/trace/span_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,43 @@
module OpenTelemetry
module Trace
# A SpanContext contains the state that must propagate to child {Span}s and across process boundaries.
# It contains the identifiers (a trace ID and span ID) associated with the {Span} and a set of
# {TraceFlags}.
# It contains the identifiers (a trace ID and span ID) associated with the {Span}, a set of
# {TraceFlags}, and a boolean indicating that the SpanContext was extracted from the wire.
class SpanContext
attr_reader :trace_id, :span_id, :trace_flags
attr_reader :trace_id, :span_id, :trace_flags, :remote

# Returns a new {SpanContext}.
#
# @param [optional String] trace_id The trace ID associated with a {Span}.
# @param [optional String] span_id The span ID associated with a {Span}.
# @param [optional TraceFlags] trace_flags The trace flags associated with a {Span}.
# @param [optional Boolean] remote Whether the {SpanContext} was extracted from the wire.
# @return [SpanContext]
def initialize(
trace_id: Trace.generate_trace_id,
span_id: Trace.generate_span_id,
trace_flags: TraceFlags::DEFAULT
trace_flags: TraceFlags::DEFAULT,
remote: false
)
@trace_id = trace_id
@span_id = span_id
@trace_flags = trace_flags
@remote = remote
end

# Returns true if the {SpanContext} has a non-zero trace ID and non-zero span ID.
#
# @return [Boolean]
def valid?
@trace_id != INVALID_TRACE_ID && @span_id != INVALID_SPAN_ID
end

# Returns true if the {SpanContext} was propagated from a remote parent.
#
# @return [Boolean]
alias remote? remote
fbogsany marked this conversation as resolved.
Show resolved Hide resolved

# Represents an invalid {SpanContext}, with an invalid trace ID and an invalid span ID.
INVALID = new(trace_id: INVALID_TRACE_ID, span_id: INVALID_SPAN_ID)
end
end
Expand Down
11 changes: 11 additions & 0 deletions api/test/opentelemetry/trace/span_context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@
end
end

describe '#remote?' do
it 'is false by default' do
span_context.wont_be(:remote?)
end

it 'reflects the value passed in' do
context = OpenTelemetry::Trace::SpanContext.new(remote: true)
context.must_be(:remote?)
end
end

describe '#trace_id' do
it 'reflects the value passed in' do
trace_id = OpenTelemetry::Trace.generate_trace_id
Expand Down