Module: Datadog::Tracing::Contrib::OpenSearch::Patcher::Client

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

Overview

Patches OpenSearch::Transport::Client module

Instance Method Summary collapse

Instance Method Details

#datadog_configurationObject



137
138
139
# File 'lib/datadog/tracing/contrib/opensearch/patcher.rb', line 137

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

#perform_request(method, path, params = {}, body = nil, headers = nil) ⇒ Object

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



33
34
35
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
# File 'lib/datadog/tracing/contrib/opensearch/patcher.rb', line 33

def perform_request(method, path, params = {}, body = nil, headers = nil)
  response = nil
  # rubocop:disable Metrics/BlockLength
  Tracing.trace('opensearch.query', service: datadog_configuration[:service_name]) do |span|
    begin
      # Set generic tags
      span.set_tag(Tracing::Metadata::Ext::TAG_COMPONENT, Ext::TAG_COMPONENT)
      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)

      # Set argument tags
      span.set_tag(OpenSearch::Ext::TAG_METHOD, method)
      span.set_tag(OpenSearch::Ext::TAG_PATH, path)

      tag_params(params, span)
      tag_body(body, span)

      # Parse url
      original_url = transport.get_connection.full_url(path, {})
      url = URI.parse(original_url)
      host = url.host
      port = url.port
      scheme = url.scheme
      # Set url.user to nil to remove sensitive information (i.e. user's username and password)
      url.user = nil

      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

      # Set url tags
      span.set_tag(OpenSearch::Ext::TAG_URL, url)
      span.set_tag(OpenSearch::Ext::TAG_HOST, host)
      span.set_tag(OpenSearch::Ext::TAG_PORT, port)
      span.set_tag(OpenSearch::Ext::TAG_SCHEME, scheme)

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

      # Define span resource
      quantized_url = if datadog_configuration[:resource_pattern] == Ext::RELATIVE_RESOURCE_PATTERN
                        OpenSearch::Quantize.format_url(url.path)
                      else # Default to Ext::ABSOLUTE_RESOURCE_PATTERN
                        OpenSearch::Quantize.format_url(url)
                      end
      span.resource = "#{method} #{quantized_url}"
      Contrib::SpanAttributeSchema.set_peer_service!(span, Ext::PEER_SERVICE_SOURCES)
    rescue StandardError => e
      Datadog.logger.error(e.message)
      Datadog::Core::Telemetry::Logger.report(e)
      # TODO: Refactor the code to streamline the execution without ensure
    ensure
      begin
        response = super
      rescue => e
        status_code = ::OpenSearch::Transport::Transport::ERRORS.key(e.class)
        span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_STATUS_CODE, status_code) if status_code
        raise
      end
      # Set post-response tags
      if response
        if response.respond_to?(:status)
          span.set_tag(Tracing::Metadata::Ext::HTTP::TAG_STATUS_CODE, response.status)
        end
        if response.respond_to?(:headers) && (response.headers || {})['content-length']
          span.set_tag(
            OpenSearch::Ext::TAG_RESPONSE_CONTENT_LENGTH,
            response.headers['content-length'].to_i
          )
        end
      end
    end
  end
  # rubocop:enable Metrics/BlockLength
  # rubocop:enable Metrics/AbcSize
  response
end

#tag_body(body, span) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
# File 'lib/datadog/tracing/contrib/opensearch/patcher.rb', line 125

def tag_body(body, span)
  return unless body

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

#tag_params(params, span) ⇒ Object



118
119
120
121
122
123
# File 'lib/datadog/tracing/contrib/opensearch/patcher.rb', line 118

def tag_params(params, span)
  return unless params

  params = JSON.generate(params) unless params.is_a?(String)
  span.set_tag(OpenSearch::Ext::TAG_PARAMS, params)
end