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

Context API improvements #215

Merged
merged 4 commits into from
Apr 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 8 additions & 3 deletions api/lib/opentelemetry/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ def current=(ctx)
# the previous context upon exiting.
#
# @param [Context] ctx The context to be made active
# @yield [context] Yields context to the block
def with_current(ctx)
prev = ctx.attach
yield
yield ctx
ensure
ctx.detach(prev)
end
Expand All @@ -53,10 +54,12 @@ def with_current(ctx)
# @param [String] key The lookup key
# @param [Object] value The object stored under key
# @param [Callable] Block to execute in a new context
# @yield [context, value] Yields the newly created context and value to
# the block
def with_value(key, value)
ctx = current.set_value(key, value)
prev = ctx.attach
yield value
yield ctx, value
ensure
ctx.detach(prev)
end
Expand All @@ -68,10 +71,12 @@ def with_value(key, value)
# @param [Hash] values Will be merged with values of the current context
# and returned in a new context
# @param [Callable] Block to execute in a new context
# @yield [context, values] Yields the newly created context and values
# to the block
def with_values(values)
ctx = current.set_values(values)
prev = ctx.attach
yield values
yield ctx, values
ensure
ctx.detach(prev)
end
Expand Down
17 changes: 13 additions & 4 deletions api/lib/opentelemetry/trace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ class Tracer

private_constant :EXTRACTED_SPAN_CONTEXT_KEY, :CURRENT_SPAN_KEY

def current_span
Context.value(CURRENT_SPAN_KEY) || Span::INVALID
# Returns the current span from the current or provided context
#
# @param [optional Context] context The context to lookup the current
# {Span} from. Defaults to Context.current
def current_span(context = Context.current)
context.value(CURRENT_SPAN_KEY) || Span::INVALID
end

# Returns the the active span context from the given {Context}, or current
Expand Down Expand Up @@ -46,9 +50,11 @@ def active_span_context(context = nil)
# On exit, the Span that was active before calling this method will be reactivated. If an
# exception occurs during the execution of the provided block, it will be recorded on the
# span and reraised.
# @yield [span, context] yields the newly created span and a context containing the
# span to the block.
def in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil, with_parent: nil, with_parent_context: nil)
span = start_span(name, attributes: attributes, links: links, start_timestamp: start_timestamp, kind: kind, with_parent: with_parent, with_parent_context: with_parent_context)
with_span(span) { |s| yield s }
with_span(span) { |s, c| yield s, c }
rescue Exception => e # rubocop:disable Lint/RescueException
span.record_error(e)
span.status = Status.new(Status::UNKNOWN_ERROR,
Expand All @@ -62,8 +68,11 @@ def in_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil,
# available implicitly.
#
# On exit, the Span that was active before calling this method will be reactivated.
#
# @param [Span] span the span to activate
# @yield [span, context] yields span and a context containing the span to the block.
def with_span(span)
Context.with_value(CURRENT_SPAN_KEY, span) { |s| yield s }
Context.with_value(CURRENT_SPAN_KEY, span) { |c, s| yield s, c }
end

def start_root_span(name, attributes: nil, links: nil, start_timestamp: nil, kind: nil)
Expand Down
27 changes: 25 additions & 2 deletions api/test/opentelemetry/context_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@

_(Context.current).must_equal(c1)
end

it 'yields the current context to the block' do
ctx = new_context
Context.with_current(ctx) do |c|
_(c).must_equal(ctx)
end
end
end

describe '.with_value' do
Expand All @@ -60,7 +67,7 @@

block_called = false

Context.with_value(foo_key, 'bar') do |value|
Context.with_value(foo_key, 'bar') do |_, value|
_(Context.current.value(foo_key)).must_equal('bar')
_(value).must_equal('bar')
block_called = true
Expand All @@ -69,6 +76,13 @@
_(Context.current).must_equal(orig_ctx)
_(block_called).must_equal(true)
end

it 'yields the current context and value to the block' do
Context.with_value(foo_key, 'bar') do |c, v|
_(v).must_equal('bar')
_(c.value(foo_key)).must_equal('bar')
end
end
end

describe '#value' do
Expand All @@ -84,7 +98,7 @@

block_called = false

Context.with_values(foo_key => 'bar', bar_key => 'baz') do |values|
Context.with_values(foo_key => 'bar', bar_key => 'baz') do |_, values|
_(Context.current.value(foo_key)).must_equal('bar')
_(Context.current.value(bar_key)).must_equal('baz')
_(values).must_equal(foo_key => 'bar', bar_key => 'baz')
Expand All @@ -94,6 +108,15 @@
_(Context.current).must_equal(orig_ctx)
_(block_called).must_equal(true)
end

it 'yields the current context and values to the block' do
values = { foo_key => 'bar', bar_key => 'baz' }
Context.with_values(values) do |c, v|
_(v).must_equal(values)
_(c.value(foo_key)).must_equal('bar')
_(c.value(bar_key)).must_equal('baz')
end
end
end

describe '#set_values' do
Expand Down
26 changes: 26 additions & 0 deletions api/test/opentelemetry/trace/tracer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ def start_span(*)
parent_span_context
)
end
let(:current_span_key) do
OpenTelemetry::Trace::Propagation::ContextKeys.current_span_key
end

describe '#current_span' do
let(:current_span) { tracer.start_span('current') }
Expand All @@ -50,6 +53,13 @@ def start_span(*)
_(tracer.current_span).must_equal(wrapper_span)
end
end

it 'returns the current span from the provided context' do
span = tracer.start_span('a-span')
context = Context.empty.set_value(current_span_key, span)
_(tracer.current_span).wont_equal(span)
_(tracer.current_span(context)).must_equal(span)
end
end

describe '#active_span_context' do
Expand Down Expand Up @@ -94,6 +104,13 @@ def start_span(*)
end
end

it 'yields context containing span' do
tracer.in_span('wrapper') do |span, context|
_(context).must_equal(OpenTelemetry::Context.current)
_(context[current_span_key]).must_equal(span)
end
end

it 'returns the result of the block' do
result = tracer.in_span('wrapper') { 'my-result' }
_(result).must_equal('my-result')
Expand Down Expand Up @@ -130,6 +147,15 @@ def start_span(*)
end
end

it 'yields context containing span' do
wrapper_span = tracer.start_span('wrapper')

tracer.with_span(wrapper_span) do |span, context|
_(context).must_equal(OpenTelemetry::Context.current)
_(context[current_span_key]).must_equal(span)
end
end

it 'should reactive the span after the block' do
outer = tracer.start_span('outer')
inner = tracer.start_span('inner')
Expand Down