app_profiler 0.2.9 → 0.4.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: 88fa4e71be7c319cba26e669752c8a7a46c9b7c56881894944a8ae4b8def6c6f
4
- data.tar.gz: 9daa41d818e4fbd0a113186c20e7a18814a3c039bc7dc91d52d26efd7a3ab5c6
3
+ metadata.gz: b5ca2ed69abcd8cba935914ee16e8427fa8070fb3b0eecb488b06a027a64667a
4
+ data.tar.gz: fbcd17ddeeabfbbb8fe52c69d8744b0a8c73a9cd54ed6cd248f40e1c1a8f2662
5
5
  SHA512:
6
- metadata.gz: 8471c04250be1ae8b393a804ee83553ccc5c374008f343ab0213464b130658ee69fa77626a0f557e7423350e60d0ac6c803228a656e48be739def63400d34fe3
7
- data.tar.gz: 216c13be42bb571a535c894f543889fa114f97ff49aa84fb59d7ab2a39e8b7f2815c5e75399a3d98b7cbcf645376e37bb4899d8e5c63761f3fac6f1ca2483848
6
+ metadata.gz: f41e6a149770dbffc1f85f3b280ef91cfc3c2ab966f6d9c8ce943c6c46e3ae2a2784b2864d2a8163593c83d0c116af1919598cc0576831423c5ed825a90b0770
7
+ data.tar.gz: d12e52408d4d5aedc048661bc96d35f55f038c1eeaedc01c6ed4b31612298d8203c33e5449d935a677a4ec3b75e0cc4c3cc938ae186a3a29c49d245da8093128
@@ -3,8 +3,27 @@
3
3
  module AppProfiler
4
4
  module Backend
5
5
  class BaseBackend
6
- def run(params = {}, &block)
7
- raise NotImplementedError
6
+ def run(params = {})
7
+ started = start(params)
8
+ start_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
9
+
10
+ yield
11
+
12
+ return unless started
13
+
14
+ duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start_time
15
+
16
+ stop
17
+ results_data = results
18
+
19
+ if results_data
20
+ results_data.metadata[:duration] = duration
21
+ end
22
+
23
+ results_data
24
+ ensure
25
+ # Only stop the profiler if profiling was started in this context.
26
+ stop if started
8
27
  end
9
28
 
10
29
  def start(params = {})
@@ -22,20 +22,6 @@ module AppProfiler
22
22
  end
23
23
  end
24
24
 
25
- def run(params = {})
26
- started = start(params)
27
-
28
- yield
29
-
30
- return unless started
31
-
32
- stop
33
- results
34
- ensure
35
- # Only stop the profiler if profiling was started in this context.
36
- stop if started
37
- end
38
-
39
25
  def start(params = {})
40
26
  # Do not start the profiler if StackProf was started somewhere else.
41
27
  return false if running?
@@ -21,20 +21,6 @@ module AppProfiler
21
21
  end
22
22
  end
23
23
 
24
- def run(params = {})
25
- started = start(params)
26
-
27
- yield
28
-
29
- return unless started
30
-
31
- stop
32
- results
33
- ensure
34
- # Only stop the profiler if profiling was started in this context.
35
- stop if started
36
- end
37
-
38
24
  def start(params = {})
39
25
  # Do not start the profiler if we already have a collector started somewhere else.
40
26
  return false if running?
@@ -47,6 +47,10 @@ module AppProfiler
47
47
  metadata[PROFILE_ID_METADATA_KEY]
48
48
  end
49
49
 
50
+ def duration
51
+ metadata[:duration]
52
+ end
53
+
50
54
  def upload
51
55
  AppProfiler.storage.upload(self).tap do |upload|
52
56
  if upload && defined?(upload.url)
@@ -7,6 +7,11 @@ require "app_profiler/middleware/view_action"
7
7
 
8
8
  module AppProfiler
9
9
  class Middleware
10
+ OTEL_PROFILE_ID = "profile.id"
11
+ OTEL_PROFILE_BACKEND = "profile.profiler"
12
+ OTEL_PROFILE_MODE = "profile.mode"
13
+ OTEL_PROFILE_CONTEXT = "profile.context"
14
+
10
15
  class_attribute :action, default: UploadAction
11
16
  class_attribute :disabled, default: false
12
17
 
@@ -29,8 +34,11 @@ module AppProfiler
29
34
  return yield unless app_profiler_params
30
35
 
31
36
  params_hash = app_profiler_params.to_h
