Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Tracer.non_recording_span to API #799

Merged
merged 8 commits into from
Jun 8, 2021
6 changes: 4 additions & 2 deletions api/benchmarks/span_bench.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
# SPDX-License-Identifier: Apache-2.0

require 'benchmark/ipsa'
require 'opentelemetry'
require 'opentelemetry/sdk'

span = OpenTelemetry::Trace::Span.new
OpenTelemetry::SDK.configure
tracer = OpenTelemetry.tracer_provider.tracer('bench')
span = tracer.start_root_span('bench')

attributes = {
'component' => 'rack',
Expand Down
10 changes: 10 additions & 0 deletions api/lib/opentelemetry/trace.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,16 @@ def context_with_span(span, parent_context: Context.current)
def with_span(span)
Context.with_value(CURRENT_SPAN_KEY, span) { |c, s| yield s, c }
end

# Wraps a SpanContext with an object implementing the Span interface. This is done in order
# to expose a SpanContext as a Span in operations such as in-process Span propagation.
#
# @param [SpanContext] span_context SpanContext to be wrapped
#
# @return [Span]
def non_recording_span(span_context)
Span.new(span_context: span_context)
end
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module OpenTelemetry
module Trace
module Propagation
module TraceContext
# Propagates baggage using the W3C Trace Context format
# Propagates trace context using the W3C Trace Context format
class TextMapPropagator
TRACEPARENT_KEY = 'traceparent'
TRACESTATE_KEY = 'tracestate'
Expand Down Expand Up @@ -53,7 +53,7 @@ def extract(carrier, context: Context.current, getter: Context::Propagation.text
trace_flags: tp.flags,
tracestate: tracestate,
remote: true)
span = Trace::Span.new(span_context: span_context)
span = OpenTelemetry::Trace.non_recording_span(span_context)
OpenTelemetry::Trace.context_with_span(span)
rescue OpenTelemetry::Error
context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
let(:context_with_tracestate) do
span_context = SpanContext.new(trace_id: ("\xff" * 16).b, span_id: ("\x11" * 8).b,
tracestate: tracestate_header)
span = Span.new(span_context: span_context)
span = OpenTelemetry::Trace.non_recording_span(span_context)
OpenTelemetry::Trace.context_with_span(span, parent_context: Context.empty)
end
let(:context_without_tracestate) do
span_context = SpanContext.new(trace_id: ("\xff" * 16).b, span_id: ("\x11" * 8).b)
span = Span.new(span_context: span_context)
span = OpenTelemetry::Trace.non_recording_span(span_context)
OpenTelemetry::Trace.context_with_span(span, parent_context: Context.empty)
end

Expand Down
2 changes: 1 addition & 1 deletion api/test/opentelemetry/trace/tracer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def start_span(*)
let(:parent_span_context) { OpenTelemetry::Trace::SpanContext.new }
let(:parent_context) do
OpenTelemetry::Trace.context_with_span(
OpenTelemetry::Trace::Span.new(span_context: parent_span_context),
OpenTelemetry::Trace.non_recording_span(parent_span_context),
parent_context: OpenTelemetry::Context.empty
)
end
Expand Down
2 changes: 1 addition & 1 deletion api/test/opentelemetry/trace_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
end

it 'returns the current span from the provided context' do
span = OpenTelemetry::Trace::Span.new(span_context: OpenTelemetry::Trace::SpanContext.new)
span = OpenTelemetry::Trace.non_recording_span(OpenTelemetry::Trace::SpanContext.new)
context = OpenTelemetry::Trace.context_with_span(span, parent_context: Context.empty)
_(OpenTelemetry::Trace.current_span).wont_equal(span)
_(OpenTelemetry::Trace.current_span(context)).must_equal(span)
Expand Down
32 changes: 16 additions & 16 deletions api/test/opentelemetry_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@
require 'tempfile'

describe OpenTelemetry do
class CustomSpan < OpenTelemetry::Trace::Span
end

class CustomTracer < OpenTelemetry::Trace::Tracer
def start_root_span(*)
CustomSpan.new
end
end

class CustomTracerProvider < OpenTelemetry::Trace::TracerProvider
def tracer(name = nil, version = nil)
CustomTracer.new
end
end

describe '.tracer_provider' do
after do
# Ensure we don't leak custom tracer factories and tracers to other tests
Expand All @@ -24,27 +39,12 @@
end

it 'returns user specified tracer provider' do
custom_tracer_provider = 'a custom tracer provider'
custom_tracer_provider = CustomTracerProvider.new
OpenTelemetry.tracer_provider = custom_tracer_provider
_(OpenTelemetry.tracer_provider).must_equal(custom_tracer_provider)
end
end

class CustomSpan < OpenTelemetry::Trace::Span
end

class CustomTracer < OpenTelemetry::Trace::Tracer
def start_root_span(*)
CustomSpan.new
end
end

class CustomTracerProvider < OpenTelemetry::Trace::TracerProvider
def tracer(name = nil, version = nil)
CustomTracer.new
end
end

describe '.tracer_provider=' do
after do
# Ensure we don't leak custom tracer factories and tracers to other tests
Expand Down
2 changes: 1 addition & 1 deletion common/lib/opentelemetry/common/utilities.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def truncate(string, size)
end

def untraced
OpenTelemetry::Trace.with_span(OpenTelemetry::Trace::Span.new) { yield }
OpenTelemetry::Trace.with_span(OpenTelemetry::Trace.non_recording_span(OpenTelemetry::Trace::SpanContext.new)) { yield }
end

# Returns a URL string with userinfo removed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

describe OpenTelemetry::Instrumentation::Rack do
let(:instrumentation) { OpenTelemetry::Instrumentation::Rack::Instrumentation.instance }
let(:new_span) { OpenTelemetry::Trace.non_recording_span(OpenTelemetry::Trace::SpanContext.new) }

it 'has #name' do
_(instrumentation.name).must_equal 'OpenTelemetry::Instrumentation::Rack'
Expand All @@ -32,16 +33,16 @@
end

it 'returns the span when set' do
test_span = OpenTelemetry::Trace::Span.new
test_span = new_span
context = OpenTelemetry::Instrumentation::Rack.context_with_span(test_span)
_(OpenTelemetry::Instrumentation::Rack.current_span(context)).must_equal(test_span)
end
end

describe '#with_span' do
it 'respects context nesting' do
test_span = OpenTelemetry::Trace::Span.new
test_span2 = OpenTelemetry::Trace::Span.new
test_span = new_span
test_span2 = new_span
OpenTelemetry::Instrumentation::Rack.with_span(test_span) do
_(OpenTelemetry::Instrumentation::Rack.current_span).must_equal(test_span)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def extracted_context(trace_id_hex, span_id_hex, sampled, debug, context)
remote: true
)

