-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b82849
commit be451b2
Showing
5 changed files
with
223 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
37 changes: 37 additions & 0 deletions
37
sampling/hierarchical/lib/opentelemetry/sampling/hierarchical/sampler.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,37 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
module OpenTelemetry | ||
module Sampling | ||
module Hierarchical | ||
class Sampler | ||
# @param [Array<OpenTelemetry::SDK::Trace::Samplers>] samplers | ||
def initialize(*samplers) | ||
@samplers = samplers | ||
end | ||
|
||
# @param [String] trace_id | ||
# @param [OpenTelemetry::Context] parent_context | ||
# @param [Enumerable<Link>] links | ||
# @param [String] name | ||
# @param [Symbol] kind | ||
# @param [Hash<String, Object>] attributes | ||
# @return [OpenTelemetry::SDK::Trace::Samplers::Result] The sampling result. | ||
def should_sample?(trace_id:, parent_context:, links:, name:, kind:, attributes:) | ||
@samplers.each do |sampler| | ||
result = sampler.should_sample?(trace_id: trace_id, parent_context: parent_context, links: links, name: name, kind: kind, attributes: attributes) | ||
return result if result.sampled? | ||
end | ||
|
||
OpenTelemetry::SDK::Trace::Samplers::Result.new( | ||
decision: OpenTelemetry::SDK::Trace::Samplers::Decision::DROP, | ||
tracestate: OpenTelemetry::Trace.current_span(parent_context).context.tracestate | ||
) | ||
end | ||
end | ||
end | ||
end | ||
end |
168 changes: 168 additions & 0 deletions
168
sampling/hierarchical/test/opentelemetry/sampling/hierarchical/sampler_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,168 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require('test_helper') | ||
|
||
describe(OpenTelemetry::Sampling::Hierarchical::Sampler) do | ||
describe('#should_sample?') do | ||
it('returns DROP if it has no samplers') do | ||
sampler = OpenTelemetry::Sampling::Hierarchical::Sampler.new([]) | ||
|
||
_( | ||
sampler.should_sample?( | ||
trace_id: SecureRandom.uuid.to_s, | ||
parent_context: nil, | ||
links: [], | ||
name: SecureRandom.uuid.to_s, | ||
kind: :internal, | ||
attributes: {} | ||
).sampled? | ||
).must_equal(false) | ||
end | ||
|
||
it('returns RECORD_AND_SAMPLE if the first sampler returns RECORD_AND_SAMPLE') do | ||
trace_id = SecureRandom.uuid.to_s | ||
name = SecureRandom.uuid.to_s | ||
|
||
first_sampler = Minitest::Mock.new | ||
second_sampler = Minitest::Mock.new | ||
|
||
first_sampler.expect( | ||
:should_sample?, | ||
OpenTelemetry::SDK::Trace::Samplers::Result.new( | ||
decision: OpenTelemetry::SDK::Trace::Samplers::Decision::RECORD_AND_SAMPLE, | ||
tracestate: OpenTelemetry::Trace::Tracestate.from_hash({}) | ||
), | ||
[], | ||
trace_id: trace_id, | ||
parent_context: nil, | ||
links: [], | ||
name: name, | ||
kind: :internal, | ||
attributes: {} | ||
) | ||
|
||
sampler = OpenTelemetry::Sampling::Hierarchical::Sampler.new([first_sampler, second_sampler]) | ||
_( | ||
sampler.should_sample?( | ||
trace_id: trace_id, | ||
parent_context: nil, | ||
links: [], | ||
name: name, | ||
kind: :internal, | ||
attributes: {} | ||
).sampled? | ||
).must_equal(true) | ||
|
||
first_sampler.verify | ||
second_sampler.verify | ||
end | ||
|
||
it('returns RECORD_AND_SAMPLE if the second sampler returns RECORD_AND_SAMPLE') do | ||
trace_id = SecureRandom.uuid.to_s | ||
name = SecureRandom.uuid.to_s | ||
|
||
first_sampler = Minitest::Mock.new | ||
second_sampler = Minitest::Mock.new | ||
|
||
first_sampler.expect( | ||
:should_sample?, | ||
OpenTelemetry::SDK::Trace::Samplers::Result.new( | ||
decision: OpenTelemetry::SDK::Trace::Samplers::Decision::DROP, | ||
tracestate: OpenTelemetry::Trace::Tracestate.from_hash({}) | ||
), | ||
[], | ||
trace_id: trace_id, | ||
parent_context: nil, | ||
links: [], | ||
name: name, | ||
kind: :internal, | ||
attributes: {} | ||
) | ||
second_sampler.expect( | ||
:should_sample?, | ||
OpenTelemetry::SDK::Trace::Samplers::Result.new( | ||
decision: OpenTelemetry::SDK::Trace::Samplers::Decision::RECORD_AND_SAMPLE, | ||
tracestate: OpenTelemetry::Trace::Tracestate.from_hash({}) | ||
), | ||
[], | ||
trace_id: trace_id, | ||
parent_context: nil, | ||
links: [], | ||
name: name, | ||
kind: :internal, | ||
attributes: {} | ||
) | ||
|
||
sampler = OpenTelemetry::Sampling::Hierarchical::Sampler.new([first_sampler, second_sampler]) | ||
_( | ||
sampler.should_sample?( | ||
trace_id: trace_id, | ||
parent_context: nil, | ||
links: [], | ||
name: name, | ||
kind: :internal, | ||
attributes: {} | ||
).sampled? | ||
).must_equal(true) | ||
|
||
first_sampler.verify | ||
second_sampler.verify | ||
end | ||
|
||
it('returns DROP if both samplers return DROP') do | ||
trace_id = SecureRandom.uuid.to_s | ||
name = SecureRandom.uuid.to_s | ||
|
||
first_sampler = Minitest::Mock.new | ||
second_sampler = Minitest::Mock.new | ||
|
||
first_sampler.expect( | ||
:should_sample?, | ||
OpenTelemetry::SDK::Trace::Samplers::Result.new( | ||
decision: OpenTelemetry::SDK::Trace::Samplers::Decision::DROP, | ||
tracestate: OpenTelemetry::Trace::Tracestate.from_hash({}) | ||
), | ||
[], | ||
trace_id: trace_id, | ||
parent_context: nil, | ||
links: [], | ||
name: name, | ||
kind: :internal, | ||
attributes: {} | ||
) | ||
second_sampler.expect( | ||
:should_sample?, | ||
OpenTelemetry::SDK::Trace::Samplers::Result.new( | ||
decision: OpenTelemetry::SDK::Trace::Samplers::Decision::DROP, | ||
tracestate: OpenTelemetry::Trace::Tracestate.from_hash({}) | ||
), | ||
[], | ||
trace_id: trace_id, | ||
parent_context: nil, | ||
links: [], | ||
name: name, | ||
kind: :internal, | ||
attributes: {} | ||
) | ||
|
||
sampler = OpenTelemetry::Sampling::Hierarchical::Sampler.new([first_sampler, second_sampler]) | ||
_( | ||
sampler.should_sample?( | ||
trace_id: trace_id, | ||
parent_context: nil, | ||
links: [], | ||
name: name, | ||
kind: :internal, | ||
attributes: {} | ||
).sampled? | ||
).must_equal(false) | ||
|
||
first_sampler.verify | ||
second_sampler.verify | ||
end | ||
end | ||
end |
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,17 @@ | ||
# frozen_string_literal: true | ||
|
||
# Copyright The OpenTelemetry Authors | ||
# | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
require('bundler/setup') | ||
Bundler.require(:default, :development, :test) | ||
|
||
SimpleCov.minimum_coverage(85) | ||
SimpleCov.start | ||
|
||
require('opentelemetry-sampling-hierarchical') | ||
require('minitest/autorun') | ||
require('webmock/minitest') | ||
|
||
OpenTelemetry.logger = Logger.new($stderr, level: ENV.fetch('OTEL_LOG_LEVEL', 'error').to_sym) |
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