Skip to content

Commit

Permalink
Feature: Add rails performance gem and save API calls as custom event…
Browse files Browse the repository at this point in the history
…s (#20)

* add rails performance gem

* save as a custom event the API calls

* disable by default the rail performance monitoring

* use directly RailsPerformance::Models::CustomRecord instead of measure
  • Loading branch information
syphax-bouazzouni authored Oct 16, 2024
1 parent 0598e00 commit 8788515
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 3 deletions.
60 changes: 57 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,44 @@ PATH
multi_json
oj
parallel
rails_performance
request_store
spawnling (= 2.1.5)

GEM
remote: https://rubygems.org/
specs:
activesupport (7.0.8.1)
actionpack (7.0.4)
actionview (= 7.0.4)
activesupport (= 7.0.4)
rack (~> 2.0, >= 2.2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.2.0)
actionview (7.0.4)
activesupport (= 7.0.4)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.1, >= 1.2.0)
activesupport (7.0.4)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 1.6, < 2)
minitest (>= 5.1)
tzinfo (~> 2.0)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
bigdecimal (3.1.7)
browser (5.3.1)
builder (3.3.0)
coderay (1.1.3)
concurrent-ruby (1.2.3)
connection_pool (2.4.1)
crack (1.0.0)
bigdecimal
rexml
crass (1.0.6)
erubi (1.13.0)
excon (0.110.0)
faraday (2.0.1)
faraday-net_http (~> 2.0)
Expand All @@ -43,11 +62,16 @@ GEM
hashdiff (1.1.0)
i18n (1.14.4)
concurrent-ruby (~> 1.0)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
lz4-ruby (0.3.3)
method_source (1.1.0)
minitest (5.22.3)
multi_json (1.15.0)
multipart-post (2.4.0)
nokogiri (1.15.6-x86_64-linux)
racc (~> 1.4)
oj (3.16.3)
bigdecimal (>= 3.0)
parallel (1.24.0)
Expand All @@ -56,21 +80,51 @@ GEM
coderay (~> 1.1)
method_source (~> 1.0)
public_suffix (5.0.5)
rack (3.0.10)
racc (1.8.1)
rack (2.2.9)
rack-test (2.1.0)
rack (>= 1.3)
rails-dom-testing (2.2.0)
activesupport (>= 5.0.0)
minitest
nokogiri (>= 1.6)
rails-html-sanitizer (1.6.0)
loofah (~> 2.21)
nokogiri (~> 1.14)
rails_performance (1.2.2)
browser
railties
redis
redis-namespace
railties (7.0.4)
actionpack (= 7.0.4)
activesupport (= 7.0.4)
method_source
rake (>= 12.2)
thor (~> 1.0)
zeitwerk (~> 2.5)
rake (13.2.1)
redis (5.3.0)
redis-client (>= 0.22.0)
redis-client (0.22.2)
connection_pool
redis-namespace (1.11.0)
redis (>= 4)
request_store (1.7.0)
rack (>= 1.4)
rexml (3.2.6)
ruby2_keywords (0.0.5)
spawnling (2.1.5)
test-unit (3.6.2)
power_assert
thor (1.3.2)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
webmock (3.23.0)
addressable (>= 2.8.0)
crack (>= 0.3.2)
hashdiff (>= 0.4.0, < 2.0.0)
zeitwerk (2.6.18)

PLATFORMS
ruby
Expand All @@ -86,4 +140,4 @@ DEPENDENCIES
webmock

BUNDLED WITH
2.4.21
2.4.22
14 changes: 14 additions & 0 deletions lib/ontologies_api_client/http.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
require 'ostruct'
require 'benchmark'
require 'active_support/cache'
require 'rails_performance' if defined?(Rails)
##
# This monkeypatch makes OpenStruct act like Struct objects
class OpenStruct
Expand Down Expand Up @@ -77,6 +78,8 @@ def self.get(path, params = {}, options = {})
req.headers[:invalidate_cache] = invalidate_cache
end
end

monitor_request(params, path, response, time)
puts "Getting: #{path} with #{params} (t: #{time}s - cache: #{response.headers["X-Rack-Cache"]})" if $DEBUG_API_CLIENT
rescue Exception => e
params = Faraday::Utils.build_query(params)
Expand Down Expand Up @@ -164,6 +167,17 @@ def self.object_from_json(json)

private

def self.monitor_request(params, path, response, time)
RailsPerformance::Models::CustomRecord.new(
tag_name: "Getting: #{path} with #{params} - cache: (#{response.headers["X-Rack-Cache"]})",
namespace_name: "API call #{path}",
status: response.status,
duration: time,
datetime: Time.current.strftime(RailsPerformance::FORMAT),
datetimei: Time.current.to_i).save if defined?(Rails) && RailsPerformance.enabled
end


def self.custom_req(obj, file, file_attribute, req)
req.headers['Content-Type'] = 'application/json'

Expand Down
1 change: 1 addition & 0 deletions ontologies_api_client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ Gem::Specification.new do |gem|
gem.add_dependency('parallel')
gem.add_dependency('request_store')
gem.add_dependency('spawnling', '2.1.5')
gem.add_dependency('rails_performance')
end

0 comments on commit 8788515

Please sign in to comment.