Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Add rails performance gem and save API calls as custom events #20

Merged
merged 5 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
x86_64-darwin-21
Expand All @@ -85,4 +139,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
Loading