infield 0.1.7 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9f31b7037fa451372f99dbdd746e5732f5c377150f57aa12fe2b9864812fdfff
4
- data.tar.gz: 95b3da1a0ba4f633deaa8161b05105d92d90e309201c7feb3d87fd05507eeae7
3
+ metadata.gz: 26abdd8a8407a04b1627a2e4df94c3e2ecca434334dfadd6b776b876f8fe1fac
4
+ data.tar.gz: 868a9bb4fcedec09f7296a6f1ab2e34c409d151c9d33ff4542ef498064c5b1d7
5
5
  SHA512:
6
- metadata.gz: 91baa2990a521179d6c90f9e532a212fcb7029cc6bddf7115c086462f18022ddcc69e8d427c3da01d2057e3967328978db7640f9d87c1f44340e7d8819191597
7
- data.tar.gz: 47df1183a08550649198745bfb0e1fea88493b61f31d025e52106e9aceed1f1acb9c043417a3eda50850b15c7e18c74c94b5d43dccbd49dc389a11136dc59638
6
+ metadata.gz: cf41bdccf63f87eaf3feb2e947a9e00ea51c33f37d0ef0e76260395035ecf9d488e21f3fa7966cced8eda3aec453cab664299df6a55a69ac6130571be744ab3f
7
+ data.tar.gz: 91a721071942764b19342fd80e53281b24f03a3fa77cd01580d98aa75eff7da18b5a4b6d0844ad885b5e506133c6f3493ead8a49a1dd47700b7cdb7db4d1b47a
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.2.3
1
+ 3.2.3
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/README.md CHANGED
@@ -1,3 +1,40 @@
1
1
  # Infield
2
2
 
3
- This gem handles reporting deprecation warnings to Infield.
3
+ This gem handles reporting deprecation warnings to Infield from a Rails app.
4
+
5
+ ## Setup
6
+
7
+ You'll need an API key and repo environment ID to use this gem. Get your API key from https://app.infield.ai/settings/api_key and your environment ID from the environments configuration page.
8
+
9
+ Add the gem to your gemfile:
10
+
11
+ gem 'infield', require: false
12
+
13
+ Then in `config/application.rb`:
14
+
15
+ if ENV['INFIELD_API_KEY']
16
+ require 'infield'
17
+ Infield.run(api_key: ENV['INFIELD_API_KEY'],
18
+ repo_environment_id: ENV['INFIELD_REPO_ENVIRONMENT_ID')
19
+ end
20
+
21
+ And in any environment you want to profile from:
22
+
23
+ config.active_support.deprecation = :notify
24
+
25
+ ## Configuration options
26
+
27
+ The infield gem batches requests and sends them asyncronously. You can configure the following options to `Infield.run` (defaults shown here):
28
+
29
+ Infield.run(
30
+ api_key: key, # required
31
+ repo_environment_id: id, # required
32
+ sleep_interval: 5, # seconds, # how long to sleep between processing events
33
+ batch_size: 10, # how many events to batch in one API request to Infield
34
+ queue_limit: 30 # If more than this number of events come in in sleep_interval, any over are dropped
35
+ )
36
+
37
+ ## Test environment notes
38
+
39
+ If you enable this gem in test and use another gem to block all web
40
+ calls in your test environment, make sure to allow access to `app.infield.ai`.
@@ -1,9 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'thread'
4
- require 'json'
5
- require 'net/http'
6
-
7
3
  module Infield
8
4
  # Takes in new deprecation warnings and sends them to the Infield API
9
5
  # in batches
@@ -22,9 +18,10 @@ module Infield
22
18
  ].freeze
23
19
 
24
20
  class << self
25
- attr_reader :queue
21
+ attr_reader :queue, :thread
26
22
 
27
23
  def enqueue(message)
24
+ @queue ||= Queue.new
28
25
  return if @queue.size >= @queue_limit
29
26
  @queue << message
30
27
  end
@@ -37,7 +34,7 @@ module Infield
37
34
  @queue_limit = queue_limit
38
35
  @batch_size = batch_size # send up to 20 messages to API at once
39
36
 
40
- Thread.new do
37
+ @thread = Thread.new do
41
38
  loop do
42
39
  sleep(@sleep_interval)
43
40
  next if @queue.empty?
