Class: Hypertrace::Instrumentation::DataCapture

Inherits:
Object
  • Object
show all
Includes:
Logging
Defined in:
lib/hypertrace/instrumentation/data_capture.rb

Constant Summary collapse

TYPE_REQUEST =
'request'
TYPE_RESPONSE =
'response'
CONTENT_TYPE_SUBSTRINGS =
%w[json x-www-form-urlencoded]

Class Method Summary collapse

Methods included from Logging

#log

Class Method Details

.body_allowed_by_config?(type) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
61
# File 'lib/hypertrace/instrumentation/data_capture.rb', line 58

def self.body_allowed_by_config? type
  return Hypertrace::RubyAgent.config.data_capture.http_body.request.value if type == TYPE_REQUEST
  Hypertrace::RubyAgent.config.data_capture.http_body.response.value
end

.can_capture?(content_type, type) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/hypertrace/instrumentation/data_capture.rb', line 39

def self.can_capture?(content_type, type)
  content_type = content_type.join('') if content_type.is_a?(Array)
  return false unless content_type
  return false unless body_allowed_by_config?(type)

  content_type = content_type.downcase
  CONTENT_TYPE_SUBSTRINGS.each do |substring|
    if content_type.include?(substring)
      return true
    end
  end
  false
end

.capturable_body(body_object) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/hypertrace/instrumentation/data_capture.rb', line 24

def self.capturable_body body_object
  begin
    max_capture = Hypertrace::RubyAgent.config.data_capture.body_max_size_bytes.value
    if body_object.is_a?(String)
      return body_object.byteslice(0..max_capture)
    elsif body_object.is_a?(StringIO)
      result =  body_object.read(max_capture)
      body_object.rewind
      return result
    end
  rescue => e
    log.error("Erroring reading response body" + e.backtrace&.join("\n"))
  end
end

.header_allowed_by_config?(type) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/hypertrace/instrumentation/data_capture.rb', line 53

def self.header_allowed_by_config? type
  return Hypertrace::RubyAgent.config.data_capture.http_headers.request.value if type == TYPE_REQUEST
  Hypertrace::RubyAgent.config.data_capture.http_headers.response.value
end

.headers_to_attribute_keys(header_hash, type, &block) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/hypertrace/instrumentation/data_capture.rb', line 7

def self.headers_to_attribute_keys header_hash, type, &block
  return {} unless header_allowed_by_config?(type)

  attrs = {}
  header_hash.each do |header_key, header_value|
    attr_key = "http.#{type}.header.#{header_key.downcase}"
    header_value = header_value.join(',') if header_value.is_a?(Array)
    if block_given?
      yield attr_key, header_value
    else
      attrs[attr_key] = header_value
    end
  end
  return if block_given?
  return attrs
end