span = Trace::Span.new(span_context: span_context)
span = OpenTelemetry::Trace.non_recording_span(span_context)
context = B3.context_with_debug(context) if debug
Trace.context_with_span(span, parent_context: context)
end
Expand Down
4 changes: 2 additions & 2 deletions propagator/b3/test/multi/text_map_propagator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -199,8 +199,8 @@ def create_context(trace_id:,
trace_flags: OpenTelemetry::Trace::TraceFlags::DEFAULT,
b3_debug: false)
context = OpenTelemetry::Trace.context_with_span(
OpenTelemetry::Trace::Span.new(
span_context: OpenTelemetry::Trace::SpanContext.new(
OpenTelemetry::Trace.non_recording_span(
OpenTelemetry::Trace::SpanContext.new(
trace_id: Array(trace_id).pack('H*'),
span_id: Array(span_id).pack('H*'),
trace_flags: trace_flags
Expand Down
4 changes: 2 additions & 2 deletions propagator/b3/test/single/text_map_propagator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ def create_context(trace_id:,
trace_flags: OpenTelemetry::Trace::TraceFlags::DEFAULT,
b3_debug: false)
context = OpenTelemetry::Trace.context_with_span(
OpenTelemetry::Trace::Span.new(
span_context: OpenTelemetry::Trace::SpanContext.new(
OpenTelemetry::Trace.non_recording_span(
OpenTelemetry::Trace::SpanContext.new(
trace_id: Array(trace_id).pack('H*'),
span_id: Array(span_id).pack('H*'),
trace_flags: trace_flags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def build_span(match, sampling_flags)
trace_flags: to_trace_flags(sampling_flags),
remote: true
)
Trace::Span.new(span_context: span_context)
OpenTelemetry::Trace.non_recording_span(span_context)
end

def context_with_extracted_baggage(carrier, context, getter)
Expand Down
4 changes: 2 additions & 2 deletions propagator/jaeger/test/text_map_propagator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ def create_context(trace_id:,
trace_flags: OpenTelemetry::Trace::TraceFlags::DEFAULT,
jaeger_debug: false)
context = OpenTelemetry::Trace.context_with_span(
OpenTelemetry::Trace::Span.new(
span_context: OpenTelemetry::Trace::SpanContext.new(
OpenTelemetry::Trace.non_recording_span(
OpenTelemetry::Trace::SpanContext.new(
trace_id: Array(trace_id).pack('H*'),
span_id: Array(span_id).pack('H*'),
trace_flags: trace_flags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def extract(carrier, context: Context.current, getter: Context::Propagation.text
remote: true
)

span = Trace::Span.new(span_context: span_context)
span = OpenTelemetry::Trace.non_recording_span(span_context)
Trace.context_with_span(span, parent_context: set_baggage(carrier: carrier, context: context, getter: getter))
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def set(carrier, key, value)

let(:context) do
OpenTelemetry::Trace.context_with_span(
OpenTelemetry::Trace::Span.new(
span_context: OpenTelemetry::Trace::SpanContext.new(
OpenTelemetry::Trace.non_recording_span(
OpenTelemetry::Trace::SpanContext.new(
trace_id: Array(trace_id).pack('H*'),
span_id: Array(span_id).pack('H*'),
trace_flags: trace_flags
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def extract(carrier, context: Context.current, getter: Context::Propagation.text
remote: true
)

span = Trace::Span.new(span_context: span_context)
span = OpenTelemetry::Trace.non_recording_span(span_context)
context = XRay.context_with_debug(context) if match['sampling_state'] == 'd'
Trace.context_with_span(span, parent_context: context)
rescue OpenTelemetry::Error
Expand Down
4 changes: 2 additions & 2 deletions propagator/xray/test/text_map_propagator_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ def create_context(trace_id:,
trace_flags: TraceFlags::DEFAULT,
xray_debug: false)
context = OpenTelemetry::Trace.context_with_span(
Span.new(
span_context: SpanContext.new(
OpenTelemetry::Trace.non_recording_span(
SpanContext.new(
trace_id: Array(trace_id).pack('H*'),
span_id: Array(span_id).pack('H*'),
trace_flags: trace_flags
Expand Down
15 changes: 8 additions & 7 deletions sdk/lib/opentelemetry/sdk/trace/export/multi_span_exporter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module SDK
module Trace
module Export
# Implementation of the SpanExporter duck type that simply forwards all
# received spans to a collection of SpanExporters.
# received SpanDatas to a collection of SpanExporters.
#
# Can be used to export to multiple backends using the same
# SpanProcessor like a {SimpleSpanProcessor} or a
Expand All @@ -19,9 +19,10 @@ def initialize(span_exporters)
@span_exporters = span_exporters.clone.freeze
end

# Called to export sampled {Span}s.
# Called to export sampled {SpanData} structs.
#
# @param [Enumerable<Span>] spans the list of sampled {Span}s to be
# @param [Enumerable<SpanData>] span_data the
# list of recorded {SpanData} structs to be
# exported.
# @param [optional Numeric] timeout An optional timeout in seconds.
# @return [Integer] the result of the export.
Expand All @@ -44,11 +45,11 @@ def export(spans, timeout: nil)
# non-specific failure occurred, TIMEOUT if a timeout occurred.
def force_flush(timeout: nil)
start_time = OpenTelemetry::Common::Utilities.timeout_timestamp
results = @span_exporters.map do |processor|
results = @span_exporters.map do |span_exporter|
remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time)
return TIMEOUT if remaining_timeout&.zero?

processor.force_flush(timeout: remaining_timeout)
span_exporter.force_flush(timeout: remaining_timeout)
end
results.uniq.max || SUCCESS
end
Expand All @@ -61,11 +62,11 @@ def force_flush(timeout: nil)
# non-specific failure occurred, TIMEOUT if a timeout occurred.
def shutdown(timeout: nil)
start_time = OpenTelemetry::Common::Utilities.timeout_timestamp
results = @span_exporters.map do |processor|
results = @span_exporters.map do |span_exporter|
remaining_timeout = OpenTelemetry::Common::Utilities.maybe_timeout(timeout, start_time)
return TIMEOUT if remaining_timeout&.zero?

processor.shutdown(timeout: remaining_timeout)
span_exporter.shutdown(timeout: remaining_timeout)
end
results.uniq.max || SUCCESS
end
Expand Down
2 changes: 1 addition & 1 deletion sdk/lib/opentelemetry/sdk/trace/tracer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def internal_create_span(result, name, kind, trace_id, parent_span_id, attribute
@instrumentation_library
)
else
OpenTelemetry::Trace::Span.new(span_context: OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id, span_id: span_id, tracestate: result.tracestate))
OpenTelemetry::Trace.non_recording_span(OpenTelemetry::Trace::SpanContext.new(trace_id: trace_id, span_id: span_id, tracestate: result.tracestate))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion sdk/test/integration/api_trace_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
describe 'tracing child-of-remote spans' do
let(:context_with_remote_parent) do
OpenTelemetry::Trace.context_with_span(
OpenTelemetry::Trace::Span.new(span_context: remote_span_context),
OpenTelemetry::Trace.non_recording_span(remote_span_context),
parent_context: OpenTelemetry::Context.empty
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
let(:parent_span_context) { OpenTelemetry::Trace::SpanContext.new(tracestate: mock_tracestate, trace_flags: OpenTelemetry::Trace::TraceFlags::SAMPLED) }
let(:parent_context) do
OpenTelemetry::Trace.context_with_span(
OpenTelemetry::Trace::Span.new(span_context: parent_span_context),
OpenTelemetry::Trace.non_recording_span(parent_span_context),
parent_context: OpenTelemetry::Context.empty
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
export = OpenTelemetry::SDK::Trace::Export

let(:captured_stdout) { StringIO.new }
let(:spans) { [OpenTelemetry::Trace::Span.new, OpenTelemetry::Trace::Span.new] }
let(:exporter) { export::ConsoleSpanExporter.new }
let(:span_data1) { OpenTelemetry::SDK::Trace::SpanData.new(name: 'name1') }
let(:span_data2) { OpenTelemetry::SDK::Trace::SpanData.new(name: 'name2') }
let(:spans) { [span_data1, span_data2] }
let(:exporter) { export::ConsoleSpanExporter.new }

before do
@original_stdout = $stdout
Expand All @@ -22,11 +24,11 @@
$stdout = @original_stdout
end

it 'accepts an Array of Spans as arg to #export and succeeds' do
it 'accepts an Array of SpanData as arg to #export and succeeds' do
_(exporter.export(spans)).must_equal export::SUCCESS
end

it 'accepts an Enumerable of Spans as arg to #export and succeeds' do
it 'accepts an Enumerable of SpanData as arg to #export and succeeds' do
fbogsany marked this conversation as resolved.
Show resolved Hide resolved
enumerable = Struct.new(:span0, :span1).new(spans[0], spans[1])

_(exporter.export(enumerable)).must_equal export::SUCCESS
Expand All @@ -35,7 +37,7 @@
it 'outputs to console (stdout)' do
exporter.export(spans)

_(captured_stdout.string).must_match(/#<OpenTelemetry::Trace::Span:/)
_(captured_stdout.string).must_match(/#<struct OpenTelemetry::SDK::Trace::SpanData/)
end

it 'accepts calls to #force_flush' do
Expand Down
Loading