Skip to content
This repository has been archived by the owner on Jan 16, 2025. It is now read-only.

Commit

Permalink
Support sampling.priority tag to control span sampling flag
Browse files Browse the repository at this point in the history
  • Loading branch information
indrekj committed Dec 21, 2018
1 parent 06f40e7 commit 07325b1
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/jaeger/client/span.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ def initialize(context, operation_name, reporter, start_time: Time.now, referenc
@reporter = reporter
@start_time = start_time
@references = references
@tags = tags.map { |key, value| ThriftTagBuilder.build(key, value) }
@tags = []
@logs = []

tags.each { |key, value| set_tag(key, value) }
end

# Set a tag value on this span
Expand All @@ -33,8 +35,21 @@ def initialize(context, operation_name, reporter, start_time: Time.now, referenc
# @param value [String, Numeric, Boolean] the value of the tag. If it's not
# a String, Numeric, or Boolean it will be encoded with to_s
def set_tag(key, value)
if key == 'sampling.priority'
if value.to_i > 0
return self if @context.debug?

@context.flags = @context.flags | SpanContext::Flags::SAMPLED | SpanContext::Flags::DEBUG
else
@context.flags = @context.flags & ~SpanContext::Flags::SAMPLED
end
return self
end

# Using Thrift::Tag to avoid unnecessary memory allocations
@tags << ThriftTagBuilder.build(key, value)

self
end

# Set a baggage item on the span
Expand Down
1 change: 1 addition & 0 deletions lib/jaeger/client/span_context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def self.create_from_parent_context(span_context)
end

attr_reader :span_id, :parent_id, :trace_id, :baggage, :flags
attr_writer :flags

def initialize(span_id:, parent_id: 0, trace_id:, flags:, baggage: {})
@span_id = span_id
Expand Down
19 changes: 19 additions & 0 deletions spec/jaeger/client/span_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,23 @@
span.set_baggage_item('foo', 'baz')
expect(span.get_baggage_item('foo')).to eq('baz')
end

describe '#set_tag' do
let(:span_context) { Jaeger::Client::SpanContext.create_parent_context }
let(:span) { described_class.new(span_context, 'operation_name', nil) }

context 'when sampling.priority' do
it 'sets debug flag to true when sampling.priority is greater than 0' do
span.set_tag('sampling.priority', 1)
expect(span.context.debug?).to eq(true)
expect(span.context.sampled?).to eq(true)
end

it 'sets sampled flag to false when sampling.priority is 0' do
span.set_tag('sampling.priority', 0)
expect(span.context.debug?).to eq(false)
expect(span.context.sampled?).to eq(false)
end
end
end
end

0 comments on commit 07325b1

Please sign in to comment.