Sha256: 80ec8bd00453f869dffa3c8ec495a7372ba9d29c16edeb7bb1229f17b964868d

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

class Freddy
  module Producers
    class SendAndForgetProducer
      CONTENT_TYPE = 'application/json'.freeze

      def initialize(channel, logger)
        @logger = logger
        @exchange = channel.default_exchange
        @topic_exchange = channel.topic Freddy::FREDDY_TOPIC_EXCHANGE_NAME
      end

      def produce(destination, payload, properties)
        span = OpenTracing.start_span("freddy:notify:#{destination}",
          child_of: Freddy.trace,
          tags: {
            'message_bus.destination': destination,
            'component': 'freddy',
            'span.kind': 'producer' # Message Bus
          }
        )
        span.log_kv event: 'Sending message', payload: payload

        properties = properties.merge(
          routing_key: destination,
          content_type: CONTENT_TYPE
        )
        OpenTracing.global_tracer.inject(span.context, OpenTracing::FORMAT_TEXT_MAP, TraceCarrier.new(properties))
        json_payload = Payload.dump(payload)

        # Connection adapters handle thread safety for #publish themselves. No
        # need to lock these.
        @topic_exchange.publish json_payload, properties.dup
        @exchange.publish json_payload, properties.dup
      ensure
        # We don't know how many listeners there are and we do not know when
        # this message gets processed. Instead we close the span immediately.
        # Listeners should use FollowsFrom to add trace information.
        # https://github.com/opentracing/specification/blob/master/specification.md
        span.finish
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
freddy-1.3.3 lib/freddy/producers/send_and_forget_producer.rb