-
Notifications
You must be signed in to change notification settings - Fork 248
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial implementation of SDK correlation context
- Loading branch information
Showing
4 changed files
with
167 additions
and
1 deletion.
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
44 changes: 44 additions & 0 deletions
44
sdk/lib/opentelemetry/sdk/distributed_context/correlation_context.rb
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,44 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright 2019 OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module SDK | ||
module DistributedContext | ||
# SDK implementation of CorrelationContext | ||
class CorrelationContext | ||
attr_reader :entries | ||
|
||
# Returns a new CorrelationContext. If a parent is provided, entries | ||
# will be inherited by the new context, but can be overridden or removed | ||
# by specifiying +entries+ or +remove_keys+. | ||
# | ||
# @param [optional CorrelationContext] parent An optional parent context | ||
# @param [optional Hash<Label::key,Label>] entries A hash of | ||
# Label::Key-Label pairs for this context | ||
# @param [optional Array<Label::Key>] remove_keys Keys to be removed | ||
# from this context | ||
def initialize(parent: nil, entries: {}, remove_keys: nil) | ||
@parent = parent | ||
@entries = if @parent | ||
entries.merge!(parent.entries) { |_, v, _| v } | ||
else | ||
@entries = entries | ||
end | ||
remove_keys&.each { |key| @entries.delete(key) } | ||
@entries.freeze | ||
end | ||
|
||
# Returns the label associated with key | ||
# | ||
# @param key | ||
# @return [Label] | ||
def [](key) | ||
@entries[key] | ||
end | ||
end | ||
end | ||
end | ||
end |
121 changes: 121 additions & 0 deletions
121
sdk/test/opentelemetry/sdk/distributed_context/correlation_context_test.rb
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,121 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright 2019 OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require 'test_helper' | ||
|
||
describe OpenTelemetry::SDK::DistributedContext::CorrelationContext do | ||
CorrelationContext = OpenTelemetry::SDK::DistributedContext::CorrelationContext | ||
Label = OpenTelemetry::DistributedContext::Label | ||
Key = OpenTelemetry::DistributedContext::Label::Key | ||
Value = OpenTelemetry::DistributedContext::Label::Value | ||
Metadata = OpenTelemetry::DistributedContext::Label::Metadata | ||
|
||
let(:foo_key) { Key.new('foo') } | ||
let(:bar_key) { Key.new('bar') } | ||
let(:baz_key) { Key.new('baz') } | ||
let(:foo_label) do | ||
Label.new( | ||
key: foo_key, | ||
value: Value.new('bar') | ||
) | ||
end | ||
let(:bar_label) do | ||
Label.new( | ||
key: bar_key, | ||
value: Value.new('baz') | ||
) | ||
end | ||
let(:baz_label) do | ||
Label.new( | ||
key: baz_key, | ||
value: Value.new('quux') | ||
) | ||
end | ||
|
||
describe '.new' do | ||
it 'does not require any defaults' do | ||
ctx = CorrelationContext.new | ||
_(ctx.entries).must_be(:empty?) | ||
end | ||
|
||
it 'reflects entries passed in' do | ||
entries = { | ||
foo_key => foo_label, | ||
bar_key => bar_label | ||
} | ||
|
||
ctx = CorrelationContext.new(entries: entries) | ||
|
||
_(ctx.entries).must_equal(entries) | ||
end | ||
|
||
it 'removes keys specified' do | ||
entries = { | ||
foo_key => foo_label, | ||
bar_key => bar_label | ||
} | ||
|
||
ctx = CorrelationContext.new(entries: entries, remove_keys: [bar_key]) | ||
|
||
_(ctx.entries).must_equal(foo_key => foo_label) | ||
end | ||
|
||
describe 'with parent' do | ||
let(:parent_ctx) do | ||
CorrelationContext.new(entries: { baz_key => baz_label }) | ||
end | ||
|
||
it 'inherits entries' do | ||
ctx = CorrelationContext.new(parent: parent_ctx) | ||
_(ctx.entries).must_equal(parent_ctx.entries) | ||
end | ||
|
||
it 'merges entries' do | ||
ctx = CorrelationContext.new( | ||
parent: parent_ctx, | ||
entries: { | ||
foo_key => foo_label, | ||
bar_key => bar_label | ||
} | ||
) | ||
_(ctx.entries).must_equal( | ||
foo_key => foo_label, | ||
bar_key => bar_label, | ||
baz_key => baz_label | ||
) | ||
end | ||
|
||
it 'replaces entries on merge' do | ||
ctx = CorrelationContext.new( | ||
parent: parent_ctx, | ||
entries: { | ||
foo_key => foo_label, | ||
baz_key => bar_label | ||
} | ||
) | ||
_(ctx.entries).must_equal( | ||
foo_key => foo_label, | ||
baz_key => bar_label | ||
) | ||
end | ||
|
||
it 'removes keys specified' do | ||
ctx = CorrelationContext.new( | ||
parent: parent_ctx, | ||
entries: { | ||
foo_key => foo_label, | ||
bar_key => bar_label | ||
}, | ||
remove_keys: [baz_key] | ||
) | ||
_(ctx.entries).must_equal( | ||
foo_key => foo_label, | ||
bar_key => bar_label | ||
) | ||
end | ||
end | ||
end | ||
end |