37
+
32
38
  return yield unless before_profile(env, params_hash)
33
39
 
40
+ add_otel_instrumentation(env, params_hash) if AppProfiler.otel_instrumentation_enabled
41
+
34
42
  profile = AppProfiler.run(**params_hash) do
35
43
  response = yield
36
44
  end
@@ -47,6 +55,27 @@ module AppProfiler
47
55
  response
48
56
  end
49
57
 
58
+ def add_otel_instrumentation(env, params)
59
+ rack_span = OpenTelemetry::Instrumentation::Rack.current_span
60
+ return unless rack_span.recording?
61
+
62
+ metadata = params[:metadata]
63
+ profile_id = if metadata[:id].present?
64
+ metadata[:id]
65
+ else
66
+ AppProfiler::ProfileId.current
67
+ end
68
+
69
+ attributes = {
70
+ OTEL_PROFILE_ID => profile_id,
71
+ OTEL_PROFILE_BACKEND => params[:backend].to_s,
72
+ OTEL_PROFILE_MODE => params[:mode].to_s,
73
+ OTEL_PROFILE_CONTEXT => AppProfiler.context,
74
+ }
75
+
76
+ rack_span.add_attributes(attributes)
77
+ end
78
+
50
79
  def profile_params(params)
51
80
  return params if params.valid?
52
81
  return unless AppProfiler.profile_sampler_enabled
@@ -51,6 +51,8 @@ module AppProfiler
51
51
  AppProfiler.profile_sampler_enabled = app.config.app_profiler.profile_sampler_enabled || false
52
52
  AppProfiler.profile_sampler_config = app.config.app_profiler.profile_sampler_config ||
53
53
  AppProfiler::Sampler::Config.new
54
+
55
+ AppProfiler.otel_instrumentation_enabled = app.config.app_profiler.otel_instrumentation_enabled || false
54
56
  end
55
57
 
56
58
  initializer "app_profiler.add_middleware" do |app|
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module AppProfiler
4
- VERSION = "0.2.9"
4
+ VERSION = "0.4.0"
5
5
  end
data/lib/app_profiler.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require "active_support"
4
4
  require "active_support/core_ext/class"
5
5
  require "active_support/core_ext/module"
6
+ require "active_support/core_ext/time/zones"
6
7
  require "logger"
7
8
  require "app_profiler/version"
8
9
 
@@ -76,6 +77,8 @@ module AppProfiler
76
77
  mattr_reader :profile_file_name
77
78
 
78
79
  class << self
80
+ attr_reader :otel_instrumentation_enabled
81
+
79
82
  def deprecator # :nodoc:
80
83
  @deprecator ||= ActiveSupport::Deprecation.new("in future releases", "app_profiler")
81
84
  end
@@ -162,6 +165,15 @@ module AppProfiler
162
165
  false
163
166
  end
164
167
 
168
+ def otel_instrumentation_enabled=(value)
169
+ if value
170
+ gem("opentelemetry-instrumentation-rack")
171
+ require("opentelemetry/instrumentation/rack")
172
+ end
173
+
174
+ @otel_instrumentation_enabled = value
175
+ end
176
+
165
177
  def backend_for(backend_name)
166
178
  if vernier_supported? &&
167
179
  backend_name&.to_sym == AppProfiler::VernierProfile::BACKEND_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.9
4
+ version: 0.4.0
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-02-10 00:00:00.000000000 Z
15
+ date: 2025-04-10 00:00:00.000000000 Z
16
16
  dependencies:
17
17
  - !ruby/object:Gem::Dependency
18
18
  name: activesupport
@@ -112,6 +112,20 @@ dependencies:
112
112
  - - ">="
113
113
  - !ruby/object:Gem::Version
114
114
  version: '0'
115
+ - !ruby/object:Gem::Dependency
116
+ name: opentelemetry-instrumentation-rack
117
+ requirement: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ type: :development
123
+ prerelease: false
124
+ version_requirements: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
115
129
  - !ruby/object:Gem::Dependency
116
130
  name: rake
117
131
  requirement: !ruby/object:Gem::Requirement
@@ -187,7 +201,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
187
201
  - !ruby/object:Gem::Version
188
202
  version: '0'
189
203
  requirements: []
190
- rubygems_version: 3.6.3
204
+ rubygems_version: 3.6.6
191
205
  specification_version: 4
192
206
  summary: Collect performance profiles for your Rails application.
193
207
  test_files: []