-
Notifications
You must be signed in to change notification settings - Fork 377
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
Add stack trace collection support #4269
Draft
vpellan
wants to merge
2
commits into
vpellan/meta-struct
Choose a base branch
from
vpellan/stack-trace-collection
base: vpellan/meta-struct
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 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
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 | ||
|
||
require_relative 'stack_trace' | ||
|
||
module Datadog | ||
module Tracing | ||
module Metadata | ||
# Adds complex structures tagging behavior through meta_struct | ||
# @public_api | ||
module MetaStruct | ||
include StackTrace | ||
|
||
def set_meta_struct(meta_struct) | ||
self.meta_struct.merge!(meta_struct) | ||
end | ||
|
||
protected | ||
|
||
def meta_struct | ||
@meta_struct ||= {} | ||
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,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
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,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 |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⚪ Code Quality Violation
Consider using ranges or between to simplify your comparison (...read more)
The rule "Prefer ranges/between over complex comparisons" advises developers to use the range or
between?
method for comparisons instead of complex conditional statements. This practice increases the readability and clarity of your code. Complex comparisons using logical operators can be difficult to understand and prone to errors.This rule is important because it promotes cleaner, more efficient, and easier-to-read code. When code is easier to read, it's easier to maintain, debug, and less likely to contain hidden bugs. Using the range or
between?
method is a more concise way to check if a value falls within a specific range.To adhere to this rule, replace complex comparison statements with the range or
between?
method. For example, instead of writingfoo >= 42 && foo <= 99
, you can write(42..99).include?(foo)
orfoo.between?(42, 99)
. These alternatives are more straightforward and visually cleaner, making your code easier to understand.