Skip to content

Commit

Permalink
Add stack trace class
Browse files Browse the repository at this point in the history
  • Loading branch information
vpellan committed Jan 14, 2025
1 parent 2456cd3 commit e8cb243
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
56 changes: 56 additions & 0 deletions lib/datadog/appsec/stack_trace.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# frozen_string_literal: true

module Datadog
module AppSec
# Stack trace
class StackTrace
attr_reader :id, :message, :frames

def initialize(id, stack_trace, message: nil)
@id = id
@message = message
@frames = stack_trace.each_with_index.map do |frame, index|
Frame.new(
index,
text: frame.to_s,
file: frame.absolute_path, # path will show 'main' instead of the actual file
line: frame.lineno,
function: frame.base_label # label will show 'block in function' instead of just 'function'
)
end
end

def to_h
@to_h ||= {
language: 'ruby',
id: @id,
message: @message,
frames: @frames.map(&:to_h)
}
end

# Stack frame
class Frame
attr_reader :id, :text, :file, :line, :function

def initialize(id, text: nil, file: nil, line: nil, function: nil)
@id = id
@text = text
@file = file
@line = line
@function = function
end

def to_h
@to_h ||= {
id: @id,
text: @text,
file: @file,
line: @line,
function: @function
}
end
end
end
end
end
27 changes: 27 additions & 0 deletions sig/datadog/appsec/stack_trace.rbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Datadog
module AppSec
class StackTrace
attr_reader id: ::String
attr_reader message: ::String?
attr_reader frames: ::Array[Frame]

@to_h: ::Hash[::Symbol, untyped]

def initialize: (::String id, ::Array[::Thread::Backtrace::Location] stack_trace, ?message: ::String?) -> void

class Frame
attr_reader id: ::Integer
attr_reader text: ::String?
attr_reader file: ::String?
attr_reader line: ::Integer?
attr_reader function: ::String?

@to_h: ::Hash[::Symbol, untyped]

def initialize: (::Integer id, ?text: ::String?, ?file: ::String?, ?line: ::Integer?, ?function: ::String?) -> void

def to_h: () -> ::Hash[::Symbol, untyped]
end
end
end
end

0 comments on commit e8cb243

Please sign in to comment.