Skip to content

Commit d7d685e

Browse files
committed
emit otel
1 parent 22a88bd commit d7d685e

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

libdd-profiling/src/cxx.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ impl Profile {
616616
// Reset the profile and get the old one to serialize
617617
let old_profile = self.inner.reset_and_return_previous()?;
618618
let end_time = Some(std::time::SystemTime::now());
619-
let encoded = old_profile.serialize_into_compressed_pprof(end_time, None)?;
619+
let encoded = old_profile.serialize_into_compressed(end_time, None)?;
620620
Ok(encoded.buffer)
621621
}
622622
}
@@ -647,7 +647,7 @@ fn prepare_profile_for_export<'a>(
647647
)> {
648648
let old_profile = profile.inner.reset_and_return_previous()?;
649649
let end_time = Some(std::time::SystemTime::now());
650-
let encoded = old_profile.serialize_into_compressed_pprof(end_time, None)?;
650+
let encoded = old_profile.serialize_into_compressed(end_time, None)?;
651651

652652
let files_to_compress_vec: Vec<exporter::File> =
653653
files_to_compress.iter().map(Into::into).collect();

libdd-profiling/src/exporter/profile_exporter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ impl ProfileExporter {
280280
let attachments: Vec<_> = additional_files
281281
.iter()
282282
.map(|f| f.name)
283-
.chain(std::iter::once("profile.pprof"))
283+
.chain(std::iter::once(profile.filename))
284284
.collect();
285285

286286
let mut internal = internal_metadata.unwrap_or_else(|| json!({}));
@@ -340,8 +340,8 @@ impl ProfileExporter {
340340

341341
// Add profile
342342
Ok(form.part(
343-
"profile.pprof",
344-
reqwest::multipart::Part::bytes(profile.buffer).file_name("profile.pprof"),
343+
profile.filename,
344+
reqwest::multipart::Part::bytes(profile.buffer).file_name(profile.filename),
345345
))
346346
}
347347
}

libdd-profiling/src/internal/profile/mod.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,9 @@ pub struct EncodedProfile {
7272
pub end: SystemTime,
7373
pub buffer: Vec<u8>,
7474
pub endpoints_stats: ProfiledEndpointsStats,
75+
/// The filename to use when uploading this profile as a multipart attachment,
76+
/// e.g. `"profile.pprof"` or `"profile.otel"`.
77+
pub filename: &'static str,
7578
}
7679

7780
impl EncodedProfile {
@@ -101,6 +104,7 @@ impl EncodedProfile {
101104
end,
102105
buffer,
103106
endpoints_stats,
107+
filename: "profile.pprof",
104108
})
105109
}
106110
}
@@ -338,6 +342,7 @@ impl Profile {
338342
upscaling_info: UpscalingInfo,
339343
) -> anyhow::Result<()> {
340344
anyhow::ensure!(offset_values.len() == 1 || offset_values.len() == 2);
345+
#[cfg(feature = "otel")]
341346
if offset_values.len() == 2 {
342347
anyhow::ensure!(
343348
self.observations.paired_samples[offset_values[0]] == Some(offset_values[1])
@@ -496,6 +501,23 @@ impl Profile {
496501
self.encode_otel(end_time, duration)
497502
}
498503

504+
/// Serialize the aggregated profile, choosing the output format at runtime.
505+
///
506+
/// If the `DD_PROFILING_OTEL_FORMAT` environment variable is set to any non-empty value **and**
507+
/// the crate was compiled with the `otel` feature, the profile is encoded as OTLP; otherwise
508+
/// it falls back to pprof.
509+
pub fn serialize_into_compressed(
510+
self,
511+
end_time: Option<SystemTime>,
512+
duration: Option<Duration>,
513+
) -> anyhow::Result<EncodedProfile> {
514+
#[cfg(feature = "otel")]
515+
if std::env::var_os("DD_PROFILING_OTEL_FORMAT").is_some() {
516+
return self.encode_otel(end_time, duration);
517+
}
518+
self.serialize_into_compressed_pprof(end_time, duration)
519+
}
520+
499521
/// Serialize the aggregated profile, adding the end time and duration.
500522
/// # Arguments
501523
/// * `end_time` - Optional end time of the profile. Passing None will use the current time.
@@ -904,6 +926,7 @@ impl Profile {
904926
end,
905927
buffer,
906928
endpoints_stats,
929+
filename: "profile.otel",
907930
})
908931
}
909932

@@ -1070,6 +1093,7 @@ impl Profile {
10701093
end,
10711094
buffer: Vec::new(),
10721095
endpoints_stats,
1096+
filename: "profile.pprof",
10731097
})
10741098
}
10751099

0 commit comments

Comments
 (0)