app_profiler 0.2.7 → 0.2.9

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: 1d088d71f0d625a8627a17aa418840597b1d26e01bc05a93848440ce710f461a
4
- data.tar.gz: 5f65cc77daaeaacbeed54cc913c08e89fddbc30e64458e1538c2cce2f5e9b381
3
+ metadata.gz: 88fa4e71be7c319cba26e669752c8a7a46c9b7c56881894944a8ae4b8def6c6f
4
+ data.tar.gz: 9daa41d818e4fbd0a113186c20e7a18814a3c039bc7dc91d52d26efd7a3ab5c6
5
5
  SHA512:
6
- metadata.gz: a99c98c4bcf876d9c7147bf552b47ae5b8052f2e3c314a2279c8e34a2f37a9781f47c9f7ee99260281490682d1ea776f00be946494dd8fcd0bb83cbd5db07537
7
- data.tar.gz: 4fafc43c3a589636928566a4bdf96fde0abf91eebde85e267852c281f7b5439ce76048c1cbca1cfe18119b99057bc7355fe41c2e48537c6b6a62f7a41b0ffa4b
6
+ metadata.gz: 8471c04250be1ae8b393a804ee83553ccc5c374008f343ab0213464b130658ee69fa77626a0f557e7423350e60d0ac6c803228a656e48be739def63400d34fe3
7
+ data.tar.gz: 216c13be42bb571a535c894f543889fa114f97ff49aa84fb59d7ab2a39e8b7f2815c5e75399a3d98b7cbcf645376e37bb4899d8e5c63761f3fac6f1ca2483848
@@ -8,7 +8,7 @@ module AppProfiler
8
8
  private_constant :INTERNAL_METADATA_KEYS
9
9
  class UnsafeFilename < StandardError; end
10
10
 
11
- attr_reader :id, :context
11
+ attr_reader :context
12
12
 
13
13
  delegate :[], to: :@data
14
14
 
@@ -34,9 +34,17 @@ module AppProfiler
34
34
  # `data` is assumed to be a Hash for Stackprof,
35
35
  # a vernier "result" object for vernier
36
36
  def initialize(data, id: nil, context: nil)
37
- @id = id.presence || SecureRandom.hex
37
+ ProfileId.current = id if id.present?
38
+
38
39
  @context = context
39
40
  @data = data
41
+
42
+ metadata[PROFILE_BACKEND_METADATA_KEY] = self.class.backend_name
43
+ metadata[PROFILE_ID_METADATA_KEY] = ProfileId.current
44
+ end
45
+
46
+ def id
47
+ metadata[PROFILE_ID_METADATA_KEY]
40
48
  end
41
49
 
42
50
  def upload
@@ -0,0 +1,39 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "securerandom"
4
+
5
+ module AppProfiler
6
+ class ProfileId
7
+ class Current
8
+ PROFILE_ID_KEY = :__app_profiler_profile_id__
9
+ class << self
10
+ # This is a thread local variable which gets reset by the middleware at the end of the request.
11
+ # Need to be mindful of the middleware order. If we try to access ProfileId after the middleware has finished,
12
+ # lets say in a middleware which runs before Profiling middleware, it will return a different value,
13
+ # as the middleware has already reset.
14
+
15
+ def id
16
+ Thread.current[PROFILE_ID_KEY] ||= SecureRandom.hex
17
+ end
18
+
19
+ def id=(id)
20
+ Thread.current[PROFILE_ID_KEY] = id
21
+ end
22
+
23
+ def reset
24
+ Thread.current[PROFILE_ID_KEY] = nil
25
+ end
26
+ end
27
+ end
28
+
29
+ class << self
30
+ def current
31
+ Current.id
32
+ end
33
+
34
+ def current=(id)
35
+ Current.id = id
36
+ end
37
+ end
38
+ end
39
+ end
@@ -33,7 +33,7 @@ module AppProfiler
33
33
 
34
34
  return false if backend != AppProfiler::Backend::StackprofBackend.name && !AppProfiler.vernier_supported?
35
35
 
36
- if AppProfiler.vernier_supported? && backend == AppProfiler::Backend::VernierBackend.name &&
36
+ if AppProfiler.vernier_supported? && backend == AppProfiler::VernierProfile::BACKEND_NAME &&
37
37
  !AppProfiler::Backend::VernierBackend::AVAILABLE_MODES.include?(mode.to_sym)
