Sha256: 44eaa365b530225321ec59bd9317de795081ff70ea91977e076824583583f36d

Contents?: true

Size: 1.09 KB

Versions: 2

Compression:

Stored size: 1.09 KB

Contents

# frozen_string_literal: true

module Sidekiq
  module Tracer
    class ClientMiddleware
      include Commons

      attr_reader :tracer, :active_span

      def initialize(tracer, active_span)
        @tracer = tracer
        @active_span = active_span
      end

      def call(_worker_class, job, _queue, _redis_pool)
        span = build_span(job)

        inject(span, job)

        yield
      rescue StandardError => e
        tag_errors(span, e) if span
        raise
      ensure
        span&.finish
      end

      private

      def build_span(job)
        tracer.start_span(operation_name(job),
                          child_of: active_span.respond_to?(:call) ? active_span.call : active_span,
                          tags: tags(job, "producer"))
      end

      def tag_errors(span, error)
        span.set_tag("error", true)
        span.log_kv(**{ event: "error", 'error.object': error })
      end

      def inject(span, job)
        carrier = {}
        tracer.inject(span.context, OpenTracing::FORMAT_TEXT_MAP, carrier)
        job[TRACE_CONTEXT_KEY] = carrier
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sidekiq-instrumentation-2.1.0 lib/sidekiq/tracer/client_middleware.rb
sidekiq-instrumentation-2.1.0.pre3 lib/sidekiq/tracer/client_middleware.rb