From 8788515be8be4101fba62dc4561b3d14b12d40a0 Mon Sep 17 00:00:00 2001 From: Syphax bouazzouni Date: Wed, 16 Oct 2024 23:47:24 +0200 Subject: [PATCH] Feature: Add rails performance gem and save API calls as custom events (#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 --- Gemfile.lock | 60 +++++++++++++++++++++++++++++-- lib/ontologies_api_client/http.rb | 14 ++++++++ ontologies_api_client.gemspec | 1 + 3 files changed, 72 insertions(+), 3 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index b147d35..0e45725 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -11,13 +11,27 @@ 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) @@ -25,11 +39,16 @@ GEM 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) @@ -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) @@ -56,8 +80,36 @@ 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) @@ -65,12 +117,14 @@ GEM 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 @@ -86,4 +140,4 @@ DEPENDENCIES webmock BUNDLED WITH - 2.4.21 + 2.4.22 diff --git a/lib/ontologies_api_client/http.rb b/lib/ontologies_api_client/http.rb index ebd8750..36dc67a 100644 --- a/lib/ontologies_api_client/http.rb +++ b/lib/ontologies_api_client/http.rb @@ -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 @@ -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) @@ -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' diff --git a/ontologies_api_client.gemspec b/ontologies_api_client.gemspec index b2ae8c2..63cc247 100644 --- a/ontologies_api_client.gemspec +++ b/ontologies_api_client.gemspec @@ -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