Sha256: a0c0907714a1e2320fb9d680c6543742b2b9bac8c067bd0025214d4d005d2369

Contents?: true

Size: 1.85 KB

Versions: 2

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

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

module OpenTelemetry
  module Instrumentation
    module Redis
      module Patches
        # Module to prepend to Redis::Client for instrumentation
        module Client
          def call(*args, &block)
            response = nil

            attributes = client_attributes
            attributes['db.statement'] = Utils.format_statement(args)
            tracer.in_span(
              Utils.format_command(args),
              attributes: attributes,
              kind: :client
            ) do
              response = super(*args, &block)
            end

            response
          end

          def call_pipeline(*args, &block)
            response = nil

            attributes = client_attributes
            attributes['db.statement'] = Utils.format_pipeline_statement(args)
            tracer.in_span(
              'pipeline',
              attributes: attributes,
              kind: :client
            ) do
              response = super(*args, &block)
            end

            response
          end

          private

          def client_attributes
            host = options[:host]
            port = options[:port]

            attributes = {
              'db.system' => 'redis',
              'net.peer.name' => host,
              'net.peer.port' => port
            }
            attributes['db.redis.database_index'] = options[:db] unless options[:db].zero?
            attributes['peer.service'] = config[:peer_service] if config[:peer_service]
            attributes.merge(OpenTelemetry::Instrumentation::Redis.attributes)
          end

          def tracer
            Redis::Instrumentation.instance.tracer
          end

          def config
            Redis::Instrumentation.instance.config
          end
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
opentelemetry-instrumentation-redis-0.17.0 lib/opentelemetry/instrumentation/redis/patches/client.rb
opentelemetry-instrumentation-redis-0.16.0 lib/opentelemetry/instrumentation/redis/patches/client.rb