Class: CherrypieRailsSdk::Middleware

Inherits:
Object
  • Object
show all
Includes:
Concurrent::Async
Defined in:
lib/cherrypie_rails_sdk.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, api_key, identity_function, api_url: "https://api.cherrypie.app") ⇒ Middleware

Returns a new instance of Middleware.



11
12
13
14
15
16
17
# File 'lib/cherrypie_rails_sdk.rb', line 11

def initialize(app, api_key, identity_function, api_url: "https://api.cherrypie.app")
  super()
  @app = app
  @api_key = api_key
  @api_url = api_url
  @identity_function = identity_function
end

Instance Method Details

#_build_request_payload(request) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/cherrypie_rails_sdk.rb', line 23

def _build_request_payload(request)
  headers = {}
  request.headers.each {|key, value|
    headers.merge(key: value)
  }
  return {
    "path"=> request.fullpath,
    "method"=> request.method,
    "headers"=> headers,
    "body"=> request.body,
  }
end

#_build_response_payload(status, headers, response) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/cherrypie_rails_sdk.rb', line 36

def _build_response_payload(status, headers, response)
  body = nil
  if response.respond_to?(:body)
    body = response.body
  end
  return {
    "headers"=> headers,
    "body"=> body,
    "statusCode"=> status,
  }
end

#_call(env) ⇒ Object



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
# File 'lib/cherrypie_rails_sdk.rb', line 52

def _call env
  request_started_on = Time.now
  @status, @headers, @response = @app.call(env)
  request_ended_on = Time.now
  request = ActionDispatch::Request.new(env)

  @log_level = :info
  # Rails.logger.send(@log_level, '=' * 50)
  # Rails.logger.send(@log_level, "Request delta time: #{request_ended_on - request_started_on} seconds.")
  # Rails.logger.send(@log_level, '=' * 50)

  response_time = (request_ended_on - request_started_on) * 1000
  
   = @identity_function[request]
  payload = {
    "logEntry" => {
        "account"=> ,
        "clientIP"=> request.ip,
        "request"=> _build_request_payload(request),
        "response"=> _build_response_payload(@status, @headers, @response),
        "responseTime"=> response_time, # ms
    }
  } .to_json

  uri = URI("#{@api_url}/v1/logs")
  http = Net::HTTP.new(uri.host, uri.port)
  http.use_ssl = true
  cherrypie_request = Net::HTTP::Post.new(
    uri.request_uri,
    {
      'Content-Type' => 'application/json',
      'Authorization' => "Bearer #{@api_key}",
      'Accept'=>'application/json',
    }
  )
  cherrypie_request.body = payload
  # Only log non-300 statuses.
  if !(@status >= 300 && @status <= 399)
    cherrypie_response = self.async._send_log(http, cherrypie_request)
  end
  [@status, @headers, @response]
end

#_send_log(http, request) ⇒ Object



48
49
50
# File 'lib/cherrypie_rails_sdk.rb', line 48

def _send_log(http, request)
  http.request(request)
end

#call(env) ⇒ Object



19
20
21
# File 'lib/cherrypie_rails_sdk.rb', line 19

def call env
  dup._call env
end