datadog 2.39.0 → 2.40.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +27 -1
- data/ext/datadog_profiling_native_extension/collectors_stack.c +218 -147
- data/ext/datadog_profiling_native_extension/collectors_stack.h +16 -2
- data/ext/datadog_profiling_native_extension/collectors_thread_context.c +12 -7
- data/ext/datadog_profiling_native_extension/crashtracking_runtime_stacks.c +5 -4
- data/ext/datadog_profiling_native_extension/datadog_ruby_common.c +19 -11
- data/ext/datadog_profiling_native_extension/datadog_ruby_common.h +7 -0
- data/ext/datadog_profiling_native_extension/extconf.rb +12 -7
- data/ext/datadog_profiling_native_extension/private_vm_api_access.c +259 -65
- data/ext/datadog_profiling_native_extension/private_vm_api_access.h +16 -10
- data/ext/libdatadog_api/datadog_ruby_common.c +19 -11
- data/ext/libdatadog_api/datadog_ruby_common.h +7 -0
- data/ext/libdatadog_api/init.c +4 -0
- data/ext/libdatadog_api/trace_exporter.c +974 -0
- data/ext/libdatadog_api/trace_exporter.h +5 -0
- data/lib/datadog/ai_guard/evaluation.rb +1 -0
- data/lib/datadog/ai_guard/ext.rb +2 -0
- data/lib/datadog/appsec/configuration.rb +9 -0
- data/lib/datadog/appsec/contrib/devise/patcher.rb +1 -1
- data/lib/datadog/appsec/contrib/rack/patcher.rb +1 -1
- data/lib/datadog/appsec/contrib/rack/request_body_middleware.rb +6 -3
- data/lib/datadog/appsec/contrib/rails/request_middleware.rb +2 -1
- data/lib/datadog/appsec/contrib/sinatra/request_middleware.rb +2 -1
- data/lib/datadog/core/configuration/settings.rb +33 -41
- data/lib/datadog/core/configuration/supported_configurations.rb +3 -0
- data/lib/datadog/core/environment/gc.rb +1 -1
- data/lib/datadog/core/environment/vm_cache.rb +1 -1
- data/lib/datadog/core/environment/yjit.rb +2 -2
- data/lib/datadog/core/telemetry/event/synth_app_client_configuration_change.rb +1 -1
- data/lib/datadog/core/utils/at_fork_monkey_patch.rb +80 -12
- data/lib/datadog/core/utils/sequence.rb +3 -6
- data/lib/datadog/core/utils/time.rb +16 -54
- data/lib/datadog/di/el/compiler.rb +65 -12
- data/lib/datadog/di/el/evaluator.rb +82 -2
- data/lib/datadog/di/el/expression.rb +10 -2
- data/lib/datadog/di/probe_builder.rb +6 -6
- data/lib/datadog/profiling/collectors/thread_context.rb +6 -13
- data/lib/datadog/profiling/component.rb +1 -0
- data/lib/datadog/profiling.rb +4 -0
- data/lib/datadog/tracing/component.rb +21 -0
- data/lib/datadog/tracing/configuration/ext.rb +1 -0
- data/lib/datadog/tracing/configuration/settings.rb +16 -0
- data/lib/datadog/tracing/contrib/hanami/router_tracing.rb +2 -1
- data/lib/datadog/tracing/contrib/kafka/events/connection/request.rb +4 -0
- data/lib/datadog/tracing/contrib/kafka/events/produce_operation/send_messages.rb +7 -2
- data/lib/datadog/tracing/contrib/kafka/events/producer/deliver_messages.rb +7 -2
- data/lib/datadog/tracing/contrib/sequel/database.rb +1 -5
- data/lib/datadog/tracing/distributed/propagation_policy.rb +5 -0
- data/lib/datadog/tracing/sync_writer.rb +8 -3
- data/lib/datadog/tracing/tracer.rb +8 -0
- data/lib/datadog/tracing/transport/native/response.rb +71 -0
- data/lib/datadog/tracing/transport/native.rb +363 -0
- data/lib/datadog/tracing/transport/trace_formatter.rb +1 -1
- data/lib/datadog/tracing/writer.rb +16 -3
- data/lib/datadog/version.rb +1 -1
- metadata +9 -5
|
@@ -30,6 +30,13 @@
|
|
|
30
30
|
#include <iseq.h>
|
|
31
31
|
#pragma GCC diagnostic pop
|
|
32
32
|
|
|
33
|
+
#ifndef NO_INTERNAL_CLASS_HEADER_INCLUDE
|
|
34
|
+
#pragma GCC diagnostic push
|
|
35
|
+
#pragma GCC diagnostic ignored "-Wunused-parameter"
|
|
36
|
+
#include <internal/class.h>
|
|
37
|
+
#pragma GCC diagnostic pop
|
|
38
|
+
#endif
|
|
39
|
+
|
|
33
40
|
#include <ruby.h>
|
|
34
41
|
|
|
35
42
|
#ifndef NO_RACTOR_HEADER_INCLUDE
|
|
@@ -38,6 +45,7 @@
|
|
|
38
45
|
#include <ractor_core.h>
|
|
39
46
|
#pragma GCC diagnostic pop
|
|
40
47
|
#endif
|
|
48
|
+
|
|
41
49
|
#endif
|
|
42
50
|
|
|
43
51
|
// This file can't include datadog_ruby_common.h so we replicate this here
|
|
@@ -50,6 +58,8 @@
|
|
|
50
58
|
#define PRIVATE_VM_API_ACCESS_SKIP_RUBY_INCLUDES
|
|
51
59
|
#include "private_vm_api_access.h"
|
|
52
60
|
|
|
61
|
+
static inline const rb_callable_method_entry_t* get_cfunc_method_entry(const rb_control_frame_t *cfp);
|
|
62
|
+
|
|
53
63
|
// MRI has a similar rb_thread_ptr() function which we can't call it directly
|
|
54
64
|
// because Ruby does not expose the thread_data_type publicly.
|
|
55
65
|
// Instead, we have our own version of that function, and we lazily initialize the thread_data_type pointer
|
|
@@ -76,18 +86,6 @@ rb_nativethread_id_t pthread_id_for(VALUE thread) {
|
|
|
76
86
|
#endif
|
|
77
87
|
}
|
|
78
88
|
|
|
79
|
-
size_t sizeof_rb_iseq_t(void) {
|
|
80
|
-
return sizeof(rb_iseq_t);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
VALUE ddtrace_iseq_base_label(const void *iseq) {
|
|
84
|
-
return rb_iseq_base_label((const rb_iseq_t *)iseq);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
VALUE ddtrace_iseq_path(const void *iseq) {
|
|
88
|
-
return rb_iseq_path((const rb_iseq_t *)iseq);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
89
|
// Queries if the current thread is the owner of the global VM lock.
|
|
92
90
|
//
|
|
93
91
|
// @ivoanjo: Ruby has a similarly-named `ruby_thread_has_gvl_p` but that API is insufficient for our needs because it can
|
|
@@ -514,48 +512,43 @@ int ddtrace_rb_profile_frames(VALUE thread, int start, int limit, frame_info *st
|
|
|
514
512
|
// rb_profile_frames does not do this check, but `backtrace_each` (`vm_backtrace.c`) does. This frame is not
|
|
515
513
|
// exposed by the Ruby backtrace APIs and for now we want to match its behavior 1:1
|
|
516
514
|
}
|
|
515
|
+
else if (cfp->ep == NULL) {
|
|
516
|
+
// Do nothing -- this frame should not be used
|
|
517
|
+
//
|
|
518
|
+
// We're not sure this can ever happen, but we've seen a crash inside `VM_FRAME_RUBYFRAME_P` below (which
|
|
519
|
+
// dereferences `cfp->ep`), so "just in case" we're adding this extra sanity check to avoid crashing on a
|
|
520
|
+
// NULL `ep`.
|
|
521
|
+
}
|
|
517
522
|
else if (VM_FRAME_RUBYFRAME_P(cfp)) {
|
|
518
523
|
if (start > 0) {
|
|
519
524
|
start--;
|
|
520
525
|
continue;
|
|
521
526
|
}
|
|
522
527
|
|
|
528
|
+
cme = rb_vm_frame_method_entry(cfp);
|
|
529
|
+
|
|
530
|
+
// Upstream (Ruby 4.0) does:
|
|
531
|
+
// if (cme && cme->def->type == VM_METHOD_TYPE_ISEQ) {
|
|
532
|
+
// buff[i] = (VALUE)cme;
|
|
533
|
+
// } else {
|
|
534
|
+
// buff[i] = (VALUE)cfp->iseq;
|
|
535
|
+
// }
|
|
536
|
+
// We get both the iseq and CME because we need both to format like Ruby backtraces
|
|
537
|
+
|
|
523
538
|
stack_buffer[i].same_frame =
|
|
524
539
|
stack_buffer[i].is_ruby_frame &&
|
|
525
|
-
stack_buffer[i].as.ruby_frame.iseq ==
|
|
526
|
-
stack_buffer[i].as.ruby_frame.caching_pc == cfp->pc
|
|
540
|
+
stack_buffer[i].as.ruby_frame.iseq == cfp->iseq &&
|
|
541
|
+
stack_buffer[i].as.ruby_frame.caching_pc == cfp->pc &&
|
|
542
|
+
stack_buffer[i].cme == cme;
|
|
527
543
|
|
|
528
544
|
if (stack_buffer[i].same_frame) { // Nothing to do, buffer already contains this frame
|
|
529
545
|
i++;
|
|
530
546
|
continue;
|
|
531
547
|
}
|
|
532
548
|
|
|
533
|
-
|
|
534
|
-
// Upstream Ruby has code here to retrieve the rb_callable_method_entry_t (cme) and in some cases to use it
|
|
535
|
-
// instead of the iseq.
|
|
536
|
-
// In practice, they are usually the same; the difference is that when you have e.g. block, one gets you a
|
|
537
|
-
// reference to the block, and the other to the method containing the block.
|
|
538
|
-
// This would be important if we used `rb_profile_frame_label` and wanted the "block in foo" label instead
|
|
539
|
-
// of just "foo". But we're currently using `rb_profile_frame_base_label` which I believe is always the same
|
|
540
|
-
// between the rb_callable_method_entry_t and the iseq. Thus, to simplify a bit our logic and reduce a bit
|
|
541
|
-
// the overhead, we always use the iseq here.
|
|
542
|
-
//
|
|
543
|
-
// @ivoanjo: I've left the upstream Ruby code commented out below for reference, so it's more obvious that
|
|
544
|
-
// we're diverging, and we can easily compare and experiment with the upstream version in the future.
|
|
545
|
-
//
|
|
546
|
-
// cme = rb_vm_frame_method_entry(cfp);
|
|
547
|
-
|
|
548
|
-
// if (cme && cme->def->type == VM_METHOD_TYPE_ISEQ &&
|
|
549
|
-
// // Fix: Do not use callable method entry when iseq is for an eval.
|
|
550
|
-
// // TL;DR: This fix is needed for us to match the Ruby reference API information in the
|
|
551
|
-
// // "when sampling an eval/instance eval inside an object" spec.
|
|
552
|
-
// cfp->iseq->body->type != ISEQ_TYPE_EVAL) {
|
|
553
|
-
// buff[i] = (VALUE)cme;
|
|
554
|
-
// }
|
|
555
|
-
// else {
|
|
556
|
-
stack_buffer[i].as.ruby_frame.iseq = (VALUE)cfp->iseq;
|
|
549
|
+
stack_buffer[i].as.ruby_frame.iseq = cfp->iseq;
|
|
557
550
|
stack_buffer[i].as.ruby_frame.caching_pc = (void *) cfp->pc;
|
|
558
|
-
|
|
551
|
+
stack_buffer[i].cme = cme;
|
|
559
552
|
|
|
560
553
|
// The topmost frame may not have an updated PC because the JIT
|
|
561
554
|
// may not have set one. The JIT compiler will update the PC
|
|
@@ -575,7 +568,7 @@ int ddtrace_rb_profile_frames(VALUE thread, int start, int limit, frame_info *st
|
|
|
575
568
|
i++;
|
|
576
569
|
}
|
|
577
570
|
else {
|
|
578
|
-
cme =
|
|
571
|
+
cme = get_cfunc_method_entry(cfp);
|
|
579
572
|
if (cme && cme->def->type == VM_METHOD_TYPE_CFUNC) {
|
|
580
573
|
if (start > 0) {
|
|
581
574
|
start--;
|
|
@@ -584,16 +577,14 @@ int ddtrace_rb_profile_frames(VALUE thread, int start, int limit, frame_info *st
|
|
|
584
577
|
|
|
585
578
|
stack_buffer[i].same_frame =
|
|
586
579
|
!stack_buffer[i].is_ruby_frame &&
|
|
587
|
-
stack_buffer[i].
|
|
580
|
+
stack_buffer[i].cme == cme;
|
|
588
581
|
|
|
589
582
|
if (stack_buffer[i].same_frame) { // Nothing to do, buffer already contains this frame
|
|
590
583
|
i++;
|
|
591
584
|
continue;
|
|
592
585
|
}
|
|
593
586
|
|
|
594
|
-
stack_buffer[i].
|
|
595
|
-
stack_buffer[i].as.native_frame.method_id = cme->def->original_id;
|
|
596
|
-
stack_buffer[i].as.native_frame.function = cme->def->body.cfunc.func;
|
|
587
|
+
stack_buffer[i].cme = cme;
|
|
597
588
|
stack_buffer[i].is_ruby_frame = false;
|
|
598
589
|
i++;
|
|
599
590
|
}
|
|
@@ -629,24 +620,24 @@ int ddtrace_rb_profile_frames(VALUE thread, int start, int limit, frame_info *st
|
|
|
629
620
|
// to support our custom rb_profile_frames (see above)
|
|
630
621
|
// Modifications:
|
|
631
622
|
// * Removed debug checks (they were ifdef'd out anyway)
|
|
623
|
+
// * Optimized to avoid recursion
|
|
632
624
|
static rb_callable_method_entry_t *
|
|
633
|
-
check_method_entry(VALUE obj, int can_be_svar)
|
|
634
|
-
{
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
}
|
|
646
|
-
// fallthrough
|
|
647
|
-
default:
|
|
648
|
-
return NULL;
|
|
625
|
+
check_method_entry(VALUE obj, int can_be_svar) {
|
|
626
|
+
if (obj == Qfalse) {
|
|
627
|
+
return NULL;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
enum imemo_type type = imemo_type(obj);
|
|
631
|
+
if (type == imemo_ment) {
|
|
632
|
+
return (rb_callable_method_entry_t *)obj;
|
|
633
|
+
} else if (can_be_svar && type == imemo_svar) {
|
|
634
|
+
VALUE cref_or_me = ((struct vm_svar *)obj)->cref_or_me;
|
|
635
|
+
if (imemo_type(cref_or_me) == imemo_ment) {
|
|
636
|
+
return (rb_callable_method_entry_t *)cref_or_me;
|
|
649
637
|
}
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
return NULL;
|
|
650
641
|
}
|
|
651
642
|
|
|
652
643
|
// Taken from upstream vm_insnhelper.c at commit 5f10bd634fb6ae8f74a4ea730176233b0ca96954 (March 2022, Ruby 3.2 trunk)
|
|
@@ -664,7 +655,9 @@ rb_vm_frame_method_entry(const rb_control_frame_t *cfp)
|
|
|
664
655
|
rb_callable_method_entry_t *me;
|
|
665
656
|
|
|
666
657
|
while (!VM_ENV_LOCAL_P(ep)) {
|
|
667
|
-
if ((me = check_method_entry(ep[VM_ENV_DATA_INDEX_ME_CREF], FALSE)) != NULL)
|
|
658
|
+
if ((me = check_method_entry(ep[VM_ENV_DATA_INDEX_ME_CREF], FALSE)) != NULL) {
|
|
659
|
+
return me;
|
|
660
|
+
}
|
|
668
661
|
ep = VM_ENV_PREV_EP(ep);
|
|
669
662
|
}
|
|
670
663
|
|
|
@@ -672,6 +665,16 @@ rb_vm_frame_method_entry(const rb_control_frame_t *cfp)
|
|
|
672
665
|
}
|
|
673
666
|
#endif // RUBY_MJIT_HEADER
|
|
674
667
|
|
|
668
|
+
// Optimized version of rb_vm_frame_method_entry() for cfunc frames.
|
|
669
|
+
// Cfunc frames always have VM_ENV_FLAG_LOCAL set, so ep[-2] is the cme directly
|
|
670
|
+
// with no EP chain walk needed. The only complication is cfuncs that set $~ or $_
|
|
671
|
+
// (e.g. Regexp#match, String#gsub) which replace ep[-2] with an imemo_svar wrapping
|
|
672
|
+
// the cme - in that case svar->cref_or_me is always the cme (never a cref).
|
|
673
|
+
static inline const rb_callable_method_entry_t *
|
|
674
|
+
get_cfunc_method_entry(const rb_control_frame_t *cfp) {
|
|
675
|
+
return check_method_entry(cfp->ep[VM_ENV_DATA_INDEX_ME_CREF], TRUE);
|
|
676
|
+
}
|
|
677
|
+
|
|
675
678
|
#ifndef NO_RACTORS
|
|
676
679
|
// This API and definition are exported as a public symbol by the VM BUT the function header is not defined in any public header, so we
|
|
677
680
|
// repeat it here to be able to use in our code.
|
|
@@ -743,7 +746,7 @@ VALUE invoke_location_for(VALUE thread, int *line_location) {
|
|
|
743
746
|
if (iseq == NULL) return Qnil;
|
|
744
747
|
|
|
745
748
|
*line_location = NUM2INT(rb_iseq_first_lineno(iseq));
|
|
746
|
-
return
|
|
749
|
+
return ddtrace_iseq_path(iseq);
|
|
747
750
|
}
|
|
748
751
|
|
|
749
752
|
void self_test_mn_enabled(void) {
|
|
@@ -872,7 +875,198 @@ bool is_raised_flag_set(VALUE thread) { return thread_struct_from_object(thread)
|
|
|
872
875
|
void self_test_current_fiber_for(void) { } // Nothing to do
|
|
873
876
|
#endif
|
|
874
877
|
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
+
// Variant of functions related to Thread::Backtrace::Location#label in Ruby 4.0
|
|
879
|
+
// (location_label(), rb_gen_method_name(), calculate_iseq_label()),
|
|
880
|
+
// but formatting using a C buffer instead of allocating Ruby Strings.
|
|
881
|
+
// Also, those functions are static functions in vm_backtrace.c so not reusable.
|
|
882
|
+
// There is also AFAIK no reasonable way to create a rb_backtrace_location_t from outside that file.
|
|
883
|
+
|
|
884
|
+
// Return true if a given location is a C method or supposed to behave like one.
|
|
885
|
+
static bool location_cfunc_p(const rb_callable_method_entry_t *cme) {
|
|
886
|
+
if (!cme) return false;
|
|
887
|
+
|
|
888
|
+
switch (cme->def->type) {
|
|
889
|
+
case VM_METHOD_TYPE_CFUNC:
|
|
890
|
+
return true;
|
|
891
|
+
// Upstream, but we don't want that behavior:
|
|
892
|
+
// case VM_METHOD_TYPE_ISEQ:
|
|
893
|
+
// return is_internal_location(loc->cme->def->body.iseq.iseqptr);
|
|
894
|
+
default:
|
|
895
|
+
return false;
|
|
896
|
+
}
|
|
897
|
+
}
|
|
898
|
+
|
|
899
|
+
// RCLASS_SINGLETON_P() is only defined on Ruby 3.4+, so we have a copy for older versions
|
|
900
|
+
static bool ddtrace_RCLASS_SINGLETON_P(VALUE klass) {
|
|
901
|
+
return RB_TYPE_P(klass, T_CLASS) && FL_TEST_RAW(klass, FL_SINGLETON);
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
static VALUE get_class_attached_object(VALUE klass) {
|
|
905
|
+
#ifndef NO_CLASS_ATTACHED_OBJECT
|
|
906
|
+
return rb_class_attached_object(klass);
|
|
907
|
+
#else
|
|
908
|
+
return rb_ivar_get(klass, rb_intern("__attached__"));
|
|
909
|
+
#endif
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
static bool is_metaclass(VALUE mod, VALUE* attached) {
|
|
913
|
+
if (ddtrace_RCLASS_SINGLETON_P(mod)) {
|
|
914
|
+
VALUE attached_object = get_class_attached_object(mod);
|
|
915
|
+
if (RB_TYPE_P(attached_object, T_CLASS) || RB_TYPE_P(attached_object, T_MODULE)) {
|
|
916
|
+
*attached = attached_object;
|
|
917
|
+
return true;
|
|
918
|
+
}
|
|
919
|
+
}
|
|
920
|
+
return false;
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
static VALUE alloc_free_rb_mod_name(VALUE mod) {
|
|
924
|
+
#ifdef NO_ALLOC_FREE_MOD_NAME
|
|
925
|
+
return rb_attr_get(mod, rb_intern("__classpath__"));
|
|
926
|
+
#else
|
|
927
|
+
return rb_mod_name(mod);
|
|
928
|
+
#endif
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
// Ruby 3.3+ has a `permanent_classpath` flag on rb_classext_struct.
|
|
932
|
+
// Checking for '#' in the name is equivalent on older Rubies:
|
|
933
|
+
// non-permanent names contain '#' from `#<Module:0x...>` or `#<Class:0x...>` prefixes.
|
|
934
|
+
static bool has_permanent_classpath(DDTRACE_UNUSED VALUE mod, DDTRACE_UNUSED VALUE mod_name) {
|
|
935
|
+
#if defined(RCLASS_PERMANENT_CLASSPATH_P) // 4.0+
|
|
936
|
+
return RCLASS_PERMANENT_CLASSPATH_P(mod);
|
|
937
|
+
#elif defined(HAVE_PERMANENT_CLASSPATH) // 3.3 - 3.4
|
|
938
|
+
return RCLASS_EXT(mod)->permanent_classpath;
|
|
939
|
+
#else // 3.2 and older
|
|
940
|
+
return memchr(RSTRING_PTR(mod_name), '#', RSTRING_LEN(mod_name)) == NULL;
|
|
941
|
+
#endif
|
|
942
|
+
}
|
|
943
|
+
|
|
944
|
+
#define ONLY_METHOD_NAME ((ssize_t) -1)
|
|
945
|
+
#define BUFFER_OUT_OF_SPACE ((ssize_t) -2)
|
|
946
|
+
|
|
947
|
+
static ssize_t rb_gen_method_name(VALUE owner, VALUE method_name, char *buf, size_t buf_size) {
|
|
948
|
+
if (!(RB_TYPE_P(owner, T_CLASS) || RB_TYPE_P(owner, T_MODULE))) {
|
|
949
|
+
return ONLY_METHOD_NAME;
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
VALUE mod = owner;
|
|
953
|
+
char separator = '#';
|
|
954
|
+
if (is_metaclass(owner, &mod)) {
|
|
955
|
+
separator = '.';
|
|
956
|
+
}
|
|
957
|
+
VALUE mod_name = alloc_free_rb_mod_name(mod);
|
|
958
|
+
|
|
959
|
+
// Exclude non-permanent names (e.g. `#<Module:0x0123>::Foo`) which break flamegraph aggregation
|
|
960
|
+
// since they contain addresses that differ across processes/runs.
|
|
961
|
+
if (NIL_P(mod_name) || !has_permanent_classpath(mod, mod_name)) {
|
|
962
|
+
return ONLY_METHOD_NAME;
|
|
963
|
+
}
|
|
964
|
+
|
|
965
|
+
size_t mod_name_len = RSTRING_LEN(mod_name);
|
|
966
|
+
size_t method_name_len = RSTRING_LEN(method_name);
|
|
967
|
+
size_t total = mod_name_len + 1 + method_name_len;
|
|
968
|
+
if (total <= buf_size) {
|
|
969
|
+
memcpy(buf, RSTRING_PTR(mod_name), mod_name_len);
|
|
970
|
+
buf[mod_name_len] = separator;
|
|
971
|
+
memcpy(buf + mod_name_len + 1, RSTRING_PTR(method_name), method_name_len);
|
|
972
|
+
return total;
|
|
973
|
+
} else {
|
|
974
|
+
return BUFFER_OUT_OF_SPACE;
|
|
975
|
+
}
|
|
976
|
+
}
|
|
977
|
+
|
|
978
|
+
static ssize_t calculate_iseq_label(VALUE owner, const rb_iseq_t *iseq, char *buf, size_t buf_size) {
|
|
979
|
+
switch (ISEQ_BODY(iseq)->type) {
|
|
980
|
+
case ISEQ_TYPE_TOP:
|
|
981
|
+
case ISEQ_TYPE_CLASS:
|
|
982
|
+
case ISEQ_TYPE_MAIN:
|
|
983
|
+
// Just the method name, not `Object#<top (required)>`
|
|
984
|
+
return ONLY_METHOD_NAME;
|
|
985
|
+
|
|
986
|
+
case ISEQ_TYPE_METHOD:
|
|
987
|
+
return rb_gen_method_name(owner, ISEQ_BODY(iseq)->location.label, buf, buf_size);
|
|
988
|
+
|
|
989
|
+
#ifdef ISEQ_TYPE_PLAIN /* 2.5 does not have it */
|
|
990
|
+
case ISEQ_TYPE_PLAIN:
|
|
991
|
+
#endif
|
|
992
|
+
case ISEQ_TYPE_BLOCK: {
|
|
993
|
+
// CRuby uses "block in Foo#bar" and ""block (N levels) in Foo#bar" in backtraces.
|
|
994
|
+
// We use "Foo#bar (block)" because it's much faster and looks better in the UI.
|
|
995
|
+
const rb_iseq_t* method_iseq = ISEQ_BODY(iseq)->local_iseq;
|
|
996
|
+
|
|
997
|
+
static const char suffix[] = " (block)";
|
|
998
|
+
size_t suffix_len = sizeof(suffix) - 1;
|
|
999
|
+
|
|
1000
|
+
// Only use the result if there is enough space for both parts
|
|
1001
|
+
ssize_t written = calculate_iseq_label(owner, method_iseq, buf, buf_size);
|
|
1002
|
+
if (written > 0 && (written + suffix_len) <= buf_size) {
|
|
1003
|
+
memcpy(buf + written, suffix, suffix_len);
|
|
1004
|
+
return written + suffix_len;
|
|
1005
|
+
} else if (written == ONLY_METHOD_NAME) {
|
|
1006
|
+
VALUE method_name = rb_iseq_base_label(method_iseq);
|
|
1007
|
+
size_t method_name_len = RSTRING_LEN(method_name);
|
|
1008
|
+
if ((method_name_len + suffix_len) <= buf_size) {
|
|
1009
|
+
memcpy(buf, RSTRING_PTR(method_name), method_name_len);
|
|
1010
|
+
memcpy(buf + method_name_len, suffix, suffix_len);
|
|
1011
|
+
return method_name_len + suffix_len;
|
|
1012
|
+
}
|
|
1013
|
+
}
|
|
1014
|
+
|
|
1015
|
+
return BUFFER_OUT_OF_SPACE;
|
|
1016
|
+
}
|
|
1017
|
+
|
|
1018
|
+
case ISEQ_TYPE_RESCUE:
|
|
1019
|
+
case ISEQ_TYPE_ENSURE:
|
|
1020
|
+
case ISEQ_TYPE_EVAL:
|
|
1021
|
+
iseq = ISEQ_BODY(iseq)->parent_iseq;
|
|
1022
|
+
return calculate_iseq_label(owner, iseq, buf, buf_size);
|
|
1023
|
+
|
|
1024
|
+
default:
|
|
1025
|
+
// Upstream: rb_bug("calculate_iseq_label: unreachable");
|
|
1026
|
+
return ONLY_METHOD_NAME;
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
|
|
1030
|
+
// MRI: location_label()
|
|
1031
|
+
// C methods don't have an iseq, only Ruby methods have
|
|
1032
|
+
ssize_t ddtrace_location_label(const rb_callable_method_entry_t *cme, const rb_iseq_t *iseq, char *buf, size_t buf_size) {
|
|
1033
|
+
if (location_cfunc_p(cme)) {
|
|
1034
|
+
VALUE method_name = rb_id2str(cme->def->original_id);
|
|
1035
|
+
return rb_gen_method_name(cme->owner, method_name, buf, buf_size);
|
|
1036
|
+
} else {
|
|
1037
|
+
VALUE owner = cme ? cme->owner : Qnil;
|
|
1038
|
+
return calculate_iseq_label(owner, iseq, buf, buf_size);
|
|
1039
|
+
}
|
|
1040
|
+
}
|
|
1041
|
+
|
|
1042
|
+
VALUE ddtrace_location_base_label(const rb_callable_method_entry_t *cme, const rb_iseq_t *iseq) {
|
|
1043
|
+
if (location_cfunc_p(cme)) {
|
|
1044
|
+
return rb_id2str(cme->def->original_id);
|
|
1045
|
+
} else {
|
|
1046
|
+
return rb_iseq_base_label(iseq);
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
|
|
1050
|
+
VALUE ddtrace_iseq_base_label(const rb_iseq_t *iseq) {
|
|
1051
|
+
return rb_iseq_base_label(iseq);
|
|
1052
|
+
}
|
|
1053
|
+
|
|
1054
|
+
VALUE ddtrace_iseq_path(const rb_iseq_t *iseq) {
|
|
1055
|
+
return rb_iseq_path(iseq);
|
|
1056
|
+
}
|
|
1057
|
+
|
|
1058
|
+
size_t sizeof_rb_iseq_t(void) {
|
|
1059
|
+
return sizeof(rb_iseq_t);
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
size_t sizeof_rb_callable_method_entry_t(void) {
|
|
1063
|
+
return sizeof(rb_callable_method_entry_t);
|
|
1064
|
+
}
|
|
1065
|
+
|
|
1066
|
+
void* ddtrace_cme_cfunc_func(const rb_callable_method_entry_t *cme) {
|
|
1067
|
+
return cme->def->body.cfunc.func;
|
|
1068
|
+
}
|
|
1069
|
+
|
|
1070
|
+
const char *ddtrace_cme_original_method_name(const rb_callable_method_entry_t *cme) {
|
|
1071
|
+
return rb_id2name(cme->def->original_id);
|
|
878
1072
|
}
|
|
@@ -8,6 +8,8 @@
|
|
|
8
8
|
#ifndef PRIVATE_VM_API_ACCESS_SKIP_RUBY_INCLUDES
|
|
9
9
|
#include <ruby/thread_native.h>
|
|
10
10
|
#include <ruby/vm.h>
|
|
11
|
+
typedef struct RubyCME rb_callable_method_entry_t;
|
|
12
|
+
typedef struct RubyISEQ rb_iseq_t;
|
|
11
13
|
#endif
|
|
12
14
|
|
|
13
15
|
#include "extconf.h"
|
|
@@ -20,18 +22,17 @@ typedef struct {
|
|
|
20
22
|
|
|
21
23
|
// If a sample is kept around for later use, some of its fields need marking. Remember to
|
|
22
24
|
// update the marking code in `sampling_buffer_mark` if new fields are added.
|
|
25
|
+
// This is very similar to rb_backtrace_location_t (cme, iseq, pc) on purpose:
|
|
26
|
+
// we want to show frames like Ruby backtraces,
|
|
27
|
+
// not like rb_profile_frame_qualified_method_name() which differs in some cases.
|
|
23
28
|
typedef struct {
|
|
24
|
-
|
|
29
|
+
const rb_callable_method_entry_t* cme; // Needs marking, kept alive by sampling_buffer
|
|
30
|
+
struct {
|
|
25
31
|
struct {
|
|
26
|
-
|
|
32
|
+
const rb_iseq_t* iseq; // Needs marking, kept alive by sampling_buffer
|
|
27
33
|
void *caching_pc; // For caching validation/invalidation only (does not need marking)
|
|
28
34
|
int line;
|
|
29
35
|
} ruby_frame;
|
|
30
|
-
struct {
|
|
31
|
-
VALUE caching_cme; // For caching validation/invalidation only (does not need marking)
|
|
32
|
-
ID method_id;
|
|
33
|
-
void *function;
|
|
34
|
-
} native_frame;
|
|
35
36
|
} as;
|
|
36
37
|
bool is_ruby_frame : 1;
|
|
37
38
|
bool same_frame : 1;
|
|
@@ -48,8 +49,9 @@ VALUE thread_name_for(VALUE thread);
|
|
|
48
49
|
int ddtrace_rb_profile_frames(VALUE thread, int start, int limit, frame_info *stack_buffer);
|
|
49
50
|
|
|
50
51
|
size_t sizeof_rb_iseq_t(void);
|
|
51
|
-
|
|
52
|
-
VALUE
|
|
52
|
+
size_t sizeof_rb_callable_method_entry_t(void);
|
|
53
|
+
VALUE ddtrace_iseq_base_label(const rb_iseq_t *iseq);
|
|
54
|
+
VALUE ddtrace_iseq_path(const rb_iseq_t *iseq);
|
|
53
55
|
|
|
54
56
|
// Returns true if the current thread belongs to the main Ractor or if Ruby has no Ractor support
|
|
55
57
|
bool ddtrace_rb_ractor_main_p(void);
|
|
@@ -84,4 +86,8 @@ VALUE current_fiber_for(VALUE thread);
|
|
|
84
86
|
|
|
85
87
|
void self_test_current_fiber_for(void);
|
|
86
88
|
|
|
87
|
-
|
|
89
|
+
ssize_t ddtrace_location_label(const rb_callable_method_entry_t *cme, const rb_iseq_t *iseq, char *buf, size_t buf_size);
|
|
90
|
+
VALUE ddtrace_location_base_label(const rb_callable_method_entry_t *cme, const rb_iseq_t *iseq);
|
|
91
|
+
void* ddtrace_cme_cfunc_func(const rb_callable_method_entry_t *cme);
|
|
92
|
+
const char *ddtrace_cme_original_method_name(const rb_callable_method_entry_t *cme);
|
|
93
|
+
|
|
@@ -6,19 +6,27 @@
|
|
|
6
6
|
static ID telemetry_message_id;
|
|
7
7
|
|
|
8
8
|
void raise_unexpected_type(VALUE value, const char *value_name, const char *type_name, const char *file, int line, const char *function_name) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
9
|
+
// All of the below come from `ENFORCE_TYPE` and are constant for their call-site
|
|
10
|
+
VALUE simple_message = rb_sprintf("wrong argument for '%s' (expected a %s) at %s:%d:in `%s'",
|
|
11
|
+
value_name,
|
|
12
|
+
type_name,
|
|
13
|
+
file,
|
|
14
|
+
line,
|
|
15
|
+
function_name
|
|
16
|
+
);
|
|
17
|
+
VALUE exception = rb_exc_new_str(
|
|
18
|
+
rb_eTypeError,
|
|
19
|
+
rb_sprintf("wrong argument %"PRIsVALUE" for '%s' (expected a %s) at %s:%d:in `%s'",
|
|
20
|
+
rb_inspect(value),
|
|
21
|
+
value_name,
|
|
22
|
+
type_name,
|
|
23
|
+
file,
|
|
24
|
+
line,
|
|
25
|
+
function_name
|
|
20
26
|
)
|
|
21
27
|
);
|
|
28
|
+
rb_ivar_set(exception, telemetry_message_id, simple_message);
|
|
29
|
+
rb_exc_raise(exception);
|
|
22
30
|
}
|
|
23
31
|
|
|
24
32
|
// Raises an exception with separate telemetry-safe and detailed messages.
|
|
@@ -5,6 +5,13 @@
|
|
|
5
5
|
#include <ruby.h>
|
|
6
6
|
#include <datadog/common.h>
|
|
7
7
|
|
|
8
|
+
// All recommended backward compatible macros for keyword argument functions can be found here:
|
|
9
|
+
// https://github.com/ruby/ruby/blob/master/doc/extension.rdoc#defining-backward-compatible-macros-for-keyword-argument-functions
|
|
10
|
+
#ifndef RB_PASS_KEYWORDS
|
|
11
|
+
/* Only define macros on Ruby <2.7 */
|
|
12
|
+
#define rb_funcallv_kw(o, m, c, v, kw) rb_funcallv(o, m, c, v)
|
|
13
|
+
#endif
|
|
14
|
+
|
|
8
15
|
// Must be called once during initialization
|
|
9
16
|
void datadog_ruby_common_init(void);
|
|
10
17
|
|
data/ext/libdatadog_api/init.c
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
#include "feature_flags.h"
|
|
7
7
|
#include "library_config.h"
|
|
8
8
|
#include "process_discovery.h"
|
|
9
|
+
#include "trace_exporter.h"
|
|
9
10
|
|
|
10
11
|
void ddsketch_init(VALUE core_module);
|
|
11
12
|
void di_init(VALUE datadog_module);
|
|
@@ -24,4 +25,7 @@ void DDTRACE_EXPORT Init_libdatadog_api(void) {
|
|
|
24
25
|
ddsketch_init(core_module);
|
|
25
26
|
feature_flags_init(core_module);
|
|
26
27
|
di_init(datadog_module);
|
|
28
|
+
|
|
29
|
+
VALUE tracing_module = rb_define_module_under(datadog_module, "Tracing");
|
|
30
|
+
trace_exporter_init(tracing_module);
|
|
27
31
|
}
|