debugbar 0.1.4 → 0.2.1

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: a11b73a9ef8ff90c2be09ed570dde59b7d0e856b958ea6a5ab9c7eab18ff6feb
4
- data.tar.gz: 15fd5168d8a6f532842f693c16a7e9c821cbe9c136a459fb58d5e5324eb193ec
3
+ metadata.gz: f81dfe96cf7e377e64afafc691615d6b1190da2b71a89c02b2a381816458c0b5
4
+ data.tar.gz: dfe249da8d79a9f5a0c4075c40ad3817d65b0854a91882c7d43eaf54dccdf142
5
5
  SHA512:
6
- metadata.gz: 0d7fa957a8453eabd1fabe1aebc4a04ba803f1e94932e6bd4d483fb7af0ca63322aef91e7db43ef087d76aaeae2e5890d5b6565af4a525a2a7c8a4433a5f0e9e
7
- data.tar.gz: 26914d0f2bd5e2b0ada5cedd00259dd225ca89e31fc6b93459a3c7bdfcd3f30e7f58d4845385d79e06a60cf3f4da632be35d5eaaa8dfe4b9399ade7676e2c343
6
+ metadata.gz: 3ac4fa1480f2f573f3002b17c1c7656978f0dde5ffd44ba349221599287655f36a2b1af3e31060c1e51dedf63c2630e5d75e0aeb2ded9cb98122f07d8e285ace
7
+ data.tar.gz: 3cf593ce79357db2e63e19be37f757c4184458ffafe3462c3f200d43984e6902b1ead3977494ed3dfb4fb1fc046fa680736a24dbcc28aae2f13f39e4c629f06e
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
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/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 Debugbar::TrackCurrentRequest, 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)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Debugbar
4
- VERSION = "0.1.4"
4
+ VERSION = "0.2.1"
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