Skip to content

Telemetry-safe error reporting for native extensions#5076

Merged
ivoanjo merged 135 commits intomasterfrom
marcotc/error-logs-remediation-custom-profiler-code
Jan 20, 2026
Merged

Telemetry-safe error reporting for native extensions#5076
ivoanjo merged 135 commits intomasterfrom
marcotc/error-logs-remediation-custom-profiler-code

Conversation

@marcotc
Copy link
Copy Markdown
Member

@marcotc marcotc commented Nov 21, 2025

(I'll squash the commits on reviews finish)

What does this PR do?

This PR ensure errors raised from native ext/ code have enough context to be valuable in telemetry.

Motivation:

Because error information sent to telemetry cannot have arbitrary, dynamic data, we must ensure that we are only sending values that are known at gem build time.

Because our fallback is to only report the exception class, this PR adds a telemetry_message to native exceptions, so that their error information is not completely lost.

Change log entry

Yes. Telemetry: Added static error reporting for native extensions.

Additional Notes:

There's a follow up PR to reduce code duplication for the datadog_ruby_common.c/h files: #5088

How to test the change?
Easiest and fastest test it: bundle exec rake clean compile && bundle exec rake spec:profiling
There are more products supported by libdatadog, but at that point, just run CI :)

bouwkast and others added 26 commits November 7, 2025 11:40
This gets us closer to allowing these errors to be
sent to telemetry.
Add ruby_helpers.h include to 8 C files that use datadog_profiling_error_class
and datadog_profiling_internal_error_class but were missing the header declaration.

This fixes the compilation error:
  error: 'datadog_profiling_error_class' undeclared

Files fixed:
- clock_id_from_pthread.c
- collectors_gc_profiling_helper.c
- collectors_stack.c
- collectors_thread_context.c
- encoded_profile.c
- libdatadog_helpers.c
- private_vm_api_access.c
- unsafe_api_calls_check.c
Move ruby_helpers.h include after private VM headers to avoid conflicts.
This file requires private VM headers to be included first before any
public Ruby headers, but ruby_helpers.h includes datadog_ruby_common.h
which includes ruby.h, causing header ordering conflicts.

Fixes compilation error: 'expected ')' before '==' token in RHASH_EMPTY_P'
Cannot include ruby_helpers.h in this file as it pulls in public Ruby headers
(via datadog_ruby_common.h) that conflict with private VM headers.

Instead, declare the exception class globals as extern, following the pattern
already established in this file for other declarations.

This fully resolves the header ordering compilation error.
Method was renamed from safe_exception_message to constant_exception_message
but the RBS signature file was not updated, causing Steep type errors.
The error method must be public but was accidentally made private when
constant_exception_message was added. Moving it before the private keyword
restores its public visibility.

Fixes test failure: NoMethodError: private method 'error' called
Serialization errors contain dynamic libdatadog content, so they should
raise ProfilingInternalError (not ProfilingError or RuntimeError).

Updated both the Ruby wrapper code and the test expectation to use
ProfilingInternalError consistently.

Fixes test failure expecting ProfilingError but getting RuntimeError.
Signed-off-by: Marco Costa <marco.costa@datadoghq.com>
Signed-off-by: Marco Costa <marco.costa@datadoghq.com>
Signed-off-by: Marco Costa <marco.costa@datadoghq.com>
Signed-off-by: Marco Costa <marco.costa@datadoghq.com>
@github-actions github-actions Bot added core Involves Datadog core libraries profiling Involves Datadog profiling labels Nov 21, 2025
@pr-commenter
Copy link
Copy Markdown

pr-commenter Bot commented Nov 21, 2025

Benchmarks

Benchmark execution time: 2026-01-20 10:09:45

Comparing candidate commit 5aae53b in PR branch marcotc/error-logs-remediation-custom-profiler-code with baseline commit ff467a6 in branch master.

Found 0 performance improvements and 0 performance regressions! Performance is the same for 44 metrics, 2 unstable metrics.

@marcotc marcotc self-assigned this Dec 8, 2025
@ivoanjo
Copy link
Copy Markdown
Member

ivoanjo commented Dec 11, 2025

The failure in the benchmarks...

+ bundle exec ruby benchmarks/profiling_sample_gvl.rb
Current pid is 3296
ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-linux]
Warming up --------------------------------------
profiling - gvl benchmark samples
benchmarks/profiling_sample_gvl.rb:64:in `_native_sample_after_gvl_running': wrong number of arguments (given 3, expected 2) (ArgumentError)
        Datadog::Profiling::Collectors::ThreadContext::Testing._native_sample_after_gvl_running(@collector, @target_thread, false)
                                                                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
	from benchmarks/profiling_sample_gvl.rb:64:in `block (2 levels) in run_benchmark'
	from (eval at /usr/local/bundle/gems/benchmark-ips-2.14.0/lib/benchmark/ips/job/entry.rb:63):6:in `call_times'
	from /usr/local/bundle/gems/benchmark-ips-2.14.0/lib/benchmark/ips/job.rb:285:in `block in run_warmup'
	from /usr/local/bundle/gems/benchmark-ips-2.14.0/lib/benchmark/ips/job.rb:268:in `each'
	from /usr/local/bundle/gems/benchmark-ips-2.14.0/lib/benchmark/ips/job.rb:268:in `run_warmup'
	from /usr/local/bundle/gems/benchmark-ips-2.14.0/lib/benchmark/ips/job.rb:253:in `block in run'

is because the "use the test code from this branch with master" doesn't work if master isn't API-compatible with this branch.

TBH what I've done in the past is just disable the benchmark and re-enable it later, but it's kinda meh workaround. Suggestions welcome 😅

Copy link
Copy Markdown
Member

@ivoanjo ivoanjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great! The one reason I didn't press the approve button is due to the raise_error in datadog_ruby_common.c -- that one doesn't quite look correct.

Comment thread ext/datadog_profiling_native_extension/datadog_ruby_common.c Outdated
Comment thread ext/datadog_profiling_native_extension/datadog_ruby_common.c Outdated
Comment thread spec/datadog/profiling/native_extension_spec.rb Outdated
Comment thread ext/datadog_profiling_native_extension/ruby_helpers.h Outdated
Comment thread ext/datadog_profiling_native_extension/ruby_helpers.c Outdated
@marcotc marcotc requested a review from ivoanjo December 24, 2025 02:02
@ivoanjo
Copy link
Copy Markdown
Member

ivoanjo commented Jan 7, 2026

@marcotc marcotc requested a review from ivoanjo 2 weeks ago

Btw this one is still on my radar, catching up on a pile of things this week! 😅

Copy link
Copy Markdown
Member

@ivoanjo ivoanjo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 THIS IS AMAZING! Thanks @marcotc for your saintly-like patience with all my feedback + all the time I took to review this. Let's get this in!

@ivoanjo ivoanjo merged commit fc16456 into master Jan 20, 2026
637 of 638 checks passed
@ivoanjo ivoanjo deleted the marcotc/error-logs-remediation-custom-profiler-code branch January 20, 2026 10:53
@github-actions github-actions Bot added this to the 2.27.0 milestone Jan 20, 2026
@lloeki lloeki mentioned this pull request Jan 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Involves Datadog core libraries profiling Involves Datadog profiling

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants