Skip to content

Commit

Permalink
Raise ArgumentError when ddog_prof_Profile_add returns a failure
Browse files Browse the repository at this point in the history
This will allow us to have early warning for when we're using the
API incorrectly.

See also DataDog/libdatadog#87 .
  • Loading branch information
ivoanjo committed Feb 3, 2023
1 parent 00b9546 commit 0a7f10a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
4 changes: 3 additions & 1 deletion ext/ddtrace_profiling_native_extension/stack_recorder.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,11 @@ void record_sample(VALUE recorder_instance, ddog_prof_Sample sample) {

struct active_slot_pair active_slot = sampler_lock_active_profile(state);

ddog_prof_Profile_add(active_slot.profile, sample);
uint64_t success = ddog_prof_Profile_add(active_slot.profile, sample);

sampler_unlock_active_profile(active_slot);

if (!success) rb_raise(rb_eArgError, "Failed to record sample: ddog_prof_Profile_add returned failure status code");
}

void record_endpoint(VALUE recorder_instance, uint64_t local_root_span_id, ddog_CharSlice endpoint) {
Expand Down
15 changes: 15 additions & 0 deletions spec/datadog/profiling/stack_recorder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,21 @@ def sample_types_from(decoded_profile)
end
end

context 'when sample is invalid' do
context 'because the local root span id is being defined using a string instead of as a number' do
let(:metric_values) { { 'cpu-time' => 123, 'cpu-samples' => 456, 'wall-time' => 789 } }

it do
# We're using `_native_sample` here to test the behavior of `record_sample` in `stack_recorder.c`
expect do
Datadog::Profiling::Collectors::Stack::Testing._native_sample(
Thread.current, stack_recorder, metric_values, { 'local root span id' => 'incorrect' }.to_a, [], 400, false
)
end.to raise_error(ArgumentError)
end
end
end

describe 'trace endpoint behavior' do
let(:metric_values) { { 'cpu-time' => 101, 'cpu-samples' => 1, 'wall-time' => 789 } }
let(:samples) { samples_from_pprof(encoded_pprof) }
Expand Down

0 comments on commit 0a7f10a

Please sign in to comment.