Sha256: 6913d62bff3bcd377be411a4f10882206143ce4ab7ad969b12b986540e07d529

Contents?: true

Size: 1.84 KB

Versions: 7

Compression:

Stored size: 1.84 KB

Contents

# encoding: utf-8
# This file is distributed under New Relic's license terms.
# See https://github.com/newrelic/newrelic-ruby-agent/blob/main/LICENSE for complete details.
# frozen_string_literal: true

module NewRelic::Agent::Instrumentation
  module Redis
    PRODUCT_NAME = 'Redis'
    CONNECT = 'connect'
    UNKNOWN = "unknown"
    LOCALHOST = "localhost"
    MULTI_OPERATION = 'multi'
    PIPELINE_OPERATION = 'pipeline'

    def call_with_tracing command, &block
      operation = command[0]
      statement = ::NewRelic::Agent::Datastores::Redis.format_command(command)

      with_tracing(operation, statement) { yield }
    end

    def call_pipeline_with_tracing pipeline
      operation = pipeline.is_a?(::Redis::Pipeline::Multi) ? MULTI_OPERATION : PIPELINE_OPERATION
      statement = ::NewRelic::Agent::Datastores::Redis.format_pipeline_commands(pipeline.commands)

      with_tracing(operation, statement) { yield }
    end

    def connect_with_tracing
      with_tracing(CONNECT) { yield }
    end

    private

    def with_tracing operation, statement = nil
      segment = NewRelic::Agent::Tracer.start_datastore_segment(
        product: PRODUCT_NAME,
        operation: operation,
        host: _nr_hostname,
        port_path_or_id: _nr_port_path_or_id,
        database_name: db
      )
      begin
        segment.notice_nosql_statement(statement) if statement
        NewRelic::Agent::Tracer.capture_segment_error(segment) { yield }
      ensure
        segment.finish if segment
      end
    end

    def _nr_hostname
      self.path ? LOCALHOST : self.host
    rescue => e
      NewRelic::Agent.logger.debug "Failed to retrieve Redis host: #{e}"
      UNKNOWN
    end

    def _nr_port_path_or_id
      self.path || self.port
    rescue => e
      NewRelic::Agent.logger.debug "Failed to retrieve Redis port_path_or_id: #{e}"
      UNKNOWN
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
newrelic_rpm-8.9.0 lib/new_relic/agent/instrumentation/redis/instrumentation.rb
newrelic_rpm-8.8.0 lib/new_relic/agent/instrumentation/redis/instrumentation.rb
newrelic_rpm-8.7.0 lib/new_relic/agent/instrumentation/redis/instrumentation.rb
newrelic_rpm-8.6.0 lib/new_relic/agent/instrumentation/redis/instrumentation.rb
newrelic_rpm-8.5.0 lib/new_relic/agent/instrumentation/redis/instrumentation.rb
newrelic_rpm-8.4.0 lib/new_relic/agent/instrumentation/redis/instrumentation.rb
newrelic_rpm-8.3.0 lib/new_relic/agent/instrumentation/redis/instrumentation.rb