Skip to content

Commit

Permalink
feat: add Tracer#context_with_span convenience method
Browse files Browse the repository at this point in the history
  • Loading branch information
mwear committed Sep 18, 2020
1 parent 6a10538 commit 6594a9e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions api/lib/opentelemetry/trace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ def current_span(context = Context.current)
context.value(CURRENT_SPAN_KEY) || Span::INVALID
end

# Returns a context containing the span, derived from the optional parent
# context, or the current context if one was not provided.
#
# @param [optional Context] context The context to use as the parent for
# the returned context
def context_with_span(span, context = Context.current)
context.set_value(CURRENT_SPAN_KEY, span)
end

# This is a helper for the default use-case of extending the current trace with a span.
#
# With this helper:
Expand Down
16 changes: 16 additions & 0 deletions api/test/opentelemetry/trace/tracer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,4 +161,20 @@ def start_span(*)
_(span.context).wont_equal(invalid_span_context)
end
end

describe '#context_with_span' do
it 'returns a context containing span' do
span = tracer.start_span('test')
ctx = tracer.context_with_span(span)
_(tracer.current_span(ctx)).must_equal(span)
end

it 'returns a context containing span' do
parent_ctx = OpenTelemetry::Context.empty.set_value('foo', 'bar')
span = tracer.start_span('test')
ctx = tracer.context_with_span(span, parent_ctx)
_(tracer.current_span(ctx)).must_equal(span)
_(ctx.value('foo')).must_equal('bar')
end
end
end

0 comments on commit 6594a9e

Please sign in to comment.