Skip to content

Commit ef62258

Browse files
committed
Respond to code cleanliness comments; still need to fix logic/impl
1 parent 6c25fde commit ef62258

File tree

15 files changed

+89
-68
lines changed

15 files changed

+89
-68
lines changed

Rakefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,8 @@ namespace :spec do
229229
t.pattern = CORE_WITH_LIBDATADOG_API.join(', ')
230230
t.rspec_opts = args.to_a.join(' ')
231231
end.tap do |t|
232+
# Core with libdatadog is dependent on profiling because crashtracking runtime stacks logic
233+
# lives in the profiling extension (they share same logic accessing Ruby internals)
232234
Rake::Task[t.name].enhance(
233235
[
234236
"compile:libdatadog_api.#{RUBY_VERSION[/\d+.\d+/]}_#{RUBY_PLATFORM}",

ext/datadog_profiling_native_extension/runtime_stacks.c renamed to ext/datadog_profiling_native_extension/crashtracking_runtime_stacks.c

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
// NOTE: This file is a part of the profiling native extension even though the
2+
// runtime stacks feature is consumed by the crashtracker. The profiling
3+
// extension already carries all the Ruby VM private header access and build
4+
// plumbing required to safely poke at internal structures. Sharing that setup
5+
// avoids duplicating another native extension with the same (fragile) access
6+
// patterns, and keeps the overall install/build surface area smaller.
17
#include "extconf.h"
28

39
#ifdef RUBY_MJIT_HEADER
@@ -32,7 +38,7 @@
3238
#endif
3339
#endif
3440

35-
#include "runtime_stacks.h"
41+
#include "crashtracking_runtime_stacks.h"
3642
#include <datadog/crashtracker.h>
3743
#include "datadog_ruby_common.h"
3844
#include "private_vm_api_access.h"
@@ -41,13 +47,9 @@
4147
#include <errno.h>
4248
#include <string.h>
4349

44-
// This was renamed in Ruby 3.2
45-
#if !defined(ccan_list_for_each) && defined(list_for_each)
46-
#define ccan_list_for_each list_for_each
47-
#endif
48-
4950
static VALUE _native_register_runtime_stack_callback(VALUE _self);
5051
static VALUE _native_is_runtime_callback_registered(DDTRACE_UNUSED VALUE _self);
52+
static const rb_data_type_t *crashtracker_thread_data_type = NULL;
5153

5254
static void ruby_runtime_stack_callback(
5355
void (*emit_frame)(const ddog_crasht_RuntimeStackFrame*)
@@ -202,13 +204,9 @@ static void ruby_runtime_stack_callback(
202204
VALUE current_thread = rb_thread_current();
203205
if (current_thread == Qnil) return;
204206

205-
static const rb_data_type_t *thread_data_type = NULL;
206-
if (thread_data_type == NULL) {
207-
thread_data_type = RTYPEDDATA_TYPE(current_thread);
208-
if (!thread_data_type) return;
209-
}
207+
if (crashtracker_thread_data_type == NULL) return;
210208

211-
rb_thread_t *th = (rb_thread_t *) rb_check_typeddata(current_thread, thread_data_type);
209+
rb_thread_t *th = (rb_thread_t *) rb_check_typeddata(current_thread, crashtracker_thread_data_type);
212210
if (!th) return;
213211

214212
const rb_execution_context_t *ec = th->ec;
@@ -374,6 +372,16 @@ static void ruby_runtime_stack_callback(
374372
}
375373

376374
static VALUE _native_register_runtime_stack_callback(DDTRACE_UNUSED VALUE _self) {
375+
if (crashtracker_thread_data_type == NULL) {
376+
VALUE current_thread = rb_thread_current();
377+
if (current_thread == Qnil) return Qfalse;
378+
379+
const rb_data_type_t *thread_data_type = RTYPEDDATA_TYPE(current_thread);
380+
if (!thread_data_type) return Qfalse;
381+
382+
crashtracker_thread_data_type = thread_data_type;
383+
}
384+
377385
enum ddog_crasht_CallbackResult result = ddog_crasht_register_runtime_frame_callback(
378386
ruby_runtime_stack_callback
379387
);
@@ -392,8 +400,10 @@ static VALUE _native_is_runtime_callback_registered(DDTRACE_UNUSED VALUE _self)
392400
return ddog_crasht_is_runtime_callback_registered() ? Qtrue : Qfalse;
393401
}
394402

395-
void runtime_stacks_init(VALUE datadog_module) {
396-
VALUE runtime_stacks_class = rb_define_class_under(datadog_module, "RuntimeStacks", rb_cObject);
403+
void crashtracking_runtime_stacks_init(VALUE datadog_module) {
404+
VALUE core_module = rb_define_module_under(datadog_module, "Core");
405+
VALUE crashtracking_module = rb_define_module_under(core_module, "Crashtracking");
406+
VALUE runtime_stacks_class = rb_define_class_under(crashtracking_module, "RuntimeStacks", rb_cObject);
397407

398408
rb_define_singleton_method(
399409
runtime_stacks_class,
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#pragma once
2+
3+
void crashtracking_runtime_stacks_init(VALUE datadog_module);
4+

ext/datadog_profiling_native_extension/profiling.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
#include "helpers.h"
1010
#include "private_vm_api_access.h"
1111
#include "ruby_helpers.h"
12-
#include "runtime_stacks.h"
12+
#include "crashtracking_runtime_stacks.h"
1313
#include "setup_signal_handler.h"
1414
#include "time_helpers.h"
1515
#include "unsafe_api_calls_check.h"
@@ -66,7 +66,7 @@ void DDTRACE_EXPORT Init_datadog_profiling_native_extension(void) {
6666
encoded_profile_init(profiling_module);
6767
http_transport_init(profiling_module);
6868
stack_recorder_init(profiling_module);
69-
runtime_stacks_init(datadog_module);
69+
crashtracking_runtime_stacks_init(datadog_module);
7070
unsafe_api_calls_check_init();
7171

7272
// Hosts methods used for testing the native code using RSpec

ext/datadog_profiling_native_extension/runtime_stacks.h

Lines changed: 0 additions & 4 deletions
This file was deleted.

lib/datadog/core.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@ module Core
2020
rescue LoadError => e
2121
e.message
2222
end
23-
24-
RUNTIME_STACKS_FAILURE =
25-
begin
26-
require 'datadog/runtime_stacks'
27-
nil
28-
rescue LoadError => e
29-
e.message
30-
end
3123
end
3224

3325
DATADOG_ENV = Core::Configuration::ConfigHelper.new

lib/datadog/core/configuration/supported_configurations.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ module Configuration
3232
"DD_APPSEC_TRACE_RATE_LIMIT" => {version: ["A"]},
3333
"DD_APPSEC_WAF_DEBUG" => {version: ["A"]},
3434
"DD_APPSEC_WAF_TIMEOUT" => {version: ["A"]},
35-
"DD_CRASHTRACKER_EMIT_RUNTIME_STACKS" => {version: ["A"]},
35+
"DD_CRASHTRACKING_EMIT_RUNTIME_STACKS" => {version: ["A"]},
3636
"DD_CRASHTRACKING_ENABLED" => {version: ["A"]},
3737
"DD_DATA_STREAMS_ENABLED" => {version: ["A"]},
3838
"DD_DBM_PROPAGATION_MODE" => {version: ["A"]},

lib/datadog/core/crashtracking/component.rb

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@
99
module Datadog
1010
module Core
1111
module Crashtracking
12+
RUNTIME_STACKS_FAILURE =
13+
begin
14+
require_relative 'crashtracking_runtime_stacks'
15+
nil
16+
rescue LoadError => e
17+
e.message
18+
end
19+
1220
# Used to report Ruby VM crashes.
1321
#
1422
# NOTE: The crashtracker native state is a singleton;
@@ -57,9 +65,7 @@ def start
5765
Utils::AtForkMonkeyPatch.apply!
5866

5967
start_or_update_on_fork(action: :start, tags: tags)
60-
if DATADOG_ENV['DD_CRASHTRACKER_EMIT_RUNTIME_STACKS'] == 'true'
61-
register_runtime_stack_callback
62-
end
68+
register_runtime_stack_callback if DATADOG_ENV['DD_CRASHTRACKING_EMIT_RUNTIME_STACKS'] == 'true'
6369

6470
ONLY_ONCE.run do
6571
Utils::AtForkMonkeyPatch.at_fork(:child) do
@@ -84,39 +90,25 @@ def stop
8490
logger.error("Failed to stop crash tracking: #{e.message}")
8591
end
8692

87-
def runtime_callback_registered?
88-
return false if Datadog::Core::RUNTIME_STACKS_FAILURE
89-
return false unless Datadog.const_defined?(:RuntimeStacks, false)
90-
91-
Datadog::RuntimeStacks._native_is_runtime_callback_registered
92-
rescue => e
93-
logger.debug("Runtime stack callback status check not available: #{e.message}")
94-
false
95-
end
96-
9793
private
9894

9995
def register_runtime_stack_callback
10096
# Check if runtime_stacks extension loaded successfully
101-
if Datadog::Core::RUNTIME_STACKS_FAILURE
102-
logger.debug("Skipping runtime stack callback registration: #{Datadog::Core::RUNTIME_STACKS_FAILURE}")
97+
if Crashtracking::RUNTIME_STACKS_FAILURE
98+
logger.debug("Skipping runtime stack callback registration: #{Crashtracking::RUNTIME_STACKS_FAILURE}")
10399
return
104100
end
105101

106-
unless Datadog.const_defined?(:RuntimeStacks, false)
107-
logger.debug('Skipping runtime stack callback registration: Datadog::RuntimeStacks not defined')
102+
unless Crashtracking.const_defined?(:RuntimeStacks, false)
103+
logger.debug('Skipping runtime stack callback registration: Crashtracking::RuntimeStacks not defined')
108104
return
109105
end
110106

111-
success = Datadog::RuntimeStacks._native_register_runtime_stack_callback
107+
success = Crashtracking::RuntimeStacks._native_register_runtime_stack_callback
112108

113-
unless success
114-
error_message = 'Failed to register runtime stack callback: registration returned false'
115-
logger.error(error_message)
116-
raise StandardError, error_message
117-
end
109+
raise StandardError, 'Failed to register runtime stack callback: registration returned false' unless success
118110
rescue => e
119-
logger.error("Failed to register runtime stack callback: #{e.message}")
111+
logger.warn("Failed to register runtime stack callback: #{e.message}")
120112
raise
121113
end
122114

lib/datadog/runtime_stacks.rb renamed to lib/datadog/core/crashtracking/crashtracking_runtime_stacks.rb

File renamed without changes.

sig/datadog/core.rbs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
module Datadog
22
module Core
33
LIBDATADOG_API_FAILURE: ::String?
4-
RUNTIME_STACKS_FAILURE: ::String?
54
extend Core::Deprecations
65
end
76
end

0 commit comments

Comments
 (0)