callstacking-rails 0.1.25 → 0.1.27

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: fafc57fcb944f390ece43b46e6417ea090df73c38aee4f6b7362d3bb2492dfc7
4
- data.tar.gz: 24344416d9261f55438b4c3848a534cfd329a1ac348ad388ee87144e78a565f2
3
+ metadata.gz: 8fa2fbfc31289e2837d702c573eb0c1f3bbd35c87a68edaedcc8345e82359003
4
+ data.tar.gz: 98228a480f6e39f17a4de540757bee6e23a5fc10db88af713cbbc71b41e3d1ca
5
5
  SHA512:
6
- metadata.gz: 86f2ccc8cc035fe6d955793fa420ad577fcf3613e3f5e847fbb73e4c6a81e144678f5c4b6b8df64074440fa2382ce88c92926da95d0f5d50433fbd9a788e7420
7
- data.tar.gz: 7693b49d937092edd0c91ba8865949cf1f29404634aa530e9523a86a84dfb5f17db3582cd84e691045eae0de6ddd0287900c674c47e1e8a21e72f34c48ca187c
6
+ metadata.gz: 142da9385fabbd53368c9e488dee76077e26fca257651447d11af284274d698c1ed09620e990ce0139cbdebbdd5718809f5dccdd9993f9ab8aadc95db78ab8b8
7
+ data.tar.gz: 90f40435ef2e0d6a95a3301c33c6023aa7d270ee442d47c4a49a92b7abc5102ba226dba3e9b8cf8ab041c6a4afab18b05f7d25a19a14ee3c14da41912c76a979
data/README.md CHANGED
@@ -43,7 +43,17 @@ And then execute:
43
43
  ```bash
44
44
  $ bundle
45
45
  ```
46
-
46
+
47
+ Add the following to your `ApplicationController`:
48
+
49
+ ```
50
+ class ApplicationController < ActionController::Base
51
+ include Callstacking::Rails::Helpers::InstrumentHelper
52
+
53
+ around_action :callstacking_setup, if: -> { params[:debug] == '1' }
54
+ ```
55
+
56
+
47
57
  Register an account at callstacking.com
