Skip to content

Commit

Permalink
Add implicit context to CorrelationContext::Manager methods
Browse files Browse the repository at this point in the history
  • Loading branch information
mwear committed Dec 24, 2019
1 parent 1b9d430 commit a6d5936
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 38 deletions.
8 changes: 4 additions & 4 deletions api/lib/opentelemetry/correlation_context/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ module OpenTelemetry
module CorrelationContext
# No op implementation of CorrelationContext::Manager
class Manager
def set_value(context, key, value)
def set_value(key, value, context: Context.current)
context
end

def value(context, key)
def value(key, context: Context.current)
nil
end

def remove_value(context, key)
def remove_value(key, context: Context.current)
context
end

def clear(context)
def clear(context: Context.current)
context
end
end
Expand Down
20 changes: 12 additions & 8 deletions sdk/lib/opentelemetry/sdk/correlation_context/manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,43 @@ class Manager

# Returns a new context with empty correlations
#
# @param [Context] context
# @param [optional Context] context Context to clear correlations from. Defaults
# to +Context.current+
# @return [Context]
def clear(context)
def clear(context: Context.current)
context.set_value(CORRELATION_CONTEXT_KEY, EMPTY_CORRELATION_CONTEXT)
end

# Returns the corresponding correlation value (or nil) for key
#
# @param [Context] context The context use to retrieve key
# @param [String] key The lookup key
# @param [optional Context] context The context use to retrieve key.
# Defaults to +Context.current+
# @return [String]
def value(context, key)
def value(key, context: Context.current)
correlations_for(context)[key]
end

# Returns a new context with new key-value pair
#
# @param [Context] context The context to update with new value
# @param [String] key The key to store this value under
# @param [String] value String value to be stored under key
# @param [optional Context] context The context to update with new
# value. Defaults to +Context.current+
# @return [Context]
def set_value(context, key, value)
def set_value(key, value, context: Context.current)
new_correlations = correlations_for(context).dup
new_correlations[key] = value.to_s
context.set_value(CORRELATION_CONTEXT_KEY, new_correlations)
end

# Returns a new context with value at key removed
#
# @param [Context] context The context to remove value from
# @param [String] key The key to remove
# @param [optional Context] context The context to remove correlation
# from. Defaults to +Context.current+
# @return [Context]
def remove_value(context, key)
def remove_value(key, context: Context.current)
correlations = correlations_for(context)
return context unless correlations.key?(key)

Expand Down
108 changes: 82 additions & 26 deletions sdk/test/opentelemetry/sdk/correlation_context/manager_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,53 +7,109 @@
require 'test_helper'

describe OpenTelemetry::SDK::CorrelationContext::Manager do
Context = OpenTelemetry::Context
let(:manager) { OpenTelemetry::SDK::CorrelationContext::Manager.new }

after do
Context.clear
end

describe '.set_value' do
it 'sets key/value in correlation context' do
ctx = OpenTelemetry::Context.empty
_(manager.value(ctx, 'foo')).must_be_nil
describe 'explicit context' do
it 'sets key/value in context' do
ctx = Context.empty
_(manager.value('foo', context: ctx)).must_be_nil

ctx2 = manager.set_value(ctx, 'foo', 'bar')
_(manager.value(ctx2, 'foo')).must_equal('bar')
ctx2 = manager.set_value('foo', 'bar', context: ctx)
_(manager.value('foo', context: ctx2)).must_equal('bar')

_(manager.value(ctx, 'foo')).must_be_nil
_(manager.value('foo', context: ctx)).must_be_nil
end
end

describe 'implicit context' do
it 'sets key/value in implicit context' do
_(manager.value('foo')).must_be_nil

Context.with_current(manager.set_value('foo', 'bar')) do
_(manager.value('foo')).must_equal('bar')
end

_(manager.value('foo')).must_be_nil
end
end

it 'coerces values to strings' do
ctx = OpenTelemetry::Context.empty
ctx = manager.set_value(ctx, 'k1', 1)
ctx = manager.set_value(ctx, 'k2', true)
_(manager.value(ctx, 'k1')).must_equal('1')
_(manager.value(ctx, 'k2')).must_equal('true')
ctx = manager.set_value('k1', 1)
ctx = manager.set_value('k2', true, context: ctx)

Context.with_current(ctx) do
_(manager.value('k1')).must_equal('1')
_(manager.value('k2')).must_equal('true')
end
end
end

describe '.clear' do
it 'returns context with empty correlation context' do
ctx = manager.set_value(OpenTelemetry::Context.empty, 'foo', 'bar')
_(manager.value(ctx, 'foo')).must_equal('bar')
describe 'explicit context' do
it 'returns context with empty correlation context' do
ctx = manager.set_value('foo', 'bar', context: Context.empty)
_(manager.value('foo', context: ctx)).must_equal('bar')

ctx2 = manager.clear(ctx)
_(manager.value(ctx2, 'foo')).must_be_nil
ctx2 = manager.clear(context: ctx)
_(manager.value('foo', context: ctx2)).must_be_nil
end
end

describe 'implicit context' do
it 'returns context with empty correlation context' do
ctx = manager.set_value('foo', 'bar')
_(manager.value('foo', context: ctx)).must_equal('bar')

ctx2 = manager.clear
_(manager.value('foo', context: ctx2)).must_be_nil
end
end
end

describe '.remove_value' do
it 'returns context with key removed from correlation context' do
ctx = manager.set_value(OpenTelemetry::Context.empty, 'foo', 'bar')
_(manager.value(ctx, 'foo')).must_equal('bar')
describe 'explicit context' do
it 'returns context with key removed from correlation context' do
ctx = manager.set_value('foo', 'bar', context: Context.empty)
_(manager.value('foo', context: ctx)).must_equal('bar')

ctx2 = manager.remove_value('foo', context: ctx)
_(manager.value('foo', context: ctx2)).must_be_nil
end

ctx2 = manager.remove_value(ctx, 'foo')
_(manager.value(ctx2, 'foo')).must_be_nil
it 'returns same context if key does not exist' do
ctx = manager.set_value('foo', 'bar', context: Context.empty)
_(manager.value('foo', context: ctx)).must_equal('bar')

ctx2 = manager.remove_value('nonexistant-key', context: ctx)
_(ctx2).must_equal(ctx)
end
end

it 'returns same context if key does not exist' do
ctx = manager.set_value(OpenTelemetry::Context.empty, 'foo', 'bar')
_(manager.value(ctx, 'foo')).must_equal('bar')
describe 'implicit context' do
it 'returns context with key removed from correlation context' do
Context.with_current(manager.set_value('foo', 'bar')) do
_(manager.value('foo')).must_equal('bar')

ctx = manager.remove_value('foo')
_(manager.value('foo', context: ctx)).must_be_nil
end
end

it 'returns same context if key does not exist' do
Context.with_current(manager.set_value('foo', 'bar')) do
_(manager.value('foo')).must_equal('bar')
ctx_before = OpenTelemetry::Context.current

ctx2 = manager.remove_value(ctx, 'nonexistant-key')
_(ctx2).must_equal(ctx)
ctx = manager.remove_value('nonexistant-key')
_(ctx).must_equal(ctx_before)
end
end
end
end
end

0 comments on commit a6d5936

Please sign in to comment.