Class: StackifyRubyAPM::Middleware Private

Inherits:
Object
  • Object
show all
Defined in:
lib/stackify_apm/middleware.rb

This class is part of a private API. You should avoid using this class if possible, as it may be removed or be changed in the future.

Constant Summary collapse

CONTENT_TYPE_REGEX =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

%r{text\/html|application\/xhtml\+xml/}
CONTENT_DISPOSITION =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'Content-Disposition'.freeze
ATTACHMENT =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

'attachment'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Middleware

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Middleware.



32
33
34
# File 'lib/stackify_apm/middleware.rb', line 32

def initialize(app)
  @app = app
end

Instance Attribute Details

#configurationObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'lib/stackify_apm/middleware.rb', line 26

def configuration
  @configuration
end

#rack_bodyObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'lib/stackify_apm/middleware.rb', line 26

def rack_body
  @rack_body
end

#rack_headersObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'lib/stackify_apm/middleware.rb', line 26

def rack_headers
  @rack_headers
end

#rack_statusObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



26
27
28
# File 'lib/stackify_apm/middleware.rb', line 26

def rack_status
  @rack_status
end

Instance Method Details

#attachment?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


116
117
118
# File 'lib/stackify_apm/middleware.rb', line 116

def attachment?
  @rack_headers[CONTENT_DISPOSITION] && @rack_headers[CONTENT_DISPOSITION].include?(ATTACHMENT)
end

#build_transaction(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Start of transaction building with params: name, type, context



92
93
94
# File 'lib/stackify_apm/middleware.rb', line 92

def build_transaction(env)
  StackifyRubyAPM.transaction 'Rack', 'request', context: StackifyRubyAPM.build_context(env)
end

#call(env) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This is where the requests are received, built into a transaction, and then submitted

rubocop:disable Metrics/CyclomaticComplexity rubocop:disable Metrics/PerceivedComplexity



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
# File 'lib/stackify_apm/middleware.rb', line 40

def call(env)
  begin
    transaction = build_transaction(env) if running?
    resp = @app.call env

    @rack_status = resp[0].to_i
    @rack_headers = resp[1]
    @rack_body = resp[2]
    @configuration = config
    response_manupulate = StackifyRubyAPM::ResponseManipulator.new(env, resp, @configuration)

    if okay_to_modify?
      @configuration.already_instrumented_flag = true
      if StackifyRubyAPM.rum_script_inject
        resp
      else
        if @configuration.rum_auto_injection
          response_string = response_manupulate.call_manipulate
        end
        if response_string
          response = Rack::Response.new(response_string, @rack_status, @rack_headers)
          resp = response.finish
        else
          resp
        end
      end
    else
      resp
    end

    submit_transaction(transaction, *resp) if transaction
  rescue InternalError
    raise # Don't report StackifyRubyAPM errors
  rescue StandardError
    transaction.submit('500', status: 500) if transaction
    raise
  ensure
    transaction.release if transaction
  end

  resp
end

#configObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



100
101
102
# File 'lib/stackify_apm/middleware.rb', line 100

def config
  StackifyRubyAPM.agent.config
end

#modifiable_content_type?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


112
113
114
# File 'lib/stackify_apm/middleware.rb', line 112

def modifiable_content_type?
  @rack_headers['Content-Type'] =~ self.class::CONTENT_TYPE_REGEX
end

#okay_to_modify?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


104
105
106
107
108
109
110
# File 'lib/stackify_apm/middleware.rb', line 104

def okay_to_modify?
  return false if xhr?
  return false unless modifiable_content_type?
  return false if @rack_status == 404 || @rack_status == 501
  return false if attachment?
  true
end

#running?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


96
97
98
# File 'lib/stackify_apm/middleware.rb', line 96

def running?
  StackifyRubyAPM.running?
end

#streaming?(env) ⇒ Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


120
121
122
123
124
# File 'lib/stackify_apm/middleware.rb', line 120

def streaming?(env)
  return false unless defined?(ActionController::Live)

  env['action_controller.instance'].class.included_modules.include?(ActionController::Live)
end

#submit_transaction(transaction, status, headers, _body) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

rubocop:enable Metrics/PerceivedComplexity rubocop:enable Metrics/CyclomaticComplexity



85
86
87
88
# File 'lib/stackify_apm/middleware.rb', line 85

def submit_transaction(transaction, status, headers, _body)
  result = status.to_i
  transaction.submit(result, status: status, headers: headers)
end

#xhr?Boolean

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns:

  • (Boolean)


126
127
128
# File 'lib/stackify_apm/middleware.rb', line 126

def xhr?
  @rack_headers['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest'
end