app_profiler 0.2.2 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 15a1a7b3bd1e60dc10753ce4b8e4f2907461b09417aecde43fce3d4565d5f211
4
- data.tar.gz: 811e16e5383f85f4e2aef6aaf50e832b8cf5714c2cacd730ce4f184c9d1f26c4
3
+ metadata.gz: 8d37c7a09664237b5de220c0eeb2e06e5d71e962c475d8b5d48379908f1fcde6
4
+ data.tar.gz: de727300e3a86062ba2924aee795553098bd5f0fa1b03bf9b422d17caa99b90b
5
5
  SHA512:
6
- metadata.gz: 6b6cd68d552a8ac41eebef2cec56ac8e1917c1e321c759297649a62d6c03a3ce7c6ffa06bee820320f62ac4abdde8b8fa26f539508841ea9440d7231160534a8
7
- data.tar.gz: c320d9e84ceca4834b7c5400dd46cef4ce3f04a7e49530b4381461f1ccb91b91da9cdc2e6eff86202b218efc1ef96754a5f8b40e7bbe433d73f2b3486ea86522
6
+ metadata.gz: 67bc3d4a7da8dd12063cae5d82c8cb8643133b36cbe42456dd903b1182b4b2c6687073cddcdcb33b84cc76a27ce7d8a2ea3fc135f6f462591bc90e02fd87cb62
7
+ data.tar.gz: 7efb402266de2269237d9d170d94d7689146a94e1a721488f462638ca18a0d8c4335e3a3c68452791a554067214429b9ce7903dfb5cb8fea293607db8bb07974
@@ -50,7 +50,6 @@ module AppProfiler
50
50
 
51
51
  def profile_params(params)
52
52
  return params if params.valid?
53
-
54
53
  return unless AppProfiler.profile_sampler_enabled
55
54
 
56
55
  AppProfiler::Sampler.profile_params(params, AppProfiler.profile_sampler_config)
@@ -5,19 +5,20 @@ require "app_profiler/sampler/vernier_config"
5
5
  module AppProfiler
6
6
  module Sampler
7
7
  class Config
8
- attr_reader :sample_rate, :paths, :cpu_interval, :backends_probability
8
+ attr_reader :sample_rate, :targets, :cpu_interval, :backends_probability
9
9
 
10
10
  SAMPLE_RATE = 0.001 # 0.1%
11
- PATHS = ["/"]
11
+ TARGETS = ["/"]
12
12
  BACKEND_PROBABILITES = { stackprof: 1.0, vernier: 0.0 }
13
13
  @backends = {}
14
14
 
15
15
  def initialize(sample_rate: SAMPLE_RATE,
16
- paths: PATHS,
16
+ targets: TARGETS,
17
17
  backends_probability: BACKEND_PROBABILITES,
18
18
  backends_config: {
19
19
  stackprof: StackprofConfig.new,
20
- })
20
+ },
21
+ paths: nil)
21
22
 
22
23
  if sample_rate < 0.0 || sample_rate > 1.0
23
24
  raise ArgumentError, "sample_rate must be between 0 and 1"
@@ -25,8 +26,10 @@ module AppProfiler
25
26
 
26
27
  raise ArgumentError, "mode probabilities must sum to 1" unless backends_probability.values.sum == 1.0
27
28
 
29
+ ActiveSupport::Deprecation.new.warn("passing paths is deprecated, use targets instead") if paths
30
+
28
31
  @sample_rate = sample_rate
29
- @paths = paths
32
+ @targets = paths || targets
30
33
  @backends_config = backends_config
31
34
  @backends_probability = backends_probability
32
35
  end
@@ -5,28 +5,30 @@ module AppProfiler
5
5
  module Sampler
6
6
  class << self
7
7
  def profile_params(request, config)
8
- random = Kernel.rand
9
- return unless sample?(random, config, request)
8
+ profile_params_for(request.path, config)
9
+ end
10
10
 
