debugbar 0.1.3 → 0.2.0

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: 389fcb115afbb56d83be8f40f60985a527c81f8d0df6aa7209720dc2413fd10b
4
- data.tar.gz: f002929f37f5821f9b663c450b3ffc487cb3892f8f94ab2b1a720987b0d4514d
3
+ metadata.gz: 1d27d82c0937dc9a2ca971fd88759a82cfafc559dc8401983b4204b2d09d13f8
4
+ data.tar.gz: b9a9c4e3e202e6abb49b5ff60656bcf3ca24c918fc6c06fb85f10cb247712268
5
5
  SHA512:
6
- metadata.gz: '0719ab3ca71da9bc48786a19108b3828e15d70230d2d1022d9d135586cb95043ced489c59bd264cb513cac6dabd9190bc4a7bedbac67ebd533a63668473efa55'
7
- data.tar.gz: 6940165a13db4afe19416945c90b98ec7b33686b04163cd1ce99f7c7a575c8084d135d673707ede5b8b6c62615cc66e137484d2ab6b5d7fbab7dac6172eedc1d
6
+ metadata.gz: 6f8063ca1547cf52310b078ed206fe4b58f96b24366796ef0d20c04e990d332cdfde512da0b40e5a99bb21a3e9e7e51e0ece460eb3a064c9e60004c928529ae9
7
+ data.tar.gz: 713327fb6e02c53566063b2232ee1aa82770b7784e54b5d852cdde46f5a31a743c9305e2e4a8bdf945267271cbeef3db3991b4a8bb9c688d1c903d5eb8c04d64
data/CHANGELOG.md ADDED
@@ -0,0 +1,23 @@
1
+ # Changelog
2
+
3
+ ## v0.2.0 - 2024-02-28
4
+
5
+ * Introduce polling in case someone cannot use ActiveCable - See [8b262be7](https://github.com/julienbourdeau/debugbar/commit/8b262be7b644c7b587a6c3348bb02076053a344f)
6
+ * https://debugbar.dev/docs/polling-mode
7
+ * Show more timings information (total time, DB runtime and CPU time) - See [c02531ed](https://github.com/julienbourdeau/debugbar/commit/c02531ed6e9d9c74df11d4d8c30e3fb7bf970852)
8
+ * Move main middleware higher in the chain - See [7ca51c10](https://github.com/julienbourdeau/debugbar/commit/7ca51c10c5999f7ad14a303c92083614551de134)
9
+ * Silence debugbar routes for quieter logs - See [efe491bd](https://github.com/julienbourdeau/debugbar/commit/efe491bde9e0544e5fb891597bb2af47854c1169)
10
+
11
+ ### Breaking changes
12
+
13
+ The frontend configuration was slightly modified. If you customized the prefix for the debugbar routes or the channelName, you must update your config.
14
+ Prefix is at the root, and channelName is nested under the cable key.
15
+
16
+ ```diff
17
+ - <%= debugbar_javascript channelName: "something_else", cable: {prefix: "custom-prefix"} %>
18
+ + <%= debugbar_javascript prefix: "custom-prefix", cable: {channelName: "something_else"} %>
19
+ ```
20
+
21
+ ## v0.1.0 - 2024-02-13
22
+
23
+ * Initial release 🎉
@@ -0,0 +1,23 @@
1
+ module Debugbar
2
+ class PollingController < ApplicationController
3
+ skip_before_action :verify_authenticity_token, only: [:confirm]
4
+ before_action :cors_set_access_control_headers
5
+
6
+ def poll
7
+ render json: RequestBuffer.all.map(&:to_h)
8
+ end
9
+
10
+ def confirm
11
+ RequestBuffer.remove params[:ids]
12
+ head :ok
13
+ end
14
+
15
+ private
16
+
17
+ def cors_set_access_control_headers
18
+ response.headers['Access-Control-Allow-Origin'] = '*'
19
+ response.headers['Access-Control-Allow-Headers'] = 'Content-Type'
20
+ response.headers['Access-Control-Allow-Methods'] = 'POST, GET'
21
+ end
22
+ end
23
+ end
@@ -1,5 +1,12 @@
1
1
  module Debugbar::TagHelpers
2
2
  def debugbar_javascript(opt = {})
3
+ opt = ActiveSupport::HashWithIndifferentAccess.new(opt)
4
+
5
+ # See https://github.com/julienbourdeau/debugbar/issues/8
6
+ if !defined?(ActionCable) && opt[:mode].nil?
7
+ opt[:mode] = 'poll'
8
+ end
9
+
3
10
  html = <<-HTML
4
11
  <div id="__debugbar"></div>
5
12
  HTML
@@ -3,5 +3,3 @@
3
3
  rm -rf client/dist
4
4
  (cd client && npm run build)
5
5
  cp client/dist/assets/debugbar-*.js public/debugbar.js
6
-
7
- gem build debugbar.gemspec
data/build_demo.sh ADDED
@@ -0,0 +1,13 @@
1
+ #!/bin/bash
2
+
3
+ ruby ./build_fixtures.rb
4
+ rm -rf client/dist-demo
5
+ (cd client/ && VITE_DEMO_MODE=true npm run build)
6
+
7
+ if [ -d "../debugbar.dev/source/assets/debugbar" ]; then
8
+ echo
9
+ echo "debugbar.dev found in parent directory"
10
+ echo " -> Copying assets"
11
+ rm -f ../debugbar.dev/source/assets/debugbar/*
12
+ cp ./client/dist-demo/assets/* ../debugbar.dev/source/assets/debugbar/
13
+ fi
data/build_fixtures.rb ADDED
@@ -0,0 +1,32 @@
1
+ require 'net/http'
2
+ require 'json'
3
+
4
+ fixtures_dir = File.expand_path('../fixtures/requests', __FILE__)
5
+
6
+ puts "Deleting old fixtures in #{fixtures_dir}"
7
+ Dir.glob("#{fixtures_dir}/*").each { |p| File.delete(p) }
8
+
9
+ Net::HTTP.start('127.0.0.1', 3000) do |http|
10
+ [
11
+ '/post-list',
12
+ '/slow-page',
13
+ '/random',
14
+ '/post/240',
15
+ '/api/jobs?name=refresh&post_id=123',
16
+ '/api/jobs?name=send_email&post_id=123',
17
+ '/api/errors?code=500',
18
+ ].each do |path|
19
+ puts "Requesting #{path}"
20
+ http.request Net::HTTP::Get.new(path)
21
+ sleep 0.5
22
+ end
23
+
24
+ puts "Requesting debugbar data"
25
+ response = http.request Net::HTTP::Get.new('/_debugbar/poll')
26
+ data = JSON.parse(response.body)
27
+ data.each_with_index do |item, idx|
28
+ name = ("%03d" % (idx +1)) + '-' + item['meta']['controller'] + '-' + item['meta']['action']
29
+ File.write("#{fixtures_dir}/#{name}.json", JSON.pretty_generate(item))
30
+ end
31
+ end
32
+
data/build_gem.sh ADDED
@@ -0,0 +1,4 @@
1
+ #!/bin/bash
2
+
3
+ source build_client.sh
4
+ gem build debugbar.gemspec
data/config/routes.rb CHANGED
@@ -1,6 +1,9 @@
1
1
  Debugbar::Engine.routes.draw do
2
2
  mount ActionCable.server => '/cable'
3
3
 
4
- # TODO: Silence logs for this route if `::Rails.application.config.assets.quiet` is true
4
+ get 'poll' => "polling#poll"
5
+ options 'poll/confirm' => "polling#confirm"
6
+ post 'poll/confirm' => "polling#confirm"
7
+
5
8
  get 'assets/script' => "assets#js"
6
9
  end
@@ -2,24 +2,9 @@ module Debugbar
2
2
  class RequestBuffer
3
3
  class << self
4
4
  def init(adapter)
5
- @enabled = false
6
5
  @adapter = adapter
7
6
  end
8
7
 
9
- def enable!
10
- @enabled = true
11
- send_all
12
- end
13
-
14
- def disable!
15
- @enabled = false
16
- end
17
-
18
- def send_all
19
- data = @collection.values.map(&:to_h)
20
- ActionCable.server.broadcast("debugbar_channel", data)
21
- end
22
-
23
8
  %w(push each all remove clear!).each do |name|
24
9
  define_method(name) do |*args, &block|
25
10
  ret = @adapter.send(name, *args, &block)
@@ -1,4 +1,5 @@
1
1
  require_relative 'config'
2
+ require_relative 'middlewares/quiet_routes'
2
3
  require_relative 'middlewares/track_current_request'
3
4
  require_relative '../../app/helpers/debugbar/tag_helpers'
4
5
 
@@ -37,7 +38,8 @@ module Debugbar
37
38
 
38
39
  initializer 'debugbar.inject_middlewares' do |app|
39
40
  next unless Debugbar.config.enabled?
40
- app.middleware.insert_after ActionDispatch::RequestId, Debugbar::TrackCurrentRequest
41
+ app.middleware.insert_after ActionDispatch::Executor, Debugbar::TrackCurrentRequest
42
+ app.middleware.insert_after Sprockets::Rails::QuietAssets, Debugbar::QuietRoutes
41
43
  end
42
44
 
43
45
  initializer 'debugbar.subscribe' do
@@ -0,0 +1,23 @@
1
+ # This middleware silences the Rails logger for requests to the Debugbar routes.
2
+ # The poll route can be *very* noisy.
3
+ # Rails already does this for the /assets route, see Sprockets::Rails::QuietAssets.
4
+ #
5
+ # @see Sprockets::Rails::QuietAssets
6
+ # @see Rails::Rack::Logger#silence
7
+ #
8
+ module Debugbar
9
+ class QuietRoutes
10
+ def initialize(app)
11
+ @app = app
12
+ @route_regex = %r(\A/{0,2}#{::Debugbar.config.prefix})
13
+ end
14
+
15
+ def call(env)
16
+ if env['PATH_INFO'] =~ @route_regex
17
+ ::Rails.logger.silence { @app.call(env) }
18
+ else
19
+ @app.call(env)
20
+ end
21
+ end
22
+ end
23
+ end
@@ -9,12 +9,15 @@ module Debugbar
9
9
 
10
10
  return @app.call(env) if Debugbar::Current.ignore?
11
11
 
12
- Debugbar::Current.new_request!(env['action_dispatch.request_id'])
12
+ Debugbar::Current.new_request!(SecureRandom.uuid)
13
13
 
14
14
  res = @app.call(env)
15
15
 
16
16
  # TODO: Remove this if statement?
17
- if Debugbar::Current.request&.id
17
+ # We check meta because the frontend doesn't support request without meta yet.
18
+ # It might happen with ActionController::Live where the following code
19
+ # will run BEFORE ActionControllerEventSubscriber.process_action is called
20
+ if Debugbar::Current.request&.meta
18
21
  # filename = "#{Time.now.to_i}--#{Debugbar::Current.request.meta.dig(:params, :controller)}_#{Debugbar::Current.request.meta.dig(:params, :action).gsub('/', '_')}.json"
19
22
  # File.open(Rails.root.join('_requests', filename), "w") do |f|
20
23
  # f.write(Debugbar::Current.request.to_json)
@@ -22,26 +22,9 @@ module Debugbar
22
22
  sql = payload[:sql]&.gsub(/\/\*.*\*\//, "") # remove comments
23
23
 
24
24
  binds = nil
25
- if payload[:binds]&.any?
26
- casted_params = type_casted_binds(payload[:type_casted_binds])
27
-
28
- binds = []
29
- payload[:binds].each_with_index do |attr, i|
30
- attribute_name = if attr.respond_to?(:name)
31
- attr.name
32
- elsif attr.respond_to?(:[]) && attr[i].respond_to?(:name)
33
- attr[i].name
34
- else
35
- nil
36
- end
37
-
38
- filtered_params = filter(attribute_name, casted_params[i])
39
-
40
- binds << render_bind(attr, filtered_params)
41
- end
42
- binds = binds.inspect
43
- binds.prepend(" ")
44
- end
25
+ # if payload[:binds]&.any?
26
+ # TODO: Restore binds when I can figure out how to get something in poayload[:binds]
27
+ # end
45
28
 
46
29
  Debugbar::Tracker.add_query({
47
30
  id: event.transaction_id,
@@ -60,27 +43,6 @@ module Debugbar
60
43
  def query_source_location
61
44
  backtrace_cleaner.clean(caller(3))[0]
62
45
  end
63
-
64
- private
65
-
66
- def type_casted_binds(casted_binds)
67
- casted_binds.respond_to?(:call) ? casted_binds.call : casted_binds
68
- end
69
-
70
- def render_bind(attr, value)
71
- case attr
72
- when ActiveModel::Attribute
73
- if attr.type.binary? && attr.value
74
- value = "<#{attr.value_for_database.to_s.bytesize} bytes of binary data>"
75
- end
76
- when Array
77
- attr = attr.first
78
- else
79
- attr = nil
80
- end
81
-
82
- [attr&.name, value]
83
- end
84
46
  end
85
47
  end
86
48
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Debugbar
4
- VERSION = "0.1.3"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/debugbar.rb CHANGED
@@ -16,17 +16,25 @@ module Debugbar
16
16
 
17
17
  SETTERS.each do |m|
18
18
  define_method("#{m}=") do |val|
19
- if Current.request.nil?
19
+ return if Debugbar::Current.ignore?
20
+
21
+ if Current.request
22
+ return Current.request.send("#{m}=", val)
23
+ end
24
+
25
+ if Current.request.nil? && ENV["DEBUGBAR_VERBOSE_MODE"] == "true"
20
26
  # TODO: Much, much better logging needed
21
- puts "The current request is not set yet. Was trying to set #{m}=[#{val.class.name}]."
22
- else
23
- Current.request.send("#{m}=", val)
27
+ puts "The current request is not set yet. Was trying to set #{m} = #{val.class.name}."
28
+ pp val
29
+ nil
24
30
  end
25
31
  end
26
32
  end
27
33
 
28
34
  METHODS.each do |m|
29
35
  define_method(m) do |*args, &block|
36
+ return if Debugbar::Current.ignore?
37
+
30
38
  if Current.request
31
39
  return Current.request.send(m, *args, &block)
32
40
  end
data/public/.gitignore ADDED
File without changes