Skip to content

Commit 16b8c46

Browse files
committed
Don't try to use the new code path on legacy rubies
1 parent 31c48b5 commit 16b8c46

File tree

4 files changed

+44
-34
lines changed

4 files changed

+44
-34
lines changed

ext/datadog_profiling_native_extension/collectors_thread_context.c

+32-25
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <ruby.h>
22

3+
#include "datadog_ruby_common.h"
34
#include "collectors_thread_context.h"
45
#include "clock_id.h"
56
#include "collectors_stack.h"
@@ -1688,35 +1689,41 @@ static VALUE _native_sample_skipped_allocation_samples(DDTRACE_UNUSED VALUE self
16881689
return Qtrue;
16891690
}
16901691

1691-
static VALUE otel_context_storage_for(thread_context_collector_state *state, VALUE thread) {
1692-
if (state->otel_context_source == OTEL_CONTEXT_SOURCE_FIBER_IVAR) { // otel-api 1.5+
1693-
VALUE current_fiber = current_fiber_for(thread);
1694-
return current_fiber == Qnil ? Qnil : rb_ivar_get(current_fiber, otel_fiber_context_storage_id /* @opentelemetry_context */);
1695-
}
1696-
1697-
if (state->otel_context_source == OTEL_CONTEXT_SOURCE_FIBER_LOCAL) { // otel-api < 1.5
1698-
return rb_thread_local_aref(thread, otel_context_storage_id /* __opentelemetry_context_storage__ */);
1699-
}
1692+
#ifndef NO_CURRENT_FIBER_FOR // Ruby 3.1+
1693+
static VALUE otel_context_storage_for(thread_context_collector_state *state, VALUE thread) {
1694+
if (state->otel_context_source == OTEL_CONTEXT_SOURCE_FIBER_IVAR) { // otel-api 1.5+
1695+
VALUE current_fiber = current_fiber_for(thread);
1696+
return current_fiber == Qnil ? Qnil : rb_ivar_get(current_fiber, otel_fiber_context_storage_id /* @opentelemetry_context */);
1697+
}
17001698

1701-
// If we got here, it means we never observed a context being set. Let's probe which one to use.
1702-
VALUE current_fiber = current_fiber_for(thread);
1703-
if (current_fiber != Qnil) {
1704-
VALUE context_storage = rb_ivar_get(current_fiber, otel_fiber_context_storage_id /* @opentelemetry_context */);
1705-
if (context_storage != Qnil) {
1706-
state->otel_context_source = OTEL_CONTEXT_SOURCE_FIBER_IVAR; // Remember for next time
1707-
return context_storage;
1699+
if (state->otel_context_source == OTEL_CONTEXT_SOURCE_FIBER_LOCAL) { // otel-api < 1.5
1700+
return rb_thread_local_aref(thread, otel_context_storage_id /* __opentelemetry_context_storage__ */);
17081701
}
1709-
} else {
1710-
VALUE context_storage = rb_thread_local_aref(thread, otel_context_storage_id /* __opentelemetry_context_storage__ */);
1711-
if (context_storage != Qnil) {
1712-
state->otel_context_source = OTEL_CONTEXT_SOURCE_FIBER_LOCAL; // Remember for next time
1713-
return context_storage;
1702+
1703+
// If we got here, it means we never observed a context being set. Let's probe which one to use.
1704+
VALUE current_fiber = current_fiber_for(thread);
1705+
if (current_fiber != Qnil) {
1706+
VALUE context_storage = rb_ivar_get(current_fiber, otel_fiber_context_storage_id /* @opentelemetry_context */);
1707+
if (context_storage != Qnil) {
1708+
state->otel_context_source = OTEL_CONTEXT_SOURCE_FIBER_IVAR; // Remember for next time
1709+
return context_storage;
1710+
}
1711+
} else {
1712+
VALUE context_storage = rb_thread_local_aref(thread, otel_context_storage_id /* __opentelemetry_context_storage__ */);
1713+
if (context_storage != Qnil) {
1714+
state->otel_context_source = OTEL_CONTEXT_SOURCE_FIBER_LOCAL; // Remember for next time
1715+
return context_storage;
1716+
}
17141717
}
1715-
}
17161718

1717-
// There's no context storage attached to the current thread
1718-
return Qnil;
1719-
}
1719+
// There's no context storage attached to the current thread
1720+
return Qnil;
1721+
}
1722+
#else
1723+
static inline VALUE otel_context_storage_for(DDTRACE_UNUSED thread_context_collector_state *state, VALUE thread) {
1724+
return rb_thread_local_aref(thread, otel_context_storage_id /* __opentelemetry_context_storage__ */);
1725+
}
1726+
#endif
17201727

17211728
// This method differs from trace_identifiers_for/ddtrace_otel_trace_identifiers_for to support the situation where
17221729
// the opentelemetry ruby library is being used for tracing AND the ddtrace tracing bits are not involved at all.

ext/datadog_profiling_native_extension/gvl_profiling_helper.c

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#include <ruby.h>
22
#include <ruby/thread.h>
3+
4+
#include "datadog_ruby_common.h"
35
#include "gvl_profiling_helper.h"
46

57
#if !defined(NO_GVL_INSTRUMENTATION) && !defined(USE_GVL_PROFILING_3_2_WORKAROUNDS) // Ruby 3.3+

ext/datadog_profiling_native_extension/gvl_profiling_helper.h

-8
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,6 @@
3939

4040
static inline void gvl_profiling_init(void) { }
4141

42-
// This header gets included in private_vm_access.c which can't include datadog_ruby_common.h so we replicate this
43-
// helper here
44-
#ifdef __GNUC__
45-
#define DDTRACE_UNUSED __attribute__((unused))
46-
#else
47-
#define DDTRACE_UNUSED
48-
#endif
49-
5042
// NOTE: This is a hack that relies on the knowledge that on Ruby 3.2 the
5143
// RUBY_INTERNAL_THREAD_EVENT_READY and RUBY_INTERNAL_THREAD_EVENT_RESUMED events always get called on the thread they
5244
// are about. Thus, we can use our thread local storage hack to get this data, even though the event doesn't include it.

ext/datadog_profiling_native_extension/private_vm_api_access.c

+10-1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@
4040
#endif
4141
#endif
4242

43+
// This file can't include datadog_ruby_common.h so we replicate this here
44+
#ifdef __GNUC__
45+
#define DDTRACE_UNUSED __attribute__((unused))
46+
#else
47+
#define DDTRACE_UNUSED
48+
#endif
49+
4350
#define PRIVATE_VM_API_ACCESS_SKIP_RUBY_INCLUDES
4451
#include "private_vm_api_access.h"
4552

@@ -847,6 +854,8 @@ bool is_raised_flag_set(VALUE thread) { return thread_struct_from_object(thread)
847854
if (expected_current_fiber != actual_current_fiber) rb_raise(rb_eRuntimeError, "current_fiber_for() self-test failed");
848855
}
849856
#else
850-
VALUE current_fiber_for(VALUE thread) { rb_raise(rb_eRuntimeError, "Not implemented for Ruby < 3.1"); }
857+
NORETURN(VALUE current_fiber_for(DDTRACE_UNUSED VALUE thread));
858+
859+
VALUE current_fiber_for(DDTRACE_UNUSED VALUE thread) { rb_raise(rb_eRuntimeError, "Not implemented for Ruby < 3.1"); }
851860
void self_test_current_fiber_for(void) { } // Nothing to do
852861
#endif

0 commit comments

Comments
 (0)