diff --git a/lib/datadog/appsec/stack_trace.rb b/lib/datadog/appsec/stack_trace.rb new file mode 100644 index 00000000000..1211ef9380b --- /dev/null +++ b/lib/datadog/appsec/stack_trace.rb @@ -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 \ No newline at end of file diff --git a/sig/datadog/appsec/stack_trace.rbs b/sig/datadog/appsec/stack_trace.rbs new file mode 100644 index 00000000000..c940b9d776f --- /dev/null +++ b/sig/datadog/appsec/stack_trace.rbs @@ -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