11
- get_profile_params(config, random)
11
+ def profile_params_for(target, config)
12
+ return unless sample?(config, target)
13
+
14
+ get_profile_params(config)
12
15
  end
13
16
 
14
17
  private
15
18
 
16
- def sample?(random, config, request)
17
- return false if random > config.sample_rate
19
+ def sample?(config, target)
20
+ return false if Kernel.rand > config.sample_rate
18
21
 
19
- path = request.path
20
- return false unless config.paths.any? { |p| path.match?(p) }
22
+ return false unless config.targets.any? { |t| target.match?(t) }
21
23
 
22
24
  true
23
25
  end
24
26
 
25
- def get_profile_params(config, random)
26
- backend_name = select_random(config.backends_probability, random)
27
+ def get_profile_params(config)
28
+ backend_name = select_random(config.backends_probability)
27
29
  backend_config = config.get_backend_config(backend_name)
28
30
 
29
- mode = select_random(backend_config.modes_probability, random)
31
+ mode = select_random(backend_config.modes_probability)
30
32
  interval = backend_config.interval_for(mode)
31
33
 
32
34
  AppProfiler::Parameters.new(
@@ -42,7 +44,8 @@ module AppProfiler
42
44
  # it will return :c
43
45
  # Assumes all probabilities sum to 1
44
46
 
45
- def select_random(options, random)
47
+ def select_random(options)
48
+ random = Kernel.rand
46
49
  current = 0
47
50
  options = options.sort_by do |_, probability|
48
51
  probability
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AppProfiler
4
- VERSION = "0.2.2"
4
+ VERSION = "0.2.4"
5
5
  end
data/lib/app_profiler.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "active_support"
3
4
  require "active_support/core_ext/class"
4
5
  require "active_support/core_ext/module"
5
6
  require "logger"
@@ -63,7 +64,7 @@ module AppProfiler
63
64
  mattr_reader :profile_enqueue_failure, default: nil
64
65
  mattr_reader :after_process_queue, default: nil
65
66
  mattr_accessor :forward_metadata_on_upload, default: false
66
- mattr_accessor :profile_sampler_enabled, default: false
67
+
67
68
  mattr_accessor :profile_sampler_config
68
69
 
69
70
  class << self
@@ -116,6 +117,28 @@ module AppProfiler
116
117
  @profiler_backend = new_profiler_backend
117
118
  end
118
119
 
120
+ def profile_sampler_enabled=(value)
121
+ if value.is_a?(Proc)
122
+ raise ArgumentError,
123
+ "profile_sampler_enabled must be a proc or a lambda that accepts no argument" if value.arity != 0
124
+ else
125
+ raise ArgumentError, "Must be TrueClass or FalseClass" unless [TrueClass, FalseClass].include?(value.class)
126
+ end
127
+
128
+ @profile_sampler_enabled = value
129
+ end
130
+
131
+ def profile_sampler_enabled
132
+ return false unless defined?(@profile_sampler_enabled)
133
+
134
+ @profile_sampler_enabled.is_a?(Proc) ? @profile_sampler_enabled.call : @profile_sampler_enabled
135
+ rescue => e
136
+ logger.error(
137
+ "[AppProfiler.profile_sampler_enabled] exception: #{e}, message: #{e.message}",
138
+ )
139
+ false
140
+ end
141
+
119
142
  def backend_for(backend_name)
120
143
  if vernier_supported? &&
121
144
  backend_name == AppProfiler::Backend::VernierBackend.name
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_profiler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gannon McGibbon
@@ -13,7 +13,7 @@ authors:
13
13
  autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
- date: 2024-08-26 00:00:00.000000000 Z
16
+ date: 2024-09-06 00:00:00.000000000 Z
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
19
19
  name: activesupport
@@ -184,7 +184,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
184
  - !ruby/object:Gem::Version
185
185
  version: '0'
186
186
  requirements: []
187
- rubygems_version: 3.5.17
187
+ rubygems_version: 3.5.18
188
188
  signing_key:
189
189
  specification_version: 4
190
190
  summary: Collect performance profiles for your Rails application.