Skip to content

Commit

Permalink
return a non-recording span without calling @sampler.should_sample
Browse files Browse the repository at this point in the history
  • Loading branch information
yoheyk committed Apr 18, 2024
1 parent 96ac5f6 commit 5f9b4a2
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
11 changes: 8 additions & 3 deletions sdk/lib/opentelemetry/sdk/trace/tracer_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,19 +126,24 @@ def add_span_processor(span_processor)
end

# @api private
def internal_start_span(name, kind, attributes, links, start_timestamp, parent_context, instrumentation_scope) # rubocop:disable Metrics/MethodLength, Metrics/PerceivedComplexity
def internal_start_span(name, kind, attributes, links, start_timestamp, parent_context, instrumentation_scope) # rubocop:disable Metrics/CyclomaticComplexity, Metrics/MethodLength, Metrics/PerceivedComplexity
parent_span = OpenTelemetry::Trace.current_span(parent_context)
parent_span_context = parent_span.context

if parent_span_context.valid?
parent_span_id = parent_span_context.span_id
trace_id = parent_span_context.trace_id
end

trace_id ||= @id_generator.generate_trace_id

if OpenTelemetry::Common::Utilities.untraced?(parent_context)
span_id = parent_span_id || @id_generator.generate_span_id
return OpenTelemetry::Trace.non_recording_span(OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id, span_id: span_id))
end

result = @sampler.should_sample?(trace_id: trace_id, parent_context: parent_context, links: links, name: name, kind: kind, attributes: attributes)
span_id = @id_generator.generate_span_id
if !OpenTelemetry::Common::Utilities.untraced?(parent_context) && result.recording? && !@stopped
if result.recording? && !@stopped
trace_flags = result.sampled? ? OpenTelemetry::Trace::TraceFlags::SAMPLED : OpenTelemetry::Trace::TraceFlags::DEFAULT
context = OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id, span_id: span_id, trace_flags: trace_flags, tracestate: result.tracestate)
attributes = attributes&.merge(result.attributes) || result.attributes.dup
Expand Down
4 changes: 3 additions & 1 deletion sdk/test/opentelemetry/sdk/trace/tracer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -332,17 +332,19 @@

it 'yields a no-op span within an untraced block' do
tracer.in_span('root') do
span_id = OpenTelemetry::Trace.current_span.context.span_id
OpenTelemetry::Common::Utilities.untraced do
tracer.in_span('op') do |span|
_(span).must_be_instance_of(OpenTelemetry::Trace::Span)
_(span.context.span_id).must_equal span_id
_(span.context.trace_flags).wont_be :sampled?
_(span).wont_be :recording?
end
end
end
end

it 'does not log "Calling finish on an ended Span" warnings' do
it 'does not log "Calling finish on an ended Span" warnings when untraced? is called' do
OpenTelemetry::TestHelpers.with_test_logger do |log_stream|
tracer.in_span('root') do
OpenTelemetry::Common::Utilities.untraced { tracer.in_span('op') {} }
Expand Down

0 comments on commit 5f9b4a2

Please sign in to comment.