Sha256: e5c39e01ffd946c95abf01237c88cbb7d312e812d4ad35cb51e1d0338ff02cd5

Contents?: true

Size: 1.9 KB

Versions: 7

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

# Copyright The OpenTelemetry Authors
#
# SPDX-License-Identifier: Apache-2.0

module OpenTelemetry
  module Instrumentation
    module Sidekiq
      module Middlewares
        module Client
          # TracerMiddleware propagates context and instruments Sidekiq client
          # by way of its middleware system
          class TracerMiddleware
            include ::Sidekiq::ClientMiddleware if defined?(::Sidekiq::ClientMiddleware)

            def call(_worker_class, job, _queue, _redis_pool)
              attributes = {
                SemanticConventions::Trace::MESSAGING_SYSTEM => 'sidekiq',
                'messaging.sidekiq.job_class' => job['wrapped']&.to_s || job['class'],
                SemanticConventions::Trace::MESSAGING_MESSAGE_ID => job['jid'],
                SemanticConventions::Trace::MESSAGING_DESTINATION => job['queue'],
                SemanticConventions::Trace::MESSAGING_DESTINATION_KIND => 'queue'
              }
              attributes[SemanticConventions::Trace::PEER_SERVICE] = instrumentation_config[:peer_service] if instrumentation_config[:peer_service]

              span_name = case instrumentation_config[:span_naming]
                          when :job_class then "#{job['wrapped']&.to_s || job['class']} publish"
                          else "#{job['queue']} publish"
                          end

              tracer.in_span(span_name, attributes: attributes, kind: :producer) do |span|
                OpenTelemetry.propagation.inject(job)
                span.add_event('created_at', timestamp: job['created_at'])
                yield
              end
            end

            private

            def instrumentation_config
              Sidekiq::Instrumentation.instance.config
            end

            def tracer
              Sidekiq::Instrumentation.instance.tracer
            end
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
opentelemetry-instrumentation-sidekiq-0.25.7 lib/opentelemetry/instrumentation/sidekiq/middlewares/client/tracer_middleware.rb
opentelemetry-instrumentation-sidekiq-0.25.6 lib/opentelemetry/instrumentation/sidekiq/middlewares/client/tracer_middleware.rb
opentelemetry-instrumentation-sidekiq-0.25.5 lib/opentelemetry/instrumentation/sidekiq/middlewares/client/tracer_middleware.rb
opentelemetry-instrumentation-sidekiq-0.25.3 lib/opentelemetry/instrumentation/sidekiq/middlewares/client/tracer_middleware.rb
opentelemetry-instrumentation-sidekiq-0.25.2 lib/opentelemetry/instrumentation/sidekiq/middlewares/client/tracer_middleware.rb
opentelemetry-instrumentation-sidekiq-0.25.1 lib/opentelemetry/instrumentation/sidekiq/middlewares/client/tracer_middleware.rb
opentelemetry-instrumentation-sidekiq-0.25.0 lib/opentelemetry/instrumentation/sidekiq/middlewares/client/tracer_middleware.rb