Module: Datadog::Tracing::Contrib::Elasticsearch::Patcher::Client

Defined in:
lib/datadog/tracing/contrib/elasticsearch/patcher.rb

Overview

Patches Elasticsearch::Transport::Client module

Instance Method Summary collapse

Instance Method Details

#datadog_configurationObject



140
141
142
# File 'lib/datadog/tracing/contrib/elasticsearch/patcher.rb', line 140

def datadog_configuration
  Datadog.configuration.tracing[:elasticsearch]
end

#perform_request(*args) ⇒ Object

rubocop:disable Metrics/MethodLength rubocop:disable Metrics/AbcSize



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/datadog/tracing/contrib/elasticsearch/patcher.rb', line 36

def perform_request(*args)
  # DEV-2.0: Remove this access, as `Client#self` in this context is not exposed to the user
  # since `elasticsearch` v8.0.0. In contrast, `Client#transport` is always available across
  # all `elasticsearch` gem versions and should be used instead.
  service = Datadog.configuration_for(self, :service_name)

  if service
    SELF_DEPRECATION_ONLY_ONCE.run do
      Datadog.logger.warn(
        'Providing configuration though the Elasticsearch client object is deprecated.' \
        'Configure the `client#transport` object instead: ' \
        'Datadog.configure_onto(client.transport, service_name: service_name, ...)'
      )
    end
  end

  # `Client#transport` is most convenient object both this integration and the library
  # user have shared access to across all `elasticsearch` versions.
  #
  # `Client#self` in this context is an internal object that the library user
  # does not have access to since `elasticsearch` v8.0.0.
  service ||= Datadog.configuration_for(transport, :service_name) || datadog_configuration[:service_name]

  method = args[0]
  path = args[1]
  params = args[2]
  body = args[3]
  full_url = URI.parse(path)
  url = full_url.path
  response = nil

  Tracing.trace(Datadog::Tracing::Contrib::Elasticsearch::Ext::SPAN_QUERY, service: service) do |span|
    begin
      connection = transport.connections.first
      host = connection.host[:host] if connection
      port = connection.host[:port] if connection

      if datadog_configuration[:peer_service]
        span.set_tag(
          Tracing::Metadata::Ext::TAG_PEER_SERVICE,
          datadog_configuration[:peer_service]
        )
      end

      # Tag original global service name if not used
      if span.service != Datadog.configuration.service
        span.set_tag(Tracing::Contrib::Ext::Metadata::TAG_BASE_SERVICE, Datadog.configuration.service)
      end

      span.span_type = Datadog::Tracing::Contrib::Elasticsearch::Ext::SPAN_TYPE_QUERY

      span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
      span.set_tag(Tracing::Metadata::Ext::TAG_OPERATION, Ext::TAG_OPERATION_QUERY)
      span.set_tag(Tracing::Metadata::Ext::TAG_KIND, Tracing::Metadata::Ext::SpanKind::TAG_CLIENT)

      span.set_tag(Contrib::Ext::DB::TAG_SYSTEM, Ext::TAG_SYSTEM)

      span.set_tag(Tracing::Metadata::Ext::TAG_PEER_HOSTNAME, host) if host

      # Set analytics sample rate
      if Contrib::Analytics.enabled?(datadog_configuration[:analytics_enabled])
        Contrib::Analytics.set_sample_rate(span, datadog_configuration[:analytics_sample_rate])
      end

      span.set_tag(Datadog::Tracing::Contrib::Elasticsearch::Ext::TAG_METHOD, method)
      tag_params(params, span)
      tag_body(body, span)
      span.set_tag(Datadog::Tracing::Contrib::Elasticsearch::Ext::TAG_URL, url)
      span.set_tag(Tracing::Metadata::Ext::NET::TAG_TARGET_HOST, host) if host
      span.set_tag(Tracing::Metadata::Ext::NET::TAG_TARGET_PORT, port) if port

      quantized_url = Datadog::Tracing::Contrib::Elasticsearch::Quantize.format_url(url)
      span.resource = "#{method} #{quantized_url}"
      Contrib::SpanAttributeSchema.set_peer_service!(span, Ext::PEER_SERVICE_SOURCES)
    rescue StandardError => e
      Datadog.logger.error(e.message)
    ensure
      # the call is still executed
      response = super
      span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_STATUS_CODE, response.status)
    end
  end
  response
end

#tag_body(body, span) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
# File 'lib/datadog/tracing/contrib/elasticsearch/patcher.rb', line 128

def tag_body(body, span)
  return unless body

  body = JSON.generate(body) unless body.is_a?(String)
  quantize_options = datadog_configuration[:quantize]
  quantized_body = Datadog::Tracing::Contrib::Elasticsearch::Quantize.format_body(
    body,
    quantize_options
  )
  span.set_tag(Datadog::Tracing::Contrib::Elasticsearch::Ext::TAG_BODY, quantized_body)
end

#tag_params(params, span) ⇒ Object



121
122
123
124
125
126
# File 'lib/datadog/tracing/contrib/elasticsearch/patcher.rb', line 121

def tag_params(params, span)
  return unless params

  params = JSON.generate(params) unless params.is_a?(String)
  span.set_tag(Datadog::Tracing::Contrib::Elasticsearch::Ext::TAG_PARAMS, params)
end