lib/freddy/delivery.rb in freddy-1.1.0 vs lib/freddy/delivery.rb in freddy-1.2.0

- old
+ new

@@ -1,15 +1,14 @@ class Freddy class Delivery - attr_reader :routing_key, :payload, :tag, :trace + attr_reader :routing_key, :payload, :tag def initialize(payload, metadata, routing_key, tag) @payload = payload @metadata = metadata @routing_key = routing_key @tag = tag - @trace = build_trace(metadata.headers || {}) end def correlation_id @metadata.correlation_id end @@ -20,19 +19,27 @@ def reply_to @metadata.reply_to end + def build_trace(operation_name, tags: {}, force_follows_from: false) + carrier = TraceCarrier.new(@metadata) + parent = + if carrier.has_required_fields? && expecting_response? && !force_follows_from + OpenTracing.global_tracer.extract(OpenTracing::FORMAT_TEXT_MAP, carrier) + else + nil + end + + # Creating a child span when the message sender is expecting a response. + # Otherwise creating a new trace because the OpenTracing client does not + # support FollowsFrom yet. + OpenTracing.start_span(operation_name, child_of: parent, tags: tags) + end + private - def build_trace(headers) - if headers['x-trace-id'] && headers['x-span-id'] - Traces::Trace.build_from_existing_trace( - id: headers['x-trace-id'], - parent_id: headers['x-span-id'] - ) - else - Traces::Trace.build - end + def expecting_response? + type == 'request' end end end