diff --git a/lib/traces/backend/open_telemetry/interface.rb b/lib/traces/backend/open_telemetry/interface.rb index 85d105b..1ca6d1f 100644 --- a/lib/traces/backend/open_telemetry/interface.rb +++ b/lib/traces/backend/open_telemetry/interface.rb @@ -16,7 +16,7 @@ module OpenTelemetry TRACER = ::OpenTelemetry.tracer_provider.tracer(Traces::Backend::OpenTelemetry.name, Traces::Backend::OpenTelemetry::VERSION) module Interface - def trace(name, attributes: nil, &block) + def trace(name, attributes: {}, &block) span = TRACER.start_span(name, attributes: attributes.transform_keys(&:to_s)) begin @@ -27,7 +27,7 @@ def trace(name, attributes: nil, &block) end rescue Exception => error span&.record_exception(error) - span&.status = ::OpenTelemetry::Traces::Status.error("Unhandled exception of type: #{error.class}") + span&.status = ::OpenTelemetry::Trace::Status.error("Unhandled exception of type: #{error.class}") raise ensure span&.finish @@ -35,27 +35,33 @@ def trace(name, attributes: nil, &block) end def trace_context=(context) - span_context = ::OpenTelemetry::Traces::SpanContext.new( + span_context = ::OpenTelemetry::Trace::SpanContext.new( trace_id: context.trace_id, span_id: context.parent_id, - trace_flags: ::OpenTelemetry::Traces::TracesFlags.from_byte(context.flags), + trace_flags: ::OpenTelemetry::Trace::TraceFlags.from_byte(context.flags), tracestate: context.state, remote: context.remote? ) span = ::OpenTelemetry::Trace.non_recording_span(span_context) - + # the span seems to be updated correctly, and evaluating line 49 seems to propagate the trace/span ids + # but its not updated in my span return ::OpenTelemetry::Trace.context_with_span(span) end def trace_context(span = ::OpenTelemetry::Trace.current_span) + # state = baggage.values(context: span.context) if span_context = span.context - state = baggage.values(context: span.context) + flags = 0 + + if span_context.trace_flags.sampled? + flags |= Context::SAMPLED + end return Context.new( span_context.trace_id, span_context.span_id, - span_context.trace_flags, + flags, span_context.tracestate, remote: span_context.remote? ) diff --git a/test/traces/backend/open_telemetry.rb b/test/traces/backend/open_telemetry.rb index 4ae5826..796edf4 100644 --- a/test/traces/backend/open_telemetry.rb +++ b/test/traces/backend/open_telemetry.rb @@ -14,18 +14,68 @@ def my_method(argument) def my_method(argument) Traces.trace("my_method", attributes: {argument: argument}) {super} end + + def my_span + Traces.trace('my_span') {|span| return span} + end + + def my_context + Traces.trace('my_context') {|span| return Traces.trace_context} + end + + def my_span_and_context + Traces.trace('my_span_and_context') {|span| return span, Traces.trace_context} + end end describe Traces::Backend::OpenTelemetry do + let(:instance) { MyClass.new } + it "has a version number" do expect(Traces::Backend::OpenTelemetry::VERSION).not.to be == nil end it "can invoke trace wrapper" do - instance = MyClass.new - expect(Traces::Backend::OpenTelemetry::TRACER).to receive(:start_span) instance.my_method(10) end + + describe OpenTelemetry::Trace::Span do + it "can yield an open telemetry span" do + expect(instance.my_span).to be_a(OpenTelemetry::Trace::Span) + end + end + + describe "span and context" do + let(:span_and_context) {instance.my_span_and_context} + let(:span) {span_and_context.first} + let(:context) {span_and_context.last} + let(:new_context) { + Traces::Context.new( + SecureRandom.uuid, + SecureRandom.uuid, + 0, + nil, + remote: false + ) + } + + it "can provide a trace context with a trace_id" do + expect(context).to have_attributes(trace_id: be != nil) + expect(span.context.trace_id).to be == context.trace_id + end + + describe "#trace_context=" do + it "can update the trace context" do + expect(Traces.trace_context).to be != new_context + + Traces.trace_context = new_context + expect(instance.my_span.context).to have_attributes( + trace_id: be == new_context.trace_id, + span_id: be == new_context.parent_id + ) + end + end + end end