-
-
Notifications
You must be signed in to change notification settings - Fork 494
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9c90d2d
commit 2b18591
Showing
8 changed files
with
141 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 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 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,87 @@ | ||
# frozen_string_literal: true | ||
|
||
require "etc" | ||
require "stackprof" | ||
require "securerandom" | ||
|
||
module Sentry | ||
class Profiler | ||
|
||
VERSION = "1" | ||
PLATFORM = "ruby" | ||
# 101 Hz in microseconds | ||
DEFAULT_INTERVAL = 1e6 / 101 | ||
|
||
def initialize | ||
@event_id = SecureRandom.uuid.delete("-") | ||
end | ||
|
||
def start | ||
# TODO-neel profiler args | ||
StackProf.start(interval: DEFAULT_INTERVAL, raw: true, aggregate: false) | ||
end | ||
|
||
def stop | ||
StackProf.stop | ||
end | ||
|
||
def to_hash | ||
return nil unless Sentry.initialized? | ||
|
||
results = StackProf.results | ||
return nil unless results | ||
return nil if results.empty? | ||
|
||
report = StackProf::Report.new(results) | ||
frame_map = {} | ||
|
||
frames = report.frames.to_enum.with_index.map do |frame, idx| | ||
frame_id, frame_data = frame | ||
|
||
# need to map over stackprof frame ids to ours | ||
frame_map[frame_id] = idx | ||
|
||
# TODO-neel module, filename | ||
{ | ||
abs_path: frame_data[:file], | ||
function: frame_data[:name], | ||
lineno: frame_data[:line] | ||
} | ||
end | ||
|
||
raw_stacks, * = report.flamegraph_stacks(results[:raw]) | ||
|
||
# map over frame ids | ||
# reverse each stack since perl flamegraph is reverse order | ||
# TODO-neel compact ok?? | ||
stacks = raw_stacks.map { |stack| stack.reverse.map { |id| frame_map[id] }.compact } | ||
|
||
# stacks are all unique so stack ids are just 0...len | ||
elapsed_since_start_ns = 0 | ||
|
||
samples = stacks.size.times.map do |stack_id| | ||
# stackprof deltas are in microseconds | ||
elapsed_since_start_ns += (results[:raw_timestamp_deltas][stack_id] * 1e3).to_i | ||
|
||
{ | ||
stack_id: stack_id, | ||
thread_id: '42', # TODO-neel not available??? | ||
elapsed_since_start_ns: elapsed_since_start_ns | ||
} | ||
end | ||
|
||
profile = { | ||
frames: frames, | ||
stacks: stacks, | ||
samples: samples | ||
} | ||
|
||
{ | ||
event_id: @event_id, | ||
platform: PLATFORM, | ||
version: VERSION, | ||
profile: profile | ||
} | ||
end | ||
end | ||
end |
This file contains 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 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 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 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 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