38
38
  AppProfiler.logger.info("[AppProfiler] unsupported profiling mode=#{mode} for backend #{backend}")
39
39
  return false
@@ -4,6 +4,17 @@ module AppProfiler
4
4
  class StackprofProfile < BaseProfile
5
5
  FILE_EXTENSION = ".stackprof.json"
6
6
 
7
+ class << self
8
+ def backend_name
9
+ Backend::StackprofBackend.name.to_s
10
+ end
11
+ end
12
+
13
+ def initialize(data, id: nil, context: nil)
14
+ data[:metadata] ||= {}
15
+ super(data, id: id, context: context)
16
+ end
17
+
7
18
  def mode
8
19
  @data[:mode]
9
20
  end
@@ -3,6 +3,19 @@
3
3
  module AppProfiler
4
4
  class VernierProfile < BaseProfile
5
5
  FILE_EXTENSION = ".vernier.json"
6
+ BACKEND_NAME = :vernier
7
+
8
+ class << self
9
+ def backend_name
10
+ # cannot reference Backend::VernierBackend because of different ruby versions we have to support
11
+ BACKEND_NAME.to_s
12
+ end
13
+ end
14
+
15
+ def initialize(data, id: nil, context: nil)
16
+ data[:meta] ||= {}
17
+ super(data, id: id, context: context)
18
+ end
6
19
 
7
20
  def mode
8
21
  @data[:meta][:mode]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AppProfiler
4
- VERSION = "0.2.7"
4
+ VERSION = "0.2.9"
5
5
  end
data/lib/app_profiler.rb CHANGED
@@ -7,6 +7,9 @@ require "logger"
7
7
  require "app_profiler/version"
8
8
 
9
9
  module AppProfiler
10
+ PROFILE_ID_METADATA_KEY = :profile_id
11
+ PROFILE_BACKEND_METADATA_KEY = :profiler
12
+
10
13
  class ConfigurationError < StandardError
11
14
  end
12
15
 
@@ -45,6 +48,7 @@ module AppProfiler
45
48
  autoload(:Backend, "app_profiler/backend")
46
49
  autoload(:Server, "app_profiler/server")
47
50
  autoload(:Sampler, "app_profiler/sampler")
51
+ autoload(:ProfileId, "app_profiler/profile_id")
48
52
 
49
53
  mattr_accessor :logger, default: Logger.new($stdout)
50
54
  mattr_accessor :root
@@ -89,6 +93,7 @@ module AppProfiler
89
93
  yield
90
94
  ensure
91
95
  self.backend = original_backend if backend
96
+ ProfileId::Current.reset
92
97
  end
93
98
 
94
99
  def start(*args, backend: nil, **kwargs)
@@ -159,7 +164,7 @@ module AppProfiler
159
164
 
160
165
  def backend_for(backend_name)
161
166
  if vernier_supported? &&
162
- backend_name&.to_sym == AppProfiler::Backend::VernierBackend.name
167
+ backend_name&.to_sym == AppProfiler::VernierProfile::BACKEND_NAME
163
168
  AppProfiler::Backend::VernierBackend
164
169
  elsif backend_name&.to_sym == AppProfiler::Backend::StackprofBackend.name
165
170
  AppProfiler::Backend::StackprofBackend
@@ -173,7 +178,7 @@ module AppProfiler
173
178
  end
174
179
 
175
180
  def vernier_supported?
176
- RUBY_VERSION >= "3.2.1" && defined?(AppProfiler::Backend::VernierBackend.name)
181
+ RUBY_VERSION >= "3.2.1" && defined?(AppProfiler::VernierProfile::BACKEND_NAME)
177
182
  end
178
183
 
179
184
  def profile_header=(profile_header)
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.7
4
+ version: 0.2.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gannon McGibbon
@@ -12,7 +12,7 @@ authors:
12
12
  - Scott Francis
13
13
  bindir: bin
14
14
  cert_chain: []
15
- date: 2025-01-30 00:00:00.000000000 Z
15
+ date: 2025-02-10 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: activesupport
@@ -144,6 +144,7 @@ files:
144
144
  - lib/app_profiler/middleware/upload_action.rb
145
145
  - lib/app_profiler/middleware/view_action.rb
146
146
  - lib/app_profiler/parameters.rb
147
+ - lib/app_profiler/profile_id.rb
147
148
  - lib/app_profiler/railtie.rb
148
149
  - lib/app_profiler/request_parameters.rb
149
150
  - lib/app_profiler/sampler.rb