Skip to content

Commit f9e8bff

Browse files
committed
Add basic span and context tests.
1 parent fd4adff commit f9e8bff

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

lib/traces/backend/open_telemetry/interface.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,12 +50,16 @@ def trace_context=(context)
5050

5151
def trace_context(span = ::OpenTelemetry::Trace.current_span)
5252
if span_context = span.context
53-
state = baggage.values(context: span.context)
53+
flags = 0
54+
55+
if span_context.trace_flags.sampled?
56+
flags |= Context::SAMPLED
57+
end
5458

5559
return Context.new(
5660
span_context.trace_id,
5761
span_context.span_id,
58-
span_context.trace_flags,
62+
flags,
5963
span_context.tracestate,
6064
remote: span_context.remote?
6165
)

test/traces/backend/open_telemetry.rb

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,23 @@ def my_method_without_attributes(arguments)
2222
def my_method_with_exception
2323
Traces.trace("my_method_with_exception") {raise "Error"}
2424
end
25+
26+
def my_span
27+
Traces.trace('my_span') {|span| return span}
28+
end
29+
30+
def my_context
31+
Traces.trace('my_context') {|span| return Traces.trace_context}
32+
end
33+
34+
def my_span_and_context
35+
Traces.trace('my_span_and_context') {|span| return span, Traces.trace_context}
36+
end
2537
end
2638

2739
describe Traces::Backend::OpenTelemetry do
40+
let(:instance) {MyClass.new}
41+
2842
it "has a version number" do
2943
expect(Traces::Backend::OpenTelemetry::VERSION).not.to be == nil
3044
end
@@ -56,4 +70,30 @@ def my_method_with_exception
5670
instance.my_method_with_exception
5771
end.to raise_exception(RuntimeError, message: be == "Error")
5872
end
73+
74+
describe OpenTelemetry::Trace::Span do
75+
let(:span) {instance.my_span}
76+
77+
it "can assign name" do
78+
span.name = "new_name"
79+
end
80+
81+
it "can assign attributes" do
82+
span["my_key"] = "tag_value"
83+
end
84+
end
85+
86+
describe OpenTelemetry::Trace::SpanContext do
87+
let(:span_and_context) {instance.my_span_and_context}
88+
let(:span) {span_and_context.first}
89+
let(:context) {span_and_context.last}
90+
91+
with '#trace_context' do
92+
it "has a valid trace id" do
93+
expect(context).to have_attributes(
94+
trace_id: be != nil
95+
)
96+
end
97+
end
98+
end
5999
end

0 commit comments

Comments
 (0)