-
Notifications
You must be signed in to change notification settings - Fork 2
Add GVL/allocation instrumentation and optional log fields #58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
29c1fcd
Add allocation and gvl statistics
e61e7ed
Support optional duration and payload fields in TinyJsonFormatter
ab47d49
Fix rubocop offenses in patch files
a872274
Remove ide file
299dade
Avoid allocating full GC.stat hash in event patch
cd26323
Cap params, rename allocation_bytes to malloc_increase_bytes
35d6dde
Add Sidekiq job stats message
25fcc21
DRY stats
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "gvltools" | ||
| require "active_support/notifications" | ||
|
|
||
| module UmbrellioUtils | ||
| # Namespace for opt-in monkey-patches. None of these files are loaded by default, | ||
| # require them explicitly from your application. | ||
| module Patches | ||
| # Extends ActiveSupport::Notifications::Event with GVL wait time and malloc stats. | ||
| # https://github.com/rails/rails/blob/main/activesupport/lib/active_support/notifications/instrumenter.rb # rubocop:disable Layout/LineLength | ||
| # | ||
| # The GVL timer must be enabled in the application as early as possible: | ||
| # GVLTools::LocalTimer.enable | ||
| module ActiveSupportEvent | ||
| STATS_PRECISION = 6 | ||
|
|
||
| def initialize(...) | ||
| super | ||
| @gvl_time_start = 0 | ||
| @gvl_time_finish = 0 | ||
| @malloc_increase_bytes_start = 0 | ||
| @malloc_increase_bytes_finish = 0 | ||
| end | ||
|
|
||
| def start! | ||
| super | ||
| @gvl_time_start = GVLTools::LocalTimer.monotonic_time | ||
| @malloc_increase_bytes_start = GC.stat(:malloc_increase_bytes) | ||
| end | ||
|
|
||
| def finish! | ||
| super | ||
| @gvl_time_finish = GVLTools::LocalTimer.monotonic_time | ||
| @malloc_increase_bytes_finish = GC.stat(:malloc_increase_bytes) | ||
| end | ||
|
|
||
| # Time the thread spent waiting for the GVL, in milliseconds | ||
| def gvl_time | ||
| (@gvl_time_finish - @gvl_time_start) / 1_000_000.0 | ||
| end | ||
|
|
||
| # Delta of the process-global malloc counter: can be negative (it resets on GC) | ||
| # and includes sibling threads' allocations under threaded servers. Guard with | ||
| # #positive? where a counter is expected. | ||
| def malloc_increase_bytes | ||
| @malloc_increase_bytes_finish - @malloc_increase_bytes_start | ||
| end | ||
|
|
||
| def stats(precision: STATS_PRECISION) | ||
| { | ||
| gc_time: gc_time.round(precision), | ||
| gvl_time: gvl_time.round(precision), | ||
| cpu_time: cpu_time.round(precision), | ||
| idle_time: idle_time.round(precision), | ||
| allocations:, | ||
| malloc_increase_bytes:, | ||
| } | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| ActiveSupport::Notifications::Event.prepend(UmbrellioUtils::Patches::ActiveSupportEvent) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative "active_support_event" | ||
|
|
||
| module UmbrellioUtils | ||
| module Patches | ||
| # Simplifies the "Completed" action controller log entry and enriches it with | ||
| # GC, GVL and allocation stats. | ||
| # https://github.com/reidmorrison/rails_semantic_logger/blob/master/lib/rails_semantic_logger/action_controller/log_subscriber.rb # rubocop:disable Layout/LineLength | ||
| # | ||
| # Require this file after the rails_semantic_logger gem has been loaded | ||
| # (e.g. from an initializer). | ||
| module RailsSemanticLogger | ||
| PRECISION = 6 | ||
| PARAMS_SIZE_LIMIT = 10_000 | ||
|
|
||
| def process_action(event) | ||
| ::Rails.logger.info do | ||
| payload = event.payload.dup | ||
| payload[:path] = extract_path(payload[:path]) if payload.key?(:path) | ||
| payload[:params] = cap_params(payload[:params]) if payload.key?(:params) | ||
|
|
||
| payload[:view_time] = payload.delete(:view_runtime).to_f.round(PRECISION) | ||
| payload[:db_time] = payload.delete(:db_runtime).to_f.round(PRECISION) | ||
| payload.merge!(event.stats) | ||
|
|
||
| # Causes excessive log output with Rails 5 RC1 | ||
| payload.delete(:headers) | ||
| # Causes recursion in Rails 6.1.rc1 | ||
| payload.delete(:request) | ||
| payload.delete(:response) | ||
| # Keep only the [class, message] pair in :exception (set by ActiveSupport), | ||
| # not the raw exception with its backtrace — consistent with sidekiq_job_metrics. | ||
| payload.delete(:exception_object) | ||
|
|
||
| { | ||
| message: "Completed ##{payload[:action]}", | ||
| duration: event.duration, | ||
| payload:, | ||
| } | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def cap_params(params) | ||
| serialized = params.to_json | ||
| return params if serialized.bytesize <= PARAMS_SIZE_LIMIT | ||
|
|
||
| { | ||
| truncated: true, | ||
| bytesize: serialized.bytesize, | ||
| preview: "#{serialized[0, PARAMS_SIZE_LIMIT]}...", | ||
| } | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| RailsSemanticLogger::ActionController::LogSubscriber.prepend( | ||
| UmbrellioUtils::Patches::RailsSemanticLogger, | ||
| ) | ||
44 changes: 44 additions & 0 deletions
44
lib/umbrellio_utils/semantic_logger/sidekiq_job_metrics.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative "../patches/active_support_event" | ||
|
|
||
| module UmbrellioUtils | ||
| module SemanticLogger | ||
| # Logs Sidekiq job duration, GC, GVL and allocation stats as a dedicated line. | ||
| # Relies on the "perform.sidekiq_job" notification published by the umbrellio | ||
| # fork of yabeda-sidekiq. Call +subscribe!+ from an initializer. | ||
| module SidekiqJobMetrics | ||
| MESSAGE = "Sidekiq job stats" | ||
|
|
||
| extend self | ||
|
|
||
| def subscribe! | ||
| ActiveSupport::Notifications.subscribe("perform.sidekiq_job") do |event| | ||
| log_event(event) | ||
| end | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def log_event(event) | ||
| logger = ::SemanticLogger[event.payload[:worker] || "Sidekiq"] | ||
| logger.info( | ||
| message: MESSAGE, | ||
| duration: event.duration, | ||
| payload: metrics_payload(event), | ||
| ) | ||
| end | ||
|
|
||
| def metrics_payload(event) | ||
| payload = { | ||
| worker: event.payload[:worker], | ||
| queue: event.payload[:queue], | ||
| **event.stats, | ||
| } | ||
| # [class, message] pair set by ActiveSupport::Notifications when the job raised | ||
| payload[:exception] = event.payload[:exception] if event.payload[:exception] | ||
| payload | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module UmbrellioUtils | ||
| VERSION = "1.14.3" | ||
| VERSION = "1.15.0" | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,4 +4,8 @@ module Rails | |
| def self.env | ||
| "development".inquiry | ||
| end | ||
|
|
||
| def self.logger | ||
| nil | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| # Minimal stub of the rails_semantic_logger class patched by | ||
| # UmbrellioUtils::Patches::RailsSemanticLogger, so that specs don't have to load | ||
| # the whole Rails stack. | ||
| module RailsSemanticLogger | ||
| module ActionController | ||
| class LogSubscriber | ||
| private | ||
|
|
||
| def extract_path(path) | ||
| index = path.index("?") | ||
| index ? path[0, index] : path | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "umbrellio_utils/patches/active_support_event" | ||
|
|
||
| describe UmbrellioUtils::Patches::ActiveSupportEvent do | ||
| let(:event) do | ||
| ActiveSupport::Notifications::Event.new("some.event", nil, nil, "42", {}) | ||
| end | ||
|
|
||
| it "tracks GVL time and malloc stats between start! and finish!" do | ||
| event.start! | ||
| Array.new(100) { "x" * 100 } | ||
| event.finish! | ||
|
|
||
| expect(event.gvl_time).to be_a(Float) | ||
| expect(event.gvl_time).to be >= 0 | ||
| expect(event.malloc_increase_bytes).to be_an(Integer) | ||
| end | ||
|
|
||
| it "returns zero values for a non-started event" do | ||
| expect(event.gvl_time).to eq(0.0) | ||
| expect(event.malloc_increase_bytes).to eq(0) | ||
| end | ||
|
|
||
| describe "#stats" do | ||
| before do | ||
| event.start! | ||
| event.finish! | ||
| end | ||
|
|
||
| it "returns the shared timing/allocation field set" do | ||
| expect(event.stats).to match( | ||
| gc_time: be_a(Numeric), | ||
| gvl_time: be_a(Float), | ||
| cpu_time: be_a(Numeric), | ||
| idle_time: be_a(Numeric), | ||
| allocations: be_an(Integer), | ||
| malloc_increase_bytes: be_an(Integer), | ||
| ) | ||
| end | ||
|
|
||
| it "rounds timings to the given precision" do | ||
| expect(event.stats(precision: 2)[:gvl_time]).to eq(event.gvl_time.round(2)) | ||
| end | ||
| end | ||
| end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.