Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(profiler) prevent emitting empty profiles #2397

Merged
merged 5 commits into from
Jan 16, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 35 additions & 28 deletions profiling/src/profiling/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,12 @@ impl TimeCollector {
profiles: &mut HashMap<ProfileIndex, InternalProfile>,
started_at: &WallTime,
) {
if message.key.sample_types.len() == 0 {
// profiling disabled, this should not happen!
warn!("You spot a bug in the profiler, please be so kind and report this do Datadog.");
return;
}

let profile: &mut InternalProfile = if let Some(value) = profiles.get_mut(&message.key) {
value
} else {
Expand Down Expand Up @@ -1034,43 +1040,44 @@ impl Profiler {

let mut sample_types = Vec::with_capacity(SAMPLE_TYPES.len());
let mut sample_values = Vec::with_capacity(SAMPLE_TYPES.len());

if locals.profiling_enabled {
// sample, wall-time, cpu-time
let len = 2 + locals.profiling_experimental_cpu_time_enabled as usize;
sample_types.extend_from_slice(&SAMPLE_TYPES[0..len]);
sample_values.extend_from_slice(&values[0..len]);
}

// alloc-samples, alloc-size
if locals.profiling_allocation_enabled {
sample_types.extend_from_slice(&SAMPLE_TYPES[3..5]);
sample_values.extend_from_slice(&values[3..5]);
}
// alloc-samples, alloc-size
if locals.profiling_allocation_enabled {
sample_types.extend_from_slice(&SAMPLE_TYPES[3..5]);
sample_values.extend_from_slice(&values[3..5]);
}

#[cfg(feature = "timeline")]
if locals.profiling_experimental_timeline_enabled {
sample_types.push(SAMPLE_TYPES[5]);
sample_values.push(values[5]);
}
#[cfg(feature = "timeline")]
if locals.profiling_experimental_timeline_enabled {
sample_types.push(SAMPLE_TYPES[5]);
sample_values.push(values[5]);
}

#[cfg(feature = "exception_profiling")]
if locals.profiling_exception_enabled {
sample_types.push(SAMPLE_TYPES[6]);
sample_values.push(values[6]);
}
#[cfg(feature = "exception_profiling")]
if locals.profiling_exception_enabled {
sample_types.push(SAMPLE_TYPES[6]);
sample_values.push(values[6]);
}

#[cfg(php_has_fibers)]
if let Some(fiber) = unsafe { ddog_php_prof_get_active_fiber().as_mut() } {
// Safety: the fcc is set by Fiber::__construct as part of zpp,
// which will always set the function_handler on success, and
// there's nothing changing that value in all of fibers
// afterwards, from start to destruction of the fiber itself.
let func = unsafe { &*fiber.fci_cache.function_handler };
if let Some(functionname) = extract_function_name(func) {
labels.push(Label {
key: "fiber",
value: LabelValue::Str(functionname.into()),
});
}
#[cfg(php_has_fibers)]
if let Some(fiber) = unsafe { ddog_php_prof_get_active_fiber().as_mut() } {
// Safety: the fcc is set by Fiber::__construct as part of zpp,
// which will always set the function_handler on success, and
// there's nothing changing that value in all of fibers
// afterwards, from start to destruction of the fiber itself.
let func = unsafe { &*fiber.fci_cache.function_handler };
if let Some(functionname) = extract_function_name(func) {
labels.push(Label {
key: "fiber",
value: LabelValue::Str(functionname.into()),
});
}
}

Expand Down