@@ -0,0 +1,48 @@
1
+ # frozen_string_literal: true
2
+ require 'debug'
3
+
4
+ module Infield
5
+ module Heartbeat
6
+ module Runner
7
+ class << self
8
+ attr_reader :thread
9
+
10
+ def run(interval: 60)
11
+ @thread = Thread.new do
12
+ loop do
13
+ if DeprecationWarning::Runner.thread&.alive?
14
+ send_heartbeat
15
+ else
16
+ stop
17
+ break
18
+ end
19
+ sleep(interval)
20
+ end
21
+ end
22
+ end
23
+
24
+ def stop
25
+ @thread&.kill
26
+ end
27
+
28
+ private
29
+
30
+ def send_heartbeat
31
+ uri = URI.parse(Infield.infield_api_url)
32
+ Net::HTTP.start(uri.host, uri.port, use_ssl: (uri.scheme == 'https')) do |http|
33
+ http.post('/api/heartbeats',
34
+ {
35
+ repo_environment_id: Infield.repo_environment_id,
36
+ environment: Infield.environment,
37
+ }.to_json,
38
+ {
39
+ 'Content-Type' => 'application/json',
40
+ 'Authorization' => "bearer #{Infield.api_key}"
41
+ })
42
+ end
43
+ rescue *DeprecationWarning::Runner::HTTP_ERRORS => e
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Infield
4
- VERSION = '0.1.7'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/infield.rb CHANGED
@@ -1,5 +1,9 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'thread'
4
+ require 'json'
5
+ require 'net/http'
6
+
3
7
  require_relative 'infield/version'
4
8
 
5
9
  # require_relative 'infield/core_ext'
@@ -9,11 +13,12 @@ module Infield
9
13
  Error = Class.new(StandardError)
10
14
 
11
15
  autoload :DeprecationWarning, "#{__dir__}/infield/deprecation_warning.rb"
16
+ autoload :Heartbeat, "#{__dir__}/infield/heartbeat.rb"
12
17
 
13
18
  class << self
14
19
  attr_accessor :api_key, :repo_environment_id, :environment, :infield_api_url
15
20
 
16
- def run(api_key: nil, repo_environment_id: nil, environment: nil, sleep_interval: 5, batch_size: 10, queue_limit: 30)
21
+ def run(api_key: nil, repo_environment_id: nil, environment: nil, sleep_interval: 5, batch_size: 10, queue_limit: 30, heartbeat_interval: 600)
17
22
  @api_key = api_key || ENV['INFIELD_API_KEY']
18
23
  @repo_environment_id = repo_environment_id
19
24
  @infield_api_url = ENV['INFIELD_API_URL'] || 'https://app.infield.ai'
@@ -21,7 +26,16 @@ module Infield
21
26
  raise 'repo_environment_id is required' unless @repo_environment_id
22
27
 
23
28
  @environment = environment || defined?(Rails) ? Rails.env : nil
29
+
30
+ # Start the deprecation warning processor
24
31
  DeprecationWarning::Runner.run(sleep_interval: sleep_interval, batch_size: batch_size, queue_limit: queue_limit)
32
+
33
+ # Start the heartbeat
34
+ Heartbeat::Runner.run(interval: heartbeat_interval)
35
+ end
36
+
37
+ def shutdown
38
+ Heartbeat::Runner.stop
25
39
  end
26
40
  end
27
41
  end
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: infield
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Infield
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-09-16 00:00:00.000000000 Z
12
- dependencies: []
11
+ date: 2024-12-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: webmock
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  description:
14
42
  email:
15
43
  - support@infield.ai
@@ -21,6 +49,7 @@ files:
21
49
  - ".rubocop.yml"
22
50
  - ".ruby-version"
23
51
  - CHANGELOG.md
52
+ - Gemfile
24
53
  - LICENSE.txt
25
54
  - README.md
26
55
  - Rakefile
@@ -28,6 +57,7 @@ files:
28
57
  - lib/infield.rb
29
58
  - lib/infield/core_ext.rb
30
59
  - lib/infield/deprecation_warning.rb
60
+ - lib/infield/heartbeat.rb
31
61
  - lib/infield/rails.rb
32
62
  - lib/infield/version.rb
33
63
  homepage: