-
Notifications
You must be signed in to change notification settings - Fork 380
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add stack trace collection to meta_struct and actions_handler
- Loading branch information
Showing
21 changed files
with
727 additions
and
1 deletion.
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
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,24 @@ | ||
# frozen_string_literal: true | ||
|
||
module Datadog | ||
module Tracing | ||
module Metadata | ||
# Adds stack traces to meta_struct | ||
# @public_api | ||
module StackTrace | ||
def set_stack_trace(stack_trace, group:) | ||
meta_struct[Metadata::Ext::MetaStruct::TAG_STACK_TRACE] ||= {} | ||
meta_struct[Metadata::Ext::MetaStruct::TAG_STACK_TRACE][group] ||= [] | ||
|
||
stack_trace_group = meta_struct[Metadata::Ext::MetaStruct::TAG_STACK_TRACE][group] | ||
max_collect = Datadog.configuration.appsec.stack_trace.max_collect | ||
return if max_collect > 0 && stack_trace_group.size >= max_collect | ||
|
||
stack_trace_group << stack_trace | ||
rescue StandardError => e | ||
Datadog.logger.debug("Unable to add stack_trace #{stack_trace.id} in meta_struct, ignoring it. Caused by: #{e}") | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# frozen_string_literal: true | ||
|
||
require_relative 'frame' | ||
|
||
module Datadog | ||
module Tracing | ||
module StackTrace | ||
# Represent a stack trace with its id and message in message pack | ||
module Collector | ||
class << self | ||
def collect(locations) | ||
return [] if locations.nil? || locations.empty? | ||
|
||
skip_frames = skip_frames(locations.size) | ||
frames = [] | ||
|
||
locations.each_with_index do |location, index| | ||
next if skip_frames.include?(index) | ||
|
||
frames << StackTrace::Frame.new( | ||
id: index, | ||
text: location.to_s.encode('UTF-8'), | ||
file: file_path(location), | ||
line: location.lineno, | ||
function: function_label(location) | ||
) | ||
end | ||
frames | ||
end | ||
|
||
private | ||
|
||
def skip_frames(locations_size) | ||
max_depth = Datadog.configuration.appsec.stack_trace.max_depth | ||
return [] if max_depth == 0 || locations_size <= max_depth | ||
|
||
top_frames_limit = (max_depth * Datadog.configuration.appsec.stack_trace.max_depth_top_percent / 100.0).round | ||
bottom_frames_limit = locations_size - (max_depth - top_frames_limit) | ||
(top_frames_limit...bottom_frames_limit) | ||
end | ||
|
||
def file_path(location) | ||
path = location.absolute_path || location.path | ||
return if path.nil? | ||
|
||
path.encode('UTF-8') | ||
end | ||
|
||
def function_label(location) | ||
label = location.label | ||
return if label.nil? | ||
|
||
label.encode('UTF-8') | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
# frozen_string_literal: true | ||
|
||
module Datadog | ||
module Tracing | ||
module StackTrace | ||
# Formatted stack frame. | ||
# This class extends a Struct as it's required by Steep to be able to add a method to it. | ||
class Frame < Struct.new(:id, :text, :file, :line, :function, keyword_init: true) # rubocop:disable Style/StructInheritance | ||
def to_msgpack(packer = nil) | ||
packer ||= MessagePack::Packer.new | ||
|
||
packer.write_map_header(5) | ||
packer.write('id') | ||
packer.write(id) | ||
packer.write('text') | ||
packer.write(text) | ||
packer.write('file') | ||
packer.write(file) | ||
packer.write('line') | ||
packer.write(line) | ||
packer.write('function') | ||
packer.write(function) | ||
packer | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module Datadog | ||
module Tracing | ||
module StackTrace | ||
# Represent a stack trace with its id and message in message pack | ||
class Representor < Struct.new(:id, :message, :frames, keyword_init: true) # rubocop:disable Style/StructInheritance | ||
def to_msgpack(packer = nil) | ||
packer ||= MessagePack::Packer.new | ||
|
||
packer.write_map_header(4) | ||
packer.write('language') | ||
packer.write('ruby') | ||
packer.write('id') | ||
packer.write(id) | ||
packer.write('message') | ||
packer.write(message) | ||
packer.write('frames') | ||
packer.write(frames) | ||
packer | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Datadog | ||
module Tracing | ||
module Metadata | ||
module StackTrace | ||
def set_stack_trace: (Datadog::Tracing::StackTrace::Representor stack_trace, group: ::String) -> void | ||
|
||
private | ||
|
||
# Class/Module that includes this module should implement this method | ||
def meta_struct: () -> Hash[String, untyped] | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Datadog | ||
module Tracing | ||
module StackTrace | ||
module Collector | ||
def self.collect: (Array[Thread::Backtrace::Location] locations) -> Array[Datadog::Tracing::StackTrace::Frame] | ||
|
||
private | ||
|
||
def self.skip_frames: (Integer locations_size) -> (Range[Integer] | Array[untyped]) | ||
|
||
def self.file_path: (Thread::Backtrace::Location location) -> String? | ||
|
||
def self.function_label: (Thread::Backtrace::Location location) -> String? | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
module Datadog | ||
module Tracing | ||
module StackTrace | ||
class Frame | ||
attr_reader id: Integer | ||
attr_reader text: String? | ||
attr_reader file: String? | ||
attr_reader line: Integer? | ||
attr_reader function: String? | ||
|
||
def initialize: (?id: Integer, ?text: String?, ?file: String?, ?line: Integer?, ?function: String?) -> void | ||
|
||
def to_msgpack: ((::MessagePack::Packer | nil) packer) -> ::MessagePack::Packer | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
module Datadog | ||
module Tracing | ||
module StackTrace | ||
class Representor | ||
attr_reader id: String? | ||
attr_reader message: String? | ||
attr_reader frames: Array[StackTrace::Frame]? | ||
|
||
def initialize: (?id: String?, ?message: String?, ?frames: Array[StackTrace::Frame]?) -> void | ||
|
||
def to_msgpack: ((::MessagePack::Packer | nil) packer) -> ::MessagePack::Packer | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.