48
58
  ```bash
49
59
  callstacking-rails register
@@ -57,17 +67,9 @@ callstacking-rails setup
57
67
 
58
68
  You're now ready to start tracing.
59
69
 
60
- ## Usage
70
+ ## CLI Setup
61
71
  Usage:
62
72
 
63
- > callstacking-rails enable
64
-
65
- Enables the callstacking tracing.
66
-
67
- > callstacking-rails disable
68
-
69
- Disables the callstacking tracing.
70
-
71
73
  > callstacking-rails register
72
74
 
73
75
  Opens a browser window to register as a callstacking.com user.
@@ -86,6 +88,9 @@ By setting the RAILS_ENV environment you can maintain multiple settings.
86
88
 
87
89
  Questions? Create an issue: https://github.com/callstacking/callstacking-rails/issues
88
90
 
91
+ ## Tracing
92
+ To initiate a trace, append the `debug=1` param to the URL of the page you want to trace. As outlined in the `around_action` you setup above.
93
+
89
94
  ## Environment
90
95
 
91
96
  You can provide the auth token via the `CALLSTACKING_AUTH_TOKEN` environment variable.
@@ -95,10 +100,12 @@ Your API token values can be viewed at https://callstacking.com/api_tokens
95
100
  You can enable/disable tracing via the `CALLSTACKING_ENABLED` environment variable (false|true).
96
101
 
97
102
  ## Trace Output
98
- When you open a page for your app, once the page has rendered, you will see a `💥` icon on the right hand side.
103
+ For HTML requests, once your page has rendered, you will see a `💥` icon on the right hand side.
99
104
 
100
105
  Click the icon and observe the full callstack context.
101
106
 
107
+ For headless API requests, visit https://callstacking.com/traces to view your traces.
108
+
102
109
  ## Tests
103
110
  ``
104
111
  rake app:test:all
@@ -11,8 +11,8 @@ require "callstacking/rails/client/base"
11
11
  require "callstacking/rails/client/authenticate"
12
12
  require "callstacking/rails/client/trace"
13
13
  require "callstacking/rails/cli"
14
- require "callstacking/rails/traces_helper"
15
14
  require "callstacking/rails/time_based_uuid"
15
+ require "callstacking/rails/helpers/instrument_helper"
16
16
 
17
17
  module Callstacking
18
18
  module Rails
@@ -32,41 +32,26 @@ module Callstacking
32
32
  app.config.assets.precompile << "checkpoint_rails_manifest.js"
33
33
  end
34
34
 
35
- initializer 'local_helper.action_controller' do
36
- ActiveSupport.on_load :action_controller do
37
- include Callstacking::Rails::TracesHelper
38
- end
39
- end
40
-
41
35
  config.after_initialize do
42
- if @@settings.enabled?
43
- puts "Call Stacking enabled (#{Callstacking::Rails::Env.environment})"
44
-
45
- ActionController::Base.send :after_action do
46
- inject_hud(@@settings)
47
- end
48
-
49
- @@loader = Callstacking::Rails::Loader.new(@@instrumenter,
50
- excluded: @@settings.excluded + EXCLUDED_TEST_CLASSES)
51
- @@loader.on_load
52
-
53
- @@trace.request_tracing
54
- else
55
- puts "Call Stacking disabled (#{Callstacking::Rails::Env.environment})"
56
- end
36
+ puts "Call Stacking enabled (#{Callstacking::Rails::Env.environment})"
37
+
38
+ @@loader = Callstacking::Rails::Loader.new(@@instrumenter, excluded: @@settings.excluded + EXCLUDED_TEST_CLASSES)
39
+ @@loader.on_load
57
40
  end
58
41
 
59
- def self.start_tracing
60
- return false if @@settings.disabled?
42
+ def self.start_tracing(controller)
43
+ @@settings.enable!
44
+ @@instrumenter.enable!(@@loader.klasses.to_a)
45
+ @@trace.begin_trace(controller)
61
46
 
62
- @@instrumenter.enable!(@@loader.klasses)
63
47
  true
64
48
  end
65
49
 
66
- def self.stop_tracing
67
- return false if @@settings.disabled?
68
-
50
+ def self.stop_tracing(controller)
51
+ @@settings.disable!
69
52
  @@instrumenter.disable!
53
+ @@trace.end_trace(controller)
54
+
70
55
  true
71
56
  end
72
57
  end
@@ -0,0 +1,55 @@
1
+ require 'action_view/helpers'
2
+ require "action_view/context.rb"
3
+
4
+ module Callstacking
5
+ module Rails
6
+ module Helpers
7
+ module HeadsUpDisplayHelper
8
+ include ActionView::Helpers::TagHelper
9
+ include ActionView::Helpers::JavaScriptHelper
10
+ include ActionView::Context
11
+
12
+ def hud(url)
13
+ frame_url = "#{url || Callstacking::Rails::Settings::PRODUCTION_URL}/traces/#{Callstacking::Rails::Trace.current_trace_id}/print"
14
+
15
+ body = []
16
+ body << (content_tag(:div, data: { turbo: false },
17
+ style: 'top: 50%; right: 10px; font-size: 24pt; :hover{text-shadow: 1px 1px 2px #000000};
18
+ padding: 0px; position: fixed; height: 50px; width: 40px; cursor: pointer;',
19
+ onclick: 'document.getElementById("callstacking-debugger").style.display = "unset";
20
+ document.getElementById("callstacking-close").style.display = "unset";') do
21
+ "<span title='ctrl-d'><center>#{Callstacking::Rails::Trace::ICON}</center></span>".html_safe
22
+ end)
23
+
24
+ body << (content_tag(:iframe, src: frame_url, id: 'callstacking-debugger', data: { turbo: false },
25
+ style: "width: 50%; height: 100%; overflow: scroll; top: 20px; right: 20px; position: fixed;
26
+ z-index: 99; opacity: 1.0; background-color: #FFF; color: #000; border: 1px solid;
27
+ margin: 0; padding: 0; box-shadow: 5px 5px; display: none;") do
28
+ end)
29
+
30
+ body << (javascript_tag('
31
+ document.onkeyup = function(e) {
32
+ // Mac - option-d Win - alt-d
33
+ if (e.ctrlKey && e.which == 68) {
34
+ if (document.getElementById("callstacking-debugger").style.display === "none") {
35
+ document.getElementById("callstacking-debugger").style.display = "block";
36
+ document.getElementById("callstacking-debugger").contentDocument.location.reload(true);
37
+ document.getElementById("callstacking-debugger").focus();
38
+ } else {
39
+ document.getElementById("callstacking-debugger").style.display = "none";
40
+ }
41
+ }
42
+ };'))
43
+
44
+ body.join
45
+ end
46
+
47
+ def inject_hud(settings, request, response)
48
+ if request.format.html?
49
+ response.body = response.body.sub(/<\/body>/i, "#{hud(settings.url)}</body>")
50
+ end
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,16 @@
1
+ module Callstacking
2
+ module Rails
3
+ module Helpers
4
+ module InstrumentHelper
5
+ extend ActiveSupport::Concern
6
+ def callstacking_setup
7
+ Callstacking::Rails::Engine.start_tracing(self)
8
+
9
+ yield
10
+ ensure
11
+ Callstacking::Rails::Engine.stop_tracing(self)
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -80,17 +80,23 @@ module Callstacking
80
80
  end
81
81
 
82
82
  def enable!(klasses)
83
- klasses.each do |klass|
83
+ Array.wrap(klasses).each do |klass|
84
84
  instrument_klass(klass, application_level: true)
85
85
  end
86
86
  end
87
87
 
88
- def disable!
89
- span_modules.each do |mod|
88
+ def disable!(modules = span_modules)
89
+ modules.each do |mod|
90
90
  mod.instance_methods.each do |method_name|
91
91
  mod.remove_method(method_name)
92
92
  end
93
93
  end
94
+
95
+ reset!
96
+ end
97
+
98
+ def reset!
99
+ span_modules.clear
94
100
  end
95
101
 
96
102
  def instrument_klass(klass, application_level: true)
@@ -133,6 +139,7 @@ module Callstacking
133
139
  return find_or_initialize_module(klass)
134
140
  end
135
141
 
142
+ span_modules << klass.ancestors[module_index]
136
143
  klass.ancestors[module_index]
137
144
  end
138
145
 
@@ -20,7 +20,6 @@ module Callstacking
20
20
  if path =~ /#{::Rails.root.to_s}/ &&
21
21
  !klasses.include?(klass) &&
22
22
  !excluded_klass
23
- instrumenter.instrument_klass(klass)
24
23
  klasses << klass
25
24
  end
26
25
  end
@@ -37,11 +37,18 @@ module Callstacking
37
37
  def self.enable!
38
38
  ::Rails.cache.write(CACHE_KEY, true)
39
39
  end
40
+ def enable!
41
+ self.class.enable!
42
+ end
40
43
 
41
44
  def self.disable!
42
45
  ::Rails.cache.write(CACHE_KEY, false)
43
46
  end
44
47
 
48
+ def disable!
49
+ self.class.disable!
50
+ end
51
+
45
52
  def enabled?
46
53
  return ::Rails.cache.read(CACHE_KEY) unless ::Rails.cache.read(CACHE_KEY).nil?
47
54
  return false if ENV[ENV_KEY] == 'false'
@@ -1,9 +1,12 @@
1
1
  require "rails"
2
2
  require 'active_support/core_ext/object/deep_dup'
3
+ require "callstacking/rails/helpers/heads_up_display_helper"
3
4
 
4
5
  module Callstacking
5
6
  module Rails
6
7
  class Trace
8
+ include Callstacking::Rails::Helpers::HeadsUpDisplayHelper
9
+
7
10
  attr_accessor :spans, :client, :lock
8
11
  attr_reader :settings
9
12
  cattr_accessor :current_trace_id
@@ -13,39 +16,36 @@ module Callstacking
13
16
  MAX_TRACE_ENTRIES = 3000
14
17
 
15
18
  def initialize(spans)
16
- @traces = []
17
- @spans = spans
19
+ @traces = []
20
+ @spans = spans
18
21
  @settings = Callstacking::Rails::Settings.new
19
22
 
20
- @lock = Mutex.new
21
- @client = Callstacking::Rails::Client::Trace.new(settings.url, settings.auth_token)
23
+ @lock = Mutex.new
24
+ @client = Callstacking::Rails::Client::Trace.new(settings.url, settings.auth_token)
22
25
 
23
26
  init_uuids(nil, nil)
24
27
  init_callbacks(nil)
25
28
  end
26
29
 
27
- def request_tracing
28
- tuid = nil
29
- trace_id = nil
30
+ def begin_trace(controller)
31
+ @trace_id, @tuid = init_uuids(controller.request&.request_id || SecureRandom.uuid, TimeBasedUUID.generate)
32
+ init_callbacks(@tuid)
30
33
 
31
- ActiveSupport::Notifications.subscribe("start_processing.action_controller") do |_name, _start, _finish, _id, payload|
32
- trace_id, tuid = init_uuids(payload[:request]&.request_id || SecureRandom.uuid, TimeBasedUUID.generate)
33
- init_callbacks(tuid)
34
+ start_request(@trace_id, @tuid,
35
+ controller.action_name, controller.controller_name,
36
+ controller.action_name, controller.request.format, ::Rails.root.to_s,
37
+ controller.request&.original_url,
38
+ controller.request.headers, controller.request.params)
39
+ end
34
40
 
35
- start_request(trace_id, tuid,
36
- payload[:method], payload[:controller],
37
- payload[:action], payload[:format], ::Rails.root.to_s,
38
- payload[:request]&.original_url || payload[:path],
39
- payload[:headers], payload[:params])
40
- end
41
+ def end_trace(controller)
42
+ complete_request(@trace_id, @tuid,
43
+ controller.action_name, controller.controller_name,
44
+ controller.action_name, controller.request.format,
45
+ controller.request&.original_url,
46
+ @traces, MAX_TRACE_ENTRIES)
41
47
 
42
- ActiveSupport::Notifications.subscribe("process_action.action_controller") do |_name, _start, _finish, _id, payload|
43
- complete_request(trace_id, tuid,
44
- payload[:method], payload[:controller],
45
- payload[:action], payload[:format],
46
- payload[:request]&.original_url || payload[:path],
47
- @traces, MAX_TRACE_ENTRIES)
48
- end
48
+ inject_hud(@settings, controller.request, controller.response)
49
49
  end
50
50
 
51
51
  private
@@ -63,7 +63,7 @@ module Callstacking
63
63
  def init_uuids(trace_id, tuid)
64
64
  Callstacking::Rails::Trace.current_trace_id = trace_id
65
65
  Callstacking::Rails::Trace.current_tuid = tuid
66
-
66
+
67
67
  return trace_id, tuid
68
68
  end
69
69
 
@@ -1,5 +1,5 @@
1
1
  module Callstacking
2
2
  module Rails
3
- VERSION = "0.1.25"
3
+ VERSION = "0.1.27"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: callstacking-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.25
4
+ version: 0.1.27
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jim Jones
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-03 00:00:00.000000000 Z
11
+ date: 2023-04-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -108,6 +108,8 @@ files:
108
108
  - lib/callstacking/rails/client/trace.rb
109
109
  - lib/callstacking/rails/engine.rb
110
110
  - lib/callstacking/rails/env.rb
111
+ - lib/callstacking/rails/helpers/heads_up_display_helper.rb
112
+ - lib/callstacking/rails/helpers/instrument_helper.rb
111
113
  - lib/callstacking/rails/instrument.rb
112
114
  - lib/callstacking/rails/loader.rb
113
115
  - lib/callstacking/rails/settings.rb
@@ -115,7 +117,6 @@ files:
115
117
  - lib/callstacking/rails/spans.rb
116
118
  - lib/callstacking/rails/time_based_uuid.rb
117
119
  - lib/callstacking/rails/trace.rb
118
- - lib/callstacking/rails/traces_helper.rb
119
120
  - lib/callstacking/rails/version.rb
120
121
  - lib/tasks/checkpoint/rails_tasks.rake
121
122
  homepage: https://github.com/callstacking/callstacking-rails
@@ -1,51 +0,0 @@
1
- require 'action_view/helpers'
2
- require "action_view/context.rb"
3
-
4
- module Callstacking
5
- module Rails
6
- module TracesHelper
7
- include ActionView::Helpers::TagHelper
8
- include ActionView::Helpers::JavaScriptHelper
9
- include ActionView::Context
10
-
11
- def hud(url)
12
- frame_url = "#{url || Callstacking::Rails::Settings::PRODUCTION_URL}/traces/#{Callstacking::Rails::Trace.current_trace_id}/print"
13
-
14
- body = []
15
- body << (content_tag( :div, data: { turbo:false },
16
- style: 'top: 50%; right: 10px; font-size: 24pt; :hover{text-shadow: 1px 1px 2px #000000};
17
- padding: 0px; position: fixed; height: 50px; width: 40px; cursor: pointer;',
18
- onclick: 'document.getElementById("callstacking-debugger").style.display = "unset";
19
- document.getElementById("callstacking-close").style.display = "unset";') do
20
- "<span title='ctrl-d'><center>#{Callstacking::Rails::Trace::ICON}</center></span>".html_safe
21
- end)
22
-
23
- body << (content_tag(:iframe, src: frame_url, id: 'callstacking-debugger', data: { turbo:false },
24
- style: "width: 50%; height: 100%; overflow: scroll; top: 20px; right: 20px; position: fixed;
25
- z-index: 99; opacity: 1.0; background-color: #FFF; color: #000; border: 1px solid;
26
- margin: 0; padding: 0; box-shadow: 5px 5px; display: none;") do
27
- end)
28
-
29
- body << (javascript_tag('
30
- document.onkeyup = function(e) {
31
- // Mac - option-d Win - alt-d
32
- if (e.ctrlKey && e.which == 68) {
33
- if (document.getElementById("callstacking-debugger").style.display === "none") {
34
- document.getElementById("callstacking-debugger").style.display = "block";
35
- document.getElementById("callstacking-debugger").contentDocument.location.reload(true);
36
- document.getElementById("callstacking-debugger").focus();
37
- } else {
38
- document.getElementById("callstacking-debugger").style.display = "none";
39
- }
40
- }
41
- };'))
42
-
43
- body.join
44
- end
45
-
46
- def inject_hud(settings)
47
- response.body = response.body.sub(/<\/body>/i, "#{hud(settings.url)}</body>")
48
- end
49
- end
50
- end
51
- end